这是3d对象的节点黄色与金属材质球的示例。
我想通过Swift为SceneKit中的3d对象应用颜色。
我该怎么做?
最佳答案
要完成所需的任务,只需使用以下代码:
override func viewDidLoad() {
super.viewDidLoad()
let scene = SCNScene(named: "art.scnassets/myScene.scn")!
let sphereNode = SCNNode(geometry: SCNSphere(radius: 5))
let goldenMaterial = SCNMaterial()
goldenMaterial.lightingModel = .physicallyBased
goldenMaterial.metalness.contents = 1.0
goldenMaterial.roughness.contents = 0.0
goldenMaterial.diffuse.contents = UIColor(red: 0.95,
green: 0.75,
blue: 0.2,
alpha: 1.0)
sphereNode.geometry?.materials = [goldenMaterial]
scene.rootNode.addChildNode(sphereNode)
//......................................................
let scnView = self.view as! SCNView
scnView.scene = scene
scnView.autoenablesDefaultLighting = true
}