简体   繁体   中英

Set a different image for each SCNNode face

I want to set a different image for each face of SCNNode , how can I do it? I've tried this but it didn't work and the first material was on all of the faces. I'm using Swift 4.2.

I've tried this:

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]

My solution: Because of I used a .scn file to design the player, what I've done didn't work, so I used another box with the same properties of the original head and added it to the player (after I removed the original head). To create the new head I did this:

    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 everyone:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM