Controlling the Sun with javascript


#1

Check this out, @sam has been working on controlling the sun. The PR is here: https://github.com/highfidelity/hifi/pull/4347/files

Through some very simple javascript you can control the sun: Intensity, position, color etc. It is very cool.

For inspiration:


#2

I modified his script - setting it to my local geo coordinates and matching local time… I can’t wait for this to hit mainline code! Had asked in gitter if the “sun” will run from server side so it will present the same day view to all visitors and got the indication of yes it will - eventually.


#3

What kind of sorcery is this? :smiley:


#4

The bestest kind! \o/


#5

I’m using this as my controller script - it sets to (roughly) my local lat/lon and updates sun position once per minute - likely once per 10 or 15 minutes would be fine - will experiment to find longest interval not leading to abrupt changes. There’s likely better ways to do it - and would be nice to have this on server side so all visitors get the desired day time effect, but, it is nice to see some sun effect.

function solar() {
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
var hour = 0.0;
hour = now.getHours() + (now.getMinutes()/60.0);
Scene.setStageYearTime(day);
Scene.setStageDayTime(hour);
}
Scene.setStageOrientation(Quat.fromPitchYawRollDegrees(0.0, 180.0, 0.0));
Scene.setStageLocation(28.0, -82.0, 0.0);
solar();
Script.setInterval(solar, 60000);


#6

One issue I see if you make a script and have in your default Interface payload at startup… you get undefined reference errors as (I’m guessing) the script starts trying to run before it has access to the “stage”. Restarting the script fixes.

One other thing that needs to be determined is Scene.setStageYearTime(x). Is it 0…364 or 1…365?

In code above it will make day 0…364. For 1…365 change;

var day = Math.floor(diff / oneDay); to var day = Math.ceil(diff / oneDay);


#7

One thing I found was that my agent’s shadow seemed in wrong place at noon local time. Facing North at noon in real life outdoors my shadow was in front - in HiFi it was behind. Changing;

Scene.setStageOrientation(Quat.fromPitchYawRollDegrees(0.0, 180.0, 0.0));

to;

Scene.setStageOrientation(Quat.fromPitchYawRollDegrees(0.0, 0.0, 0.0));

Corrects it and follows what I see in RL.