Search code examples
swift33dtexturesscenekit

SCENE KIT: Load material error


Could you explain me - why my textures is not showing in 3D view of Scene Kit ?

I'm trying to add diffuse color and ambient - but it's doesn't works.

So here is my question - why ?

Showing only gray-material colour: enter image description here Here is example of my GameViewController code in swift :

import UIKit
import QuartzCore
import SceneKit
import SpriteKit

class GameViewController: UIViewController {

var sceneView: SCNView!
var scene: SCNScene!
var label: UILabel!
var timer: Timer!
var time: Int = 0
var globeNode: SCNNode!

override func viewDidLoad() {
    super.viewDidLoad()

    sceneView = self.view as! SCNView
    sceneView.allowsCameraControl = true
    scene = SCNScene()
    sceneView.scene = scene

    let camera = SCNCamera()
    let cameraNode = SCNNode()
    cameraNode.camera = camera
    cameraNode.position = SCNVector3(x: -3.0, y: 3.0, z: 3.0)

    let ambientLight = SCNLight()
    ambientLight.type = SCNLight.LightType.ambient
    ambientLight.color = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1.0)
    cameraNode.light = ambientLight

    let light = SCNLight()
    light.type = SCNLight.LightType.spot
    light.spotInnerAngle = 30.0
    light.spotOuterAngle = 80.0
    light.castsShadow = true
    let lightNode = SCNNode()
    lightNode.light = light
    lightNode.position = SCNVector3(x: 1.5, y: 1.5, z: 1.5)

    let cubeGeometry = SCNBox(width: 1.0, height: 1.0, length: 1.0, chamferRadius: 0.0)
    let cubeNode = SCNNode(geometry: cubeGeometry)

    let planeGeometry = SCNPlane(width: 100.0, height: 100.0)
    let planeNode = SCNNode(geometry: planeGeometry)
    planeNode.eulerAngles = SCNVector3(x: GLKMathDegreesToRadians(-90), y: 0, z: 0)
    planeNode.position = SCNVector3(x: 0, y: -0.5, z: 0)

    self.globeNode = SCNNode()
    let theGlobeGeometry = SCNSphere(radius: 110)
    theGlobeGeometry.firstMaterial?.diffuse.contents = UIImage(named:"earth_diffuse.jpg")
    theGlobeGeometry.firstMaterial?.ambient.contents = UIImage(named:"earth_ambient2.jpeg")
    //      theGlobeGeometry.firstMaterial.ambient.contents = UIImage(named:"earth_ambient.jpg")
    theGlobeGeometry.firstMaterial?.specular.contents = UIImage(named:"earth_specular.jpg")
    theGlobeGeometry.firstMaterial?.emission.contents = nil
    theGlobeGeometry.firstMaterial?.transparent.contents = nil
    theGlobeGeometry.firstMaterial?.reflective.contents = nil
    theGlobeGeometry.firstMaterial?.multiply.contents = nil
    theGlobeGeometry.firstMaterial?.normal.contents = UIImage(named:"earth_normal.jpg")

    let theGlobeModelNode = SCNNode(geometry: theGlobeGeometry)
    self.globeNode.addChildNode(theGlobeModelNode)

    let theCloudGeometry = SCNSphere(radius:0.501)
    theCloudGeometry.firstMaterial?.diffuse.contents = nil
    theCloudGeometry.firstMaterial?.ambient.contents = nil
    theCloudGeometry.firstMaterial?.specular.contents = nil
    theCloudGeometry.firstMaterial?.emission.contents = nil
    theCloudGeometry.firstMaterial?.transparent.contents = UIImage(named:"earth_clouds.png")
    theCloudGeometry.firstMaterial?.reflective.contents = nil
    theCloudGeometry.firstMaterial?.multiply.contents = nil
    theCloudGeometry.firstMaterial?.normal.contents = nil

    let theCloudModelNode = SCNNode(geometry: theCloudGeometry)
    self.globeNode.addChildNode(theCloudModelNode)

    // animate the 3d object
    let animation: CABasicAnimation = CABasicAnimation(keyPath: "rotation")
    animation.toValue = NSValue(scnVector4: SCNVector4(x: 1, y: 1, z: 0, w: Float(M_PI)*2))
    animation.duration = 5
    animation.repeatCount = MAXFLOAT //repeat forever
    globeNode.addAnimation(animation, forKey: nil)

    let redMaterial = SCNMaterial()
    redMaterial.diffuse.contents = UIColor.red
    cubeGeometry.materials = [redMaterial]

    let greenMaterial = SCNMaterial()
    greenMaterial.diffuse.contents = UIColor.green
    planeGeometry.materials = [greenMaterial]

    let constraint = SCNLookAtConstraint(target: cubeNode)
    constraint.isGimbalLockEnabled = true
    cameraNode.constraints = [constraint]
    lightNode.constraints = [constraint]

    scene.rootNode.addChildNode(lightNode)
    scene.rootNode.addChildNode(cameraNode)
    //scene.rootNode.addChildNode(cubeNode)
    scene.rootNode.addChildNode(planeNode)
    scene.rootNode.addChildNode(globeNode)

}

func addSceneToScene() {
    let geoScene = SCNScene(named: "art.scnassets/driod.dae")
    scene.rootNode.addChildNode((geoScene?.rootNode.childNode(withName: "Head_009", recursively: true))!)

}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
   // UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")

    return UIInterfaceOrientationMask(rawValue: UIInterfaceOrientationMask.landscapeLeft.rawValue)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

Thanks in advance!


Solution

  • That is probably because it is not finding the texture images. Are the images in your bundle?

    Or did you forget to include the images in your target?