SceneKit 导入 COLLADA Box 未 "Lit"
SceneKit Imported COLLADA Box not "Lit"
我有一个 SceneKit 项目,场景视图中有两个对象。第一个对象是通过 SCNPlane 创建的平面。第二个对象是在 Blender 中创建的一个简单的盒子。在代码中,我设置了环境光和全向光。它的灯光效果适用于飞机:
但是,当我在飞机顶部添加盒子时,灯光效果在飞机上起作用,但在从 COLLADA 文件导入的盒子上不起作用:
我怀疑问题与法线有关,但我不确定。有没有人通过 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 创建盒子的代码,并且灯光效果似乎有效:
Duh,盒子是 [2000 x 2000 x 2000],它的节点位于 (0, 0, 0)。全光源节点的位置为(0, 2000, 0)。只需要将光源向上移动。这就引出了一个问题,当我通过 SCNBox 函数创建具有相同尺寸的盒子而不是从 DAE 文件导入时,为什么盒子正确点亮
我有一个 SceneKit 项目,场景视图中有两个对象。第一个对象是通过 SCNPlane 创建的平面。第二个对象是在 Blender 中创建的一个简单的盒子。在代码中,我设置了环境光和全向光。它的灯光效果适用于飞机:
但是,当我在飞机顶部添加盒子时,灯光效果在飞机上起作用,但在从 COLLADA 文件导入的盒子上不起作用:
我怀疑问题与法线有关,但我不确定。有没有人通过 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 创建盒子的代码,并且灯光效果似乎有效:
Duh,盒子是 [2000 x 2000 x 2000],它的节点位于 (0, 0, 0)。全光源节点的位置为(0, 2000, 0)。只需要将光源向上移动。这就引出了一个问题,当我通过 SCNBox 函数创建具有相同尺寸的盒子而不是从 DAE 文件导入时,为什么盒子正确点亮