ARKit makes a SCNLight point in front where the camera is looking?

So, I have access to sceneView.pointOfView

, and I want the AR feel to look like I'm shining with flash light right in front of my objects I'm looking at, light up when I point at them.

I tried to create a spotlight like this:

let spotLight = SCNLight()
spotLight.type = .spot
spotLight.spotInnerAngle = 60
spotLight.spotOuterAngle = 60
let spotNode = SCNNode()
spotNode.light = spotLight
spotNode.position = position

      

Then I thought that adding the light to the pointOfView node would make it so that the light moves with the camera, which meant it always shines in front ...

sceneView.pointOfView?.addChildNode(spotLight)

      

it doesn't work, objects look black, no light.

What am I doing wrong? I want to make the light indicate what I am viewing through the AR experience, just as if I was holding a flashlight and I was shining the light straight ahead.

Any suggestion?

+3


source to share


1 answer


Are you looking for a virtual flashlight that really illuminates the real scene in the camera feed? This is a very high order.

You are basically talking about projecting-mapping a light cone onto every real surface that is visible, which requires detecting and modeling those surfaces, which is much more than defining ARKit on a horizontal plane.

(If you don't want to realistically light your real world scene, you only need to place a semi-transparent white circle in the middle of the view. You might like the blend mode.realistic, resize or 3D distance to your circle based on hit-testing at the center of your view against AR scenes.)


Do you need a virtual flashlight that illuminates virtual objects that you've placed in AR? It's easy to do. And in fact, your code is most of that. Probable problems:



spotNode.position = position

      

Your snippet above doesn't say where you get this position. If you want the spotlight node to be attached to the camera, it must be a null vector. That is, you want it to have a neutral position in the local camera space of the node. Setting this vector to a nonzero value would offset your light from the camera position by a constant offset (perhaps so much that it caused the light to do nothing useful in your scene).

sceneView.pointOfView?.addChildNode(spotLight)

      

Depending on when this code works, it sceneView.pointOfView

can be zero and therefore you are not actually making the call addChildNode

. Make sure the camera node exists before adding the child to it. (I tried to add this code to the sample code from the ARKit WWDC17 session - if you put it in your function ARSCNView.setup()

, you should see any virtual objects you place completely destroyed by the bright spotlight.)

By the way, you don't have to add a child node to a pointOfView

node to hold the light - a pointOfView

node can have both a camera and a light.

+4


source







All Articles