141 lines
No EOL
4.7 KiB
JavaScript
141 lines
No EOL
4.7 KiB
JavaScript
/*
|
|
materialPulser.js
|
|
|
|
Created by Mark Brosche, on 2/28/19
|
|
|
|
|
|
Copyright 2019 High Fidelity, Inc.
|
|
|
|
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 _entityID = null;
|
|
var pulseInterval = false;
|
|
var userData = null;
|
|
var fps = null;
|
|
var frequency = null;
|
|
var props = null;
|
|
var increasing = false;
|
|
var brightProps = [];
|
|
var dullProps = [];
|
|
var delta = null;
|
|
function preload(entityID) {
|
|
_entityID = entityID;
|
|
userData = Entities.getEntityProperties(_entityID, ["userData"]).userData;
|
|
if (!userData || userData === "{}") {
|
|
print("spawner ", _entityID, " is missing user data.");
|
|
return;
|
|
}
|
|
try {
|
|
userData = JSON.parse(userData);
|
|
fps = userData.speedFPS;
|
|
frequency = 1000/fps;
|
|
brightProps = userData.maxEmissive;
|
|
dullProps = userData.minEmissive;
|
|
delta = Vec3.multiply(1/(2*fps), brightProps);
|
|
} catch (e) {
|
|
console.log(e, "error parsing userData -- PRELOAD");
|
|
if (pulseInterval) {
|
|
Script.clearInterval(pulseInterval);
|
|
pulseInterval = false;
|
|
}
|
|
return;
|
|
}
|
|
if (pulseInterval) {
|
|
Script.clearInterval(pulseInterval);
|
|
pulseInterval = false;
|
|
}
|
|
pulseInterval = Script.setInterval(function() {
|
|
props = Entities.getEntityProperties(_entityID, ["materialData"]);
|
|
try {
|
|
props.materials = JSON.parse(props.materialData);
|
|
} catch (e) {
|
|
console.log(e, "error parsing materialData -- INTERVAL");
|
|
if (pulseInterval) {
|
|
Script.clearInterval(pulseInterval);
|
|
pulseInterval = false;
|
|
}
|
|
return;
|
|
}
|
|
var emissive = [
|
|
props.materials.materials.emissive.x,
|
|
props.materials.materials.emissive.y,
|
|
props.materials.materials.emissive.z
|
|
];
|
|
if (emissive >= brightProps && increasing) {
|
|
decrementBright();
|
|
increasing = false;
|
|
} else if (emissive <= dullProps && !increasing) {
|
|
incrementBright();
|
|
increasing = true;
|
|
} else if (increasing) {
|
|
incrementBright();
|
|
} else {
|
|
decrementBright();
|
|
}
|
|
}, frequency);
|
|
}
|
|
|
|
function incrementBright() {
|
|
props = Entities.getEntityProperties(_entityID, ["name", "materialData"]);
|
|
try {
|
|
props.materials = JSON.parse(props.materialData);
|
|
|
|
} catch (e) {
|
|
console.log(e, "error parsing materialData -- INCREMENT");
|
|
if (pulseInterval) {
|
|
Script.clearInterval(pulseInterval);
|
|
pulseInterval = false;
|
|
}
|
|
return;
|
|
}
|
|
props.materials.materials.emissive = Vec3.sum(props.materials.materials.emissive,delta);
|
|
props.materials.materials.opacity <= 1 ? props.materials.materials.opacity+= 0.1 : props.materials.materials.opacity;
|
|
Entities.editEntity(_entityID, {
|
|
"materialData": JSON.stringify(props.materials)
|
|
});
|
|
}
|
|
|
|
function decrementBright() {
|
|
props = Entities.getEntityProperties(_entityID, ["name", "materialData"]);
|
|
try {
|
|
props.materials = JSON.parse(props.materialData);
|
|
|
|
} catch (e) {
|
|
console.log(e, "error parsing materialData -- DECREMENT");
|
|
if (pulseInterval) {
|
|
Script.clearInterval(pulseInterval);
|
|
pulseInterval = false;
|
|
}
|
|
return;
|
|
}
|
|
props.materials.materials.emissive = Vec3.subtract(props.materials.materials.emissive,delta);
|
|
props.materials.materials.opacity >= 0 ? props.materials.materials.opacity -= 0.1 : props.materials.materials.opacity;
|
|
Entities.editEntity(_entityID, {
|
|
"materialData": JSON.stringify(props.materials)
|
|
});
|
|
}
|
|
|
|
function unload() {
|
|
if (pulseInterval) {
|
|
Script.clearInterval(pulseInterval);
|
|
pulseInterval = false;
|
|
}
|
|
}
|
|
|
|
|
|
function MaterialPulser() {}
|
|
|
|
|
|
MaterialPulser.prototype = {
|
|
preload: preload,
|
|
incrementBright: incrementBright,
|
|
decrementBright: decrementBright,
|
|
unload: unload
|
|
};
|
|
|
|
|
|
return new MaterialPulser();
|
|
}); |