overte/examples/lights/light_modifier.js

341 lines
9.6 KiB
JavaScript
Raw Normal View History

2015-12-12 16:17:00 -08:00
// given a selected light, instantiate some entities that represent various values you can dynamically adjust
//
var BOX_SCRIPT_URL = Script.resolvePath('box.js');
var RED = {
r: 255,
g: 0,
b: 0
};
var GREEN = {
r: 0,
g: 255,
b: 0
};
var BLUE = {
r: 0,
g: 0,
b: 255
};
var PURPLE = {
r: 255,
g: 0,
b: 255
};
var WHITE = {
r: 255,
2015-12-12 16:17:00 -08:00
g: 255,
b: 255
};
2015-12-12 17:14:43 -08:00
var AXIS_SCALE = 1;
2015-12-14 11:25:30 -08:00
2015-12-12 17:14:43 -08:00
var BOX_DIMENSIONS = {
2015-12-12 17:22:57 -08:00
x: 0.05,
y: 0.05,
z: 0.05
};
var PER_ROW_OFFSET = {
x: 0,
y: 0.2,
z: 0
};
2015-12-12 17:14:43 -08:00
2015-12-14 11:25:30 -08:00
function entitySlider(light, color, sliderType, row) {
this.light = light;
this.lightID = light.id.replace(/[{}]/g, "");
2015-12-14 11:25:30 -08:00
this.initialProperties = light.initialProperties;
this.color = color;
this.sliderType = sliderType;
this.verticalOffset = Vec3.multiply(row, PER_ROW_OFFSET);
var message = {
lightID: this.lightID,
sliderType: this.sliderType,
sliderValue: null
};
if (this.sliderType === 'color_red') {
message.sliderValue = this.initialProperties.color.red
this.setValueFromMessage(message);
}
if (this.sliderType === 'color_green') {
message.sliderValue = this.initialProperties.color.green
this.setValueFromMessage(message);
}
if (this.sliderType === 'color_blue') {
message.sliderValue = this.initialProperties.color.blue
this.setValueFromMessage(message);
2015-12-14 11:25:30 -08:00
}
if (this.sliderType === 'intensity') {
message.sliderValue = this.initialProperties.intensity
this.setValueFromMessage(message);
}
if (this.sliderType === 'exponent') {
message.sliderValue = this.initialProperties.exponent
this.setValueFromMessage(message);
}
if (this.sliderType === 'cutoff') {
message.sliderValue = this.initialProperties.cutoff
this.setValueFromMessage(message);
}
2015-12-14 11:25:30 -08:00
// this.setInitialSliderPositions();
this.subscribeToBoxMessages();
2015-12-14 11:25:30 -08:00
return this;
}
2015-12-12 16:17:00 -08:00
//what's the ux for adjusting values? start with simple entities, try image overlays etc
entitySlider.prototype = {
createAxis: function() {
2015-12-14 11:25:30 -08:00
//start of line
var position;
//1 meter along orientationAxis
var endOfAxis;
var properties = {
type: 'Line',
color: this.color,
collisionsWillMove: false,
ignoreForCollisions: true,
dimensions: {
x: 3,
y: 3,
z: 3
},
position: position,
linePoints: [{
x: 0,
y: 0,
z: 0
}, endOfAxis],
lineWidth: 5,
};
2015-12-12 16:17:00 -08:00
this.axis = Entities.addEntity(properties);
},
createBoxIndicator: function() {
var properties = {
type: 'Box',
2015-12-12 17:14:43 -08:00
dimensions: BOX_DIMENSIONS,
2015-12-12 16:17:00 -08:00
color: this.color,
position: position,
script: BOX_SCRIPT_URL,
userData: JSON.stringify({
lightModifierKey: {
lightID: this.lightID,
sliderType: this.sliderType
}
})
2015-12-12 16:17:00 -08:00
};
this.boxIndicator = Entities.addEntity(properties);
},
setValueFromMessage: function(message) {
print('VALUE MESSAGE::'+JSON.stringify(message))
print('LIGHT ID::'+this.lightID);
//message is not for our light
if (message.lightID !== this.lightID) {
print('not our light')
2015-12-14 11:25:30 -08:00
return;
}
//message is not our type
if (message.sliderType !== this.sliderType) {
print('not our slider type')
return
}
print('SHOULD SET SOME VALUE:::' + message.sliderType);
2015-12-12 17:22:57 -08:00
var lightProperties = Entities.getEntityProperties(this.lightID);
// print('LIGHT PROPERTIES::'+JSON.stringify(lightProperties));
2015-12-12 17:14:43 -08:00
if (this.sliderType === 'color_red') {
2015-12-12 17:22:57 -08:00
Entities.editEntity(this.lightID, {
2015-12-12 17:14:43 -08:00
color: {
red: message.sliderValue,
green: lightProperties.color.green,
blue: lightProperties.color.blue
2015-12-12 17:14:43 -08:00
}
2015-12-12 17:22:57 -08:00
});
2015-12-12 17:14:43 -08:00
}
if (this.sliderType === 'color_green') {
2015-12-12 17:22:57 -08:00
Entities.editEntity(this.lightID, {
2015-12-12 17:14:43 -08:00
color: {
red: lightProperties.color.red,
2015-12-12 17:14:43 -08:00
green: message.sliderValue,
blue: lightProperties.color.blue
2015-12-12 17:14:43 -08:00
}
2015-12-12 17:22:57 -08:00
});
2015-12-12 17:14:43 -08:00
}
if (this.sliderType === 'color_blue') {
2015-12-12 17:22:57 -08:00
Entities.editEntity(this.lightID, {
2015-12-12 17:14:43 -08:00
color: {
red: lightProperties.color.red,
green: lightProperties.color.green,
2015-12-12 17:14:43 -08:00
blue: message.sliderValue,
}
2015-12-12 17:22:57 -08:00
});
2015-12-12 17:14:43 -08:00
}
2015-12-12 16:17:00 -08:00
2015-12-12 17:14:43 -08:00
if (this.sliderType === 'intensity') {
print('CHANGING INTENSITY TO::' + message.sliderValue)
2015-12-12 17:22:57 -08:00
Entities.editEntity(this.lightID, {
2015-12-12 17:14:43 -08:00
intensity: message.sliderValue
2015-12-12 17:22:57 -08:00
});
2015-12-12 17:14:43 -08:00
}
if (this.sliderType === 'cutoff') {
2015-12-12 17:22:57 -08:00
Entities.editEntity(this.lightID, {
2015-12-12 17:14:43 -08:00
cutoff: message.sliderValue
2015-12-12 17:22:57 -08:00
});
2015-12-12 17:14:43 -08:00
}
if (this.sliderType === 'exponent') {
2015-12-12 17:22:57 -08:00
Entities.editEntity(this.lightID, {
2015-12-12 17:14:43 -08:00
exponent: message.sliderValue
2015-12-12 17:22:57 -08:00
});
2015-12-12 17:14:43 -08:00
}
},
subscribeToBoxMessages: function() {
print('subscribing to box messages');
2015-12-12 17:14:43 -08:00
Messages.subscribe('Hifi-Slider-Value-Reciever');
Messages.messageReceived.connect(handleValueMessages);
2015-12-12 17:14:43 -08:00
},
setInitialSliderPositions: function() {
var COLOR_MAX = 255;
var INTENSITY_MAX = 10;
var CUTOFF_MAX = 360;
var EXPONENT_MAX = 1;
var distanceRed = (this.initialProperties.color.red / COLOR_MAX) * AXIS_SCALE;
var distanceGreen = (this.initialProperties.color.green / COLOR_MAX) * AXIS_SCALE;
var distanceBlue = (this.initialProperties.color.blue / COLOR_MAX) * AXIS_SCALE;
var distanceIntensity = (this.initialProperties.intensity / INTENSITY_MAX) * AXIS_SCALE;
var distanceCutoff = (this.initialProperties.cutoff / CUTOFF_MAX) * AXIS_SCALE;
var distanceExponent = (this.initialProperties.exponent / EXPONENT_MAX) * AXIS_SCALE;
2015-12-14 11:25:30 -08:00
},
2015-12-12 17:14:43 -08:00
cleanup: function() {
Entities.deleteEntity(this.boxIndicator);
Entities.deleteEntity(this.axis);
Messages.messageReceived.disconnect(this.handleValueMessages);
2015-12-12 16:17:00 -08:00
}
};
var sliders = [];
var slidersRef = {
'color_red': null,
'color_green': null,
'color_blue': null,
intensity: null,
cutoff: null,
exponent: null
}
2015-12-12 16:17:00 -08:00
function makeSliders(light) {
print('light in makesliders:::' + light)
2015-12-12 16:17:00 -08:00
if (light.type === 'spotlight') {
var USE_COLOR_SLIDER = true;
var USE_INTENSITY_SLIDER = true;
var USE_CUTOFF_SLIDER = true;
var USE_EXPONENT_SLIDER = true;
}
if (light.type === 'pointlight') {
var USE_COLOR_SLIDER = true;
var USE_INTENSITY_SLIDER = true;
var USE_CUTOFF_SLIDER = false;
var USE_EXPONENT_SLIDER = false;
}
if (USE_COLOR_SLIDER === true) {
slidersRef.color_red = new entitySlider(light, RED, 'color_red', 1);
slidersRef.color_green = new entitySlider(light, GREEN, 'color_green', 2);
slidersRef.color_blue = new entitySlider(light, BLUE, 'color_blue', 3);
sliders.push(slidersRef.color_red);
sliders.push(slidersRef.color_green);
sliders.push(slidersRef.color_blue);
2015-12-12 16:17:00 -08:00
}
if (USE_INTENSITY_SLIDER === true) {
slidersRef.intensity = new entitySlider(light, WHITE, 'intensity', 4);
sliders.push(slidersRef.intensity);
2015-12-12 16:17:00 -08:00
}
if (USE_CUTOFF_SLIDER === true) {
slidersRef.cutoff = new entitySlider(light, PURPLE, 'cutoff', 5);
sliders.push(slidersRef.cutoff);
2015-12-12 16:17:00 -08:00
}
if (USE_EXPONENT_SLIDER === true) {
slidersRef.exponent = new entitySlider(light, PURPLE, 'exponent', 6);
sliders.push(slidersRef.exponent);
2015-12-12 16:17:00 -08:00
}
2015-12-12 17:14:43 -08:00
};
2015-12-14 11:25:30 -08:00
function subScribeToNewLights() {
print('subscribing to light messages')
2015-12-14 11:25:30 -08:00
Messages.subscribe('Hifi-Light-Mod-Receiver');
Messages.messageReceived.connect(handleLightModMessages);
}
function handleLightModMessages(channel, message, sender) {
if (channel !== 'Hifi-Light-Mod-Receiver') {
return;
}
if (sender !== MyAvatar.sessionUUID) {
return;
}
var parsedMessage = JSON.parse(message);
print('MESSAGE LIGHT:::' + message)
makeSliders(parsedMessage.light);
}
function handleValueMessages(channel, message, sender) {
if (channel !== 'Hifi-Slider-Value-Reciever') {
return;
}
print('HANDLE VALUE MESSAGE')
//easily protect from other people editing your values, but group editing might be fun so lets try that first.
// if (sender !== MyAvatar.sessionUUID) {
// return;
// }
var parsedMessage = JSON.parse(message);
slidersRef[parsedMessage.sliderType].setValueFromMessage(parsedMessage)
// this.setValueFromMessage(parsedMessage);
}
function cleanup() {
while (sliders.length > 0) {
var slider = sliders.pop();
slider.cleanup();
}
Messages.messageReceived.disconnect(handleLightModMessages);
delete sliders
2015-12-14 11:25:30 -08:00
}
Script.scriptEnding.connect(cleanup);
2015-12-14 11:25:30 -08:00
subScribeToNewLights();
// diffuseColor: { red: 255, green: 255, blue: 255 },
// ambientColor: { red: 255, green: 255, blue: 255 },
// specularColor: { red: 255, green: 255, blue: 255 },
2015-12-14 11:25:30 -08:00
// constantAttenuation: 1,
// linearAttenuation: 0,
// quadraticAttenuation: 0,
// exponent: 0,
// cutoff: 180, // in degrees