【问题标题】:Cannot change color of SCNCone - Scenekit无法更改 SCNCone 的颜色 - Scenekit
【发布时间】:2017-05-12 08:18:21
【问题描述】:

我有一个“附加节点”,它有 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)

【问题讨论】:

    标签: swift scenekit


    【解决方案1】:

    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)。无需在没有几何体的情况下更改圆锥体的材质颜色,您必须通过其几何参数访问它的材质颜色。

    【讨论】:

    • 谢谢!做coneNode.geometry?.firstMaterial?.diffuse.contents = UIColor(red: 255.0/255.0, green: 108.0/255.0, blue: 91.0/255.0, alpha: 0.2) 已经成功了。再次感谢。
    【解决方案2】:
    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值

    【讨论】:

      猜你喜欢
      • 2018-05-06
      • 1970-01-01
      • 2017-06-23
      • 1970-01-01
      • 2023-04-11
      • 2017-08-24
      • 1970-01-01
      • 2021-11-26
      • 2022-01-22
      相关资源
      最近更新 更多