首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SCNScene中SCNCamera的默认位置

SCNScene中SCNCamera的默认位置
EN

Stack Overflow用户
提问于 2021-07-24 10:45:57
回答 1查看 57关注 0票数 0

我想我并不完全理解SCNCamera是如何工作的。

所以我得到了以下代码:

代码语言:javascript
复制
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的情况下得到的东西,并从那里以编程方式旋转地球。

谢谢大家!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-24 12:44:55

如果您创建了一个camera类,关闭allowsCameraControl,并取出PointOfView deal,那么您的lookAtConstraint将指向地球节点,您可以通过为其中任何一个设置.position来处理距离。这是一个让你专注于地球的例子- strafe只是让你知道你可以用相机节点做什么。旋转地球不会影响摄影机节点。你可以在那里做任何你想做的旋转,相机仍然会指向地球。

代码语言:javascript
复制
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
        }
    }
    //********************************************************************
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68506605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档