在SceneKit中场景渲染不正确
我在场景中创建了一个地板,并将collada文件(.dae)加载到我的场景中,并试图在该模型下创建一个平面。但我有像下面这样的问题。仅当使用自定义照相机在SceneKit中场景渲染不正确
此问题产生。场景呈现在正确的系统相机即如果使用不加定制相机 我的代码如下:
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) }
不知道为什么,上述问题的存在,但我做了一个不同的方式事情。您可以使用SCNShape在地板上绘制覆盖图。我使用UIBezierPath绘制了一个矩形路径,并设置了SCNShape类的路径属性。 :)
你可能看到的是被称为“z战斗”。基本上,你的地板和你的飞机是共存的 - 它们是共面的 - 所以它们的渲染是不明确的。试着将飞机的位置设置得比地面飞机高一些,试着抬起飞机,你应该看到这个问题消失了。
是的,我已经把红色飞机放在地板上了,但问题是一样的。我在我的问题中添加了一些细节。我发现了这个选择,但如果你有任何理由为什么会发生这种情况,然后PLZ添加您的答案。谢谢 –
好吧,有可能你没有提起*足够*。根据您的“远”设置,相机可能没有足够的精度来正确渲染它(即,它可能看不到两个标高之间的差异)。我会尝试把它提升得更远,看看它是否有效,然后试验你的“近”和“远”设置。另外,如果情况只发生在自定义相机中,我会查看其他设置相机,或只是重新使用默认相机。 –
这可能是这个dae特有的问题。你尝试过另一种模式吗?你正在运行iOS 8或9吗?使用Metal渲染器还是GL渲染器? – Toyos
是的,我也尝试过其他模型,但结果相同。问题发生在iOS 8和iOS 9中。 –