Reputation: 467
I try to apply two different materials on a same geometry.
One material for tiling and second for Ambient Occlusion and other effects.
I export a model with 2 UVSet (it's OK) in DAE format. When I try to apply the second material, it never appear. On second material, I try to apply blendMode, transparencyMode, png file, etc. Nothing works :(( If I change mappingChannel on the first material, mapping is OK !
let mat1 = SCNMaterial()
mat1.name = "Mat1"
mat1.diffuse.mappingChannel = 0
mat1.diffuse.contents = UIImage(named: "somePNGFile.png")
let mat2 = SCNMaterial()
mat2.name = "Mat2"
mat2.diffuse.mappingChannel = 1
mat2.diffuse.contents = UIImage(named: "otherPNGFile.png")
cube.geometry!.materials = [mat1, mat2]
Here is my SCN file : https://drive.google.com/open?id=1ddbE815hlS0VbKPsbwcJoS9cL8hOf6Mt
What's wrong in my code ? Maybe the 3D File... If you have an idea, a suggestion, a solution, it's great.
Thanks
Upvotes: 2
Views: 1451
Reputation: 467
Finally, I found a solution with shaderModifiers.
It's possible to assign different mappingChannel on different slots (ambient, diffuse, specular, etc). Reference doc from Apple : https://developer.apple.com/documentation/scenekit/scnshadermodifierentrypointsurface?language=objc
Here is my code :
var mat:SCNMaterial!
var model:SCNReferenceNode!
override init() {
super.init()
// INIT
initModel()
initShader()
assignTextures()
}
func initModel() {
let url = Bundle.main.url(forResource: "Test_MixMaterials", withExtension: "scn", subdirectory: "Test.scnassets")!
model = SCNReferenceNode(url: url)!
model.load()
self.addChildNode(model)
if let cube = model.childNode(withName: "pCube1", recursively: true) {
mat = SCNMaterial()
// Define mapping Channel 0 on diffuse slot
mat.diffuse.mappingChannel = 0
// Define mapping Channel 1 on ambient slot
mat.ambient.mappingChannel = 1
// Assign material
cube.geometry!.firstMaterial = mat
}
}
func initShader() {
let shader = """
// Samplers 2D.
uniform sampler2D texture_UV1;
uniform sampler2D texture_UV2;
// Directives
#pragma transparent
#pragma body
// Source Diffuse ------------------------------------------------------------------------------------------------------------
vec4 source = texture2D(texture_UV1, _surface.diffuseTexcoord);
// Destination Diffuse --------------------------------------------------------------------------------------------------------
vec4 dest = texture2D(texture_UV2, _surface.ambientTexcoord);
// MULTIPLY Diffuses ---------------------------------------------------------------------------------------------------------------
_surface.diffuse = source * dest;
"""
mat.shaderModifiers = [
.surface:shader
]
}
func assignTextures() {
let setTexture_1 = SCNMaterialProperty(contents: UIImage(named: "someImage.png")!)
let setTexture_2 = SCNMaterialProperty(contents: UIImage(named: "otherImage.png")!)
mat.setValue(setTexture_1, forKey: "texture_UV1")
mat.setValue(setTexture_2, forKey: "texture_UV2")
}
And that's work...
If you want to test, here is the model I used : https://drive.google.com/open?id=1ddbE815hlS0VbKPsbwcJoS9cL8hOf6Mt
Upvotes: 2