Light position in HDR

Assuming there is only one main source of light in an environment texture, is there a way to get the position of this source of light?

The goal will be to place a directional light at this point in order to have shadows matching with the HDR in the scene.

Thanks, Pichou

The light is environmental meaning it comes from everywhere

I see what you are saying. But take this HDRI for instance, we can see there is the sun which is the main source of light. And it will be great to match a directional light of babylonJS with that origin so that the result will be even more realistic thanks to shadows!

Even if it is not by using environment texture, maybe there is another way to find the sun position in the HDRI and then in the 3D scene?

If you want to create a directional light, finding the sun position won’t help you as what you need is a direction. So you would need an additional point to create a direction, and I don’t really see what point you could choose based only on the environment picture, except maybe (0,0,0)…

Anyway, what you would need is a transformation from a 2D coordinate of the equirectangular HDRI texture to a 3D coordinate in the cube in which the texture is mapped to.

Must be doable, but pretending the HDRI texture is projected onto a sphere instead of a cube is easier:

I think you can reuse the computation from this PG to convert a 2D coordinate from your HDRI picture to a 3D coordinate. The radius (rho) value is not relevant here as it will produce the same direction whatever the value you choose (if second point for direction is (0,0,0)), so using 1 is ok. The final 3D coordinate you get in the end is your direction (after normalizing) if you choose (0,0,0) as the second point to construct your vector.

1 Like

Thanks a lot @Evgeni_Popov! I will try that and keep you in touch