overte/examples/lights/lightModifier.js

395 lines
11 KiB
JavaScript
Raw Normal View History

2015-12-15 18:20:12 -08:00
//
2015-12-16 01:31:24 -08:00
// lightModifier.js
2015-12-15 18:20:12 -08:00
//
2015-12-16 01:31:24 -08:00
// Created by James Pollack @imgntn on 12/15/2015
2015-12-15 18:20:12 -08:00
// Copyright 2015 High Fidelity, Inc.
//
// Given a selected light, instantiate some entities that represent various values you can dynamically adjust by grabbing and moving.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
2015-12-12 16:17:00 -08:00
//
2015-12-15 18:20:12 -08:00
var AXIS_SCALE = 1;
var COLOR_MAX = 255;
var INTENSITY_MAX = 0.05;
var CUTOFF_MAX = 360;
var EXPONENT_MAX = 1;
2015-12-14 16:51:12 -08:00
2015-12-15 18:20:12 -08:00
var SLIDER_SCRIPT_URL = Script.resolvePath('slider.js?' + Math.random(0, 100));
2015-12-12 16:17:00 -08:00
var RED = {
2015-12-15 15:55:03 -08:00
red: 255,
green: 0,
blue: 0
2015-12-12 16:17:00 -08:00
};
var GREEN = {
2015-12-15 15:55:03 -08:00
red: 0,
green: 255,
blue: 0
2015-12-12 16:17:00 -08:00
};
var BLUE = {
2015-12-15 15:55:03 -08:00
red: 0,
green: 0,
blue: 255
2015-12-12 16:17:00 -08:00
};
var PURPLE = {
2015-12-15 15:55:03 -08:00
red: 255,
green: 0,
blue: 255
2015-12-12 16:17:00 -08:00
};
var WHITE = {
2015-12-15 15:55:03 -08:00
red: 255,
green: 255,
blue: 255
2015-12-12 16:17:00 -08:00
};
2015-12-16 00:58:00 -08:00
var ORANGE={
red:255,
green:0,
blue:128
}
2015-12-15 18:20:12 -08:00
var SLIDER_DIMENSIONS = {
x: 0.075,
y: 0.075,
z: 0.075
2015-12-12 17:22:57 -08:00
};
2015-12-15 18:20:12 -08:00
2015-12-12 17:22:57 -08:00
var PER_ROW_OFFSET = {
x: 0,
2015-12-15 15:55:03 -08:00
y: -0.2,
2015-12-12 17:22:57 -08:00
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);
2015-12-14 16:51:12 -08:00
this.avatarRot = Quat.fromPitchYawRollDegrees(0, MyAvatar.bodyYaw, 0.0);
2015-12-15 18:20:12 -08:00
this.basePosition = Vec3.sum(MyAvatar.position, Vec3.multiply(1.5, Quat.getFront(this.avatarRot)));
2015-12-14 16:51:12 -08:00
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
2015-12-14 17:27:01 -08:00
this.setInitialSliderPositions();
this.createAxis();
2015-12-15 18:20:12 -08:00
this.createSliderIndicator();
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
2015-12-14 16:51:12 -08:00
var position = Vec3.sum(this.basePosition, this.verticalOffset);
//line starts on left and goes to right
//set the end of the line to the right
var rightVector = Quat.getRight(this.avatarRot);
var extension = Vec3.multiply(AXIS_SCALE, rightVector);
var endOfAxis = Vec3.sum(position, extension);
2015-12-15 15:55:03 -08:00
this.endOfAxis = endOfAxis;
2015-12-14 11:25:30 -08:00
var properties = {
type: 'Line',
2015-12-15 15:55:03 -08:00
name: 'Hifi-Slider-Axis::' + this.sliderType,
2015-12-14 11:25:30 -08:00
color: this.color,
collisionsWillMove: false,
ignoreForCollisions: true,
dimensions: {
x: 3,
y: 3,
z: 3
},
position: position,
linePoints: [{
x: 0,
y: 0,
z: 0
2015-12-15 15:55:03 -08:00
}, extension],
2015-12-14 11:25:30 -08:00
lineWidth: 5,
};
2015-12-12 16:17:00 -08:00
this.axis = Entities.addEntity(properties);
},
2015-12-15 18:20:12 -08:00
createSliderIndicator: function() {
2015-12-14 17:27:01 -08:00
var position = Vec3.sum(this.basePosition, this.verticalOffset);
//line starts on left and goes to right
//set the end of the line to the right
var rightVector = Quat.getRight(this.avatarRot);
var initialDistance;
if (this.sliderType === 'color_red') {
initialDistance = this.distanceRed;
}
if (this.sliderType === 'color_green') {
initialDistance = this.distanceGreen;
}
if (this.sliderType === 'color_blue') {
initialDistance = this.distanceBlue;
}
if (this.sliderType === 'intensity') {
2015-12-15 18:20:12 -08:00
initialDistance = this.distanceIntensity;
2015-12-14 17:27:01 -08:00
}
if (this.sliderType === 'cutoff') {
initialDistance = this.distanceCutoff;
}
if (this.sliderType === 'exponent') {
initialDistance = this.distanceExponent;
}
var extension = Vec3.multiply(initialDistance, rightVector);
2015-12-15 15:55:03 -08:00
var sliderPosition = Vec3.sum(position, extension);
2015-12-12 16:17:00 -08:00
var properties = {
2015-12-15 18:20:12 -08:00
type: 'Sphere',
2015-12-15 15:55:03 -08:00
name: 'Hifi-Slider::' + this.sliderType,
2015-12-15 18:20:12 -08:00
dimensions: SLIDER_DIMENSIONS,
2015-12-15 15:55:03 -08:00
collisionsWillMove: true,
2015-12-12 16:17:00 -08:00
color: this.color,
2015-12-15 15:55:03 -08:00
position: sliderPosition,
2015-12-15 18:20:12 -08:00
script: SLIDER_SCRIPT_URL,
userData: JSON.stringify({
lightModifierKey: {
lightID: this.lightID,
2015-12-15 15:55:03 -08:00
sliderType: this.sliderType,
2015-12-15 18:20:12 -08:00
axisStart: position,
axisEnd: this.endOfAxis,
}
})
2015-12-12 16:17:00 -08:00
};
2015-12-15 18:20:12 -08:00
this.sliderIndicator = Entities.addEntity(properties);
2015-12-12 16:17:00 -08:00
},
2015-12-15 18:20:12 -08:00
setValueFromMessage: function(message) {
//message is not for our light
if (message.lightID !== this.lightID) {
2015-12-16 01:31:24 -08:00
// print('not our light')
2015-12-14 11:25:30 -08:00
return;
}
//message is not our type
if (message.sliderType !== this.sliderType) {
2015-12-16 01:31:24 -08:00
// print('not our slider type')
return
}
2015-12-12 17:22:57 -08:00
var lightProperties = Entities.getEntityProperties(this.lightID);
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') {
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
}
},
setInitialSliderPositions: function() {
2015-12-14 17:27:01 -08:00
this.distanceRed = (this.initialProperties.color.red / COLOR_MAX) * AXIS_SCALE;
this.distanceGreen = (this.initialProperties.color.green / COLOR_MAX) * AXIS_SCALE;
this.distanceBlue = (this.initialProperties.color.blue / COLOR_MAX) * AXIS_SCALE;
this.distanceIntensity = (this.initialProperties.intensity / INTENSITY_MAX) * AXIS_SCALE;
this.distanceCutoff = (this.initialProperties.cutoff / CUTOFF_MAX) * AXIS_SCALE;
this.distanceExponent = (this.initialProperties.exponent / EXPONENT_MAX) * AXIS_SCALE;
2015-12-12 16:17:00 -08:00
}
2015-12-15 18:20:12 -08:00
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-15 18:20:12 -08:00
var light = null;
2015-12-12 16:17:00 -08:00
function makeSliders(light) {
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) {
2015-12-16 00:58:00 -08:00
slidersRef.exponent = new entitySlider(light, ORANGE, 'exponent', 6);
sliders.push(slidersRef.exponent);
2015-12-12 16:17:00 -08:00
}
2015-12-15 18:20:12 -08:00
subscribeToSliderMessages();
2015-12-12 17:14:43 -08:00
};
2015-12-14 11:25:30 -08:00
function subScribeToNewLights() {
Messages.subscribe('Hifi-Light-Mod-Receiver');
Messages.messageReceived.connect(handleLightModMessages);
}
2015-12-15 18:20:12 -08:00
function subscribeToSliderMessages() {
Messages.subscribe('Hifi-Slider-Value-Reciever');
Messages.messageReceived.connect(handleValueMessages);
}
2015-12-14 11:25:30 -08:00
function handleLightModMessages(channel, message, sender) {
if (channel !== 'Hifi-Light-Mod-Receiver') {
return;
}
if (sender !== MyAvatar.sessionUUID) {
return;
}
var parsedMessage = JSON.parse(message);
makeSliders(parsedMessage.light);
2015-12-15 18:20:12 -08:00
light = parsedMessage.light.id
}
function handleValueMessages(channel, message, sender) {
if (channel !== 'Hifi-Slider-Value-Reciever') {
return;
}
//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);
2015-12-15 18:20:12 -08:00
slidersRef[parsedMessage.sliderType].setValueFromMessage(parsedMessage);
}
function cleanup() {
2015-12-15 18:20:12 -08:00
var i;
for (i = 0; i < sliders.length; i++) {
Entities.deleteEntity(sliders[i].axis);
Entities.deleteEntity(sliders[i].sliderIndicator);
}
2015-12-15 18:20:12 -08:00
Messages.messageReceived.disconnect(handleLightModMessages);
2015-12-15 18:20:12 -08:00
Messages.messageReceived.disconnect(handleValueMessages);
Entities.deletingEntity.disconnect(deleteEntity);
2015-12-14 11:25:30 -08:00
}
Script.scriptEnding.connect(cleanup);
2015-12-14 11:25:30 -08:00
subScribeToNewLights();
2015-12-15 18:20:12 -08:00
function deleteEntity(entityID) {
if (entityID === light) {
// cleanup();
}
}
Entities.deletingEntity.connect(deleteEntity);
2015-12-14 17:27:01 -08:00
//other light properties
// diffuseColor: { red: 255, green: 255, blue: 255 },
// ambientColor: { red: 255, green: 255, blue: 255 },
// specularColor: { red: 255, green: 255, blue: 255 },
// constantAttenuation: 1,
// linearAttenuation: 0,
// quadraticAttenuation: 0,
// exponent: 0,
// cutoff: 180, // in degrees