Ranknoodle
Ranknoodle

Reputation: 897

How to add texture (image) to SceneKit model so that it covers the model (mesh) uniformly?

Just started playing around with SceneKit & ARKit. One issue I'm having is covering the 3DModel with an image texture. What is happening is that part of the 3DModel when rendered in ARKit uses the texture correctly. The other parts are not.

For example I'm using a sofa 3DModel at scale roughly (80 inches W x 36 inches H x 36 inches D). I'm going to cover the mesh with a swatch fabric image texture that measures 290 x 290 pixels.

The results that I'm seeing are the following:

enter image description here

In my code:

        if let matts = child.geometry?.materials {
          var matIndex = 0
          for mat in matts {
            let material = SCNMaterial()
            material.diffuse.contents = UIImage(named: "fabric-coral")
            material.isDoubleSided = true
            child.geometry?.replaceMaterial(at: matIndex, with: material)
            matIndex += 1
          }
        }

My question is how to take the UIImage and have it repeat and cover the mesh without the weird square pattern that it is doing now?

Upvotes: 3

Views: 3083

Answers (1)

lock
lock

Reputation: 2897

In this case your UV texture coordinates must extend outside the 0.0 - 1.0 range, which is fine, but it would explain what you are seeing. The default texture wrapping behaviour of SCNMaterialProperty is CLAMP, that means it takes the pixel values at the edge of your texture and uses this to 'fill in' the rest of the model (parts with UV coords outside 0.0 - 1.0 range).

Changing the wrapT and wrapS properties should fix this.

. . .
material.diffuse.contents = UIImage(named: "fabric-coral")
material.diffuse.wrapT = SCNWrapMode.repeat
material.diffuse.wrapS = SCNWrapMode.repeat
material.isDoubleSided = true
. . .

The SCNWrapMode documentation includes a figure showing the effect of each mode, including one that looks close to what you are currently observing.

Upvotes: 6

Related Questions