Sun (directional light) system suggestions

Hi, can anyone recommend any sunlight system scripts/libraries that position a directional light (the sun) according to input lat/long, time of day etc? I have found one but the explanation of how to use it is minimal and its not clear how time is input.

It looks like the library you linked is designed to update the time automatically - that is, you just input a latitude and longitude, and it calculates the time at that location automatically.

From a quick glance you should be able to override this by setting this.localDate to whatever time you want, relative to the place you are simulating - so if itā€™s 4pm now in London, and you want to simulate 6pm in Bangkok, you would set this.localDate = new Date() - 4 hours (not real code!) since 4pm London = 10pm Bangkok.

Then in the function below set update_date_ = false to prevent it overwriting your custom time.

SunLight.prototype.updateOrientation = function ( update_date_ = true ) {

There might be a bit more you have to do to update the time manually yourself.

Also, looking at the source code of this lib. The first parameter is THREE.Vector2(), which is, if to trust to comments, the current location, given in latitude-longitude coordinates. Having this, I think itā€™s not enough just to add/subtract hours, as the Sun will be at different position on the sky dome at (for example) 10am in London and at 10am in Bangkok.

Many thanks @looeee and @prisoner849, I need to investigate and test the library some more to see if I can understand it and get it to suit my needs. Your comments have helped.

I am arriving kind of late to this, but if you are still looking for an alternative you could give suncalc.js a try. Basically you specify time input with a regular date object and then calculate sun position with

SunCalc.getPosition( date, lat, lng)

Which will give you altitude and azimuth in radians. That should be enough for converting them to spherical coordinates, let me know if you need further help

3 Likes

Iā€™ve used SunCalc as well for this exact same purpose.

Although, since weā€™re on the topic, perhaps someone could help with the following:

Iā€™m not very strong in maths in this category, so Iā€™m kind of helpless here. I would like a class or function in which you can specify in e.g. how many seconds a day is, to specify at which point the sun rises and when it sets.

Lets say there are 86400 seconds in a day (24 hours). You want the sun to start rising at exactly 6:00 and let it set around 20:00. This isnā€™t a simple linear calculation, since the daytime would take longer than night time.

How would you make something like this, and also to calculate/convert the seconds (current time of day) into a sun-direction or position vector?

Any help is greaaaatly appreciated. Iā€™ve been struggling with this doing nothing but trial & error for nearly 3 days straight and itā€™s driving me nuts :sweat_smile:

To illustrate:

When I call world.setTimeOfDay('6:00') the method transforms the given time to seconds (since itā€™s easier to work with, so it results in 21600 (3600 * 6)). By ā€˜configurationā€™, the system knows the sun position should be at a specific vector that represents a ā€œsunriseā€. It should smoothly transition towards ā€œsunsetā€ position at a configured time (seconds, .e.g 129600 (20:00 / 8pm in seconds)).

I hope this makes senseā€¦ :see_no_evil: