1

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]
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
Shahar Melamed
  • 1,534
  • 5
  • 15
  • 23
  • What shape is the 'head' node? – BlackMirrorz Aug 21 '18 at 14:20
  • @JoshRobbins Box – Shahar Melamed Aug 21 '18 at 14:22
  • If you `print(head.geometry)` does it show `SCNBox` or `SCNGeometry`? If it's a box then your code should work. – James P Aug 21 '18 at 14:32
  • @JamesP it prints "Optional()" – Shahar Melamed Aug 21 '18 at 14:39
  • 1
    I'm guessing you're not using a `SCNBox` like in the question you linked. If you created your box with another app you'll have to modify the source file to have 6 materials, one for each face, with the appropriate UV maps. – EmilioPelaez Aug 21 '18 at 14:53
  • @EmilioPelaez I'm using a .scn file that I've created in my project. – Shahar Melamed Aug 21 '18 at 15:03
  • I have used a .dae file with 6 materials for each face. However, it never really worked with the above code modified by using 1 image 6 times. I did not create UV maps! I also converted my .dae file into a .scn file and it didnt solve the issue. Using a SCNBox it would work. My "Optional()" object is not a SCNBox (tested with is SCNBox) – Darkwonder Feb 19 '21 at 18:10

1 Answers1

0

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

Shahar Melamed
  • 1,534
  • 5
  • 15
  • 23