如何将ScnNode用作ScnLight?

时间:2017-11-01 13:25:45

标签: ios swift scenekit scnnode scnlight

 var sun = SCNNode(geometry: SCNSphere(radius:0.35))

 sun.geometry?.firstMaterial?.diffuse.contents=#imageLiteral(resourceName: "Sun")

 sun.position=SCNVector3(0,0,-1)

我想用太阳SCNSphere作为全方位光源。

 let OmniLight = SCNLight()      
  OmniLight.type = SCNLight.LightType.omni
       OmniLight.color = UIColor.white

但如果我运行此代码,太阳就会全黑。

1 个答案:

答案 0 :(得分:0)

将其添加到场景中:

scene.rootNode.addChildNode(sun)

这是示例游乐场代码:

import UIKit
import SceneKit
import PlaygroundSupport


// create a scene view with an empty scene
var sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: 640, height: 480))
var scene = SCNScene()
sceneView.scene = scene
PlaygroundPage.current.liveView = sceneView

// default lighting
sceneView.autoenablesDefaultLighting = true

// a camera
var cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.position = SCNVector3(x: 0, y: 0, z: 3)
scene.rootNode.addChildNode(cameraNode)

// your code (minus the image)
var sun = SCNNode(geometry: SCNSphere(radius:0.35))
sun.geometry?.firstMaterial?.diffuse.contents = UIColor.orange
sun.position=SCNVector3(0,0,-1)
let OmniLight = SCNLight()
OmniLight.type = SCNLight.LightType.omni
OmniLight.color = UIColor.white

// add to scene
scene.rootNode.addChildNode(sun)