Search code examples
scenekitmetaltexture-mappingmaterial-swift

SceneKit texture mapping issue on material


I'm trying to achieve with SceneKit the drawing of a simple set of 4 boxes which are next to each other with textures applied on each of them (Minecraft style) :

enter image description here

The texture are like this (png files) :

enter image description here

I would like to avoid the small blue lines between the boxes (which are not in the textures) but whatever setup I try, these artefacts stay.

Any idea how to avoid them ?

Below the setup of the material in the scenekit editor (exactly the same for each box, except the diffuse part which is refering to the right texture file)

enter image description here

enter image description here

The issue in the scenekit editor is also appearing in the app running on the device.

What is strange is that if I just do a full black (or whatever color) texture (with or without anything inside), these artefacts do not appear, example below :

enter image description here


Solution

  • It seems you need to programmatically set the wrap modes of each material property in order to avoid this "wrap-around" behavior. Configure each material property to which you've assigned a texture such that its wrapS and wrapT properties are .clamp, rather than .repeat, which appears to be the default when loading materials from an .scn file.

    let nodes = scene.rootNode.childNodes // get a list of relevant nodes
    for node in nodes {
        guard let materials = node.geometry?.materials else { continue }
        for material in materials {
            material.diffuse.wrapS = .clamp
            material.diffuse.wrapT = .clamp
            // ...confgure other material properties as necessary...
        }
    }