From fc0c24ab65e693e79aace6b35a84075454ad0f60 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Fri, 25 Mar 2016 19:26:01 -0700 Subject: [PATCH] end of the road --- .../Home/dressingRoom/dressingRoom.js | 1361 ++++++++--------- 1 file changed, 675 insertions(+), 686 deletions(-) diff --git a/unpublishedScripts/DomainContent/Home/dressingRoom/dressingRoom.js b/unpublishedScripts/DomainContent/Home/dressingRoom/dressingRoom.js index 2489c19246..b3056e1483 100644 --- a/unpublishedScripts/DomainContent/Home/dressingRoom/dressingRoom.js +++ b/unpublishedScripts/DomainContent/Home/dressingRoom/dressingRoom.js @@ -1,750 +1,739 @@ (function() { - var utilsPath = Script.resolvePath("../utils.js"); - Script.include(utilsPath); - var avatarModelURL = 'https://s3.amazonaws.com/hifi-public/ozan/avatars/albert/albert/albert.fbx'; + var utilsPath = Script.resolvePath("../utils.js"); + Script.include(utilsPath); + var avatarModelURL = 'https://s3.amazonaws.com/hifi-public/ozan/avatars/albert/albert/albert.fbx'; - DressingRoom = function() { - return this - } + DressingRoom = function() { + return this + } - DressingRoom.prototype = { - preload: function(entityID) { - print('PRELOAD DRESSING ROOM'); - this.entityID = entityID; - // avatarModelURL = getAvatarFBX(); - }, - enterEntity: function() { - print('ENTER DRESSING ROOM'); - // avatarModelURL = getAvatarFBX(); - makeDoppelgangerForMyAvatar(); - subscribeToWearableMessages(); - // subscribeToFreezeMessages(); - createWearable(); - this.setOccupied(); - var doppelProps = Entities.getEntityProperties(this.entityID); - Script.setTimeout(function() { - Entities.editEntity(doppelgangers[0], { - dimensions: doppelProps.naturalDimensions, + DressingRoom.prototype = { + preload: function(entityID) { + print('PRELOAD DRESSING ROOM'); + this.entityID = entityID; + // avatarModelURL = getAvatarFBX(); + }, + enterEntity: function() { + print('ENTER DRESSING ROOM'); + // avatarModelURL = getAvatarFBX(); + makeDoppelgangerForMyAvatar(); + subscribeToWearableMessages(); + // subscribeToFreezeMessages(); + createWearable(); + this.setOccupied(); + var doppelProps = Entities.getEntityProperties(this.entityID); + Script.setTimeout(function() { + Entities.editEntity(doppelgangers[0], { + dimensions: doppelProps.naturalDimensions, + }); + }, 1000) + + }, + leaveEntity: function() { + print('EXIT DRESSING ROOM!'); + this.setUnoccupied(); + cleanup(); + }, + checkIfOccupied: function() { + var data = getEntityCustomData('hifi-home-dressing-room', this.entityID, { + occupied: false }); - }, 1000) - - }, - leaveEntity: function() { - print('EXIT DRESSING ROOM!'); - this.setUnoccupied(); - cleanup(); - }, - checkIfOccupied: function() { - var data = getEntityCustomData('hifi-home-dressing-room', this.entityID, { - occupied: false - }); - return data.occupied; - }, - setOccupied: function() { - setEntityCustomData('hifi-home-dressing-room', this.entityID, { - occupied: true - }); - }, - setUnoccupied: function() { - setEntityCustomData('hifi-home-dressing-room', this.entityID, { - occupied: false - }); - }, - unload: function() { - this.setUnoccupied(); - cleanup(); - }, - }; - - // - // doppelganger.js - // - // Created by James B. Pollack @imgntn on 12/28/2015 - // Copyright 2015 High Fidelity, Inc. - // - // This script shows how to hook up a model entity to your avatar to act as a doppelganger. - // - // Distributed under the Apache License, Version 2.0. - // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html - // - - var MIRROR_JOINT_DATA = true; - var MIRRORED_ENTITY_SCRIPT_URL = Script.resolvePath('mirroredEntity.js'); - var FREEZE_TOGGLER_SCRIPT_URL = Script.resolvePath('freezeToggler.js?' + Math.random(0, 1000)) - var THROTTLE = false; - var THROTTLE_RATE = 100; - var doppelgangers = []; - - - function Doppelganger(avatar) { - this.initialProperties = { - name: 'Hifi-Doppelganger', - type: 'Model', - modelURL: avatarModelURL, - // dimensions: getAvatarDimensions(avatar), - position: matchBasePosition(), - rotation: matchBaseRotation(), - collisionsWillMove: false, - ignoreForCollisions: false, - // script: FREEZE_TOGGLER_SCRIPT_URL, - userData: JSON.stringify({ - grabbableKey: { - grabbable: false, - wantsTrigger: true - } - }) + return data.occupied; + }, + setOccupied: function() { + setEntityCustomData('hifi-home-dressing-room', this.entityID, { + occupied: true + }); + }, + setUnoccupied: function() { + setEntityCustomData('hifi-home-dressing-room', this.entityID, { + occupied: false + }); + }, + unload: function() { + this.setUnoccupied(); + cleanup(); + }, }; - this.id = createDoppelgangerEntity(this.initialProperties); - this.avatar = avatar; - return this; - } + // + // doppelganger.js + // + // Created by James B. Pollack @imgntn on 12/28/2015 + // Copyright 2015 High Fidelity, Inc. + // + // This script shows how to hook up a model entity to your avatar to act as a doppelganger. + // + // Distributed under the Apache License, Version 2.0. + // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html + // - function getAvatarFBX() { - var skeletonURL = MyAvatar.skeletonModelURL; - var req = new XMLHttpRequest(); - req.open("GET", skeletonURL, false); - req.send(); + var MIRROR_JOINT_DATA = true; + var MIRRORED_ENTITY_SCRIPT_URL = Script.resolvePath('mirroredEntity.js'); + var FREEZE_TOGGLER_SCRIPT_URL = Script.resolvePath('freezeToggler.js?' + Math.random(0, 1000)) + var THROTTLE = false; + var THROTTLE_RATE = 100; + var doppelgangers = []; - var fst = req.responseText; - var fbxURL; - - var split = fst.split('\n'); - split.forEach(function(line) { - if (line.indexOf('filename') > -1) { - var innerSplit = line.split(" "); - innerSplit.forEach(function(inner) { - if (inner.indexOf('.fbx') > -1) { - fbxURL = inner; + function Doppelganger(avatar) { + this.initialProperties = { + name: 'Hifi-Doppelganger', + type: 'Model', + modelURL: avatarModelURL, + // dimensions: getAvatarDimensions(avatar), + position: matchBasePosition(), + rotation: matchBaseRotation(), + collisionsWillMove: false, + ignoreForCollisions: false, + // script: FREEZE_TOGGLER_SCRIPT_URL, + userData: JSON.stringify({ + grabbableKey: { + grabbable: false, + wantsTrigger: true } }) - } - }); + }; - return fbxURL - } + this.id = createDoppelgangerEntity(this.initialProperties); + this.avatar = avatar; + return this; + } - function getJointData(avatar) { - var allJointData = []; - var jointNames = MyAvatar.jointNames; - jointNames.forEach(function(joint, index) { - var translation = MyAvatar.getJointTranslation(index); - var rotation = MyAvatar.getJointRotation(index) - allJointData.push({ - joint: joint, - index: index, - translation: translation, - rotation: rotation + function getAvatarFBX() { + var skeletonURL = MyAvatar.skeletonModelURL; + var req = new XMLHttpRequest(); + req.open("GET", skeletonURL, false); + req.send(); + + var fst = req.responseText; + + var fbxURL; + + var split = fst.split('\n'); + split.forEach(function(line) { + if (line.indexOf('filename') > -1) { + var innerSplit = line.split(" "); + innerSplit.forEach(function(inner) { + if (inner.indexOf('.fbx') > -1) { + fbxURL = inner; + } + }) + } }); - }); - return allJointData; - } - - function setJointData(doppelganger, relativeXforms) { - print('setting joint data for ' + doppelganger.id) - var jointRotations = []; - var i, l = relativeXforms.length; - for (i = 0; i < l; i++) { - jointRotations.push(relativeXforms[i].rot); + return fbxURL } - var setJointSuccess = Entities.setAbsoluteJointRotationsInObjectFrame(doppelganger.id, jointRotations); - // print('JOINT ROTATIONS:: ' + JSON.stringify(jointRotations)); - print('SUCCESS SETTING JOINTS?' + setJointSuccess + "for " + doppelganger.id) - return; - } - // maps joint names to their mirrored joint - var JOINT_MIRROR_NAME_MAP = { - RightUpLeg: "LeftUpLeg", - RightLeg: "LeftLeg", - RightFoot: "LeftFoot", - LeftUpLeg: "RightUpLeg", - LeftLeg: "RightLeg", - LeftFoot: "RightFoot", - RightShoulder: "LeftShoulder", - RightArm: "LeftArm", - RightForeArm: "LeftForeArm", - RightHand: "LeftHand", - RightHandThumb1: "LeftHandThumb1", - RightHandThumb2: "LeftHandThumb2", - RightHandThumb3: "LeftHandThumb3", - RightHandThumb4: "LeftHandThumb4", - RightHandIndex1: "LeftHandIndex1", - RightHandIndex2: "LeftHandIndex2", - RightHandIndex3: "LeftHandIndex3", - RightHandIndex4: "LeftHandIndex4", - RightHandMiddle1: "LeftHandMiddle1", - RightHandMiddle2: "LeftHandMiddle2", - RightHandMiddle3: "LeftHandMiddle3", - RightHandMiddle4: "LeftHandMiddle4", - RightHandRing1: "LeftHandRing1", - RightHandRing2: "LeftHandRing2", - RightHandRing3: "LeftHandRing3", - RightHandRing4: "LeftHandRing4", - RightHandPinky1: "LeftHandPinky1", - RightHandPinky2: "LeftHandPinky2", - RightHandPinky3: "LeftHandPinky3", - RightHandPinky4: "LeftHandPinky4", - LeftShoulder: "RightShoulder", - LeftArm: "RightArm", - LeftForeArm: "RightForeArm", - LeftHand: "RightHand", - LeftHandThumb1: "RightHandThumb1", - LeftHandThumb2: "RightHandThumb2", - LeftHandThumb3: "RightHandThumb3", - LeftHandThumb4: "RightHandThumb4", - LeftHandIndex1: "RightHandIndex1", - LeftHandIndex2: "RightHandIndex2", - LeftHandIndex3: "RightHandIndex3", - LeftHandIndex4: "RightHandIndex4", - LeftHandMiddle1: "RightHandMiddle1", - LeftHandMiddle2: "RightHandMiddle2", - LeftHandMiddle3: "RightHandMiddle3", - LeftHandMiddle4: "RightHandMiddle4", - LeftHandRing1: "RightHandRing1", - LeftHandRing2: "RightHandRing2", - LeftHandRing3: "RightHandRing3", - LeftHandRing4: "RightHandRing4", - LeftHandPinky1: "RightHandPinky1", - LeftHandPinky2: "RightHandPinky2", - LeftHandPinky3: "RightHandPinky3", - LeftHandPinky4: "RightHandPinky4", - LeftHandPinky: "RightHandPinky", - }; + function getJointData(avatar) { + var allJointData = []; + var jointNames = MyAvatar.jointNames; + jointNames.forEach(function(joint, index) { + var translation = MyAvatar.getJointTranslation(index); + var rotation = MyAvatar.getJointRotation(index) + allJointData.push({ + joint: joint, + index: index, + translation: translation, + rotation: rotation + }); + }); - // maps joint names to parent joint names. - var JOINT_PARENT_NAME_MAP = { - Hips: "", - RightUpLeg: "Hips", - RightLeg: "RightUpLeg", - RightFoot: "RightLeg", - LeftUpLeg: "Hips", - LeftLeg: "LeftUpLeg", - LeftFoot: "LeftLeg", - Spine: "Hips", - Spine1: "Spine", - Spine2: "Spine1", - Spine3: "Spine2", - Neck: "Spine3", - Head: "Neck", - RightShoulder: "Spine3", - RightArm: "RightShoulder", - RightForeArm: "RightArm", - RightHand: "RightForeArm", - RightHandThumb1: "RightHand", - RightHandThumb2: "RightHandThumb1", - RightHandThumb3: "RightHandThumb2", - RightHandThumb4: "RightHandThumb3", - RightHandIndex1: "RightHand", - RightHandIndex2: "RightHandIndex1", - RightHandIndex3: "RightHandIndex2", - RightHandIndex4: "RightHandIndex3", - RightHandMiddle1: "RightHand", - RightHandMiddle2: "RightHandMiddle1", - RightHandMiddle3: "RightHandMiddle2", - RightHandMiddle4: "RightHandMiddle3", - RightHandRing1: "RightHand", - RightHandRing2: "RightHandRing1", - RightHandRing3: "RightHandRing2", - RightHandRing4: "RightHandRing3", - RightHandPinky1: "RightHand", - RightHandPinky2: "RightHandPinky1", - RightHandPinky3: "RightHandPinky2", - RightHandPinky4: "RightHandPinky3", - LeftShoulder: "Spine3", - LeftArm: "LeftShoulder", - LeftForeArm: "LeftArm", - LeftHand: "LeftForeArm", - LeftHandThumb1: "LeftHand", - LeftHandThumb2: "LeftHandThumb1", - LeftHandThumb3: "LeftHandThumb2", - LeftHandThumb4: "LeftHandThumb3", - LeftHandIndex1: "LeftHand", - LeftHandIndex2: "LeftHandIndex1", - LeftHandIndex3: "LeftHandIndex2", - LeftHandIndex4: "LeftHandIndex3", - LeftHandMiddle1: "LeftHand", - LeftHandMiddle2: "LeftHandMiddle1", - LeftHandMiddle3: "LeftHandMiddle2", - LeftHandMiddle4: "LeftHandMiddle3", - LeftHandRing1: "LeftHand", - LeftHandRing2: "LeftHandRing1", - LeftHandRing3: "LeftHandRing2", - LeftHandRing4: "LeftHandRing3", - LeftHandPinky1: "LeftHand", - LeftHandPinky2: "LeftHandPinky1", - LeftHandPinky3: "LeftHandPinky2", - LeftHandPinky: "LeftHandPinky3", - }; + return allJointData; + } - // maps joint indices to parent joint indices. - var JOINT_PARENT_INDEX_MAP; - var JOINT_MIRROR_INDEX_MAP; - - // ctor - function Xform(rot, pos) { - this.rot = rot; - this.pos = pos; - }; - Xform.ident = function() { - return new Xform({ - x: 0, - y: 0, - z: 0, - w: 1 - }, { - x: 0, - y: 0, - z: 0 - }); - } - Xform.mul = function(lhs, rhs) { - var rot = Quat.multiply(lhs.rot, rhs.rot); - var pos = Vec3.sum(lhs.pos, Vec3.multiplyQbyV(lhs.rot, rhs.pos)); - return new Xform(rot, pos); - }; - Xform.prototype.inv = function() { - var invRot = Quat.inverse(this.rot); - var invPos = Vec3.multiply(-1, this.pos); - return new Xform(invRot, Vec3.multiplyQbyV(invRot, invPos)); - }; - Xform.prototype.mirrorX = function() { - return new Xform({ - x: this.rot.x, - y: -this.rot.y, - z: -this.rot.z, - w: this.rot.w - }, { - x: -this.pos.x, - y: this.pos.y, - z: this.pos.z - }); - } - Xform.prototype.toString = function() { - var rot = this.rot; - var pos = this.pos; - return "Xform rot = (" + rot.x + ", " + rot.y + ", " + rot.z + ", " + rot.w + "), pos = (" + pos.x + ", " + pos.y + ", " + pos.z + ")"; - }; - - function buildAbsoluteXformsFromMyAvatar() { - var jointNames = MyAvatar.getJointNames(); - - // lazy init of JOINT_PARENT_INDEX_MAP - if (jointNames.length > 0 && !JOINT_PARENT_INDEX_MAP) { - JOINT_PARENT_INDEX_MAP = {}; - var keys = Object.keys(JOINT_PARENT_NAME_MAP); - var i, l = keys.length; - var keyIndex, valueName, valueIndex; + function setJointData(doppelganger, relativeXforms) { + print('setting joint data for ' + doppelganger.id) + var jointRotations = []; + var i, l = relativeXforms.length; for (i = 0; i < l; i++) { - keyIndex = MyAvatar.getJointIndex(keys[i]); - valueName = JOINT_PARENT_NAME_MAP[keys[i]]; - if (valueName) { - valueIndex = MyAvatar.getJointIndex(valueName); + jointRotations.push(relativeXforms[i].rot); + } + var setJointSuccess = Entities.setAbsoluteJointRotationsInObjectFrame(doppelganger.id, jointRotations); + // print('JOINT ROTATIONS:: ' + JSON.stringify(jointRotations)); + print('SUCCESS SETTING JOINTS?' + setJointSuccess + "for " + doppelganger.id) + return; + } + + // maps joint names to their mirrored joint + var JOINT_MIRROR_NAME_MAP = { + RightUpLeg: "LeftUpLeg", + RightLeg: "LeftLeg", + RightFoot: "LeftFoot", + LeftUpLeg: "RightUpLeg", + LeftLeg: "RightLeg", + LeftFoot: "RightFoot", + RightShoulder: "LeftShoulder", + RightArm: "LeftArm", + RightForeArm: "LeftForeArm", + RightHand: "LeftHand", + RightHandThumb1: "LeftHandThumb1", + RightHandThumb2: "LeftHandThumb2", + RightHandThumb3: "LeftHandThumb3", + RightHandThumb4: "LeftHandThumb4", + RightHandIndex1: "LeftHandIndex1", + RightHandIndex2: "LeftHandIndex2", + RightHandIndex3: "LeftHandIndex3", + RightHandIndex4: "LeftHandIndex4", + RightHandMiddle1: "LeftHandMiddle1", + RightHandMiddle2: "LeftHandMiddle2", + RightHandMiddle3: "LeftHandMiddle3", + RightHandMiddle4: "LeftHandMiddle4", + RightHandRing1: "LeftHandRing1", + RightHandRing2: "LeftHandRing2", + RightHandRing3: "LeftHandRing3", + RightHandRing4: "LeftHandRing4", + RightHandPinky1: "LeftHandPinky1", + RightHandPinky2: "LeftHandPinky2", + RightHandPinky3: "LeftHandPinky3", + RightHandPinky4: "LeftHandPinky4", + LeftShoulder: "RightShoulder", + LeftArm: "RightArm", + LeftForeArm: "RightForeArm", + LeftHand: "RightHand", + LeftHandThumb1: "RightHandThumb1", + LeftHandThumb2: "RightHandThumb2", + LeftHandThumb3: "RightHandThumb3", + LeftHandThumb4: "RightHandThumb4", + LeftHandIndex1: "RightHandIndex1", + LeftHandIndex2: "RightHandIndex2", + LeftHandIndex3: "RightHandIndex3", + LeftHandIndex4: "RightHandIndex4", + LeftHandMiddle1: "RightHandMiddle1", + LeftHandMiddle2: "RightHandMiddle2", + LeftHandMiddle3: "RightHandMiddle3", + LeftHandMiddle4: "RightHandMiddle4", + LeftHandRing1: "RightHandRing1", + LeftHandRing2: "RightHandRing2", + LeftHandRing3: "RightHandRing3", + LeftHandRing4: "RightHandRing4", + LeftHandPinky1: "RightHandPinky1", + LeftHandPinky2: "RightHandPinky2", + LeftHandPinky3: "RightHandPinky3", + LeftHandPinky4: "RightHandPinky4", + LeftHandPinky: "RightHandPinky", + }; + + // maps joint names to parent joint names. + var JOINT_PARENT_NAME_MAP = { + Hips: "", + RightUpLeg: "Hips", + RightLeg: "RightUpLeg", + RightFoot: "RightLeg", + LeftUpLeg: "Hips", + LeftLeg: "LeftUpLeg", + LeftFoot: "LeftLeg", + Spine: "Hips", + Spine1: "Spine", + Spine2: "Spine1", + Spine3: "Spine2", + Neck: "Spine3", + Head: "Neck", + RightShoulder: "Spine3", + RightArm: "RightShoulder", + RightForeArm: "RightArm", + RightHand: "RightForeArm", + RightHandThumb1: "RightHand", + RightHandThumb2: "RightHandThumb1", + RightHandThumb3: "RightHandThumb2", + RightHandThumb4: "RightHandThumb3", + RightHandIndex1: "RightHand", + RightHandIndex2: "RightHandIndex1", + RightHandIndex3: "RightHandIndex2", + RightHandIndex4: "RightHandIndex3", + RightHandMiddle1: "RightHand", + RightHandMiddle2: "RightHandMiddle1", + RightHandMiddle3: "RightHandMiddle2", + RightHandMiddle4: "RightHandMiddle3", + RightHandRing1: "RightHand", + RightHandRing2: "RightHandRing1", + RightHandRing3: "RightHandRing2", + RightHandRing4: "RightHandRing3", + RightHandPinky1: "RightHand", + RightHandPinky2: "RightHandPinky1", + RightHandPinky3: "RightHandPinky2", + RightHandPinky4: "RightHandPinky3", + LeftShoulder: "Spine3", + LeftArm: "LeftShoulder", + LeftForeArm: "LeftArm", + LeftHand: "LeftForeArm", + LeftHandThumb1: "LeftHand", + LeftHandThumb2: "LeftHandThumb1", + LeftHandThumb3: "LeftHandThumb2", + LeftHandThumb4: "LeftHandThumb3", + LeftHandIndex1: "LeftHand", + LeftHandIndex2: "LeftHandIndex1", + LeftHandIndex3: "LeftHandIndex2", + LeftHandIndex4: "LeftHandIndex3", + LeftHandMiddle1: "LeftHand", + LeftHandMiddle2: "LeftHandMiddle1", + LeftHandMiddle3: "LeftHandMiddle2", + LeftHandMiddle4: "LeftHandMiddle3", + LeftHandRing1: "LeftHand", + LeftHandRing2: "LeftHandRing1", + LeftHandRing3: "LeftHandRing2", + LeftHandRing4: "LeftHandRing3", + LeftHandPinky1: "LeftHand", + LeftHandPinky2: "LeftHandPinky1", + LeftHandPinky3: "LeftHandPinky2", + LeftHandPinky: "LeftHandPinky3", + }; + + // maps joint indices to parent joint indices. + var JOINT_PARENT_INDEX_MAP; + var JOINT_MIRROR_INDEX_MAP; + + // ctor + function Xform(rot, pos) { + this.rot = rot; + this.pos = pos; + }; + Xform.ident = function() { + return new Xform({ + x: 0, + y: 0, + z: 0, + w: 1 + }, { + x: 0, + y: 0, + z: 0 + }); + } + Xform.mul = function(lhs, rhs) { + var rot = Quat.multiply(lhs.rot, rhs.rot); + var pos = Vec3.sum(lhs.pos, Vec3.multiplyQbyV(lhs.rot, rhs.pos)); + return new Xform(rot, pos); + }; + Xform.prototype.inv = function() { + var invRot = Quat.inverse(this.rot); + var invPos = Vec3.multiply(-1, this.pos); + return new Xform(invRot, Vec3.multiplyQbyV(invRot, invPos)); + }; + Xform.prototype.mirrorX = function() { + return new Xform({ + x: this.rot.x, + y: -this.rot.y, + z: -this.rot.z, + w: this.rot.w + }, { + x: -this.pos.x, + y: this.pos.y, + z: this.pos.z + }); + } + Xform.prototype.toString = function() { + var rot = this.rot; + var pos = this.pos; + return "Xform rot = (" + rot.x + ", " + rot.y + ", " + rot.z + ", " + rot.w + "), pos = (" + pos.x + ", " + pos.y + ", " + pos.z + ")"; + }; + + function buildAbsoluteXformsFromMyAvatar() { + var jointNames = MyAvatar.getJointNames(); + + // lazy init of JOINT_PARENT_INDEX_MAP + if (jointNames.length > 0 && !JOINT_PARENT_INDEX_MAP) { + JOINT_PARENT_INDEX_MAP = {}; + var keys = Object.keys(JOINT_PARENT_NAME_MAP); + var i, l = keys.length; + var keyIndex, valueName, valueIndex; + for (i = 0; i < l; i++) { + keyIndex = MyAvatar.getJointIndex(keys[i]); + valueName = JOINT_PARENT_NAME_MAP[keys[i]]; + if (valueName) { + valueIndex = MyAvatar.getJointIndex(valueName); + } else { + valueIndex = -1; + } + JOINT_PARENT_INDEX_MAP[keyIndex] = valueIndex; + } + } + + // lazy init of JOINT_MIRROR_INDEX_MAP + if (jointNames.length > 0 && !JOINT_MIRROR_INDEX_MAP) { + JOINT_MIRROR_INDEX_MAP = {}; + var keys = Object.keys(JOINT_MIRROR_NAME_MAP); + var i, l = keys.length; + var keyIndex, valueName, valueIndex; + for (i = 0; i < l; i++) { + keyIndex = MyAvatar.getJointIndex(keys[i]); + valueIndex = MyAvatar.getJointIndex(JOINT_MIRROR_NAME_MAP[keys[i]]); + if (valueIndex > 0) { + JOINT_MIRROR_INDEX_MAP[keyIndex] = valueIndex; + } + } + } + + // build absolute xforms by multiplying by parent Xforms + var absoluteXforms = []; + var i, l = jointNames.length; + var parentXform; + for (i = 0; i < l; i++) { + var parentIndex = JOINT_PARENT_INDEX_MAP[i]; + if (parentIndex >= 0) { + parentXform = absoluteXforms[parentIndex]; } else { - valueIndex = -1; + parentXform = Xform.ident(); } - JOINT_PARENT_INDEX_MAP[keyIndex] = valueIndex; + var localXform = new Xform(MyAvatar.getJointRotation(i), MyAvatar.getJointTranslation(i)); + absoluteXforms.push(Xform.mul(parentXform, localXform)); } + return absoluteXforms; } - // lazy init of JOINT_MIRROR_INDEX_MAP - if (jointNames.length > 0 && !JOINT_MIRROR_INDEX_MAP) { - JOINT_MIRROR_INDEX_MAP = {}; - var keys = Object.keys(JOINT_MIRROR_NAME_MAP); - var i, l = keys.length; - var keyIndex, valueName, valueIndex; - for (i = 0; i < l; i++) { - keyIndex = MyAvatar.getJointIndex(keys[i]); - valueIndex = MyAvatar.getJointIndex(JOINT_MIRROR_NAME_MAP[keys[i]]); - if (valueIndex > 0) { - JOINT_MIRROR_INDEX_MAP[keyIndex] = valueIndex; - } - } - } + function buildRelativeXformsFromAbsoluteXforms(absoluteXforms) { - // build absolute xforms by multiplying by parent Xforms - var absoluteXforms = []; - var i, l = jointNames.length; - var parentXform; - for (i = 0; i < l; i++) { - var parentIndex = JOINT_PARENT_INDEX_MAP[i]; - if (parentIndex >= 0) { - parentXform = absoluteXforms[parentIndex]; - } else { - parentXform = Xform.ident(); - } - var localXform = new Xform(MyAvatar.getJointRotation(i), MyAvatar.getJointTranslation(i)); - absoluteXforms.push(Xform.mul(parentXform, localXform)); - } - return absoluteXforms; - } - - function buildRelativeXformsFromAbsoluteXforms(absoluteXforms) { - - // build relative xforms by multiplying by the inverse of the parent Xforms - var relativeXforms = []; - var i, l = absoluteXforms.length; - var parentXform; - for (i = 0; i < l; i++) { - var parentIndex = JOINT_PARENT_INDEX_MAP[i]; - if (parentIndex >= 0) { - parentXform = absoluteXforms[parentIndex]; - } else { - parentXform = Xform.ident(); - } - relativeXforms.push(Xform.mul(parentXform.inv(), absoluteXforms[i])); - } - return relativeXforms; - } - - function createDoppelganger(avatar) { - return new Doppelganger(avatar); - } - - function createDoppelgangerEntity(initialProperties) { - return Entities.addEntity(initialProperties); - } - - function matchBasePosition() { - var ids = Entities.findEntities(MyAvatar.position, 20); - for (var i = 0; i < ids.length; i++) { - var entityID = ids[i]; - var props = Entities.getEntityProperties(entityID, "name"); - var name = props.name; - if (name === "Hifi-Dressing-Room-Base") { - var details = Entities.getEntityProperties(entityID, ["position", "dimensions"]); - details.position.y += getAvatarFootOffset(); - details.position.y += details.dimensions.y / 2; - print('JBP BASE POSITION ' + JSON.stringify(details.position)) - return details.position; - } - } - } - - - function getAvatarFootOffset() { - var data = getJointData(); - var upperLeg, lowerLeg, foot, toe, toeTop; - data.forEach(function(d) { - - var jointName = d.joint; - if (jointName === "RightUpLeg") { - upperLeg = d.translation.y; - } - if (jointName === "RightLeg") { - lowerLeg = d.translation.y; - } - if (jointName === "RightFoot") { - foot = d.translation.y; - } - if (jointName === "RightToeBase") { - toe = d.translation.y; - } - if (jointName === "RightToe_End") { - toeTop = d.translation.y - } - }) - - var myPosition = MyAvatar.position; - var offset = upperLeg + lowerLeg + foot + toe + toeTop; - offset = offset / 100; - return offset - } - - function matchBaseRotation() { - var ids = Entities.findEntities(MyAvatar.position, 20); - var hasBase = false; - for (var i = 0; i < ids.length; i++) { - var entityID = ids[i]; - var props = Entities.getEntityProperties(entityID, "name"); - var name = props.name; - if (name === "Hifi-Dressing-Room-Base") { - var details = Entities.getEntityProperties(entityID, "rotation"); - print('JBP DOPPELGANGER ROTATION SET TO BASE:: ' + JSON.stringify(details.rotation)) - return details.rotation; - } - } - } - - - - function connectDoppelgangerUpdates() { - Script.update.connect(updateDoppelganger); - - } - - function disconnectDoppelgangerUpdates() { - Script.update.disconnect(updateDoppelganger); - } - - var sinceLastUpdate = 0; - - function updateDoppelganger(deltaTime) { - if (THROTTLE === true) { - sinceLastUpdate = sinceLastUpdate + deltaTime * 100; - if (sinceLastUpdate > THROTTLE_RATE) { - sinceLastUpdate = 0; - } else { - return; - } - } - - - var absoluteXforms = buildAbsoluteXformsFromMyAvatar(); - if (MIRROR_JOINT_DATA) { - var mirroredAbsoluteXforms = []; + // build relative xforms by multiplying by the inverse of the parent Xforms + var relativeXforms = []; var i, l = absoluteXforms.length; + var parentXform; for (i = 0; i < l; i++) { - var mirroredIndex = JOINT_MIRROR_INDEX_MAP[i]; - if (mirroredIndex === undefined) { - mirroredIndex = i; + var parentIndex = JOINT_PARENT_INDEX_MAP[i]; + if (parentIndex >= 0) { + parentXform = absoluteXforms[parentIndex]; + } else { + parentXform = Xform.ident(); } - mirroredAbsoluteXforms[mirroredIndex] = absoluteXforms[i].mirrorX(); + relativeXforms.push(Xform.mul(parentXform.inv(), absoluteXforms[i])); } - absoluteXforms = mirroredAbsoluteXforms; - } - var relativeXforms = buildRelativeXformsFromAbsoluteXforms(absoluteXforms); - // print('DOPPELGANGERS:::: ' + doppelgangers.length); - // print('first doppel id:: ' + doppelgangers[0].id); - doppelgangers.forEach(function(doppelganger) { - setJointData(doppelganger, relativeXforms); - }) - - } - - function makeDoppelgangerForMyAvatar() { - doppelgangers = []; - var doppelganger = createDoppelganger(MyAvatar); - doppelgangers.push(doppelganger); - connectDoppelgangerUpdates(); - } - - function subscribeToWearableMessages() { - Messages.subscribe('Hifi-Doppelganger-Wearable'); - Messages.messageReceived.connect(handleWearableMessages); - } - - function subscribeToFreezeMessages() { - Messages.subscribe('Hifi-Doppelganger-Freeze'); - Messages.messageReceived.connect(handleFreezeMessages); - } - - function handleFreezeMessages(channel, message, sender) { - if (channel !== 'Hifi-Doppelganger-Freeze') { - return; - } - if (sender !== MyAvatar.sessionUUID) { - return; + return relativeXforms; } - var parsedMessage = null; + function createDoppelganger(avatar) { + return new Doppelganger(avatar); + } - try { - parsedMessage = JSON.parse(message); - } catch (e) { - print('error parsing wearable message'); + function createDoppelgangerEntity(initialProperties) { + return Entities.addEntity(initialProperties); } - print('MESSAGE ACTION::' + parsedMessage.action) - if (parsedMessage.action === 'freeze') { - print('ACTUAL FREEZE') - disconnectDoppelgangerUpdates(); + + function matchBasePosition() { + var ids = Entities.findEntities(MyAvatar.position, 20); + for (var i = 0; i < ids.length; i++) { + var entityID = ids[i]; + var props = Entities.getEntityProperties(entityID, "name"); + var name = props.name; + if (name === "Hifi-Dressing-Room-Base") { + var details = Entities.getEntityProperties(entityID, ["position", "dimensions"]); + details.position.y += getAvatarFootOffset(); + details.position.y += details.dimensions.y / 2; + print('JBP BASE POSITION ' + JSON.stringify(details.position)) + return details.position; + } + } } - if (parsedMessage.action === 'unfreeze') { - print('ACTUAL UNFREEZE') + + + function getAvatarFootOffset() { + var data = getJointData(); + var upperLeg, lowerLeg, foot, toe, toeTop; + data.forEach(function(d) { + + var jointName = d.joint; + if (jointName === "RightUpLeg") { + upperLeg = d.translation.y; + } + if (jointName === "RightLeg") { + lowerLeg = d.translation.y; + } + if (jointName === "RightFoot") { + foot = d.translation.y; + } + if (jointName === "RightToeBase") { + toe = d.translation.y; + } + if (jointName === "RightToe_End") { + toeTop = d.translation.y + } + }) + + var myPosition = MyAvatar.position; + var offset = upperLeg + lowerLeg + foot + toe + toeTop; + offset = offset / 100; + return offset + } + + function matchBaseRotation() { + var ids = Entities.findEntities(MyAvatar.position, 20); + var hasBase = false; + for (var i = 0; i < ids.length; i++) { + var entityID = ids[i]; + var props = Entities.getEntityProperties(entityID, "name"); + var name = props.name; + if (name === "Hifi-Dressing-Room-Base") { + var details = Entities.getEntityProperties(entityID, "rotation"); + print('JBP DOPPELGANGER ROTATION SET TO BASE:: ' + JSON.stringify(details.rotation)) + return details.rotation; + } + } + } + + + + function connectDoppelgangerUpdates() { + Script.update.connect(updateDoppelganger); + + } + + function disconnectDoppelgangerUpdates() { + Script.update.disconnect(updateDoppelganger); + } + + var sinceLastUpdate = 0; + + function updateDoppelganger(deltaTime) { + if (THROTTLE === true) { + sinceLastUpdate = sinceLastUpdate + deltaTime * 100; + if (sinceLastUpdate > THROTTLE_RATE) { + sinceLastUpdate = 0; + } else { + return; + } + } + + + var absoluteXforms = buildAbsoluteXformsFromMyAvatar(); + if (MIRROR_JOINT_DATA) { + var mirroredAbsoluteXforms = []; + var i, l = absoluteXforms.length; + for (i = 0; i < l; i++) { + var mirroredIndex = JOINT_MIRROR_INDEX_MAP[i]; + if (mirroredIndex === undefined) { + mirroredIndex = i; + } + mirroredAbsoluteXforms[mirroredIndex] = absoluteXforms[i].mirrorX(); + } + absoluteXforms = mirroredAbsoluteXforms; + } + var relativeXforms = buildRelativeXformsFromAbsoluteXforms(absoluteXforms); + // print('DOPPELGANGERS:::: ' + doppelgangers.length); + // print('first doppel id:: ' + doppelgangers[0].id); + doppelgangers.forEach(function(doppelganger) { + setJointData(doppelganger, relativeXforms); + }) + + } + + function makeDoppelgangerForMyAvatar() { + doppelgangers = []; + var doppelganger = createDoppelganger(MyAvatar); + doppelgangers.push(doppelganger); connectDoppelgangerUpdates(); } - } + // function subscribeToWearableMessages() { + // Messages.subscribe('Hifi-Doppelganger-Wearable'); + // Messages.messageReceived.connect(handleWearableMessages); + // } - var wearablePairs = []; - - function handleWearableMessages(channel, message, sender) { - if (channel !== 'Hifi-Doppelganger-Wearable' || 'Hifi-Doppelganger-Wearable-Avatar') { - return; + function subscribeToFreezeMessages() { + Messages.subscribe('Hifi-Doppelganger-Freeze'); + Messages.messageReceived.connect(handleFreezeMessages); } - if (sender !== MyAvatar.sessionUUID) { - return; - } - - var parsedMessage = null; - - try { - parsedMessage = JSON.parse(message); - } catch (e) { - print('error parsing wearable message'); - } - print('parsed message!!!') - - if (channel === 'Hifi-Doppelganger-Wearable') { - mirrorEntitiesForDoppelganger(doppelgangers[0], parsedMessage); - } - if (channel === 'Hifi-Doppelganger-Wearable') { - mirrorEntitiesForAvatar(parsedMessge); - } - - } - - function mirrorEntitiesForDoppelganger(doppelganger, parsedMessage) { - var doppelgangerProps = Entities.getEntityProperties(doppelganger.id); - - var action = parsedMessage.action; - print('IN MIRROR ENTITIES CALL' + action) - - var baseEntity = parsedMessage.baseEntity; - - var wearableProps = Entities.getEntityProperties(baseEntity); - - print('WEARABLE PROPS::') - delete wearableProps.id; - delete wearableProps.created; - delete wearableProps.age; - delete wearableProps.ageAsText; - //delete wearableProps.position; - // add to dg - // add to avatar - // moved item on dg - // moved item on avatar - // remove item from dg - // remove item from avatar - - var joint = wearableProps.parentJointIndex; - if (action === 'add') { - print('IN DOPPELGANGER ADD'); - - wearableProps.parentID = doppelganger.id; - wearableProps.parentJointIndex = joint; - - //create a new one - wearableProps.script = MIRRORED_ENTITY_SCRIPT_URL; - wearableProps.name = 'Hifi-Doppelganger-Mirrored-Entity'; - wearableProps.userData = JSON.stringify({ - doppelgangerKey: { - baseEntity: baseEntity, - doppelganger: doppelganger.id - } - }) - var mirrorEntity = Entities.addEntity(wearableProps); - - var mirrorEntityProps = Entities.getEntityProperties(mirrorEntity) - print('MIRROR PROPS::' + JSON.stringify(mirrorEntityProps)) - var wearablePair = { - baseEntity: baseEntity, - mirrorEntity: mirrorEntity + function handleFreezeMessages(channel, message, sender) { + if (channel !== 'Hifi-Doppelganger-Freeze') { + return; + } + if (sender !== MyAvatar.sessionUUID) { + return; + } + + var parsedMessage = null; + + try { + parsedMessage = JSON.parse(message); + } catch (e) { + print('error parsing wearable message'); + } + print('MESSAGE ACTION::' + parsedMessage.action) + if (parsedMessage.action === 'freeze') { + print('ACTUAL FREEZE') + disconnectDoppelgangerUpdates(); + } + if (parsedMessage.action === 'unfreeze') { + print('ACTUAL UNFREEZE') + connectDoppelgangerUpdates(); } - wearablePairs.push(wearablePair); } - if (action === 'update') { - wearableProps.parentID = doppelganger.id; + var wearablePairs = []; - var mirrorEntity = getMirrorEntityForBaseEntity(baseEntity); - // print('MIRROR ENTITY, newPosition' + mirrorEntity + ":::" + JSON.stringify(newPosition)) - Entities.editEntity(mirrorEntity, wearableProps) - } - if (action === 'remove') { - Entities.deleteEntity(getMirrorEntityForBaseEntity(baseEntity)) - wearablePairs = wearablePairs.filter(function(obj) { - return obj.baseEntity !== baseEntity; - }); - } - if (action === 'updateBase') { - //this gets called when the mirrored entity gets grabbed. now we move the - var mirrorEntityProperties = Entities.getEntityProperties(message.mirrorEntity) - var doppelgangerToMirrorEntity = Vec3.subtract(doppelgangerProps.position, mirrorEntityProperties.position); - var newPosition = Vec3.sum(MyAvatar.position, doppelgangerToMirrorEntity); + // function getMirrorEntityForBaseEntity(baseEntity) { + // var result = wearablePairs.filter(function(obj) { + // return obj.baseEntity === baseEntity; + // }); + // if (result.length === 0) { + // return false; + // } else { + // return result[0].mirrorEntity + // } + // } - delete mirrorEntityProperties.id; - delete mirrorEntityProperties.created; - delete mirrorEntityProperties.age; - delete mirrorEntityProperties.ageAsText; - mirrorEntityProperties.position = newPosition; - mirrorEntityProperties.parentID = MyAvatar.sessionUUID; - Entities.editEntity(message.baseEntity, mirrorEntityProperties); - } - } + // function getBaseEntityForMirrorEntity(mirrorEntity) { + // var result = wearablePairs.filter(function(obj) { + // return obj.mirrorEntity === mirrorEntity; + // }); + // if (result.length === 0) { + // return false; + // } else { + // return result[0].baseEntity + // } + // } - function getMirrorEntityForBaseEntity(baseEntity) { - var result = wearablePairs.filter(function(obj) { - return obj.baseEntity === baseEntity; - }); - if (result.length === 0) { - return false; - } else { - return result[0].mirrorEntity - } - } - function getBaseEntityForMirrorEntity(mirrorEntity) { - var result = wearablePairs.filter(function(obj) { - return obj.mirrorEntity === mirrorEntity; - }); - if (result.length === 0) { - return false; - } else { - return result[0].baseEntity - } - } - var wearable; + var wearable; - function createWearable() { - var WEARABLE_POSITION = { - x: 1107.5726, - y: 460.4974, - z: -77.0471 - } - var properties = { - type: 'Model', - modelURL: 'https://s3.amazonaws.com/hifi-public/tony/cowboy-hat.fbx', - name: 'Hifi-Wearable', - dimensions: { - x: 0.25, - y: 0.25, - z: 0.25 - }, - color: { - red: 0, - green: 255, - blue: 0 - }, - position: WEARABLE_POSITION, - userData: JSON.stringify({ - "grabbableKey": { - "invertSolidWhileHeld": false + function createWearable() { + var WEARABLE_POSITION = { + x: 1107.5726, + y: 460.4974, + z: -77.0471 + } + var properties = { + type: 'Model', + modelURL: 'https://s3.amazonaws.com/hifi-public/tony/cowboy-hat.fbx', + name: 'Hifi-Wearable', + dimensions: { + x: 0.25, + y: 0.25, + z: 0.25 }, - "wearable": { - "joints": ["head", "Head", "hair", "neck"] + color: { + red: 0, + green: 255, + blue: 0 }, - handControllerKey: { - disableReleaseVelocity: true, - disableMoveWithHead: true, + position: WEARABLE_POSITION, + userData: JSON.stringify({ + "grabbableKey": { + "invertSolidWhileHeld": false + }, + "wearable": { + "joints": ["head", "Head", "hair", "neck"] + }, + handControllerKey: { + disableReleaseVelocity: true, + disableMoveWithHead: true, + } + }) + } + wearable = Entities.addEntity(properties); + } + + + function updateEntityOnAvatar(entityOnAvatar, entityOnDoppelganger) { + //this updates an entity on the avatar when you move one on the doppelganger. + + //copy doppelganger entity properties to avatar but replace its parent with our avatar id. delete some things before we do that + + var entityOnAvatarProps = Entities.getEntityProperties(entityOnDoppelganger) + + delete entityOnAvatarProps.id; + delete entityOnAvatarProps.created; + delete entityOnAvatarProps.age; + delete entityOnAvatarProps.ageAsText; + + entityOnAvatarProps.parentID = MyAvatar.id; + Entities.editEntity(entityOnAvatar, entityOnAvatarProps); + } + + function removeEntityFromAvatar(entityOnAvatar) { + Entities.deleteEntity(entityOnAvatar); + } + + function addEntityToDoppelganger(entityToAdd,bestJointIndex) { + + var entityToAddProps = Entities.getEntityProperties(entityToAdd); + + entityToAddProps.parentID = doppelgangers[0].id; + entityToAddProps.parentJointIndex =bestJointIndex; + Entities.addEntity(entityToAddProps); + + } + + function updateEntityOnDoppelganger(entityOnAvatar, entityOnDoppelganger) { + //this updates an entity on the doppelganger when you move one on the avatar. + + //copy avatar entity properties to doppelganger but replace its parent with our doppelganger id. delete some things before we do that + + var entityOnDoppelgangerProps = Entities.getEntityProperties(entityOnAvatar) + + delete entityOnDoppelgangerProps.id; + delete entityOnDoppelgangerProps.created; + delete entityOnDoppelgangerProps.age; + delete entityOnDoppelgangerProps.ageAsText; + + entityOnDoppelgangerProps.parentID = doppelgangers[0].id; + Entities.editEntity(entityOnDoppelganger, entityOnDoppelgangerProps); + + } + + function removeEntityFromDoppelganger(entityOnDoppelganger) { + Entities.deleteEntity(entityOnDoppelganger); + } + + function checkIfWearableOnDoppelganger(grabbedEntity) { + // when you drop an entity near a doppelganger... + var allowedJoints = getEntityCustomData('wearable', grabbedEntity, DEFAULT_WEARABLE_DATA).joints; + + var props = Entities.getEntityProperties(grabbedEntity, ["position", "parentID"]); + //if the thing doesn't have a parent + if (props.parentID === NULL_UUID) { + var bestJointName = ""; + var bestJointIndex = -1; + var bestJointDistance = 0; + for (var jointName in allowedJoints) { + //do this for the model + var jointIndex = Entities.getJointIndex(doppelganger.id, jointName); + var jointPosition = Entities.getJointPosition(doppelganger.id, jointIndex); + var distanceFromJoint = Vec3.distance(jointPosition, props.position); + if (distanceFromJoint < 0.4) { + if (bestJointIndex == -1 || distanceFromJoint < bestJointDistance) { + bestJointName = jointName; + bestJointIndex = jointIndex; + bestJointDistance = distanceFromJoint; + } + } } - }) + + if (bestJointIndex !== -1) { + //this was dropped close to a valid joint on the doppelganger. + addEntityToDoppelganger(grabbedEntity,bestJointIndex) + } else { + //this was dropped far from a valid joint on the doppelganger + } + } } - wearable = Entities.addEntity(properties); + + function handleReleaseMessagesFromGrabScript() { + var grabbedEntity; + checkIfWearableOnDoppelganger(grabbedEntity); + } + + function cleanup() { + + Script.update.disconnect(updateDoppelganger); + + doppelgangers.forEach(function(doppelganger) { + print('DELETING DOPPELGANGER' + doppelganger.id) + Entities.deleteEntity(doppelganger.id); + }); + + doppelgangers = []; + + //temporarily + Entities.deleteEntity(wearable); + } + } - function cleanup() { - Script.update.disconnect(updateDoppelganger); - - doppelgangers.forEach(function(doppelganger) { - print('DELETING DOPPELGANGER' + doppelganger.id) - Entities.deleteEntity(doppelganger.id); - }); - - doppelgangers = []; - - //temporarily - Entities.deleteEntity(wearable); - } return new DressingRoom(); }); \ No newline at end of file