gpt4 book ai didi

swift - 当我使用 SceneKit 在虚拟房间内的 SCNBox 周围移动 SCNCamera 时,房间纹理未完全渲染

转载 作者:行者123 更新时间:2023-11-30 10:47:32 25 4
gpt4 key购买 nike

我尝试使用 SceneKit 创建一个虚拟房间并放入一个盒子。此外,我还创建了一个带有相机的单独节点,名为“cameraOrbitNode”(相机位于房间的墙壁和盒子之间)。主要目标是在房间内的盒子周围移动“cameraOrbitNode”。我通过平移手势识别器实现了它。

导入UIKit导入SceneKit

枚举 WallType { 案例剩余 案例右 外壳顶部 箱底 案例转发 表背}

ViewController 类:UIViewController {

//MARK: - Outlets
@IBOutlet fileprivate weak var sceneView: SCNView!

//MARK: - Properties
fileprivate let roomLength: CGFloat = 12
fileprivate let cubeLength: CGFloat = 1
fileprivate var cameraOrbitNode: SCNNode?
fileprivate let panModifier: CGFloat = 100

//MARK: - Life cycle
override func viewDidLoad() {
super.viewDidLoad()

setupSceneView()
setupRoom()
setupCameraOrbitNode()
setupGestures()
}

func setupSceneView() {
sceneView.scene = SCNScene()
sceneView.autoenablesDefaultLighting = true
}

func setupRoom() {
let roomNode = SCNNode()
roomNode.position = SCNVector3(0, 0, 0)
for wallType in Array<WallType>([.left, .right, .top, .bottom, .forward, .back]) {
roomNode.addChildNode(getWallNodeWith(wallType: wallType))
}
roomNode.addChildNode(getCubeNode())
sceneView.scene?.rootNode.addChildNode(roomNode)
}

func getWallNodeWith(wallType: WallType) -> SCNNode {
let wallGeometrySize = getWallSize()
let wallGeometry = SCNPlane(width: wallGeometrySize.width, height: wallGeometrySize.height)
wallGeometry.firstMaterial?.diffuse.contents = UIColor.darkGray
let wallNode = SCNNode(geometry: wallGeometry)
wallNode.position = getWallPositionWith(wallType: wallType)
wallNode.eulerAngles = getWallEulerAnglesWith(wallType: wallType)
return wallNode
}

func getWallSize() -> CGSize {
return CGSize(width: roomLength, height: roomLength)
}

func getWallPositionWith(wallType: WallType) -> SCNVector3 {
switch wallType {
case .left:
return SCNVector3(-roomLength / 2, 0, 0)
case .right:
return SCNVector3(roomLength / 2, 0, 0)
case .forward:
return SCNVector3(0, 0, -roomLength / 2)
case .back:
return SCNVector3(0, 0, roomLength / 2)
case .top:
return SCNVector3(0, roomLength / 2, 0)
case .bottom:
return SCNVector3(0, -roomLength / 2, 0)
}
}

func getWallEulerAnglesWith(wallType: WallType) -> SCNVector3 {
switch wallType {
case .left, .right:
return SCNVector3(0, CGFloat.pi / 2, 0)
case .forward, .back:
return SCNVector3(0, 0, 0)
case .top, .bottom:
return SCNVector3(CGFloat.pi / 2, 0, 0)
}
}

func getCubeNode() -> SCNNode {
let cubeGeometry = SCNBox(width: cubeLength, height: cubeLength, length: cubeLength, chamferRadius: 0)
cubeGeometry.firstMaterial?.diffuse.contents = UIColor.blue
let cubeNode = SCNNode(geometry: cubeGeometry)
cubeNode.position = SCNVector3(0, 0, 0)
return cubeNode
}

func setupCameraOrbitNode() {
let camera = SCNCamera()
camera.zNear = 0
camera.zFar = Double(roomLength)
let cameraNode = SCNNode()
cameraNode.position = SCNVector3(0, 0, (roomLength + cubeLength) / 4)
cameraNode.camera = camera
cameraOrbitNode = SCNNode()
guard let cameraOrbitNode = cameraOrbitNode else { return }

cameraOrbitNode.addChildNode(cameraNode)
cameraOrbitNode.position = SCNVector3(0, 0, 0)
sceneView.scene?.rootNode.addChildNode(cameraOrbitNode)
}

@objc func actionPan(gesture: UIPanGestureRecognizer) {
let translation = gesture.velocity(in: gesture.view)
cameraOrbitNode?.eulerAngles.y -= Float(translation.x/CGFloat(panModifier)) * .pi / 180
cameraOrbitNode?.eulerAngles.x -= Float(translation.y/CGFloat(panModifier)) * .pi / 180
}

func setupGestures() {
sceneView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(actionPan(gesture:))))
}

}

但在某些情况下,您可以看到房间的透明墙壁(基本上,当我在盒子周围移动相机时,房间纹理并未完全渲染)。我正在更改一些相机属性(zNear、zFar),但没有任何帮助。请参阅下面所附图像的链接。非常感谢您的帮助。 https://www.dropbox.com/s/ihxjrsgjfzidoff/IMG_0187.PNG?dl=0 https://www.dropbox.com/s/wd23xepcir3wggk/IMG_0188.PNG?dl=0

最佳答案

将 isDoubleSdided 设置为 true

node.geometry?.firstMaterial?.isDoubleSided = true

关于swift - 当我使用 SceneKit 在虚拟房间内的 SCNBox 周围移动 SCNCamera 时,房间纹理未完全渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55459895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com