【问题标题】:Scene not rendering properly in SceneKit场景无法在 SceneKit 中正确渲染
【发布时间】:2016-01-03 08:23:54
【问题描述】:

我在场景中创建了一个地板,并将一个 collada 文件 (.dae) 加载到我的场景中,并尝试在该模型下方创建一个平面。但我有如下问题。

仅在使用自定义相机时才会出现此问题。使用系统摄像头时场景渲染正确,即如果没有添加自定义摄像头 我的代码如下:

import UIKit import SceneKit

class TestVC: UIViewController, SCNSceneRendererDelegate {

var cameraNode: SCNNode!
var modelNode: SCNNode!
var scnView: SCNView!
var camNode: SCNNode!

var cameraPosition: SCNVector3!

override func viewDidLoad() {
    super.viewDidLoad()

    scnView = SCNView(frame: self.view.frame)
    scnView.delegate = self
    view.addSubview(scnView)
    scnView.delegate = self

    let scene = SCNScene()
    scnView.scene = scene

    //camera
    let camera = SCNCamera()
    cameraNode = SCNNode()
    cameraNode.camera = camera
    camera.zFar = 1000
    cameraPosition = SCNVector3(0, 100, 150)
    cameraNode.position = cameraPosition
    scene.rootNode.addChildNode(cameraNode)

    //floor
    let floor = SCNFloor()
    floor.reflectivity = 0
    let floorNode = SCNNode(geometry: floor)
    let firstmaterial = SCNMaterial()
    firstmaterial.diffuse.contents = UIImage(named: "art.scnassets/grass.jpg")
    firstmaterial.diffuse.wrapS = SCNWrapMode.Repeat
    firstmaterial.diffuse.wrapT = SCNWrapMode.Repeat
    floor.materials = [firstmaterial]
    scene.rootNode.addChildNode(floorNode)

    let light = SCNLight()
    let lightNode = SCNNode()
    lightNode.light = light
    light.type = SCNLightTypeAmbient
    light.color = UIColor.darkGrayColor()
    scene.rootNode.addChildNode(lightNode)

    let light1 = SCNLight()
    let light1Node = SCNNode()
    light1Node.light = light1
    light1Node.position = SCNVector3(0, 200, -100)
    light1.type = SCNLightTypeOmni
    scene.rootNode.addChildNode(light1Node)

    let modelScene = SCNScene(named: "Barcelona Chair.dae")
    let solNode = modelScene?.rootNode
    solNode?.eulerAngles = SCNVector3(GLKMathDegreesToRadians(-90), 0, 0)
    scene.rootNode.addChildNode(solNode!)
    scnView.allowsCameraControl = true

    let plane = SCNPlane(width: 100, height: 100)
    let planeNode = SCNNode(geometry: plane)
    planeNode.pivot = SCNMatrix4MakeRotation(Float(M_PI_2), 1, 0, 0)//(CGFloat(M_PI_2), 1, 0, 0)
    planeNode.position = SCNVector3(0, 1, 0)
    plane.firstMaterial?.diffuse.contents = UIColor.redColor()
    scene.rootNode.addChildNode(planeNode)

}

【问题讨论】:

  • 这可能是这个 dae 特有的问题。你试过其他型号吗?您运行的是 iOS 8 还是 9?使用 Metal 渲染器还是 GL 渲染器?
  • 是的,我也尝试过其他型号,但结果是一样的。 iOS 8 和 iOS 9 都会出现问题。

标签: ios swift scenekit


【解决方案1】:

您可能会看到所谓的“z-fighting”。基本上,你的地板和你的飞机是共存的——它们是共面的——所以它们的渲染是模棱两可的。尝试通过将飞机的位置设置为高于地板平面将其稍微抬起,您应该会看到这个问题消失了。

【讨论】:

  • 是的,我已经把红色飞机带到了地板上,但问题是一样的。我在我的问题中添加了一些细节。我找到了替代方案,但如果您有任何原因会发生这种情况,请添加您的答案。谢谢
  • 好吧,您可能没有提出足够。根据您的“远”设置,相机可能没有足够的精度来正确渲染它(即,它可能看不到两个高度之间的差异)。我会尝试进一步提高它并查看它是否有效,然后尝试使用您的“近”和远“设置。此外,如果这种情况仅发生在自定义相机中,我会查看其他设置相机,或者只是重新使用默认相机。
【解决方案2】:

不知道为什么会出现上述问题,但我有不同的处理方式。您可以使用 SCNShape 在地板上绘制叠加层。我使用 UIBezierPath 绘制了一个矩形路径,并设置了 SCNShape 类的 path 属性。 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 2019-07-21
    相关资源
    最近更新 更多