Scenekit: How to get all of node's materials?

1.4k views Asked by At

Ok, from what I understand materials can be created for .dae or any 3D model being used as an SCNNode in Xcode here, in the model's editor:

enter image description here

The topmost material gets applied automatically and all is well. My problem is that I want to programmatically SWITCH between these materials that have been created throughout my game.

I tried to get an array of these materials by doing:

node.geometry?.materials

however this only returns that first material. Ive tried everybting but cant find a way to get the other materials and switch to them. Right now I am trying:

childNode.geometry?.materials = [(childNode.geometry?.material(named: "test"))!]
//childNode is the node

where test was that second material but it finds that as nil. How can I programmatically switch between multiple materials?

1

There are 1 answers

2
Xartec On BEST ANSWER

If the material is not actually assigned to one of the material slots (such as diffuse) it’s not part of the geometry either.

You could assign the second material to another slot and then reset its color in code after you read the material into a property to be used later.

Another option I used myself is assigning multiple materials to different faces of the same model (in third party 3d software). Exported as dae, added to xcode which then automatically divided the geometry into separate elements with each their own material. Which I can then adjust in xcode, and itterate in the same way you are trying to do.