【问题标题】:Can you change the properties of scnView.autoenablesDefaultLighting?你能改变 scnView.autoenablesDefaultLighting 的属性吗?
【发布时间】:2021-08-27 16:05:04
【问题描述】:

我需要灯光在我的场景中保持“静止”。到目前为止我发现的最好的照明方法是实际使用scnView.autoenablesDefaultLighting = true 但是,我不知道是否有任何方法可以控制某些属性。光的强度有点太亮了,光的位置与我想要的位置有点不同,这些属性。

我尝试过使用各种其他灯光,将它们单独编码,但是因为它们作为节点添加到场景中,所以当我设置 scnView.allowsCameraControl = true 时,灯光(在这些情况下)本身会移动。一旦用户开始四处移动相机,默认照明是唯一保持“静止”的照明。您可以访问/控制默认照明的属性吗?

【问题讨论】:

  • 这里的诀窍是将新灯光添加到相机节点或其子节点。这样,当相机移动时,光线也会移动。尝试通过scnView.pointOfView property 访问默认相机节点。
  • 让你的灯光成为相机的孩子,让相机成为根的孩子。将几何节点放入容器中,使其成为根的子节点。保持几何独立于相机。 (永远不要移动根!)默认照明和相机在选项中是基本的。自己创建会更好,也不会那么难。
  • 谢谢!我试试看!
  • @lock,@bpedit,好的,我刚刚尝试了这两个,但我仍然遇到同样的问题。首先,我将我的灯光设为相机cameraNode.addChild(Omnilight) 的子级,同时将相机设为主场景scene.rootnode.addChild(cameraNode) 的子级。毕竟,scnView.allowsCameraControl 仍然在移动场景,灯光也随之移动。我还尝试访问视点属性scnView.pointOfView = cameraNode,即使它没有标记错误,最终结果也是一样的!场景移动,灯光随之移动!!想法??
  • allowsCameraControl 正在移动您的整个场景。您需要操作并查看您的 SCNCamera

标签: ios swift scenekit


【解决方案1】:

如果您想控制场景,请忘记 allowsCameraControl 和默认摄像机和灯光。

let sceneView = SCNView()
let cameraNode = SCNNode()          // the camera
var baseNode = SCNNode()            // the basic model-root
let keyLight = SCNLight()       ;   let keyLightNode = SCNNode()
let ambientLight = SCNLight()   ;   let ambientLightNode = SCNNode()

func sceneSetup() {
    let scene = SCNScene()
    // add to an SCNView
    sceneView.scene = scene

    // add the container node containing all model elements
    sceneView.scene!.rootNode.addChildNode(baseNode)

    cameraNode.camera = SCNCamera()
    cameraNode.position = SCNVector3Make(0, 0, 50)
    scene.rootNode.addChildNode(cameraNode)

    keyLight.type = SCNLightTypeOmni
    keyLightNode.light = keyLight
    keyLightNode.position = SCNVector3(x: 10, y: 10, z: 5)
    cameraNode.addChildNode(keyLightNode)

    ambientLight.type = SCNLightTypeAmbient
    let shade: CGFloat = 0.40
    ambientLight.color = UIColor(red: shade, green: shade, blue: shade, alpha: 1.0)
    ambientLightNode.light = ambientLight
    cameraNode.addChildNode(ambientLightNode)

    // view the scene through your camera  
    sceneView.pointOfView = cameraNode

    // add gesture recognizers here
}

移动或旋转cameraNode 以在视图中实现运动。或者,移动或旋转baseNode。无论哪种方式,您的灯光都相对于相机保持固定。

如果您希望您的灯光相对于模型固定,请将它们设为 baseNode 而不是相机的子级。

【讨论】:

  • 我对节点位置很好奇,如果你能解释一下相机节点 z 是 50 和 Omni light 位置是如何一起的,那就太好了
【解决方案2】:

如果有人想知道如何通过新的 Scenekit 与 swift ui 的集成来设置整个场景,请参考这里。这工作正常。

struct TestControllerNew: View {
    let sceneView = SCNView()
    let cameraNode = SCNNode()
    var baseNode = SCNNode()
    let id = "D69A09F8-EA80-4231-AD35-4A9908B4343C"
    var scene = SCNScene()

    var body: some View {
        SceneView(
            scene: sceneSetup(),
            pointOfView: cameraNode,
            options: [
                .autoenablesDefaultLighting
            ]
            )
 
}
    
    func getTermsOfUseURL() -> URL {
        let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        return paths[0].appendingPathComponent("\(id).usdz")
    }
    
    func sceneSetup() -> SCNScene {
        let scene = try! SCNScene(url: getTermsOfUseURL())
        // add to an SCNView
        sceneView.scene = scene

        // add the container node containing all model elements
        sceneView.scene!.rootNode.addChildNode(baseNode)

        cameraNode.camera = SCNCamera()
        cameraNode.position = SCNVector3Make(0, 1, 10)
        scene.rootNode.addChildNode(cameraNode)


        // view the scene through your camera
        sceneView.pointOfView = cameraNode

        // add gesture recognizers here
        return scene
    }
}

这里从文档目录获取的USDZ文件作为URL,你可以用name代替。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 2015-05-03
    • 1970-01-01
    • 2023-04-11
    • 2011-02-26
    • 1970-01-01
    相关资源
    最近更新 更多