Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
AleziaKurdis committed Jul 25, 2023
1 parent 2ed5310 commit 28823cd
Show file tree
Hide file tree
Showing 3 changed files with 258 additions and 1 deletion.
256 changes: 256 additions & 0 deletions AK004/atmosphericManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
//
// atmosphericManager.js
//
// Created by Alezia Kurdis on July 25th, 2023.
// Copyright 2023, Overte e.V.
//
// Manage all the atmospheric events for AK004 scenery.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
(function () {
var jsMainFileName = "atmosphericManager.js";
var ROOT = Script.resolvePath('').split(jsMainFileName)[0];
var thisEntity;
var renderWithZones;
var HALF = 0.5;
var universeCenter;
var universeDimensions;
var updateTimerInterval = 2000; // 2 sec
var cycleCount = 0;
var MAX_COUNT = 4; //3 X 2sec = 8 sec.
var processTimer = 0;
var skyID = Uuid.NULL;

var DAY_DURATION = 104400; //29h
var WEEK_DURATION = DAY_DURATION * 9;
var MONTH_DURATION = WEEK_DURATION * 4;
var YEAR_DURATION = MONTH_DURATION * 10;
var D29_HOUR_DURATION = DAY_DURATION/24;

this.preload = function(entityID) {
thisEntity = entityID;
var prop = Entities.getEntityProperties(entityID, ["position", "dimensions", "renderWithZones"]);
renderWithZones = prop.renderWithZones;
universeCenter = prop.position;
universeDimensions = prop.dimensions;

manageSky();
var today = new Date();
processTimer = today.getTime();
Script.update.connect(myTimer);
};

this.unload = function(entityID) {
Script.update.disconnect(myTimer);

if (skyID !== Uuid.NULL) {
Entities.deleteEntity(skyID);
skyID = Uuid.NULL;
}

};

function myTimer(deltaTime) {
var today = new Date();
var i;
if ((today.getTime() - processTimer) > updateTimerInterval ) {
var isPresent = positionIsInsideBox(universeCenter, Quat.IDENTITY, universeDimensions);
if (isPresent) {
cycleCount++;
if (cycleCount > MAX_COUNT) {
manageSky();
cycleCount = 0;
}
} else {
if (skyID !== Uuid.NULL) {
Entities.deleteEntity(skyID);
skyID = Uuid.NULL;
}
}

today = new Date();
processTimer = today.getTime();
}
}

function manageSky() {
var hue, saturation, lightness, lighColor, intensity;
var today = new Date();
var hour = GetCurrentCycleValue(today.getTime(), 24, DAY_DURATION);
if (hour > 6 && hour < 18) {
hue = 28/360;
saturation = 1;
lightness = .75 + Math.abs(Math.cos((hour/24) * (2 * Math.PI)) * 0.25);
lighColor = hslToRgb(hue, saturation, lightness);
} else if (hour > 5 && hour < 6) {
hue = (210 + ((hour - 5) * 182))/360;
saturation = 1;
lightness = .75 + Math.abs(Math.sin((hour/24) * (2 * Math.PI)) * 0.25);
lighColor = hslToRgb(hue, saturation, lightness);
} else if (hour > 18 && hour < 19) {
hue = (28 + ((hour - 19) * 182))/360;
saturation = 1;
lightness = .75 + Math.abs(Math.sin((hour/24) * (2 * Math.PI)) * 0.25);
lighColor = hslToRgb(hue, saturation, lightness);
} else {
hue = 210/360;
saturation = 1;
lightness = .75 + Math.abs(Math.sin((hour/24) * (2 * Math.PI)) * 0.25);
lighColor = hslToRgb(hue, saturation, lightness);
}
intensity = 0.5 + (Math.cos((hour/24) * (2 * Math.PI)) * 1.5);

var zoneRotation = QuatfromVec3Radians({"x": Math.sin((GetCurrentCycleValue(today.getTime(), 2 * Math.PI, MONTH_DURATION)) * (2 * Math.PI)), "y": 0.0, "z": GetCurrentCycleValue(today.getTime(), Math.PI, D29_HOUR_DURATION)});
var anglVelo = (Math.PI/D29_HOUR_DURATION) * 2000 * MAX_COUNT;
var ambientIntensity = intensity/10;
var currentsky = "https://aleziakurdis.github.io/inertia/AK004/images/sky.jpg";

if (skyID === Uuid.NULL) {
//CREATE
skyID = Entities.addEntity({
"type": "Zone",
"name": "AK004-SKY",
"dimensions": {
"x": universeDimensions.x - 20,
"y": universeDimensions.y - 20,
"z": universeDimensions.z - 20
},
"parentID": thisEntity,
"renderWithZones": renderWithZones,
"localPosition": {"x": 0.0, "y": 0.0, "z": 0.0},
"localRotation": zoneRotation,
"angularVelocity": {"x": 0.0, "y": anglVelo, "z": 0.0},
"angularDamping": 0.0,
"grab": {
"grabbable": false
},
"shapeType": "sphere",
"keyLight": {
"color": {
"red": lighColor[0],
"green": lighColor[1],
"blue": lighColor[2]
},
"intensity": intensity,
"direction": Vec3.fromPolar( 89.9 * DEGREES_TO_RADIANS, 0 * DEGREES_TO_RADIANS),
"castShadows": true,
"shadowBias": 0.03,
"shadowMaxDistance": 75
},
"ambientLight": {
"ambientIntensity": ambientIntensity,
"ambientURL": currentsky
},
"skybox": {
"color": {
"red": 255,
"green": 255,
"blue": 255
},
"url": currentsky
},
"bloom": {
"bloomIntensity": 0.5,
"bloomThreshhold": 0.7,
"bloomSize": 0.8
},
"keyLightMode": "enabled",
"ambientLightMode": "enabled",
"skyboxMode": "enabled",
"hazeMode": "inherit",
"bloomMode": "enabled"
}, "local");

} else {
//UPDATE
Entities.editEntity(skyID, {
"localRotation": zoneRotation,
"angularVelocity": {"x": 0.0, "y": anglVelo, "z": 0.0},
"ambientLight": {
"ambientIntensity": ambientIntensity
},
"keyLight": {
"color": {
"red": lighColor[0],
"green": lighColor[1],
"blue": lighColor[2]
},
"intensity": intensity
}
});
}

}

function positionIsInsideBox(boxPosition, boxRotation, boxDimensions) {
var targetPosition = MyAvatar.position;

var worldOffset = Vec3.subtract(targetPosition, boxPosition);
targetPosition = Vec3.multiplyQbyV(Quat.inverse(boxRotation), worldOffset);

var minX = -boxDimensions.x * HALF;
var maxX = boxDimensions.x * HALF;
var minY = -boxDimensions.y * HALF;
var maxY = boxDimensions.y * HALF;
var minZ = -boxDimensions.z * HALF;
var maxZ = boxDimensions.z * HALF;

return (targetPosition.x >= minX && targetPosition.x <= maxX
&& targetPosition.y >= minY && targetPosition.y <= maxY
&& targetPosition.z >= minZ && targetPosition.z <= maxZ);
}





// ################## CYLCE AND TIME FUNCTIONS ###########################
function GetCurrentCycleValue(timestamp, cyclelength, cycleduration){
var TodaySec = timestamp/1000;
var CurrentSec = TodaySec%cycleduration;

return (CurrentSec/cycleduration)*cyclelength;

}


/*
* Converts an HSL color value to RGB. Conversion formula
* adapted from http://en.wikipedia.org/wiki/HSL_color_space.
* Assumes h, s, and l are contained in the set [0, 1] and
* returns r, g, and b in the set [0, 255].
*
* @param {number} h The hue
* @param {number} s The saturation
* @param {number} l The lightness
* @return {Array} The RGB representation
*/
function hslToRgb(h, s, l){
var r, g, b;

if(s == 0){
r = g = b = l; // achromatic
}else{
var hue2rgb = function hue2rgb(p, q, t){
if(t < 0) t += 1;
if(t > 1) t -= 1;
if(t < 1/6) return p + (q - p) * 6 * t;
if(t < 1/2) return q;
if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
return p;
}

var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
var p = 2 * l - q;
r = hue2rgb(p, q, h + 1/3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1/3);
}

return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}


})
Binary file added AK004/images/sky.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion AK004/inertia.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"z":0.70710676908493
},
"castShadows":true
}
},
"script": "https://aleziakurdis.github.io/inertia/AK004/atmosphericManager.js"
},
{
"id":"{b8ab3aac-1446-494b-9e41-50127851c768}",
Expand Down

0 comments on commit 28823cd

Please sign in to comment.