2016-05-19 5 views
0

Я создаю простой проект с использованием SceneKit и не могу получить никаких теней, появляющихся в Редакторе сцены или в скомпилированном приложении, независимо от того, что я пытаюсь.Xcode SceneKit Shadows не Render

Я попытался создать простую коробку, поместив ее на плоскость и добавив свет пятна. Код будет выглядеть следующим образом:

// 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 подал добавить точечный свет, или направленный свет, используя Редактор сцену я могу осветить сцену, но нет тени, когда я устанавливаю Cast Shadows свойство в Инспекторе атрибутов.

Может ли кто-нибудь осветить мою проблему?

Благодаря

ответ

0

размеры в вашей сцене огромны (в SceneKit 1 единица = 1 метр). Вы захотите изменить свой собственный объект zFar (и, возможно, zNear) соответственно.

+0

Проверка на нашу трехмерную модель, импортированную из майя, SceneKit 1 единица = 1 см. Я буду экспериментировать с различными масштабируемыми моделями и различными светлыми масштабами, чтобы увидеть, как это влияет на рендеринг в SceneKit. – TDC

1

Сцена большая, потому что 3D-модель была создана в 1: 1, и на ней изображено большое здание. После долгих проб и ошибок я наконец нашел решение - я изменил масштаб света до 10, 10, 10 и появились тени.