2016-08-15 9 views
3

박스입니다. 나는 SceneKit geometry SCNBox을 사용하고 있습니다. 불행하게도, 결과는 질감 대신에만 해당 부분 사용의 각면에 완전히 예상된다는 점이다 : 나는 형상에 쉐이더 수정을 사용할 수 있습니다 알고SceneKit -지도 큐브 텍스처 내가이 SceneKit보기에서 큐브를 사용하고 싶습니다 <p><a href="https://i.stack.imgur.com/QwOgw.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QwOgw.jpg" alt="enter image description here"></a></p> <p> </p>처럼 보이는 큐브 텍스처를했습니다

let videoGeometry = SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0) 
videoGeometry.firstMaterial?.isDoubleSided = true 
videoGeometry.firstMaterial?.diffuse.contents = UIImage(named: "test")! 

을하지만, 시작 위치를 잘 모르겠어요 . 텍스처가 현재 6 번 사용 되었기 때문에 내 직감은 SCNBox 지오메트리가 내 목표에 맞지 않을 수도 있지만 실제로 어떻게 바꾸는 지 모르겠습니다.

답변

6

커스텀 지오메트리로 작업 할 수 있습니다. 큐브를 생성하고 (시작하려면 적절한 위치에 http://ronnqvi.st/custom-scenekit-geometry/) 상단에 맞춤 텍스처 매핑을 추가 할 수 있습니다. 색인을 올바르게 얻는 것이 약간 까다 롭지 만 (결국 나를위한 것입니다.) 결국 괜찮 았습니다.

func getSimpleCubeGeo() -> SCNGeometry { 
    let halfSide = Float(0.5) 

    /* The cube vertex are like: 

     5---------4 
     /.  /| 
    /.  /| 
    7---------6 | 
    | .  | | 
    | .  | | 
    | 1......|..0 
    | .  |/
    |.  |/ 
    3---------2 

    */ 
    let _positions = [ 
     SCNVector3(x:-halfSide, y:-halfSide, z: halfSide), 
     SCNVector3(x: halfSide, y:-halfSide, z: halfSide), 
     SCNVector3(x:-halfSide, y:-halfSide, z: -halfSide), 
     SCNVector3(x: halfSide, y:-halfSide, z: -halfSide), 
     SCNVector3(x:-halfSide, y: halfSide, z: halfSide), 
     SCNVector3(x: halfSide, y: halfSide, z: halfSide), 
     SCNVector3(x:-halfSide, y: halfSide, z: -halfSide), 
     SCNVector3(x: halfSide, y: halfSide, z: -halfSide), 
    ] 

    // points are tripled since they are each used on 3 faces 
    // and there's no continuity in the UV mapping 
    // so we need to duplicate the points 
    // 
    // we'll use the first third for the faces orthogonal to the X (left) axis, 
    // the second for the Y (top) axis and the third for the Z (front) axis 
    let positions = _positions + _positions + _positions 

    let X = 0 
    let Y = 8 
    let Z = 16 

    let indices = [ 
     // bottom 
     0 + Y, 2 + Y, 1 + Y, 
     1 + Y, 2 + Y, 3 + Y, 
     // back 
     2 + Z, 6 + Z, 3 + Z, 
     3 + Z, 6 + Z, 7 + Z, 
     // left 
     0 + X, 4 + X, 2 + X, 
     2 + X, 4 + X, 6 + X, 
     // right 
     1 + X, 3 + X, 5 + X, 
     3 + X, 7 + X, 5 + X, 
     // front 
     0 + Z, 1 + Z, 4 + Z, 
     1 + Z, 5 + Z, 4 + Z, 
     // top 
     4 + Y, 5 + Y, 6 + Y, 
     5 + Y, 7 + Y, 6 + Y, 
    ] 

    // get the points in the texture where the faces are split 
    var textureSplitPoints = [CGPoint]() 
    for i in 0...12 { 
     let x = Double(i % 4) 
     let y = Double(i/4) 
     textureSplitPoints.append(CGPoint(x: x/3.0, y: y/2.0)) 
    } 
    let textCoords = [ 
     textureSplitPoints[4], 
     textureSplitPoints[6], 
     textureSplitPoints[5], 
     textureSplitPoints[5], 
     textureSplitPoints[8], 
     textureSplitPoints[10], 
     textureSplitPoints[9], 
     textureSplitPoints[9], 

     textureSplitPoints[5], 
     textureSplitPoints[4], 
     textureSplitPoints[1], 
     textureSplitPoints[0], 
     textureSplitPoints[7], 
     textureSplitPoints[6], 
     textureSplitPoints[11], 
     textureSplitPoints[10], 

     textureSplitPoints[2], 
     textureSplitPoints[1], 
     textureSplitPoints[2], 
     textureSplitPoints[3], 
     textureSplitPoints[6], 
     textureSplitPoints[5], 
     textureSplitPoints[6], 
     textureSplitPoints[7], 
    ] 

    let vertexSource = SCNGeometrySource(vertices: positions) 

    let textSource = SCNGeometrySource(textureCoordinates: textCoords) 
    let indexData = NSData(bytes: indices, length: sizeof(Int) * indices.count) 
    let elements = SCNGeometryElement(
     data: indexData as Data, 
     primitiveType: SCNGeometryPrimitiveType.triangles, 
     primitiveCount: indices.count/3, 
     bytesPerIndex: sizeof(Int) 
    ) 
    return SCNGeometry(sources: [vertexSource, textSource], elements: [elements]) 
}