SceneKit导入的COLLADA Box没有“点亮”

时间:2016-12-28 15:01:41

标签: swift macos blender scenekit collada

我有一个SceneKit项目,在场景视图中有两个对象。第一个对象是通过SCNPlane创建的平面。第二个对象是在Blender中创建的简单框。在代码中,我设置了环境和全向照明。它的照明效果适用于飞机: enter image description here

但是,当我在飞机顶部添加框时,灯光效果在飞机上起作用,但不是从COLLADA文件导入的框: enter image description here

我怀疑问题与法线有关,但我不确定。有没有人通过SceneKit导入DAE经历过这个?照明和对象的设置代码如下:

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)
}

更新

所以我评论了从DAE导入盒子的代码,而是添加了代码来通过SCNBox创建盒子,并且灯光效果似乎起作用: enter image description here

1 个答案:

答案 0 :(得分:0)

Duh,该框为[2000 x 2000 x 2000],其节点位于(0,0,0)。全光源节点的位置是(0,2000,0)。只需要移动光源。然后,当我通过SCNBox函数创建具有相同尺寸的盒子而不是从DAE文件导入时,问题为什么盒子正确点亮了