# 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.

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.

USE THE DIRECTION. JUST THE DIRECTION. DONâ€™T CAST THE RAY. JUST USE ITS DIRECTION.

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.

Direction.
Direction.
Direction.
Direction.
Direction.
Direction.
Direction.
Direction.
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.

3 Likes

https://jmonkeyengine.github.io/wiki/jme3/beginner/hello_picking.html#picking-action-using-mouse-pointer

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.

Instructions unclear, got vector stuck in nan value.

1 Like

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

3 Likes

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 https://github.com/stephengold/jme3-utilities/blob/master/textures/src/main/java/jme3utilities/sky/textures/MakeStarMaps.java 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