5

SceneKit beginner here, doing an iOS project.

I have been wrapping textures around SCN geometry (spheres, so far) like this:

let scene = SCNScene()
var planet : SCNGeometry
planet = SCNSphere(radius: 1.0)
let material = SCNMaterial()
material.diffuse.contents = UIImage(named: "texture.jpg")
planet.materials = [material]
let planetNode = SCNNode(geometry: planet)
scene.rootNode.addChildNode(planetNode)

And this seems to be working fine. I have textures that are meant to apply to spheres anyways.

However, when I try to load a custom obj file, texture materials seem to refuse to map onto it when I load the geometry from the obj like so:

let scene = SCNScene()
var planet : SCNGeometry
let tempScene = SCNScene(named: "model.obj")
planet = tempScene!.rootNode.childNodes[0].geometry!
let material = SCNMaterial()
material.diffuse.contents = UIImage(named: "texture.jpg")
planet.materials = [material]
let planetNode = SCNNode(geometry: planet)
scene.rootNode.addChildNode(planetNode)

The model object, however, appears to show just fine, albeit, without the texture.
Any pointers?

Brandon Slaght
  • 1,025
  • 1
  • 12
  • 34
  • I am actually very tired, so forgive if I am overseeing it, but do you acutally set the material `planet.geometry.firstMaterial = material`? – shallowThought Nov 06 '16 at 20:11
  • @shallowThought I do, I forgot to include that line. Editing. – Brandon Slaght Nov 06 '16 at 20:33
  • What is the geometry type of the scene `let_model`? Also a sphere? – Byron Coetsee Nov 06 '16 at 20:44
  • @ByronCoetsee no, I updated to reflect that it is an object file. Its a sphereoid. – Brandon Slaght Nov 06 '16 at 20:45
  • Since a "Sphereoid" is not a regular SCNScene type and you say it's a custom object, what are you subclassing? Do you have any code to go with it? You need to declare it as a type somewhere other than simply `SCNGeometry`... For example, append `as! SCNSphere(oid)` to your line `planet = tempScene!.rootNode.childNodes[0].geometry!` – Byron Coetsee Nov 06 '16 at 20:58
  • @ByronCoetsee by custom object, I mean that it is an obj file that is not one of the shapes built in to scenekit. Not that I am subclassing anything. – Brandon Slaght Nov 06 '16 at 21:06
  • Ok then unfortunately that is beyond me :) I could hazard a guess and say it's to do with the ambiguity of the object type... Since it's not a normal scenekit shape and you never declare it's type, I would wonder how it know what it should be texturizing. Note: even if you built your own file, you might still be dragging a sphere in there, in which case you should cast to SCNSphere in your code when retrieving. – Byron Coetsee Nov 06 '16 at 21:11
  • 3
    does your model have texture coordinates? These are used to specify how a texture is mapped onto an object. You can verify that in the SceneKit Scene Editor inside Xcode (see illustration at http://stackoverflow.com/a/39699260/2997825) – mnuages Nov 07 '16 at 10:15

0 Answers0