SceneKit Imported COLLADA Box not "Lit"

307 views Asked by At

I have a SceneKit project with two object in the scene view. The first object is a plane created via SCNPlane. The second object is a simple box created in Blender. In code, I setup ambient and omnidirectional lighting. It lighting effects work for the plane: enter image description here

But, when I add the box on top of the plane, the lighting effects work on the plane but not the box imported from COLLADA file: enter image description here

I suspect the problem has to do with normals, but I am not sure. Has anyone importing DAE via SceneKit experienced this? The setup code for the lighting and objects is this:

private func setupAmbientLight() {

    // setup ambient light source
    let ambientLightNode = SCNNode()
    ambientLightNode.light = SCNLight()
    ambientLightNode.light!.type = SCNLight.LightType.ambient
    ambientLightNode.light!.color = NSColor(white: 0.35, alpha: 1.0).cgColor

    // add to scene
    guard let scene = sceneView.scene else {

        return
    }
    scene.rootNode.addChildNode(ambientLightNode)
}

private func setupOmniDirectionalLight() {

    // initialize noe
    let omniLightNode = SCNNode()
    // assign light
    omniLightNode.light = SCNLight()
    // set type
    omniLightNode.light!.type = SCNLight.LightType.omni
    // color and position
    omniLightNode.light!.color = NSColor(white: 0.56, alpha: 1.0).cgColor
    omniLightNode.position = SCNVector3Make(0.0, 2000.0, 0.0)

    // add to scene
    guard let scene = sceneView.scene else {

        return
    }
    scene.rootNode.addChildNode(omniLightNode)
}

private func setupPlane() {

    // create plane geometry with size and material properties
    let myPlane = SCNPlane(width: planeSideLength, height: planeSideLength)
    myPlane.firstMaterial!.diffuse.contents = NSColor.orange.cgColor
    myPlane.firstMaterial!.specular.contents = NSColor.white.cgColor

    // intialize node
    let planeNode = SCNNode()
    // assign plane geometry to the node
    planeNode.geometry = myPlane

    // rotate -90.0 about the x-axis
    let rotMat = SCNMatrix4MakeRotation(-CGFloat(M_PI/2.0), 1.0, 0.0, 0.0)
    planeNode.transform = rotMat
    planeNode.position = SCNVector3Make(0.0, 0.0, 0.0)

    // setup the node's physics body property
    planeNode.physicsBody = SCNPhysicsBody(type: .static, shape: SCNPhysicsShape(geometry: myPlane, options: nil))
    planeNode.physicsBody!.categoryBitMask = PhysicsMask3DOF.plane.rawValue

    // add to scene
    guard let scene = sceneView.scene else {

        return
    }

    scene.rootNode.addChildNode(planeNode)
}

private func setupRobot() {

    guard let mainScene = sceneView.scene else {

        return
    }

    let bundle = Bundle.main

    guard let url = bundle.url(forResource: "robot.scnassets/test_cube", withExtension: "dae") else {

        return
    }

    var cubeScene: SCNScene?

    do {

        try cubeScene = SCNScene.init(url: url, options: nil)
    }
    catch {

        return
    }

    guard let cubeNode = cubeScene!.rootNode.childNode(withName: "Cube", recursively: true) else {

        return
    }

    cubeNode.removeFromParentNode()
    cubeNode.scale = SCNVector3Make(2000.0, 2000.0, 2000.0)
    cubeNode.geometry!.firstMaterial!.diffuse.contents = NSColor.blue.cgColor
    cubeNode.geometry!.firstMaterial!.specular.contents = NSColor.white.cgColor

    mainScene.rootNode.addChildNode(cubeNode)
}

Update:

So I commented the code for importing the box from DAE and instead added code to create the box via SCNBox and the lighting effects appear to work: enter image description here

1

There are 1 answers

3
xBACP On BEST ANSWER

Duh, the box is [2000 x 2000 x 2000] and its node is position at (0, 0, 0). The position of the omni-light source node is (0, 2000, 0). Just needed to move the light source up. Which then begs the question of why was the box properly lit when I created the box with the same dimensions via SCNBox function instead of importing from the DAE file