无法更改 SCNCone 的颜色 - Scenekit

Cannot change color of SCNCone - Scenekit

我有一个 'attach node',它有 2 个子节点,它们是 Blender 模型。我已将第三个节点添加到此附加节点,即 SCNCone。出于某种原因,我无法更改圆锥节点的颜色,只能更改透明度。我似乎看不出代码有什么问题,但在运行期间,无论我将其设置为什么颜色,圆锥体始终是黑色。

let coneGeo = SCNCone(topRadius: 0.1, bottomRadius: 0.7, height: 4)
let coneMaterial = SCNMaterial()

coneMaterial.diffuse.contents = UIColor(red: 255.0/255.0, green: 108.0/255.0, blue: 91.0/255.0, alpha: 0.2)
coneGeo.materials = [coneMaterial]

let coneNode = SCNNode(geometry: coneGeo)
coneNode.position = SCNVector3(0, -1.5, 0)
coneNode.name = "coneNode"

AttachNode.addChildNode(coneNode)

coneMaterial.diffuse.contents = UIColor(red: 255.0/255.0, green: 108.0/255.0, blue: 91.0/255.0, alpha: 0.2) 替换为 coneGeo.geometry?.firstMaterial?.diffuse.contents.diffuse.contents = UIColor(red: 255.0/255.0, green: 108.0/255.0, blue: 91.0/255.0, alpha: 0.2)。您必须通过它的几何参数访问它的 material 颜色,而不是在没有几何的情况下更改圆锥体的 material 颜色。

coneGeo.materials = [coneMaterial]

这也行得通。我通过将圆锥体节点添加到空场景来测试您的代码。 我只是黑屏。

但是如果我将 alpha 值更改为 0.5,这就是我得到的结果。

代码。

    override func viewDidLoad()
    {
        super.viewDidLoad()

        // create a new scene
        let scene = SCNScene()

        let coneGeo = SCNCone(topRadius: 0.1, bottomRadius: 0.7, height: 4)
        let coneMaterial = SCNMaterial()

        coneMaterial.diffuse.contents = UIColor(red: 255.0 / 255.0,
                                                green: 108.0 / 255.0,
                                                blue: 91.0 / 255.0, alpha: 0.5)
        coneGeo.materials = [coneMaterial]

        let coneNode = SCNNode(geometry
                                   : coneGeo)
        coneNode.position = SCNVector3(0, -1.5, 0)
        coneNode.name = "coneNode"

        scene.rootNode.addChildNode(coneNode)

        // retrieve the SCNView
        let scnView = self.view as! SCNView

        // set the scene to the view
        scnView.scene = scene

        // allows the user to manipulate the camera
        scnView.allowsCameraControl = true

        // show statistics such as fps and timing information
        scnView.showsStatistics = true

        // configure the view
        scnView.backgroundColor = UIColor.black
    }

所以我会说,检查 UIColor(red: 255.0/255.0, green: 108.0/255.0, blue: 91.0/255.0, alpha: 0.2)

中的 alpha 值