2015-10-06 124 views
0

我在場景中創建了一個地板,並將collada文件(.dae)加載到我的場景中,並試圖在該模型下創建一個平面。但我有像下面這樣的問題。僅當使用自定義照相機在SceneKit中場景渲染不正確

enter image description here

此問題產生。場景呈現在正確的系統相機即如果使用不加定製相機 我的代碼如下:

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) 

} 
+0

這可能是這個dae特有的問題。你嘗試過另一種模式嗎?你正在運行iOS 8或9嗎?使用Metal渲染器還是GL渲染器? – Toyos

+0

是的,我也嘗試過其他模型,但結果相同。問題發生在iOS 8和iOS 9中。 –

回答

0

不知道爲什麼,上述問題的存在,但我做了一個不同的方式事情。您可以使用SCNShape在地板上繪製覆蓋圖。我使用UIBezierPath繪製了一個矩形路徑,並設置了SCNShape類的路徑屬性。 :)

1

你可能看到的是被稱爲「z戰鬥」。基本上,你的地板和你的飛機是共存的 - 它們是共面的 - 所以它們的渲染是不明確的。試着將飛機的位置設置得比地面飛機高一些,試着擡起飛機,你應該看到這個問題消失了。

+0

是的,我已經把紅色飛機放在地板上了,但問題是一樣的。我在我的問題中添加了一些細節。我發現了這個選擇,但如果你有任何理由爲什麼會發生這種情況,然後PLZ添加您的答案。謝謝 –

+0

好吧,有可能你沒有提起*足夠*。根據您的「遠」設置,相機可能沒有足夠的精度來正確渲染它(即,它可能看不到兩個標高之間的差異)。我會嘗試把它提升得更遠,看看它是否有效,然後試驗你的「近」和「遠」設置。另外,如果情況只發生在自定義相機中,我會查看其他設置相機,或只是重新使用默認相機。 –