SceneKit 中的场景渲染不正确
Scene not rendering properly in SceneKit
我在场景中创建了一个地板并将 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)
}
不知道为什么会出现上述问题,但我有不同的处理方法。您可以使用 SCNShape 在地板上绘制叠加层。我使用 UIBezierPath 绘制了一个矩形路径,并设置了 SCNShape class 的路径 属性。 :)
您可能看到的是 "z-fighting"。基本上,你的地板和你的平面是共存的——它们是共面的——所以它们的渲染是不明确的。尝试将平面的位置设置为高于地板平面,将其抬高一点,您应该会看到此问题消失。
我在场景中创建了一个地板并将 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) }
不知道为什么会出现上述问题,但我有不同的处理方法。您可以使用 SCNShape 在地板上绘制叠加层。我使用 UIBezierPath 绘制了一个矩形路径,并设置了 SCNShape class 的路径 属性。 :)
您可能看到的是 "z-fighting"。基本上,你的地板和你的平面是共存的——它们是共面的——所以它们的渲染是不明确的。尝试将平面的位置设置为高于地板平面,将其抬高一点,您应该会看到此问题消失。