mirror of
https://github.com/overte-org/overte.git
synced 2025-08-09 12:08:54 +02:00
Add on/off by pressure; add point bulb to end of flashlight
This commit is contained in:
parent
71e668470f
commit
fd67a2b86f
2 changed files with 153 additions and 120 deletions
|
@ -15,30 +15,33 @@
|
||||||
Script.include("https://hifi-public.s3.amazonaws.com/scripts/utilities.js");
|
Script.include("https://hifi-public.s3.amazonaws.com/scripts/utilities.js");
|
||||||
|
|
||||||
|
|
||||||
var scriptURL = "https://hifi-public.s3.amazonaws.com/scripts/toys/flashlight/flashlight.js?"+randInt(0,1000);
|
var scriptURL = Script.resolvePath('flashlight.js?12322');
|
||||||
|
|
||||||
var modelURL = "https://hifi-public.s3.amazonaws.com/models/props/flashlight.fbx";
|
var modelURL = "https://hifi-public.s3.amazonaws.com/models/props/flashlight.fbx";
|
||||||
|
|
||||||
|
var center = Vec3.sum(Vec3.sum(MyAvatar.position, {
|
||||||
var center = Vec3.sum(Vec3.sum(MyAvatar.position, {x: 0, y: 0.5, z: 0}), Vec3.multiply(0.5, Quat.getFront(Camera.getOrientation())));
|
x: 0,
|
||||||
|
y: 0.5,
|
||||||
|
z: 0
|
||||||
|
}), Vec3.multiply(0.5, Quat.getFront(Camera.getOrientation())));
|
||||||
|
|
||||||
var flashlight = Entities.addEntity({
|
var flashlight = Entities.addEntity({
|
||||||
type: "Model",
|
type: "Model",
|
||||||
modelURL: modelURL,
|
modelURL: modelURL,
|
||||||
position: center,
|
position: center,
|
||||||
dimensions: {
|
dimensions: {
|
||||||
x: 0.04,
|
x: 0.04,
|
||||||
y: 0.15,
|
y: 0.15,
|
||||||
z: 0.04
|
z: 0.04
|
||||||
},
|
},
|
||||||
collisionsWillMove: true,
|
collisionsWillMove: true,
|
||||||
shapeType: 'box',
|
shapeType: 'box',
|
||||||
script: scriptURL
|
script: scriptURL
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
function cleanup() {
|
function cleanup() {
|
||||||
Entities.deleteEntity(flashlight);
|
Entities.deleteEntity(flashlight);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
// Script Type: Entity
|
// Script Type: Entity
|
||||||
//
|
//
|
||||||
// Created by Sam Gateau on 9/9/15.
|
// Created by Sam Gateau on 9/9/15.
|
||||||
|
// Additions by James B. Pollack @imgntn on 9/21/2015
|
||||||
// Copyright 2015 High Fidelity, Inc.
|
// Copyright 2015 High Fidelity, Inc.
|
||||||
//
|
//
|
||||||
// This is a toy script that can be added to the Flashlight model entity:
|
// This is a toy script that can be added to the Flashlight model entity:
|
||||||
|
@ -13,15 +14,9 @@
|
||||||
// Distributed under the Apache License, Version 2.0.
|
// Distributed under the Apache License, Version 2.0.
|
||||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||||
//
|
//
|
||||||
// TODO: update to use new grab signals, which will include handedness.
|
|
||||||
// BONUS: dim the light with pressure instead of binary on/off
|
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|
||||||
function debugPrint(message) {
|
|
||||||
//print(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
Script.include("../../libraries/utils.js");
|
Script.include("../../libraries/utils.js");
|
||||||
|
|
||||||
var _this;
|
var _this;
|
||||||
|
@ -30,11 +25,11 @@
|
||||||
// our this object, so we can access it in cases where we're called without a this (like in the case of various global signals)
|
// our this object, so we can access it in cases where we're called without a this (like in the case of various global signals)
|
||||||
Flashlight = function() {
|
Flashlight = function() {
|
||||||
_this = this;
|
_this = this;
|
||||||
_this._hasSpotlight = false;
|
|
||||||
_this._spotlight = null;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var DISABLE_LIGHT_THRESHOLD = 0.5;
|
//if the trigger value goes below this while held, the flashlight will turn off. if it goes above, it will
|
||||||
|
var DISABLE_LIGHT_THRESHOLD = 0.7;
|
||||||
|
|
||||||
// These constants define the Spotlight position and orientation relative to the model
|
// These constants define the Spotlight position and orientation relative to the model
|
||||||
var MODEL_LIGHT_POSITION = {
|
var MODEL_LIGHT_POSITION = {
|
||||||
x: 0,
|
x: 0,
|
||||||
|
@ -47,101 +42,131 @@
|
||||||
z: 0
|
z: 0
|
||||||
});
|
});
|
||||||
|
|
||||||
// Evaluate the world light entity position and orientation from the model ones
|
var GLOW_LIGHT_POSITION = {
|
||||||
|
x: 0,
|
||||||
|
y: -0.1,
|
||||||
|
z: 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Evaluate the world light entity positions and orientations from the model ones
|
||||||
function evalLightWorldTransform(modelPos, modelRot) {
|
function evalLightWorldTransform(modelPos, modelRot) {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
p: Vec3.sum(modelPos, Vec3.multiplyQbyV(modelRot, MODEL_LIGHT_POSITION)),
|
p: Vec3.sum(modelPos, Vec3.multiplyQbyV(modelRot, MODEL_LIGHT_POSITION)),
|
||||||
q: Quat.multiply(modelRot, MODEL_LIGHT_ROTATION)
|
q: Quat.multiply(modelRot, MODEL_LIGHT_ROTATION)
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function glowLightWorldTransform(modelPos, modelRot) {
|
||||||
|
return {
|
||||||
|
p: Vec3.sum(modelPos, Vec3.multiplyQbyV(modelRot, GLOW_LIGHT_POSITION)),
|
||||||
|
q: Quat.multiply(modelRot, MODEL_LIGHT_ROTATION)
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
Flashlight.prototype = {
|
Flashlight.prototype = {
|
||||||
lightOn: false,
|
lightOn: false,
|
||||||
|
hand: null,
|
||||||
|
whichHand: null,
|
||||||
|
hasSpotlight: false,
|
||||||
|
spotlight: null,
|
||||||
|
setRightHand: function() {
|
||||||
|
this.hand = 'RIGHT';
|
||||||
|
},
|
||||||
|
setLeftHand: function() {
|
||||||
|
this.hand = 'LEFT';
|
||||||
|
},
|
||||||
|
startNearGrab: function() {
|
||||||
|
if (!_this.hasSpotlight) {
|
||||||
|
|
||||||
|
//this light casts the beam
|
||||||
|
this.spotlight = Entities.addEntity({
|
||||||
|
type: "Light",
|
||||||
|
isSpotlight: true,
|
||||||
|
dimensions: {
|
||||||
|
x: 2,
|
||||||
|
y: 2,
|
||||||
|
z: 20
|
||||||
|
},
|
||||||
|
color: {
|
||||||
|
red: 255,
|
||||||
|
green: 255,
|
||||||
|
blue: 255
|
||||||
|
},
|
||||||
|
intensity: 2,
|
||||||
|
exponent: 0.3,
|
||||||
|
cutoff: 20
|
||||||
|
});
|
||||||
|
|
||||||
// update() will be called regulary, because we've hooked the update signal in our preload() function
|
//this light creates the effect of a bulb at the end of the flashlight
|
||||||
// we will check out userData for the grabData. In the case of the hydraGrab script, it will tell us
|
this.glowLight = Entities.addEntity({
|
||||||
// if we're currently being grabbed and if the person grabbing us is the current interfaces avatar.
|
type: "Light",
|
||||||
// we will watch this for state changes and print out if we're being grabbed or released when it changes.
|
dimensions: {
|
||||||
update: function() {
|
x: 0.25,
|
||||||
var GRAB_USER_DATA_KEY = "grabKey";
|
y: 0.25,
|
||||||
|
z: 0.25
|
||||||
|
},
|
||||||
|
isSpotlight: false,
|
||||||
|
color: {
|
||||||
|
red: 255,
|
||||||
|
green: 255,
|
||||||
|
blue: 255
|
||||||
|
},
|
||||||
|
exponent: 0,
|
||||||
|
cutoff: 90, // in degrees
|
||||||
|
});
|
||||||
|
|
||||||
// because the update() signal doesn't have a valid this, we need to use our memorized _this to access our entityID
|
this.hasSpotlight = true;
|
||||||
var entityID = _this.entityID;
|
|
||||||
|
|
||||||
// we want to assume that if there is no grab data, then we are not being grabbed
|
|
||||||
var defaultGrabData = {
|
|
||||||
activated: false,
|
|
||||||
avatarId: null
|
|
||||||
};
|
|
||||||
|
|
||||||
// this handy function getEntityCustomData() is available in utils.js and it will return just the specific section
|
|
||||||
// of user data we asked for. If it's not available it returns our default data.
|
|
||||||
var grabData = getEntityCustomData(GRAB_USER_DATA_KEY, entityID, defaultGrabData);
|
|
||||||
|
|
||||||
|
|
||||||
// if the grabData says we're being grabbed, and the owner ID is our session, then we are being grabbed by this interface
|
|
||||||
if (grabData.activated && grabData.avatarId == MyAvatar.sessionUUID) {
|
|
||||||
|
|
||||||
// remember we're being grabbed so we can detect being released
|
|
||||||
_this.beingGrabbed = true;
|
|
||||||
|
|
||||||
var modelProperties = Entities.getEntityProperties(entityID);
|
|
||||||
var lightTransform = evalLightWorldTransform(modelProperties.position, modelProperties.rotation);
|
|
||||||
|
|
||||||
// Create the spot light driven by this model if we don;t have one yet
|
|
||||||
// Or make sure to keep it's position in sync
|
|
||||||
if (!_this._hasSpotlight) {
|
|
||||||
|
|
||||||
_this._spotlight = Entities.addEntity({
|
|
||||||
type: "Light",
|
|
||||||
position: lightTransform.p,
|
|
||||||
rotation: lightTransform.q,
|
|
||||||
isSpotlight: true,
|
|
||||||
dimensions: {
|
|
||||||
x: 2,
|
|
||||||
y: 2,
|
|
||||||
z: 20
|
|
||||||
},
|
|
||||||
color: {
|
|
||||||
red: 255,
|
|
||||||
green: 255,
|
|
||||||
blue: 255
|
|
||||||
},
|
|
||||||
intensity: 2,
|
|
||||||
exponent: 0.3,
|
|
||||||
cutoff: 20
|
|
||||||
});
|
|
||||||
_this._hasSpotlight = true;
|
|
||||||
|
|
||||||
|
|
||||||
debugPrint("Flashlight:: creating a spotlight");
|
|
||||||
} else {
|
|
||||||
// Updating the spotlight
|
|
||||||
Entities.editEntity(_this._spotlight, {
|
|
||||||
position: lightTransform.p,
|
|
||||||
rotation: lightTransform.q
|
|
||||||
});
|
|
||||||
_this.changeLightWithTriggerPressure();
|
|
||||||
debugPrint("Flashlight:: updating the spotlight");
|
|
||||||
}
|
|
||||||
|
|
||||||
debugPrint("I'm being grabbed...");
|
|
||||||
|
|
||||||
} else if (_this.beingGrabbed) {
|
|
||||||
|
|
||||||
if (_this._hasSpotlight) {
|
|
||||||
Entities.deleteEntity(_this._spotlight);
|
|
||||||
debugPrint("Destroying flashlight spotlight...");
|
|
||||||
}
|
|
||||||
_this._hasSpotlight = false;
|
|
||||||
_this._spotlight = null;
|
|
||||||
|
|
||||||
// if we are not being grabbed, and we previously were, then we were just released, remember that
|
|
||||||
// and print out a message
|
|
||||||
_this.beingGrabbed = false;
|
|
||||||
debugPrint("I'm was released...");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
},
|
||||||
|
setWhichHand: function() {
|
||||||
|
this.whichHand = this.hand;
|
||||||
|
},
|
||||||
|
continueNearGrab: function() {
|
||||||
|
if (this.whichHand === null) {
|
||||||
|
//only set the active hand once -- if we always read the current hand, our 'holding' hand will get overwritten
|
||||||
|
this.setWhichHand();
|
||||||
|
} else {
|
||||||
|
this.updateLightPositions();
|
||||||
|
this.changeLightWithTriggerPressure(this.whichHand);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
releaseGrab: function() {
|
||||||
|
//delete the lights and reset state
|
||||||
|
if (this.hasSpotlight) {
|
||||||
|
Entities.deleteEntity(this.spotlight);
|
||||||
|
Entities.deleteEntity(this.glowLight);
|
||||||
|
this.hasSpotlight = false;
|
||||||
|
this.glowLight = null;
|
||||||
|
this.spotlight = null;
|
||||||
|
this.whichHand = null;
|
||||||
|
|
||||||
|
}
|
||||||
|
},
|
||||||
|
updateLightPositions: function() {
|
||||||
|
var modelProperties = Entities.getEntityProperties(this.entityID, ['position', 'rotation']);
|
||||||
|
|
||||||
|
//move the two lights along the vectors we set above
|
||||||
|
var lightTransform = evalLightWorldTransform(modelProperties.position, modelProperties.rotation);
|
||||||
|
var glowLightTransform = glowLightWorldTransform(modelProperties.position, modelProperties.rotation);
|
||||||
|
|
||||||
|
|
||||||
|
//move them with the entity model
|
||||||
|
Entities.editEntity(this.spotlight, {
|
||||||
|
position: lightTransform.p,
|
||||||
|
rotation: lightTransform.q,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
Entities.editEntity(this.glowLight, {
|
||||||
|
position: glowLightTransform.p,
|
||||||
|
rotation: glowLightTransform.q,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
},
|
},
|
||||||
changeLightWithTriggerPressure: function(flashLightHand) {
|
changeLightWithTriggerPressure: function(flashLightHand) {
|
||||||
|
|
||||||
|
@ -156,33 +181,34 @@
|
||||||
} else if (this.triggerValue >= DISABLE_LIGHT_THRESHOLD && this.lightOn === false) {
|
} else if (this.triggerValue >= DISABLE_LIGHT_THRESHOLD && this.lightOn === false) {
|
||||||
this.turnLightOn();
|
this.turnLightOn();
|
||||||
}
|
}
|
||||||
|
return
|
||||||
return triggerValue
|
|
||||||
},
|
},
|
||||||
turnLightOff: function() {
|
turnLightOff: function() {
|
||||||
Entities.editEntity(_this._spotlight, {
|
print('turn light off')
|
||||||
|
Entities.editEntity(this.spotlight, {
|
||||||
|
intensity: 0
|
||||||
|
});
|
||||||
|
Entities.editEntity(this.glowLight, {
|
||||||
intensity: 0
|
intensity: 0
|
||||||
});
|
});
|
||||||
this.lightOn = false
|
this.lightOn = false
|
||||||
},
|
},
|
||||||
turnLightOn: function() {
|
turnLightOn: function() {
|
||||||
Entities.editEntity(_this._spotlight, {
|
print('turn light on')
|
||||||
|
Entities.editEntity(this.glowLight, {
|
||||||
|
intensity: 2
|
||||||
|
});
|
||||||
|
Entities.editEntity(this.spotlight, {
|
||||||
intensity: 2
|
intensity: 2
|
||||||
});
|
});
|
||||||
this.lightOn = true
|
this.lightOn = true
|
||||||
},
|
},
|
||||||
|
|
||||||
// preload() will be called when the entity has become visible (or known) to the interface
|
// preload() will be called when the entity has become visible (or known) to the interface
|
||||||
// it gives us a chance to set our local JavaScript object up. In this case it means:
|
// it gives us a chance to set our local JavaScript object up. In this case it means:
|
||||||
// * remembering our entityID, so we can access it in cases where we're called without an entityID
|
// * remembering our entityID, so we can access it in cases where we're called without an entityID
|
||||||
// * connecting to the update signal so we can check our grabbed state
|
// * connecting to the update signal so we can check our grabbed state
|
||||||
preload: function(entityID) {
|
preload: function(entityID) {
|
||||||
_this.entityID = entityID;
|
this.entityID = entityID;
|
||||||
|
|
||||||
var modelProperties = Entities.getEntityProperties(entityID);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Script.update.connect(this.update);
|
Script.update.connect(this.update);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -191,11 +217,15 @@
|
||||||
// to the update signal
|
// to the update signal
|
||||||
unload: function(entityID) {
|
unload: function(entityID) {
|
||||||
|
|
||||||
if (_this._hasSpotlight) {
|
if (this.hasSpotlight) {
|
||||||
Entities.deleteEntity(_this._spotlight);
|
Entities.deleteEntity(this.spotlight);
|
||||||
|
Entities.deleteEntity(this.glowLight);
|
||||||
|
this.hasSpotlight = false;
|
||||||
|
this.glowLight = null;
|
||||||
|
this.spotlight = null;
|
||||||
|
this.whichHand = null;
|
||||||
}
|
}
|
||||||
_this._hasSpotlight = false;
|
|
||||||
_this._spotlight = null;
|
|
||||||
|
|
||||||
Script.update.disconnect(this.update);
|
Script.update.disconnect(this.update);
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in a new issue