2016-05-19 145 views
0

我正在使用SceneKit創建一個簡單的項目,無論我嘗試什麼,都無法在場景編輯器或編譯的應用程序中出現任何陰影。Xcode SceneKit陰影不會渲染

我試圖創建一個簡單的框,將其放置在一個平面上並添加斑點光。代碼將如下所示:

// Create some properties 
var scnView: SCNView! 
var scnMasterScene: SCNScene! 
var boxNode: SCNNode! 
var cameraPerspNode: SCNNode! 
var cameraOrthNode: SCNNode! 

func applicationDidFinishLaunching(aNotification: NSNotification) { 
    // Insert code here to initialize your application 

    setupView() 
    createAndAddBoxScene() 
    setupCamera() 
    createAndAddSpotLight() 
} 

// Setup the view. 
func setupView() { 
    scnView = self.sceneView as SCNView 

    // Set the scnView properties. 
    scnView.showsStatistics = true 
    scnView.autoenablesDefaultLighting = false 
    scnView.allowsCameraControl = true 

    // Create a Master scene. 
    scnMasterScene = SCNScene() 

    // Set the scene view's scene node to the master scene. 
    scnView.scene = scnMasterScene 
} 

// Setup the scene. 
func createAndAddBoxScene() { 

    // Create a box of type SCNGeometry 
    let boxGeometry: SCNGeometry = SCNBox(width: 2000, height: 2000, length: 2000, chamferRadius: 100) 

    // Add a difuse colour to the box' first material. 
    boxGeometry.firstMaterial?.diffuse.contents = NSColor.redColor() 

    // Create a node of type SCNNode and attach the boxGeometry to it. 
    // Note: A node can only have 1 geometry object attached to it. 
    boxNode = SCNNode(geometry: boxGeometry) 

    // Add the new boxNode to the scene's root node. 
    scnMasterScene.rootNode.addChildNode(boxNode) 


    // Create a floor plane. 
    let floorGeometry: SCNGeometry = SCNPlane(width: 20000, height: 20000) 
    // Add a difuse colour to the floor's first material. 
    floorGeometry.firstMaterial?.diffuse.contents = NSColor.yellowColor() 

    // Create a floorPlaneNode and attach the floor plane to it. 
    let floorNode: SCNNode = SCNNode(geometry: floorGeometry) 

    // Tilt the floorPlaneNode in x. 
    let floorNodeTiltDegreesX: Double = -90 
    let floorNodeTiltRadiansX: Double = floorNodeTiltDegreesX * (π/180) 
    floorNode.rotation = SCNVector4Make(1, 0, 0, CGFloat(floorNodeTiltRadiansX)) 

    // Add the floorPlaneNode to the master scene. 
    scnMasterScene.rootNode.addChildNode(floorNode) 
} 

// Create a camera, position it and add it to the scene. 
func setupCamera() { 

    // Create a camera node which will be used to contain the camera. 
    cameraPerspNode = SCNNode() 

    // Create a new camera. 
    let cameraPersp: SCNCamera = SCNCamera() 

    // Set camera properties. 
    cameraPersp.name = "myPerspCamera" 
    cameraPersp.usesOrthographicProjection = false 
    cameraPersp.orthographicScale = 9 
    cameraPersp.xFov = 30 
    cameraPersp.zNear = 1 
    cameraPersp.zFar = 20000 

    // Assign the camera to the .camera property of the node. 
    cameraPerspNode.camera = cameraPersp 

    // Set the position and rotation of the camera node (NOT the camera). 
    cameraPerspNode.position = SCNVector3(x: 0, y: 4000, z: 6000) 
    let cameraPerspTiltDegrees: Double = -30 
    let cameraPerspTiltRadians: Double = cameraPerspTiltDegrees * (π/180) 
    cameraPerspNode.rotation = SCNVector4Make(1, 0, 0, CGFloat(cameraPerspTiltRadians)) 

    // Add the new cameraNode to the scene's root. 
    scnMasterScene.rootNode.addChildNode(cameraPerspNode) 
} 

func createAndAddSpotLight() -> Void { 

    let spot = SCNLight() 
    spot.type = SCNLightTypeSpot 
    spot.castsShadow = true 
    spot.color = NSColor(hue: 1, saturation: 0, brightness: 0, alpha: 1) 
    spot.spotInnerAngle = 30 
    spot.spotOuterAngle = 60 


    let spotNode = SCNNode() 
    spotNode.light = spot 
    spotNode.position = SCNVector3(x: 0, y: 2000, z: 2000) 

    let lookAt = SCNLookAtConstraint(target: boxNode) 
    spotNode.constraints = [lookAt] 
} 

如果我在一個.dae帶來提起添加一個點光源,或定向光,使用場景編輯器,我可以照亮現場,但沒有陰影當我設置投射陰影屬性檢查器中的屬性。

任何人都可以照亮我的問題嗎?

感謝

回答

0

場景中的尺寸是巨大的(在SceneKit 1個單位= 1米)。您需要相應地更改燈光的zFar屬性(也可能是zNear)。

+0

檢查我們從Maya導入的3D模型,SceneKit 1 unit = 1cm。我將試驗不同的縮放模型和不同的光照尺寸,以瞭解它如何影響SceneKit中的渲染。 – TDC

1

場景很大,因爲3D模型已經以1:1創建並描繪了一個大型建築物。經過多次試驗和錯誤,我終於找到了解決方案 - 我將光的比例改爲10,10,10,並出現陰影。