【问题标题】:Default Position of a SCNCamera in an SCNSceneSCNScene 中 SCNCamera 的默认位置
【发布时间】:2021-10-01 00:28:13
【问题描述】:

我想我不完全理解 SCNCamera 应该如何工作。

所以我有以下代码:

import SwiftUI
import SceneKit

struct ContentView: View {
    let scene = SCNScene(named: "Earth.usdz")
    var cameraNode: SCNNode? {
        let cameraNode = SCNNode()
        cameraNode.camera = SCNCamera()
        cameraNode.position = SCNVector3(x: 0, y: 0, z: 2)
        return cameraNode
    }
    var body: some View {
        VStack {
            SceneView(
                scene: scene,
                pointOfView: cameraNode,
                options: [
                    .allowsCameraControl,
                    .autoenablesDefaultLighting
                ]
            )
            .frame(width: UIScreen.main.bounds.width , height: UIScreen.main.bounds.height/2)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

删除或添加 pointOfView: cameraNode 会导致:

没有

我假设有一些默认的相机位置(也许还有其他属性?)?我怎么看那些是什么?如何取消反转地球仪?

我想在不设置pointOfView 的情况下或多或少地模仿我得到的东西,然后以编程方式旋转地球。

谢谢大家!

【问题讨论】:

    标签: ios swiftui scenekit 3d-modelling


    【解决方案1】:

    如果您创建一个相机类,关闭allowsCameraControl,并取出PointOfView 交易,然后您的lookAtConstraint 指向地球节点,您可以通过设置.position 来调整距离。这是一个让您专注于地球的示例 - 扫射只是让您了解可以使用相机节点做什么。旋转地球不会影响相机节点。您可以在那里进行任何您想要的旋转,并且相机仍将指向地球。

    class Camera
    {
        var cameraEye = SCNNode()
        var cameraFocus = SCNNode()
            
        var centerX: Int = 100
        var strafeDelta: Float = 0.8
        var zoomLevel: Int = 35
        var zoomLevelMax: Int = 35              // Max number of zoom levels
        
        //********************************************************************
        init()
        {
            cameraEye.name = "Camera Eye"
            cameraFocus.name = "Camera Focus"
            
            cameraFocus.isHidden = true
            cameraFocus.position  =  SCNVector3(x: 0, y: 0, z: 0)
            
            cameraEye.camera = SCNCamera()
            cameraEye.constraints = []
            cameraEye.position = SCNVector3(x: 0, y: 15, z: 0.1)
            
            let vConstraint = SCNLookAtConstraint(target: cameraFocus)
            vConstraint.isGimbalLockEnabled = true
            cameraEye.constraints = [vConstraint]
        }
        //********************************************************************
        func reset()
        {
            centerX = 100
            cameraFocus.position  =  SCNVector3(x: 0, y: 0, z: 0)
            cameraEye.constraints = []
            cameraEye.position = SCNVector3(x: 0, y: 32, z: 0.1)
            cameraFocus.position = SCNVector3Make(0, 0, 0)
            
            let vConstraint = SCNLookAtConstraint(target: cameraFocus)
            vConstraint.isGimbalLockEnabled = true
            cameraEye.constraints = [vConstraint]
        }
        //********************************************************************
        func strafeRight()
        {
            if(centerX + 1 < 112)
            {
                centerX += 1
                cameraEye.position.x += strafeDelta
                cameraFocus.position.x += strafeDelta
            }
        }
        //********************************************************************
        func strafeLeft()
        {
            if(centerX - 1 > 90)
            {
                centerX -= 1
                cameraEye.position.x -= strafeDelta
                cameraFocus.position.x -= strafeDelta
            }
        }
        //********************************************************************
    }
    

    【讨论】:

    • 没有真正的“默认”,因为它取决于很多东西,例如节点的规模、您想要的距离等。
    猜你喜欢
    • 2016-08-27
    • 2017-08-15
    • 2019-10-12
    • 2012-08-26
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    • 2018-06-07
    相关资源
    最近更新 更多