我正在尝试使用Swift 4和Scenekit制作3D旋转四面体的每一面,该四面体具有不同的颜色(或图像)。

我设法创建了四面体并使它旋转得很好,但是当我尝试使每一面都具有不同的颜色或图像时,我发现它以指定的第一种颜色显示每一面。

我以为我已经正确定义了node.geometry?.materials,但是它只使用列表中的第一种颜色。
如果我重新排序列表,则它将使用新的第一种颜色作为唯一颜色。

我做错了什么?

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let sceneView = SCNView(frame: self.view.frame)
    self.view.addSubview(sceneView)

    let myScene = SCNScene()
    sceneView.scene = myScene

    let mySphere = SCNSphere(radius: 5)
    let mySphereNode = SCNNode(geometry: mySphere)
    mySphereNode.position = SCNVector3(x: 0, y: 0, z: 0)

    let vertices:[SCNVector3] = [
        SCNVector3(x:0, y:1, z:1),      // b    // 0
        SCNVector3(x:1, y:0, z:1),      // d    // 1
        SCNVector3(x:0, y:0, z:0),      // a    // 2
        SCNVector3(x:1, y:1, z:0),      // c    // 3
    ]

    let vertexSource = SCNGeometrySource(vertices: vertices)

    let indices: [UInt16] = [
        2, 3, 1,        // acd
        0, 2, 1,        // bad
        3, 0, 1,        // cbd
        3, 2, 0         // cab
    ]

    let element = SCNGeometryElement(indices: indices, primitiveType: .triangles)

    let geometry = SCNGeometry(sources: [vertexSource], elements: [element])

    let blueMaterial = material(withColor: UIColor.blue)
    let yellowMaterial = material(withColor: UIColor.yellow)
    let purpleMaterial = material(withColor: UIColor.purple)
    let magentaMaterial = material(withColor: UIColor.magenta)

    let node = SCNNode(geometry: geometry)
    node.position = SCNVector3(x: 0, y: 0, z: 0)
    node.scale = SCNVector3(x: 3, y: 3, z: 3)

    node.geometry?.materials  = [blueMaterial, yellowMaterial, purpleMaterial, magentaMaterial]

    myScene.rootNode.addChildNode(node)

    node.runAction(SCNAction.rotateBy(x: CGFloat(0), y: CGFloat(10), z: CGFloat(0), duration: 30))

    let myCamera = SCNCamera()

    let myCameraNode = SCNNode()
    myCameraNode.camera = myCamera
    myCameraNode.position = SCNVector3(x: -25, y: 10, z: 30)
    myCameraNode.orientation = SCNQuaternion(x: -0.26, y: -0.32, z: 0, w: 0.91)
    myScene.rootNode.addChildNode(myCameraNode)

}


func material(withColor color : UIColor) -> SCNMaterial {
    let material = SCNMaterial();
    material.diffuse.contents = color
    material.locksAmbientWithDiffuse = true
    return material
}

最佳答案

您的代码是完美的。唯一的问题是,在构建几何时,您没有足够的几何元素。如下所示的快速修复方法,您可以获得四种颜色。

  let element0 = SCNGeometryElement(indices: [UInt16](indices[0...2]), primitiveType: .triangles)
  let element1 = SCNGeometryElement(indices:  [UInt16](indices[3...5]), primitiveType: .triangles)
  let element2 = SCNGeometryElement(indices:  [UInt16](indices[6...8]), primitiveType: .triangles)
  let element3 = SCNGeometryElement(indices:  [UInt16](indices[9...11]), primitiveType: .triangles)

  let geometry = SCNGeometry(sources: [vertexSource], elements: [element0, element1,element2,element3])

关于ios - Swift和SceneKit 3D多色四面体,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52500294/

10-11 19:55