diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index d9e75c8836..c452519522 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -218,6 +218,7 @@ function getSpatialOffsetPosition(hand, spatialKey) { } var yFlip = Quat.angleAxis(180, Vec3.UNIT_Y); + function getSpatialOffsetRotation(hand, spatialKey) { var rotation = Quat.IDENTITY; @@ -261,9 +262,9 @@ function MyController(hand) { this.triggerValue = 0; // rolling average of trigger value this.rawTriggerValue = 0; this.rawBumperValue = 0; - + this.overlayLine = null; - + this.ignoreIK = false; this.offsetPosition = Vec3.ZERO; this.offsetRotation = Quat.IDENTITY; @@ -816,33 +817,36 @@ function MyController(hand) { Entities.callEntityMethod(this.grabbedEntity, "continueDistantGrab"); - var defaultConstrainData = { - axisBasePosition:false, - endOfAxis: false, - } - var constraintData = getEntityCustomData('lightModifierKey', this.grabbedEntity, defaultConstrainData); - - // var constrainX = constraintData.constrain.x; - // var constrainY = constraintData.constrain.y; - // var constrainZ = constraintData.constrain.z; - - // print('constrainY'+constrainY); // mix in head motion if (MOVE_WITH_HEAD) { var objDistance = Vec3.length(objectToAvatar); - var before = Vec3.multiplyQbyV(this.currentCameraOrientation, { x: 0.0, y: 0.0, z: objDistance }); - var after = Vec3.multiplyQbyV(Camera.orientation, { x: 0.0, y: 0.0, z: objDistance }); + var before = Vec3.multiplyQbyV(this.currentCameraOrientation, { + x: 0.0, + y: 0.0, + z: objDistance + }); + var after = Vec3.multiplyQbyV(Camera.orientation, { + x: 0.0, + y: 0.0, + z: objDistance + }); var change = Vec3.subtract(before, after); this.currentCameraOrientation = Camera.orientation; this.currentObjectPosition = Vec3.sum(this.currentObjectPosition, change); } + var defaultConstraintData = { + axisStart: false, + axisEnd: false, + } + + var constraintData = getEntityCustomData('lightModifierKey', this.grabbedEntity, defaultConstraintData); var clampedVector; var targetPosition; - if (constraintData.axisBasePosition !== false) { - clampedVector = this.projectVectorAlongAxis(this.currentObjectPosition, constraintData.axisBasePosition, constraintData.endOfAxis); + if (constraintData.startAxis !== false) { + clampedVector = this.projectVectorAlongAxis(this.currentObjectPosition, constraintData.axisStart, constraintData.axisEnd); targetPosition = clampedVector; } else { targetPosition = { @@ -859,121 +863,121 @@ function MyController(hand) { angularTimeScale: DISTANCE_HOLDING_ACTION_TIMEFRAME, ttl: ACTION_TTL }); + this.actionTimeout = now + (ACTION_TTL * MSEC_PER_SEC); + }; this.projectVectorAlongAxis = function(position, axisStart, axisEnd) { - var aPrime = Vec3.subtract(position, axisStart); + var aPrime = Vec3.subtract(position, axisStart); - var bPrime = Vec3.subtract(axisEnd, axisStart); + var bPrime = Vec3.subtract(axisEnd, axisStart); - var bPrimeMagnitude = Vec3.length(bPrime); + var bPrimeMagnitude = Vec3.length(bPrime); - var dotProduct = Vec3.dot(aPrime, bPrime); + var dotProduct = Vec3.dot(aPrime, bPrime); - var scalar = dotProduct / bPrimeMagnitude; + var scalar = dotProduct / bPrimeMagnitude; - print('SCALAR:::'+scalar); + if (scalar < 0) { + scalar = 0; + } - if(scalar<0){ - scalar = 0; - } + if (scalar > 1) { + scalar = 1; + } - if(scalar>1){ - scalar = 1; - } + var projection = Vec3.sum(axisStart, Vec3.multiply(scalar, Vec3.normalize(bPrime))); - var projection = Vec3.sum(axisStart, Vec3.multiply(scalar, Vec3.normalize(bPrime))); + return projection - return projection + }, - }, + this.nearGrabbing = function() { + var now = Date.now(); + var grabbableData = getEntityCustomData(GRABBABLE_DATA_KEY, this.grabbedEntity, DEFAULT_GRABBABLE_DATA); - this.nearGrabbing = function() { - var now = Date.now(); - var grabbableData = getEntityCustomData(GRABBABLE_DATA_KEY, this.grabbedEntity, DEFAULT_GRABBABLE_DATA); + if (this.state == STATE_NEAR_GRABBING && this.triggerSmoothedReleased()) { + this.setState(STATE_RELEASE); + Entities.callEntityMethod(this.grabbedEntity, "releaseGrab"); + return; + } - if (this.state == STATE_NEAR_GRABBING && this.triggerSmoothedReleased()) { - this.setState(STATE_RELEASE); - Entities.callEntityMethod(this.grabbedEntity, "releaseGrab"); - return; - } + this.lineOff(); + this.overlayLineOff(); - this.lineOff(); - this.overlayLineOff(); + var grabbedProperties = Entities.getEntityProperties(this.grabbedEntity, GRABBABLE_PROPERTIES); + this.activateEntity(this.grabbedEntity, grabbedProperties); + if (grabbedProperties.collisionsWillMove && NEAR_GRABBING_KINEMATIC) { + Entities.editEntity(this.grabbedEntity, { + collisionsWillMove: false + }); + } - var grabbedProperties = Entities.getEntityProperties(this.grabbedEntity, GRABBABLE_PROPERTIES); - this.activateEntity(this.grabbedEntity, grabbedProperties); - if (grabbedProperties.collisionsWillMove && NEAR_GRABBING_KINEMATIC) { - Entities.editEntity(this.grabbedEntity, { - collisionsWillMove: false + var handRotation = this.getHandRotation(); + var handPosition = this.getHandPosition(); + + var grabbableData = getEntityCustomData(GRABBABLE_DATA_KEY, this.grabbedEntity, DEFAULT_GRABBABLE_DATA); + + if (this.state != STATE_NEAR_GRABBING && grabbableData.spatialKey) { + // if an object is "equipped" and has a spatialKey, use it. + this.ignoreIK = grabbableData.spatialKey.ignoreIK ? grabbableData.spatialKey.ignoreIK : false; + this.offsetPosition = getSpatialOffsetPosition(this.hand, grabbableData.spatialKey); + this.offsetRotation = getSpatialOffsetRotation(this.hand, grabbableData.spatialKey); + } else { + this.ignoreIK = false; + + var objectRotation = grabbedProperties.rotation; + this.offsetRotation = Quat.multiply(Quat.inverse(handRotation), objectRotation); + + var currentObjectPosition = grabbedProperties.position; + var offset = Vec3.subtract(currentObjectPosition, handPosition); + this.offsetPosition = Vec3.multiplyQbyV(Quat.inverse(Quat.multiply(handRotation, this.offsetRotation)), offset); + } + + this.actionID = NULL_ACTION_ID; + this.actionID = Entities.addAction("hold", this.grabbedEntity, { + hand: this.hand === RIGHT_HAND ? "right" : "left", + timeScale: NEAR_GRABBING_ACTION_TIMEFRAME, + relativePosition: this.offsetPosition, + relativeRotation: this.offsetRotation, + ttl: ACTION_TTL, + kinematic: NEAR_GRABBING_KINEMATIC, + kinematicSetVelocity: true, + ignoreIK: this.ignoreIK }); - } - - var handRotation = this.getHandRotation(); - var handPosition = this.getHandPosition(); - - var grabbableData = getEntityCustomData(GRABBABLE_DATA_KEY, this.grabbedEntity, DEFAULT_GRABBABLE_DATA); - - if (this.state != STATE_NEAR_GRABBING && grabbableData.spatialKey) { - // if an object is "equipped" and has a spatialKey, use it. - this.ignoreIK = grabbableData.spatialKey.ignoreIK ? grabbableData.spatialKey.ignoreIK : false; - this.offsetPosition = getSpatialOffsetPosition(this.hand, grabbableData.spatialKey); - this.offsetRotation = getSpatialOffsetRotation(this.hand, grabbableData.spatialKey); - } else { - this.ignoreIK = false; - - var objectRotation = grabbedProperties.rotation; - this.offsetRotation = Quat.multiply(Quat.inverse(handRotation), objectRotation); - - var currentObjectPosition = grabbedProperties.position; - var offset = Vec3.subtract(currentObjectPosition, handPosition); - this.offsetPosition = Vec3.multiplyQbyV(Quat.inverse(Quat.multiply(handRotation, this.offsetRotation)), offset); - } - - this.actionID = NULL_ACTION_ID; - this.actionID = Entities.addAction("hold", this.grabbedEntity, { - hand: this.hand === RIGHT_HAND ? "right" : "left", - timeScale: NEAR_GRABBING_ACTION_TIMEFRAME, - relativePosition: this.offsetPosition, - relativeRotation: this.offsetRotation, - ttl: ACTION_TTL, - kinematic: NEAR_GRABBING_KINEMATIC, - kinematicSetVelocity: true, - ignoreIK: this.ignoreIK - }); - if (this.actionID === NULL_ACTION_ID) { - this.actionID = null; - } else { - this.actionTimeout = now + (ACTION_TTL * MSEC_PER_SEC); - if (this.state == STATE_NEAR_GRABBING) { - this.setState(STATE_CONTINUE_NEAR_GRABBING); + if (this.actionID === NULL_ACTION_ID) { + this.actionID = null; } else { - // equipping - Entities.callEntityMethod(this.grabbedEntity, "startEquip", [JSON.stringify(this.hand)]); - this.startHandGrasp(); + this.actionTimeout = now + (ACTION_TTL * MSEC_PER_SEC); + if (this.state == STATE_NEAR_GRABBING) { + this.setState(STATE_CONTINUE_NEAR_GRABBING); + } else { + // equipping + Entities.callEntityMethod(this.grabbedEntity, "startEquip", [JSON.stringify(this.hand)]); + this.startHandGrasp(); + + this.setState(STATE_CONTINUE_EQUIP_BD); + } + + if (this.hand === RIGHT_HAND) { + Entities.callEntityMethod(this.grabbedEntity, "setRightHand"); + } else { + Entities.callEntityMethod(this.grabbedEntity, "setLeftHand"); + } + + Entities.callEntityMethod(this.grabbedEntity, "setHand", [this.hand]); + + Entities.callEntityMethod(this.grabbedEntity, "startNearGrab"); - this.setState(STATE_CONTINUE_EQUIP_BD); } - if (this.hand === RIGHT_HAND) { - Entities.callEntityMethod(this.grabbedEntity, "setRightHand"); - } else { - Entities.callEntityMethod(this.grabbedEntity, "setLeftHand"); - } + this.currentHandControllerTipPosition = + (this.hand === RIGHT_HAND) ? MyAvatar.rightHandTipPosition : MyAvatar.leftHandTipPosition; - Entities.callEntityMethod(this.grabbedEntity, "setHand", [this.hand]); - - Entities.callEntityMethod(this.grabbedEntity, "startNearGrab"); - - } - - this.currentHandControllerTipPosition = - (this.hand === RIGHT_HAND) ? MyAvatar.rightHandTipPosition : MyAvatar.leftHandTipPosition; - - this.currentObjectTime = Date.now(); - }; + this.currentObjectTime = Date.now(); + }; this.continueNearGrabbing = function() { if (this.state == STATE_CONTINUE_NEAR_GRABBING && this.triggerSmoothedReleased()) { @@ -1358,10 +1362,10 @@ Controller.enableMapping(MAPPING_NAME); var handToDisable = 'none'; function update() { - if (handToDisable !== LEFT_HAND && handToDisable!=='both') { + if (handToDisable !== LEFT_HAND && handToDisable !== 'both') { leftController.update(); } - if (handToDisable !== RIGHT_HAND && handToDisable!=='both') { + if (handToDisable !== RIGHT_HAND && handToDisable !== 'both') { rightController.update(); } } @@ -1369,7 +1373,7 @@ function update() { Messages.subscribe('Hifi-Hand-Disabler'); handleHandDisablerMessages = function(channel, message, sender) { - + if (sender === MyAvatar.sessionUUID) { if (message === 'left') { handToDisable = LEFT_HAND; @@ -1377,11 +1381,11 @@ handleHandDisablerMessages = function(channel, message, sender) { if (message === 'right') { handToDisable = RIGHT_HAND; } - if(message==='both'){ - handToDisable='both'; + if (message === 'both') { + handToDisable = 'both'; } - if(message==='none'){ - handToDisable='none'; + if (message === 'none') { + handToDisable = 'none'; } } @@ -1396,4 +1400,4 @@ function cleanup() { } Script.scriptEnding.connect(cleanup); -Script.update.connect(update); +Script.update.connect(update); \ No newline at end of file diff --git a/examples/lights/light_loader.js b/examples/lights/light_loader.js new file mode 100644 index 0000000000..8edaa20f57 --- /dev/null +++ b/examples/lights/light_loader.js @@ -0,0 +1,8 @@ +var grabScript = Script.resolvePath('../controllers/handControllerGrab.js'); +Script.load(grabScript); +var lightModifier = Script.resolvePath('light_modifier.js'); +Script.load(lightModifier); +Script.setTimeout(function() { + var lightModifierTestScene = Script.resolvePath('light_modifier_test_scene.js'); + Script.load(lightModifierTestScene); +}, 750) \ No newline at end of file diff --git a/examples/lights/light_modifier.js b/examples/lights/light_modifier.js index f54af5b54f..cbb4e3ff10 100644 --- a/examples/lights/light_modifier.js +++ b/examples/lights/light_modifier.js @@ -1,9 +1,22 @@ -// given a selected light, instantiate some entities that represent various values you can dynamically adjust +// +// light_modifier.js +// +// Created byJames Pollack @imgntn on 10/19/2015 +// 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 // +var AXIS_SCALE = 1; +var COLOR_MAX = 255; +var INTENSITY_MAX = 0.05; +var CUTOFF_MAX = 360; +var EXPONENT_MAX = 1; - -var BOX_SCRIPT_URL = Script.resolvePath('box.js?'+Math.random(0,100)); +var SLIDER_SCRIPT_URL = Script.resolvePath('slider.js?' + Math.random(0, 100)); var RED = { red: 255, @@ -35,13 +48,12 @@ var WHITE = { blue: 255 }; -var AXIS_SCALE = 1; - -var BOX_DIMENSIONS = { - x: 0.05, - y: 0.05, - z: 0.05 +var SLIDER_DIMENSIONS = { + x: 0.075, + y: 0.075, + z: 0.075 }; + var PER_ROW_OFFSET = { x: 0, y: -0.2, @@ -55,10 +67,8 @@ function entitySlider(light, color, sliderType, row) { this.color = color; this.sliderType = sliderType; this.verticalOffset = Vec3.multiply(row, PER_ROW_OFFSET); - print('slider : ' + this.sliderType + "should have an offset of : " + this.verticalOffset); - this.avatarRot = Quat.fromPitchYawRollDegrees(0, MyAvatar.bodyYaw, 0.0); - this.basePosition = Vec3.sum(MyAvatar.position, Vec3.multiply(2, Quat.getFront(this.avatarRot))); + this.basePosition = Vec3.sum(MyAvatar.position, Vec3.multiply(1.5, Quat.getFront(this.avatarRot))); var message = { lightID: this.lightID, @@ -95,9 +105,8 @@ function entitySlider(light, color, sliderType, row) { } this.setInitialSliderPositions(); - this.subscribeToBoxMessages(); this.createAxis(); - this.createBoxIndicator(); + this.createSliderIndicator(); return this; } @@ -113,7 +122,6 @@ entitySlider.prototype = { var extension = Vec3.multiply(AXIS_SCALE, rightVector); var endOfAxis = Vec3.sum(position, extension); this.endOfAxis = endOfAxis; - print('endOfAxis:::' + JSON.stringify(endOfAxis)) var properties = { type: 'Line', name: 'Hifi-Slider-Axis::' + this.sliderType, @@ -136,10 +144,8 @@ entitySlider.prototype = { this.axis = Entities.addEntity(properties); }, - createBoxIndicator: function() { - print('BOX COLOR IS:::' + JSON.stringify(this.color)); + createSliderIndicator: function() { 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); @@ -154,7 +160,7 @@ entitySlider.prototype = { initialDistance = this.distanceBlue; } if (this.sliderType === 'intensity') { - initialDistance = this.distanceRed; + initialDistance = this.distanceIntensity; } if (this.sliderType === 'cutoff') { initialDistance = this.distanceCutoff; @@ -165,30 +171,26 @@ entitySlider.prototype = { var extension = Vec3.multiply(initialDistance, rightVector); var sliderPosition = Vec3.sum(position, extension); var properties = { - type: 'Box', + type: 'Sphere', name: 'Hifi-Slider::' + this.sliderType, - dimensions: BOX_DIMENSIONS, + dimensions: SLIDER_DIMENSIONS, collisionsWillMove: true, color: this.color, position: sliderPosition, - script: BOX_SCRIPT_URL, + script: SLIDER_SCRIPT_URL, userData: JSON.stringify({ lightModifierKey: { lightID: this.lightID, sliderType: this.sliderType, - axisBasePosition: position, - endOfAxis: this.endOfAxis, - }, - constraintKey: { - constrain: { - y: position.y - } + axisStart: position, + axisEnd: this.endOfAxis, } }) }; - this.boxIndicator = Entities.addEntity(properties); + this.sliderIndicator = Entities.addEntity(properties); }, + setValueFromMessage: function(message) { //message is not for our light @@ -203,8 +205,6 @@ entitySlider.prototype = { return } - print('should set:::' + this.sliderType); - var lightProperties = Entities.getEntityProperties(this.lightID); if (this.sliderType === 'color_red') { @@ -255,28 +255,15 @@ entitySlider.prototype = { }); } }, - subscribeToBoxMessages: function() { - Messages.subscribe('Hifi-Slider-Value-Reciever'); - Messages.messageReceived.connect(handleValueMessages); - }, setInitialSliderPositions: function() { - var COLOR_MAX = 255; - var INTENSITY_MAX = 10; - var CUTOFF_MAX = 360; - var EXPONENT_MAX = 1; - 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; - }, - cleanup: function() { - Entities.deleteEntity(this.boxIndicator); - Entities.deleteEntity(this.axis); - Messages.messageReceived.disconnect(this.handleValueMessages); } + }; var sliders = []; @@ -288,9 +275,9 @@ var slidersRef = { cutoff: null, exponent: null } +var light = null; function makeSliders(light) { - print('light in makesliders:::' + light) if (light.type === 'spotlight') { var USE_COLOR_SLIDER = true; var USE_INTENSITY_SLIDER = true; @@ -325,14 +312,19 @@ function makeSliders(light) { slidersRef.exponent = new entitySlider(light, PURPLE, 'exponent', 6); sliders.push(slidersRef.exponent); } + subscribeToSliderMessages(); }; function subScribeToNewLights() { - print('subscribing to light messages') Messages.subscribe('Hifi-Light-Mod-Receiver'); Messages.messageReceived.connect(handleLightModMessages); } +function subscribeToSliderMessages() { + Messages.subscribe('Hifi-Slider-Value-Reciever'); + Messages.messageReceived.connect(handleValueMessages); +} + function handleLightModMessages(channel, message, sender) { if (channel !== 'Hifi-Light-Mod-Receiver') { return; @@ -343,6 +335,7 @@ function handleLightModMessages(channel, message, sender) { var parsedMessage = JSON.parse(message); makeSliders(parsedMessage.light); + light = parsedMessage.light.id } function handleValueMessages(channel, message, sender) { @@ -356,27 +349,39 @@ function handleValueMessages(channel, message, sender) { // } var parsedMessage = JSON.parse(message); - slidersRef[parsedMessage.sliderType].setValueFromMessage(parsedMessage) + slidersRef[parsedMessage.sliderType].setValueFromMessage(parsedMessage); } function cleanup() { - while (sliders.length > 0) { - var slider = sliders.pop(); - slider.cleanup(); + var i; + for (i = 0; i < sliders.length; i++) { + Entities.deleteEntity(sliders[i].axis); + Entities.deleteEntity(sliders[i].sliderIndicator); } + Messages.messageReceived.disconnect(handleLightModMessages); - delete sliders + Messages.messageReceived.disconnect(handleValueMessages); + Entities.deletingEntity.disconnect(deleteEntity); + } Script.scriptEnding.connect(cleanup); subScribeToNewLights(); +function deleteEntity(entityID) { + if (entityID === light) { + // cleanup(); + } +} + + +Entities.deletingEntity.connect(deleteEntity); + //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, diff --git a/examples/lights/light_modifier_test_scene.js b/examples/lights/light_modifier_test_scene.js new file mode 100644 index 0000000000..d997816957 --- /dev/null +++ b/examples/lights/light_modifier_test_scene.js @@ -0,0 +1,128 @@ +// +// light_modifier_test_scene.js +// +// Created byJames Pollack @imgntn on 10/19/2015 +// Copyright 2015 High Fidelity, Inc. +// +// Given a selected light, instantiate some entities that represent various values you can dynamically adjust. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// +var MODEL_LIGHT_POSITION = { + x: 0, + y: -0.3, + z: 0 +}; +var MODEL_LIGHT_ROTATION = Quat.angleAxis(-90, { + x: 1, + y: 0, + z: 0 +}); + +var basePosition, avatarRot; +avatarRot = Quat.fromPitchYawRollDegrees(0, MyAvatar.bodyYaw, 0.0); +basePosition = Vec3.sum(MyAvatar.position, Vec3.multiply(-3, Quat.getUp(avatarRot))); + +var ground = Entities.addEntity({ + name: 'Hifi-Light-Mod-Floor', + //type: "Model", + type: 'Box', + color: { + red: 100, + green: 100, + blue: 100 + }, + //modelURL: "https://hifi-public.s3.amazonaws.com/eric/models/woodFloor.fbx", + dimensions: { + x: 100, + y: 2, + z: 100 + }, + position: basePosition, + shapeType: 'box' +}); + +var light, block; + +function createLight() { + var blockProperties = Entities.getEntityProperties(block, ["position", "rotation"]); + var lightTransform = evalLightWorldTransform(blockProperties.position, blockProperties.rotation); + var lightProperties = { + name: 'Hifi-Spotlight', + type: "Light", + isSpotlight: true, + dimensions: { + x: 2, + y: 2, + z: 20 + }, + parentID: block, + color: { + red: 255, + green: 0, + blue: 255 + }, + intensity: 0.035, + exponent: 1, + cutoff: 30, + lifetime: -1, + position: lightTransform.p, + rotation: lightTransform.q + }; + + light = Entities.addEntity(lightProperties); + + var message = { + light: { + id: light, + type: 'spotlight', + initialProperties: lightProperties + } + }; + + Messages.sendMessage('Hifi-Light-Mod-Receiver', JSON.stringify(message)); + +} + +function createBlock() { + var position = basePosition; + position.y += 5; + var blockProperties = { + name: 'Hifi-Spotlight-Block', + type: 'Box', + dimensions: { + x: 1, + y: 1, + z: 1 + }, + collisionsWillMove: true, + color: { + red: 0, + green: 0, + blue: 255 + }, + position: position + } + + block = Entities.addEntity(blockProperties); +} + +function evalLightWorldTransform(modelPos, modelRot) { + return { + p: Vec3.sum(modelPos, Vec3.multiplyQbyV(modelRot, MODEL_LIGHT_POSITION)), + q: Quat.multiply(modelRot, MODEL_LIGHT_ROTATION) + }; +} + +function cleanup() { + print('CLEANUP TEST SCENE SCRIPT') + Entities.deleteEntity(block); + Entities.deleteEntity(ground); + Entities.deleteEntity(light); +} + +Script.scriptEnding.connect(cleanup); + +createBlock(); +createLight(); \ No newline at end of file diff --git a/examples/lights/box.js b/examples/lights/slider.js similarity index 67% rename from examples/lights/box.js rename to examples/lights/slider.js index 3bcebc64d3..ab90019b9f 100644 --- a/examples/lights/box.js +++ b/examples/lights/slider.js @@ -2,61 +2,40 @@ var AXIS_SCALE = 1; var COLOR_MAX = 255; - var INTENSITY_MAX = 10; + var INTENSITY_MAX = 0.05; var CUTOFF_MAX = 360; var EXPONENT_MAX = 1; - function Box() { + function Slider() { return this; } - Box.prototype = { + Slider.prototype = { preload: function(entityID) { this.entityID = entityID; var entityProperties = Entities.getEntityProperties(this.entityID, "userData"); - print('USER DATA:::' + entityProperties.userData) var parsedUserData = JSON.parse(entityProperties.userData); this.userData = parsedUserData.lightModifierKey; - this.bPrime = Vec3.subtract(this.userData.endOfAxis, this.userData.axisBasePosition); - this.bPrimeMagnitude = Vec3.length(this.bPrime); - }, startNearGrab: function() { this.setInitialProperties(); }, startDistantGrab: function() { - // Entities.editEntity(this.entityID, { - // parentID: MyAvatar.sessionUUID, - // parentJointIndex: MyAvatar.getJointIndex("LeftHand") - // }); this.setInitialProperties(); }, setInitialProperties: function() { this.initialProperties = Entities.getEntityProperties(this.entityID); }, - clampPosition: function() { - - var currentProperties = Entities.getEntityProperties(this.entityID); - - var aPrime = Vec3.subtract(this.userData.axisBasePosition, currentProperties.position); - - var dotProduct = Vec3.dot(aPrime, this.bPrime); - - var scalar = dotProduct / this.bPrimeMagnitude; - - print('SCALAR:::' + scalar); - - var projection = Vec3.sum(this.userData.axisBasePosition, Vec3.multiply(scalar, Vec3.normalize(this.bPrime))); - - this.currentProjection = projection; - + continueNearGrab: function() { + // this.continueDistantGrab(); }, continueDistantGrab: function() { - // this.clampPosition(); - print('distant grab') + this.setSliderValueBasedOnDistance(); + }, + setSliderValueBasedOnDistance: function() { var currentPosition = Entities.getEntityProperties(this.entityID, "position").position; - var distance = Vec3.distance(this.axisBasePosition, this.currentProjection); + var distance = Vec3.distance(this.userData.axisStart, currentPosition); if (this.userData.sliderType === 'color_red' || this.userData.sliderType === 'color_green' || this.userData.sliderType === 'color_blue') { this.sliderValue = this.scaleValueBasedOnDistanceFromStart(distance, COLOR_MAX); @@ -71,6 +50,8 @@ this.sliderValue = this.scaleValueBasedOnDistanceFromStart(distance, EXPONENT_MAX); }; + print('SLIDER VALUE:::' + this.sliderValue) + this.sendValueToSlider(); }, releaseGrab: function() { @@ -80,7 +61,11 @@ y: 0, z: 0 }, - parentID: null + angularVelocity: { + x: 0, + y: 0, + z: 0 + } }) this.sendValueToSlider(); @@ -103,5 +88,5 @@ } }; - return new Box(); + return new Slider(); }); \ No newline at end of file diff --git a/examples/lights/testScene.js b/examples/lights/testScene.js deleted file mode 100644 index 0e0a226c0b..0000000000 --- a/examples/lights/testScene.js +++ /dev/null @@ -1,117 +0,0 @@ - // These constants define the Spotlight position and orientation relative to the model - var MODEL_LIGHT_POSITION = { - x: 0, - y: -0.3, - z: 0 - }; - var MODEL_LIGHT_ROTATION = Quat.angleAxis(-90, { - x: 1, - y: 0, - z: 0 - }); - - var basePosition, avatarRot; - avatarRot = Quat.fromPitchYawRollDegrees(0, MyAvatar.bodyYaw, 0.0); - basePosition = Vec3.sum(MyAvatar.position, Vec3.multiply(-3, Quat.getUp(avatarRot))); - - var ground = Entities.addEntity({ - name:'Hifi-Light-Mod-Floor', - type: "Model", - modelURL: "https://hifi-public.s3.amazonaws.com/eric/models/woodFloor.fbx", - dimensions: { - x: 100, - y: 2, - z: 100 - }, - position: basePosition, - shapeType: 'box' - }); - - var light, block; - - // basePosition.y += 2; - - function createLight() { - print('making light' + block) - var blockProperties = Entities.getEntityProperties(block, ["position", "rotation"]); - var lightTransform = evalLightWorldTransform(blockProperties.position, blockProperties.rotation); - var lightProperties = { - name: 'Hifi-Spotlight', - type: "Light", - isSpotlight: true, - dimensions: { - x: 2, - y: 2, - z: 20 - }, - parentID: block, - color: { - red: 255, - green: 0, - blue: 255 - }, - intensity: 2, - exponent: 0.3, - cutoff: 20, - lifetime: -1, - position: lightTransform.p, - rotation: lightTransform.q - }; - - light = Entities.addEntity(lightProperties); - - var message = { - light: { - id: light, - type: 'spotlight', - initialProperties: lightProperties - } - }; - - Messages.sendMessage('Hifi-Light-Mod-Receiver', JSON.stringify(message)); - print('SENT MESSAGE') - - } - - function createBlock() { - print('making block'); - - var position = basePosition; - position.y += 5; - var blockProperties = { - name: 'Hifi-Spotlight-Block', - type: 'Box', - dimensions: { - x: 1, - y: 1, - z: 1 - }, - collisionsWillMove: true, - color: { - red: 0, - green: 0, - blue: 255 - }, - position: position - } - - block = Entities.addEntity(blockProperties); - } - - function evalLightWorldTransform(modelPos, modelRot) { - return { - p: Vec3.sum(modelPos, Vec3.multiplyQbyV(modelRot, MODEL_LIGHT_POSITION)), - q: Quat.multiply(modelRot, MODEL_LIGHT_ROTATION) - }; - } - - function cleanup() { - Entities.deleteEntity(block); - Entities.deleteEntity(ground); - Entities.deleteEntity(light); - } - - Script.scriptEnding.connect(cleanup); - - createBlock(); - createLight(); \ No newline at end of file