你能改变scnView.autoenablesDefaultLighting的属性吗?

Can you change the properties of scnView.autoenablesDefaultLighting?

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

我试过使用各种其他灯光,单独对它们进行编码,但是因为它们作为节点添加到场景中,当我设置 scnView.allowsCameraControl = true 时,灯光(在那些情况下)本身会移动。一旦用户开始四处移动相机,默认照明是唯一会保持 "stationary" 的照明。你能access/control默认光照的属性吗?

如果您想控制场景,请忘记 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 而不是相机的子项。

如果有人想知道如何使用 swift ui 的新 Scenekit 集成来设置整个场景,请看这里。这个工作正常。

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,您可以使用名称代替它。