为每个SCNNode面孔设置不同的图像

时间:2018-08-21 13:25:41

标签: ios swift scenekit

我想为SCNNode的每张脸设置不同的图像,我该怎么做? 我尝试过this,但没有用,所有材料的第一个材料都在上面。我正在使用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]

1 个答案:

答案 0 :(得分:0)

我的解决方案: 因为我使用.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)

Tnx每个人:)