Flashlight Follow Camera

Hi, I’d like to build some kind of flashlight or Torch effect where the ground is only lit where to camera’s looking at.

Here is my PG so far - I don’t know how to make to make the camera follow my mousemove

https://playground.babylonjs.com/#CQNGRK#547

scene.onBeforeRenderObservable.add(()=>{
        spotLight.position = camera.position;
		spotLight.setDirectionToTarget(camera.getFrontPosition(1))
    })
2 Likes

Another approach is you could parent the spotLight to the camera, then you don’t need to use onBeforeRenderObservable

You probably need something other than ArcRotateCamera also, because torch light for that is still going to point at the one place.

https://playground.babylonjs.com/#CQNGRK#549

1 Like

I have a similar problem, where I want to attach a spotlight to an object (e.g. a cube) and have it always hovering at a distance over one of its sides. (It’s actually for a procedural light but this does not matter here)

Parenting seems the right approach there but I am getting into serious trouble there. Somehow the spotlight does not have a rotation property and therefore very hard to work with a local coordinate system. Also the setDirectionToTarget function does strange things when the spotlight is parented to another object.
Does anyone know a simple way to get a parenting behaviour with spotlights which preserves the relative orientation between the spotlight and the parent object?.
Maybe one could write a derived spotlight class which does know about rotation. The torch mentioned above (including ilan actual torch object) would also be a good use case of such a class.

Sounds like this could be a bug, can you share a repro so that we could try to fix it ?

@sebavan , @carolhmj, Here is an example, demonstrating the problems I am having.
The aim was to have a light-source (with a projection texture) attached to an object (here the big box).
It should always be “rigidly” attached in the sense that it follows all rotation and translation movements of the parent. Here always the same corner of the cube should always be green, three sides of the box should be lit up (as it is the case for alpha=0), and the projection should not change scaling etc…

Note that unparenting and reparenting before “setDirectionToTarget()” somewhat stabilizes the light-direction, albeit not to the wanted position, but then messes up the texture even more.
I was trying to get this right for a number of days now, but somehow always failed. Maybe multiple things do not quite work correctly here?

@Evgeni_Popov can you have a look ?

setDirectionToTarget does the calculation in local space. So, the target should be (0,0,0):

However, there’s also a bug in the projection texture code, so the PG above will only work once this PR is merged:

1 Like

Thanks! Yes. I again fell into the trap to assume that “local space” means the origin of the light, but it really means the origin of the parent of the light. Very confusing.
I am thrilled to see it working then, once the merge has proceeded. In the mean time I will try out to replace this entire idea by using a procedural emission texture instead. Its not really the same for me application (laser light impinging on an object), but the effect may looks somewhat similar, I guess.

1 Like

Great. It works now. Also in my own app it works flawlessly since this bug was fixed and the PR merged. Many thanks for the swift action!

1 Like