How to use ScnNode as a ScnLight?

1k views Asked by At
 var sun = SCNNode(geometry: SCNSphere(radius:0.35))

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

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

And i want to use the sun SCNSphere as a omni light source.

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

But if i run this code, the sun is full black.

1

There are 1 answers

2
jglasse On

add it to the scene:

scene.rootNode.addChildNode(sun)

Here's example playground code:

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 = false

// 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)