How to find the direction of the sun in a skybox

Hello, i have a skybox which consists of 6 images that are then arranged by the sky factory to make a sky box. I was wondering how to.find the direction of the sun so i could add some directional light that faced in the same direction. Thanks in advance.

1 Like

The sun direction is whatever way you decide it is. A skybox is just a box with images painted on it… there isn’t any “sun” unless one of your sky images has the sun in it. If you think about it, this makes sense. If you had a nighttime scene, you wouldn’t want a sun, and you might not even want a moon.

In short: if you want to match light direction with where the skybox has a sun “painted on”, you’ll just need to figure out which direction it is relative to your scene and set your light direction accordingly.

Thanks for responding. Sorry, i forgot to mention that there is a sun in the image. How would in go about finding he direction relative to the scene though?

Trial and error… or code something that lets you click on the screen and calculate a direction from that. (See the mouse picking tutorials.)

Ah ok, thanks I’ll have a look at the mouse picking Tutorial. Im on my mobile currently so I’ll get back with some results later.

No problem. Quick note about calculating direction… remember that the sun is “infinitely” far away. With this in mind, the math to calculate light direction is very simple: pick the “sun point” as @pspeed described, then get the vector FROM the sun point to the center of your scene, which is probably at <0,0,0>. If this is the case, negate your sun position vector components and you have the right direction (this is just a “shorthand” for subtracting <0,0,0> - sunPos).

The sky box doesn’t “move” relative to the camera. So using the direction calculated in the picking examples should be enough. No special translation required. (in fact it will break things to use the scene center.)

Oi! And this is why I probably shouldn’t comment on 3D maths if I haven’t tried them myself first. :stuck_out_tongue:

For my own curiosity, why would it break things to use the center of the scene?

If i can’t use the scene center, the where should I take the direction from?

The camera… as already said… just like in the picking tutorials. Just use the direction from those picking tutorials… just the direction. No translation. The direction you’d pass to the ray. No location needed.

Because the center of the scene could be 100,000 meters away from where the camera currently is. You just don’t know. You only need a direction. Trying to bring any other location than the camera’s into it is going to mess things up. And really, the mouse picking example has everything you need… take the screen coordinate and turn it into a world coordinate near/far then calculate a direction. Done.

I have now repeated myself enough. You guys are on your own now.

Gotcha. Thanks.

I’ll report my findings this evening, as I am working late… I have read the mouse picking tutorial, and i understand where you’re coming from.

Sorry i haven’t replied for ages, however i tried the mouse picking tutorials, however my only problem is that you can’t detect the skybox with a ray,been though it is attached to the root node. Any ideas?

You don’t need to detect the sky box.


The direction is all you need. Because the direction is what you want.

So just use the direction.

The direction of the ray.

The one you get by calculating the near and far point. The one you would pass to the ray as the direction.

That’s the direction of the sun when you click.

Just the direction.


Edit: and because I feel I probably need to mention it, don’t forget to negate the direction… because the direction of the sun is the opposite way the light is traveling.


The “dir” from that… is the sun direction if you click on the sun. You don’t need any collision stuff at all. (Not even sure what you’d do with the collision information if you had it…)

Ok. Thanks I’ll have a look. :grinning:

Instructions unclear, got vector stuck in nan value.

1 Like

We love you paul. Always sharing your knowledge and dedicated af. <3


For a single instance, I would load the skybox into a simple application and use a camera control (such as flyCam) to interactively point the camera at the center of the sun’s image, then press the “C” key to print the camera direction to the console. (The direction of the distant light should be the opposite of the camera direction, of course.)

For the general case, I would start by looking at the cubeUV() function in which solves the inverse problem (converting a direction vector to a pixel in a skybox). By reverse-engineering cubeUV, you might be able to convert the texture coordinates of the sun-image to a direction.

you could try casting a ray every frame and using that to determine the direction