【问题标题】:Room texture isn't fully rendered when I move SCNCamera around SCNBox inside the virtual room using SceneKit当我使用 SceneKit 在虚拟房间内的 SCNBox 周围移动 SCNCamera 时,房间纹理未完全渲染
【发布时间】:2019-08-22 21:37:52
【问题描述】:

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

导入 UIKit 导入 SceneKit

枚举 WallType { 左案 案例权 案例顶部 外壳底部 案例转发 后盖 }

类视图控制器: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

【问题讨论】:

    标签: swift scenekit


    【解决方案1】:

    将 isDoubleSdided 设置为 true

    node.geometry?.firstMaterial?.isDoubleSided = true
    

    【讨论】:

      猜你喜欢
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 2021-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-18
      相关资源
      最近更新 更多