This commit is contained in:
Philip Rosedale 2014-11-20 19:20:57 -06:00
commit 9a9f6bc485
5 changed files with 453 additions and 25 deletions

View file

@ -0,0 +1,381 @@
//
// sitOnEntity.js
// examples/entityScripts
//
// Created by Brad Hefta-Gaub on 11/1/14.
// Copyright 2014 High Fidelity, Inc.
//
// This is an example of an entity script for sitting.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
(function(){
this.entityID = null;
this.properties = null;
this.standUpButton = null;
this.indicatorsAdded = false;
this.indicator = new Array();
var buttonImageUrl = "https://worklist-prod.s3.amazonaws.com/attachment/0aca88e1-9bd8-5c1d.svg";
var windowDimensions = Controller.getViewportDimensions();
var buttonWidth = 37;
var buttonHeight = 46;
var buttonPadding = 50; // inside the normal toolbar position
var buttonPositionX = windowDimensions.x - buttonPadding - buttonWidth;
var buttonPositionY = (windowDimensions.y - buttonHeight) / 2 - (buttonHeight + buttonPadding);
var passedTime = 0.0;
var startPosition = null;
var startRotation = null;
var animationLenght = 2.0;
var avatarOldPosition = { x: 0, y: 0, z: 0 };
var sittingSettingsHandle = "SitJsSittingPosition";
var sitting = Settings.getValue(sittingSettingsHandle, false) == "true";
print("Original sitting status: " + sitting);
var frame = 0;
var seat = new Object();
var hiddingSeats = false;
// This is the pose we would like to end up
var pose = [
{joint:"RightUpLeg", rotation: {x:100.0, y:15.0, z:0.0}},
{joint:"RightLeg", rotation: {x:-130.0, y:15.0, z:0.0}},
{joint:"RightFoot", rotation: {x:30, y:15.0, z:0.0}},
{joint:"LeftUpLeg", rotation: {x:100.0, y:-15.0, z:0.0}},
{joint:"LeftLeg", rotation: {x:-130.0, y:-15.0, z:0.0}},
{joint:"LeftFoot", rotation: {x:30, y:15.0, z:0.0}}
];
var startPoseAndTransition = [];
function storeStartPoseAndTransition() {
for (var i = 0; i < pose.length; i++){
var startRotation = Quat.safeEulerAngles(MyAvatar.getJointRotation(pose[i].joint));
var transitionVector = Vec3.subtract( pose[i].rotation, startRotation );
startPoseAndTransition.push({joint: pose[i].joint, start: startRotation, transition: transitionVector});
}
}
function updateJoints(factor){
for (var i = 0; i < startPoseAndTransition.length; i++){
var scaledTransition = Vec3.multiply(startPoseAndTransition[i].transition, factor);
var rotation = Vec3.sum(startPoseAndTransition[i].start, scaledTransition);
MyAvatar.setJointData(startPoseAndTransition[i].joint, Quat.fromVec3Degrees( rotation ));
}
}
var sittingDownAnimation = function(deltaTime) {
passedTime += deltaTime;
var factor = passedTime/animationLenght;
if ( passedTime <= animationLenght ) {
updateJoints(factor);
var pos = { x: startPosition.x - 0.3 * factor, y: startPosition.y - 0.5 * factor, z: startPosition.z};
MyAvatar.position = pos;
} else {
Script.update.disconnect(sittingDownAnimation);
if (seat.model) {
MyAvatar.setModelReferential(seat.model.id);
}
}
}
var standingUpAnimation = function(deltaTime) {
passedTime += deltaTime;
var factor = 1 - passedTime/animationLenght;
if ( passedTime <= animationLenght ) {
updateJoints(factor);
var pos = { x: startPosition.x + 0.3 * (passedTime/animationLenght), y: startPosition.y + 0.5 * (passedTime/animationLenght), z: startPosition.z};
MyAvatar.position = pos;
} else {
Script.update.disconnect(standingUpAnimation);
}
}
var externalThis = this;
var goToSeatAnimation = function(deltaTime) {
passedTime += deltaTime;
var factor = passedTime/animationLenght;
if (passedTime <= animationLenght) {
var targetPosition = Vec3.sum(seat.position, { x: 0.3, y: 0.5, z: 0 });
MyAvatar.position = Vec3.sum(Vec3.multiply(startPosition, 1 - factor), Vec3.multiply(targetPosition, factor));
} else if (passedTime <= 2 * animationLenght) {
//Quat.print("MyAvatar: ", MyAvatar.orientation);
//Quat.print("Seat: ", seat.rotation);
MyAvatar.orientation = Quat.mix(startRotation, seat.rotation, factor - 1);
} else {
Script.update.disconnect(goToSeatAnimation);
externalThis.sitDown();
externalThis.showIndicators(false);
}
}
var globalMouseClick = function(event) {
print("globalMouseClick");
var clickedOverlay = Overlays.getOverlayAtPoint({x: event.x, y: event.y});
if (clickedOverlay == externalThis.standUpButton) {
seat.model = null;
externalThis.standUp();
Controller.mousePressEvent.disconnect(globalMouseClick);
}
};
this.sitDown = function() {
sitting = true;
Settings.setValue(sittingSettingsHandle, sitting);
print("sitDown sitting status: " + Settings.getValue(sittingSettingsHandle, false));
passedTime = 0.0;
startPosition = MyAvatar.position;
storeStartPoseAndTransition();
try {
Script.update.disconnect(standingUpAnimation);
} catch(e){
// no need to handle. if it wasn't connected no harm done
}
Script.update.connect(sittingDownAnimation);
Overlays.editOverlay(this.standUpButton, { visible: true });
Controller.mousePressEvent.connect(globalMouseClick);
}
this.standUp = function() {
sitting = false;
Settings.setValue(sittingSettingsHandle, sitting);
print("standUp sitting status: " + Settings.getValue(sittingSettingsHandle, false));
passedTime = 0.0;
startPosition = MyAvatar.position;
MyAvatar.clearReferential();
try{
Script.update.disconnect(sittingDownAnimation);
} catch (e){}
Script.update.connect(standingUpAnimation);
Overlays.editOverlay(this.standUpButton, { visible: false });
}
function SeatIndicator(modelProperties, seatIndex) {
var halfDiagonal = Vec3.length(modelProperties.dimensions) / 2.0;
this.position = Vec3.sum(modelProperties.position,
Vec3.multiply(Vec3.multiplyQbyV(modelProperties.rotation, modelProperties.sittingPoints[seatIndex].position),
halfDiagonal)); // hack
this.orientation = Quat.multiply(modelProperties.rotation,
modelProperties.sittingPoints[seatIndex].rotation);
this.scale = MyAvatar.scale / 3;
this.sphere = Overlays.addOverlay("billboard", {
subImage: { x: 0, y: buttonHeight, width: buttonWidth, height: buttonHeight},
url: buttonImageUrl,
position: this.position,
scale: this.scale,
size: this.scale,
solid: true,
color: { red: 255, green: 255, blue: 255 },
alpha: 0.8,
visible: true,
isFacingAvatar: true
});
this.show = function(doShow) {
Overlays.editOverlay(this.sphere, { visible: doShow });
}
this.update = function() {
Overlays.editOverlay(this.sphere, {
position: this.position,
size: this.scale
});
}
this.cleanup = function() {
Overlays.deleteOverlay(this.sphere);
}
}
function update(deltaTime){
var newWindowDimensions = Controller.getViewportDimensions();
if( newWindowDimensions.x != windowDimensions.x || newWindowDimensions.y != windowDimensions.y ){
windowDimensions = newWindowDimensions;
var newX = windowDimensions.x - buttonPadding - buttonWidth;
var newY = (windowDimensions.y - buttonHeight) / 2 ;
Overlays.editOverlay( this.standUpButton, {x: newX, y: newY} );
}
// For a weird reason avatar joint don't update till the 10th frame
// Set the update frame to 20 to be safe
var UPDATE_FRAME = 20;
if (frame <= UPDATE_FRAME) {
if (frame == UPDATE_FRAME) {
if (sitting == true) {
print("Was seated: " + sitting);
storeStartPoseAndTransition();
updateJoints(1.0);
Overlays.editOverlay(this.standUpButton, { visible: true });
}
}
frame++;
}
var locationChanged = false;
if (location.hostname != oldHost || !location.isConnected) {
this.removeIndicators();
oldHost = location.hostname;
locationChanged = true;
}
}
var oldHost = location.hostname;
this.addIndicators = function() {
if (!this.indicatorsAdded) {
if (this.properties.sittingPoints.length > 0) {
for (var i = 0; i < this.properties.sittingPoints.length; ++i) {
this.indicator[i] = new SeatIndicator(this.properties, i);
}
this.indicatorsAdded = true;
}
}
}
this.removeIndicators = function() {
for (var i = 0; i < this.properties.sittingPoints.length; ++i) {
this.indicator[i].cleanup();
}
}
this.showIndicators = function(doShow) {
this.addIndicators();
if (this.indicatorsAdded) {
for (var i = 0; i < this.properties.sittingPoints.length; ++i) {
this.indicator[i].show(doShow);
}
}
hiddingSeats = !doShow;
}
function raySphereIntersection(origin, direction, center, radius) {
var A = origin;
var B = Vec3.normalize(direction);
var P = center;
var x = Vec3.dot(Vec3.subtract(P, A), B);
var X = Vec3.sum(A, Vec3.multiply(B, x));
var d = Vec3.length(Vec3.subtract(P, X));
return (x > 0 && d <= radius);
}
this.cleanup = function() {
this.standUp();
MyAvatar.clearReferential();
for (var i = 0; i < pose.length; i++){
MyAvatar.clearJointData(pose[i].joint);
}
Overlays.deleteOverlay(this.standUpButton);
for (var i = 0; i < this.indicator.length; ++i) {
this.indicator[i].cleanup();
}
};
this.createStandupButton = function() {
this.standUpButton = Overlays.addOverlay("image", {
x: buttonPositionX, y: buttonPositionY, width: buttonWidth, height: buttonHeight,
subImage: { x: buttonWidth, y: buttonHeight, width: buttonWidth, height: buttonHeight},
imageURL: buttonImageUrl,
visible: false,
alpha: 1.0
});
};
this.handleClickEvent = function(event) {
var clickedOverlay = Overlays.getOverlayAtPoint({x: event.x, y: event.y});
if (clickedOverlay == this.standUpButton) {
seat.model = null;
this.standUp();
} else {
this.addIndicators();
if (this.indicatorsAdded) {
var pickRay = Camera.computePickRay(event.x, event.y);
var clickedOnSeat = false;
for (var i = 0; i < this.properties.sittingPoints.length; ++i) {
if (raySphereIntersection(pickRay.origin,
pickRay.direction,
this.indicator[i].position,
this.indicator[i].scale / 2)) {
clickedOnSeat = true;
seat.model = this.entityID; // ??
seat.position = this.indicator[i].position;
seat.rotation = this.indicator[i].orientation;
}
}
if (clickedOnSeat) {
passedTime = 0.0;
startPosition = MyAvatar.position;
startRotation = MyAvatar.orientation;
try{ Script.update.disconnect(standingUpAnimation); } catch(e){}
try{ Script.update.disconnect(sittingDownAnimation); } catch(e){}
Script.update.connect(goToSeatAnimation);
}
}
}
};
// All callbacks start by updating the properties
this.updateProperties = function(entityID) {
if (this.entityID === null || !this.entityID.isKnownID) {
this.entityID = Entities.identifyEntity(entityID);
}
this.properties = Entities.getEntityProperties(this.entityID);
};
this.unload = function(entityID) {
this.cleanup();
};
this.preload = function(entityID) {
this.updateProperties(entityID); // All callbacks start by updating the properties
this.createStandupButton();
Script.update.connect(update); // do we want to do this??? how expensive will this be?
};
this.hoverOverEntity = function(entityID, mouseEvent) {
this.updateProperties(entityID); // All callbacks start by updating the properties
this.showIndicators(true);
};
this.hoverLeaveEntity = function(entityID, mouseEvent) {
this.updateProperties(entityID); // All callbacks start by updating the properties
this.showIndicators(false);
};
this.clickDownOnEntity = function(entityID, mouseEvent) {
this.updateProperties(entityID); // All callbacks start by updating the properties
this.handleClickEvent(mouseEvent);
};
this.clickReleaseOnEntity = function(entityID, mouseEvent) {
this.updateProperties(entityID); // All callbacks start by updating the properties
};
})

View file

@ -22,14 +22,20 @@ var orbNaturalExtentsMax = { x: 1.230353, y: 1.229819, z: 1.210487 };
var panelsNaturalExtentsMin = { x: -1.223182, y: -0.348487, z: 0.0451369 };
var panelsNaturalExtentsMax = { x: 1.223039, y: 0.602978, z: 1.224298 };
var orbDimensions = Vec3.subtract(orbNaturalExtentsMax, orbNaturalExtentsMin);
var panelsDimensions = Vec3.subtract(panelsNaturalExtentsMax, panelsNaturalExtentsMin);
var orbNaturalDimensions = Vec3.subtract(orbNaturalExtentsMax, orbNaturalExtentsMin);
var panelsNaturalDimensions = Vec3.subtract(panelsNaturalExtentsMax, panelsNaturalExtentsMin);
var orbCenter = Vec3.sum(orbNaturalExtentsMin, Vec3.multiply(orbDimensions, 0.5));
var panelsCenter = Vec3.sum(panelsNaturalExtentsMin, Vec3.multiply(panelsDimensions, 0.5));
var SCALING_FACTOR = 10;
var orbDimensions = Vec3.multiply(orbNaturalDimensions, SCALING_FACTOR);
var panelsDimensions = Vec3.multiply(panelsNaturalDimensions, SCALING_FACTOR);
var orbNaturalCenter = Vec3.sum(orbNaturalExtentsMin, Vec3.multiply(orbNaturalDimensions, 0.5));
var panelsNaturalCenter = Vec3.sum(panelsNaturalExtentsMin, Vec3.multiply(panelsNaturalDimensions, 0.5));
var orbCenter = Vec3.multiply(orbNaturalCenter, SCALING_FACTOR);
var panelsCenter = Vec3.multiply(panelsNaturalCenter, SCALING_FACTOR);
var panelsCenterShift = Vec3.subtract(panelsCenter, orbCenter);
var ORB_SHIFT = { x: 0, y: -0.2, z: 0.05};
var ORB_SHIFT = { x: 0, y: -1.4, z: -0.8};
var HELMET_ATTACHMENT_URL = HIFI_PUBLIC_BUCKET + "models/attachments/IronManMaskOnly.fbx"
@ -38,10 +44,10 @@ var currentDrone = null;
var latinSound = SoundCache.getSound(HIFI_PUBLIC_BUCKET + "sounds/Lobby/latin.stereo.raw")
var elevatorSound = SoundCache.getSound(HIFI_PUBLIC_BUCKET + "sounds/Lobby/elevator.stereo.raw")
var currentMusakInjector = null;
var currentMuzakInjector = null;
var currentSound = null;
var inOculusMode = Menu.isOptionChecked("Enable VR Mode");
var inOculusMode = false;
function reticlePosition() {
var RETICLE_DISTANCE = 1;
@ -63,13 +69,15 @@ function drawLobby() {
var panelWallProps = {
url: HIFI_PUBLIC_BUCKET + "models/sets/Lobby/Lobby_v8/forStephen1/PanelWall2.fbx",
position: Vec3.sum(orbPosition, Vec3.multiplyQbyV(towardsMe, panelsCenterShift)),
rotation: towardsMe
rotation: towardsMe,
dimensions: panelsDimensions
};
var orbShellProps = {
url: HIFI_PUBLIC_BUCKET + "models/sets/Lobby/Lobby_v8/forStephen1/LobbyShell1.fbx",
position: orbPosition,
rotation: towardsMe,
dimensions: orbDimensions,
ignoreRayIntersection: true
};
@ -78,6 +86,8 @@ function drawLobby() {
panelWall = Overlays.addOverlay("model", panelWallProps);
orbShell = Overlays.addOverlay("model", orbShellProps);
inOculusMode = Menu.isOptionChecked("Enable VR Mode");
// for HMD wearers, create a reticle in center of screen
if (inOculusMode) {
var CURSOR_SCALE = 0.025;
@ -98,8 +108,8 @@ function drawLobby() {
// start the drone sound
currentDrone = Audio.playSound(droneSound, { stereo: true, loop: true, localOnly: true, volume: DRONE_VOLUME });
// start one of our musak sounds
playRandomMusak();
// start one of our muzak sounds
playRandomMuzak();
}
}
@ -125,9 +135,9 @@ function changeLobbyTextures() {
Overlays.editOverlay(panelWall, textureProp);
}
var MUSAK_VOLUME = 0.1;
var MUZAK_VOLUME = 0.1;
function playNextMusak() {
function playNextMuzak() {
if (panelWall) {
if (currentSound == latinSound) {
if (elevatorSound.downloaded) {
@ -139,11 +149,11 @@ function playNextMusak() {
}
}
currentMusakInjector = Audio.playSound(currentSound, { localOnly: true, volume: MUSAK_VOLUME });
currentMuzakInjector = Audio.playSound(currentSound, { localOnly: true, volume: MUZAK_VOLUME });
}
}
function playRandomMusak() {
function playRandomMuzak() {
currentSound = null;
if (latinSound.downloaded && elevatorSound.downloaded) {
@ -155,11 +165,11 @@ function playRandomMusak() {
}
if (currentSound) {
// pick a random number of seconds from 0-10 to offset the musak
// pick a random number of seconds from 0-10 to offset the muzak
var secondOffset = Math.random() * 10;
currentMusakInjector = Audio.playSound(currentSound, { localOnly: true, secondOffset: secondOffset, volume: MUSAK_VOLUME });
currentMuzakInjector = Audio.playSound(currentSound, { localOnly: true, secondOffset: secondOffset, volume: MUZAK_VOLUME });
} else {
currentMusakInjector = null;
currentMuzakInjector = null;
}
}
@ -188,8 +198,8 @@ function cleanupLobby() {
Audio.stopInjector(currentDrone);
currentDrone = null;
Audio.stopInjector(currentMusakInjector);
currentMusakInjector = null;
Audio.stopInjector(currentMuzakInjector);
currentMuzakInjector = null;
locations = {};
toggleEnvironmentRendering(true);
@ -259,9 +269,9 @@ function update(deltaTime) {
});
}
// if the reticle is up then we may need to play the next musak
if (!Audio.isInjectorPlaying(currentMusakInjector)) {
playNextMusak();
// if the reticle is up then we may need to play the next muzak
if (!Audio.isInjectorPlaying(currentMuzakInjector)) {
playNextMuzak();
}
}
}

View file

@ -2317,7 +2317,7 @@ void Application::update(float deltaTime) {
updateDialogs(deltaTime); // update various stats dialogs if present
updateCursor(deltaTime); // Handle cursor updates
{
if (!_aboutToQuit) {
PerformanceTimer perfTimer("entities");
_entities.update(); // update the models...
{

View file

@ -86,7 +86,7 @@ void EntityTreeRenderer::init() {
connect(entityTree, &EntityTree::deletingEntity, this, &EntityTreeRenderer::deletingEntity);
connect(entityTree, &EntityTree::addingEntity, this, &EntityTreeRenderer::checkAndCallPreload);
connect(entityTree, &EntityTree::entityScriptChanging, this, &EntityTreeRenderer::checkAndCallPreload);
connect(entityTree, &EntityTree::entityScriptChanging, this, &EntityTreeRenderer::entitySciptChanging);
connect(entityTree, &EntityTree::changingEntityID, this, &EntityTreeRenderer::changingEntityID);
}
@ -192,6 +192,22 @@ QScriptValue EntityTreeRenderer::loadEntityScript(EntityItem* entity) {
return entityScriptObject; // newly constructed
}
QScriptValue EntityTreeRenderer::getPreviouslyLoadedEntityScript(const EntityItemID& entityItemID) {
EntityItem* entity = static_cast<EntityTree*>(_tree)->findEntityByEntityItemID(entityItemID);
return getPreviouslyLoadedEntityScript(entity);
}
QScriptValue EntityTreeRenderer::getPreviouslyLoadedEntityScript(EntityItem* entity) {
if (entity) {
EntityItemID entityID = entity->getEntityItemID();
if (_entityScripts.contains(entityID)) {
EntityScriptDetails details = _entityScripts[entityID];
return details.scriptObject; // previously loaded
}
}
return QScriptValue(); // no script
}
void EntityTreeRenderer::setTree(Octree* newTree) {
OctreeRenderer::setTree(newTree);
static_cast<EntityTree*>(_tree)->setFBXService(this);
@ -842,9 +858,16 @@ void EntityTreeRenderer::mouseMoveEvent(QMouseEvent* event, unsigned int deviceI
}
void EntityTreeRenderer::deletingEntity(const EntityItemID& entityID) {
checkAndCallUnload(entityID);
_entityScripts.remove(entityID);
}
void EntityTreeRenderer::entitySciptChanging(const EntityItemID& entityID) {
checkAndCallUnload(entityID);
checkAndCallPreload(entityID);
}
void EntityTreeRenderer::checkAndCallPreload(const EntityItemID& entityID) {
// load the entity script if needed...
QScriptValue entityScript = loadEntityScript(entityID);
@ -854,6 +877,15 @@ void EntityTreeRenderer::checkAndCallPreload(const EntityItemID& entityID) {
}
}
void EntityTreeRenderer::checkAndCallUnload(const EntityItemID& entityID) {
QScriptValue entityScript = getPreviouslyLoadedEntityScript(entityID);
if (entityScript.property("unload").isValid()) {
QScriptValueList entityArgs = createEntityArgs(entityID);
entityScript.property("unload").call(entityScript, entityArgs);
}
}
void EntityTreeRenderer::changingEntityID(const EntityItemID& oldEntityID, const EntityItemID& newEntityID) {
if (_entityScripts.contains(oldEntityID)) {
EntityScriptDetails details = _entityScripts[oldEntityID];

View file

@ -106,9 +106,12 @@ signals:
public slots:
void deletingEntity(const EntityItemID& entityID);
void changingEntityID(const EntityItemID& oldEntityID, const EntityItemID& newEntityID);
void checkAndCallPreload(const EntityItemID& entityID);
void entitySciptChanging(const EntityItemID& entityID);
private:
void checkAndCallPreload(const EntityItemID& entityID);
void checkAndCallUnload(const EntityItemID& entityID);
QList<Model*> _releasedModels;
void renderProxies(const EntityItem* entity, RenderArgs* args);
PickRay computePickRay(float x, float y);
@ -127,6 +130,8 @@ private:
QScriptValue loadEntityScript(EntityItem* entity);
QScriptValue loadEntityScript(const EntityItemID& entityItemID);
QScriptValue getPreviouslyLoadedEntityScript(const EntityItemID& entityItemID);
QScriptValue getPreviouslyLoadedEntityScript(EntityItem* entity);
QString loadScriptContents(const QString& scriptMaybeURLorText);
QScriptValueList createMouseEventArgs(const EntityItemID& entityID, QMouseEvent* event, unsigned int deviceID);
QScriptValueList createMouseEventArgs(const EntityItemID& entityID, const MouseEvent& mouseEvent);