我想为SCNNode
的每张脸设置不同的图像,我该怎么做?
我试过了 this但它没有用,第一种 Material 出现在所有的脸上。我正在使用 Swift 4.2。
我已经试过了:
self.addChildNode(playerNode!)
let head = playerNode?.childNode(withName: "head",
recursively: true)!
let greenMaterial = SCNMaterial()
greenMaterial.diffuse.contents = UIColor.green
greenMaterial.locksAmbientWithDiffuse = true;
let redMaterial = SCNMaterial()
redMaterial.diffuse.contents = UIColor.red
redMaterial.locksAmbientWithDiffuse = true;
let blueMaterial = SCNMaterial()
blueMaterial.diffuse.contents = UIColor.blue
blueMaterial.locksAmbientWithDiffuse = true;
let yellowMaterial = SCNMaterial()
yellowMaterial.diffuse.contents = UIColor.yellow
yellowMaterial.locksAmbientWithDiffuse = true;
let purpleMaterial = SCNMaterial()
purpleMaterial.diffuse.contents = UIColor.purple
purpleMaterial.locksAmbientWithDiffuse = true
let WhiteMaterial = SCNMaterial()
WhiteMaterial.diffuse.contents = UIColor.white
WhiteMaterial.locksAmbientWithDiffuse = true
head?.geometry?.materials = [redMaterial,
greenMaterial,
blueMaterial,
WhiteMaterial,
yellowMaterial,
purpleMaterial]
最佳答案
我的解决方案: 因为我用了一个.scn文件来设计播放器,我所做的没有奏效,所以我使用另一个与原始头部相同属性的盒子并将其添加到播放器中(在我移除原始头部之后) .为了创建新的头像,我这样做了:
let head = playerNode?.childNode(withName: "head", recursively: true)!
let head2 = SCNNode(geometry: SCNBox(width: 0.3, height: 0.3, length: 0.3, chamferRadius: 0))
let greenMaterial = SCNMaterial()
greenMaterial.diffuse.contents = UIColor.green
greenMaterial.locksAmbientWithDiffuse = true;
let redMaterial = SCNMaterial()
redMaterial.diffuse.contents = UIColor.red
redMaterial.locksAmbientWithDiffuse = true;
let blueMaterial = SCNMaterial()
blueMaterial.diffuse.contents = UIColor.blue
blueMaterial.locksAmbientWithDiffuse = true;
let yellowMaterial = SCNMaterial()
yellowMaterial.diffuse.contents = UIColor.yellow
yellowMaterial.locksAmbientWithDiffuse = true;
let purpleMaterial = SCNMaterial()
purpleMaterial.diffuse.contents = UIColor.purple
purpleMaterial.locksAmbientWithDiffuse = true
let WhiteMaterial = SCNMaterial()
WhiteMaterial.diffuse.contents = UIColor.white
WhiteMaterial.locksAmbientWithDiffuse = true
head2.geometry?.materials = [redMaterial, greenMaterial, blueMaterial, WhiteMaterial, yellowMaterial, purpleMaterial]
head2.position = head?.position ?? SCNVector3(0, 0.95, 0)
head2.scale = head?.scale ?? SCNVector3(1, 1, 1)
head?.removeFromParentNode()
playerHead = head2
playerNode?.addChildNode(head2)
谢谢大家:)
关于ios - 为每个 SCNNode 人脸设置不同的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51949792/