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



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

导入 UIKit导入场景套件

枚举墙类型 { 剩余案例 案例右 表壳顶部 表壳底部 案件转发 底盖}

类视图控制器: 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=0https://www.dropbox.com/s/wd23xepcir3wggk/IMG_0188.PNG?dl=0

set isDoubleSdided to true

node.geometry?.firstMaterial?.isDoubleSided = true

最新更新