Search code examples
iosswifttexturesoffsetscenekit

SceneKit - Textures not properly displayed


I have a cube (rounded) and want to display a texture on one of it's side. I can access the material on that side with:

var tex1: SCNMaterial! = cube.geometry?.materialWithName("_1")!

I then set it's image contents:

tex1.diffuse.contents = "cube1"

This then looks like this:

cube with texture

This shows me that it does work, but the white part is not in the center as it should be. (The image I am using has the white part in the center.)
I tried to use offset to move the image around on the surface, I would like scale it as well. I tried it like this:

tex1.diffuse.contents.offset = SCNVector3Make(20, 0, 0)

That gives me errors: it says it cannot assign the result of that expression. (I also tried contentMode, same error, I think because these are for UI, not SCN)

Questions

  1. Does anyone know what I can do?
  2. Maybe offset is not the way to go?
  3. How can I scale the image?

Solution

  • The type of a material property's contents is AnyObject, which means the compiler will allow you to call any method (defined on any object type) on it. That doesn't mean all methods or property accessors are actually really implemented by the actual class that's in your particular contents.

    Material properties do have a contentsTransform option, though. Have you looked at that?