gpt4 book ai didi

swift - SceneKit 导入的 COLLADA 框不是 "Lit"

转载 作者:行者123 更新时间:2023-11-30 12:48:05 25 4
gpt4 key购买 nike

我有一个 SceneKit 项目,场景 View 中有两个对象。第一个对象是通过 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

最佳答案

呃,盒子是 [2000 x 2000 x 2000],它的节点位置在 (0, 0, 0)。全光源节点的位置为(0, 2000, 0)。只需要将光源向上移动即可。这就引出了一个问题:当我通过 SCNBox 函数创建具有相同尺寸的盒子而不是从 DAE 文件导入时,为什么盒子被正确点亮

关于swift - SceneKit 导入的 COLLADA 框不是 "Lit",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41364587/

25 4 0
文章推荐: swift - viewDidLoad 中的异步任务
文章推荐: C# 将 Excel 图表作为对象(而不是图片)导出到 Word
文章推荐: Javascript:设置