From 91db29e92f6e445c6f6ba98fea427e15e26df056 Mon Sep 17 00:00:00 2001 From: Mike Moody Date: Fri, 2 Jun 2017 15:34:25 -0700 Subject: [PATCH 1/6] laser initial commit --- .../marketplace/laser/laser-a.svg | 107 ++++++++ .../marketplace/laser/laser.svg | 106 ++++++++ .../marketplace/laser/laserPointerApp.js | 237 ++++++++++++++++++ 3 files changed, 450 insertions(+) create mode 100644 unpublishedScripts/marketplace/laser/laser-a.svg create mode 100644 unpublishedScripts/marketplace/laser/laser.svg create mode 100644 unpublishedScripts/marketplace/laser/laserPointerApp.js diff --git a/unpublishedScripts/marketplace/laser/laser-a.svg b/unpublishedScripts/marketplace/laser/laser-a.svg new file mode 100644 index 0000000000..74033b3e7a --- /dev/null +++ b/unpublishedScripts/marketplace/laser/laser-a.svg @@ -0,0 +1,107 @@ + + + + + + + + + + image/svg+xml + + + + + + + + + + + + diff --git a/unpublishedScripts/marketplace/laser/laser.svg b/unpublishedScripts/marketplace/laser/laser.svg new file mode 100644 index 0000000000..a1f8887e90 --- /dev/null +++ b/unpublishedScripts/marketplace/laser/laser.svg @@ -0,0 +1,106 @@ + + + + + + + + + + image/svg+xml + + + + + + + + + + + + diff --git a/unpublishedScripts/marketplace/laser/laserPointerApp.js b/unpublishedScripts/marketplace/laser/laserPointerApp.js new file mode 100644 index 0000000000..e77f7fae67 --- /dev/null +++ b/unpublishedScripts/marketplace/laser/laserPointerApp.js @@ -0,0 +1,237 @@ +'use strict'; + +(function () { + Script.include("/~/system/libraries/controllers.js"); + + var APP_NAME = 'LASER', + APP_ICON = 'https://binaryrelay.com/files/public-docs/hifi/laser/laser.svg', + APP_ICON_ACTIVE = 'https://binaryrelay.com/files/public-docs/hifi/laser/laser-a.svg'; + + + var POINT_INDEX_CHANNEL = "Hifi-Point-Index", + GRAB_DISABLE_CHANNEL = "Hifi-Grab-Disable", + POINTER_DISABLE_CHANNEL = "Hifi-Pointer-Disable"; + + var tablet = Tablet.getTablet('com.highfidelity.interface.tablet.system'); + + var button = tablet.addButton({ + icon: APP_ICON, + activeIcon: APP_ICON_ACTIVE, + text: APP_NAME + }); + + var laserEntities = { + left: { + beam: null, + sphere: null + }, + right: { + beam: null, + sphere: null + } + + }; + var rayExclusionList = []; + + + function laser(hand) { + + var PICK_MAX_DISTANCE = 500; + + var isNewEntityNeeded = (laserEntities[hand].beam === null); + + var _hand = hand === 'right' ? Controller.Standard.RightHand : Controller.Standard.LeftHand; + var _joint = hand === 'right' ? 'RightHand' : 'LeftHand'; //'RightHandIndex4' : 'LeftHandIndex4' + var controllerLocation = getControllerWorldLocation(_hand, true); + + var worldHandRotation = controllerLocation.orientation; + + var pickRay = { + origin: MyAvatar.getJointPosition(_joint), + direction: Quat.getUp(worldHandRotation), + length: PICK_MAX_DISTANCE + }; + + + var ray = Entities.findRayIntersection(pickRay, true, [], rayExclusionList, true); + var avatarRay = AvatarManager.findRayIntersection(pickRay, true, [], rayExclusionList, true); + + var dist = PICK_MAX_DISTANCE; + var intersection = null; + + if (avatarRay.intersects) { + intersection = avatarRay.intersection; + dist = Vec3.distance(pickRay.origin, avatarRay.intersection); + } else if (ray.intersects) { + intersection = ray.intersection; + dist = Vec3.distance(pickRay.origin, ray.intersection); + } + + if (!ray.intersects && !avatarRay.intersects && laserEntities[hand].sphere !== null) { + Entities.editEntity(laserEntities[hand].sphere, { + visible: false + }); + } else { + Entities.editEntity(laserEntities[hand].sphere, { + visible: true + }); + } + + var sphereSize = dist * 0.01; + + if (isNewEntityNeeded) { + + var sphere = { + lifetime: 360, + type: 'Shape', + shape: 'circle3d', + dimensions: {x: sphereSize, y: sphereSize, z: sphereSize}, + color: {red: 0, green: 255, blue: 0}, + position: intersection, + collisionless: true + + }; + + var beam = { + lifetime: 360, + type: 'Line', + glow: 1.0, + lineWidth: 5, + alpha: 0.5, + ignoreRayIntersection: true, + drawInFront: true, + color: {red: 0, green: 255, blue: 0}, + parentID: MyAvatar.sessionUUID, + parentJointIndex: MyAvatar.getJointIndex(_joint), + localPosition: {x: 0, y: .2, z: 0}, + localRotation: Quat.normalize({}), + dimensions: Vec3.multiply(PICK_MAX_DISTANCE * 2, Vec3.ONE), + linePoints: [Vec3.ZERO, {x: 0, y: dist, z: 0}] + }; + + + laserEntities[hand].beam = Entities.addEntity(beam); + rayExclusionList.push(laserEntities[hand].beam); + + if (ray.intersects || avatarRay.intersects) { + laserEntities[hand].sphere = Entities.addEntity(sphere); + rayExclusionList.push(laserEntities[hand].sphere); + } + + } else { + if (ray.intersects || avatarRay.intersects) { + + Entities.editEntity(laserEntities[hand].beam, { + parentID: MyAvatar.sessionUUID, + parentJointIndex: MyAvatar.getJointIndex(_joint), + localPosition: {x: 0, y: .2, z: 0}, + localRotation: Quat.normalize({}), + dimensions: Vec3.multiply(PICK_MAX_DISTANCE * 2, Vec3.ONE), + linePoints: [Vec3.ZERO, {x: 0, y: dist, z: 0}] + }); + + Entities.editEntity(laserEntities[hand].sphere, { + dimensions: {x: sphereSize, y: sphereSize, z: sphereSize}, + position: intersection + + }); + } else { + Entities.editEntity(laserEntities[hand].beam, { + parentID: MyAvatar.sessionUUID, + parentJointIndex: MyAvatar.getJointIndex(_joint), + localPosition: {x: 0, y: .2, z: 0}, + localRotation: Quat.normalize({}), + dimensions: Vec3.multiply(PICK_MAX_DISTANCE * 2, Vec3.ONE), + linePoints: [Vec3.ZERO, {x: 0, y: dist, z: 0}] + }); + } + + } + + } + + function triggerWatcher(deltaTime) { + + var deleteBeamLeft = true, + deleteBeamRight = true; + + if (Controller.getValue(Controller.Standard.LT) > .95) { + deleteBeamLeft = false; + laser('left', deltaTime); + } + + if (Controller.getValue(Controller.Standard.RT) > .95) { + deleteBeamRight = false; + laser('right', deltaTime); + } + + if (deleteBeamLeft && laserEntities.left.beam !== null) { + Entities.deleteEntity(laserEntities.left.beam); + Entities.deleteEntity(laserEntities.left.sphere); + + laserEntities.left.beam = null; + laserEntities.left.sphere = null; + + } + if (deleteBeamRight && laserEntities.right.beam !== null) { + Entities.deleteEntity(laserEntities.right.beam); + Entities.deleteEntity(laserEntities.right.sphere); + + laserEntities.right.beam = null; + laserEntities.right.sphere = null; + + } + if (deleteBeamRight && laserEntities.right.beam !== null && deleteBeamLeft && laserEntities.left.beam !== null) { + rayExclusionList = []; + } + } + + function selectionBeamSwitch(bool) { + Messages.sendMessage(GRAB_DISABLE_CHANNEL, JSON.stringify({ + holdEnabled: bool, + nearGrabEnabled: bool, + farGrabEnabled: bool + }), true); + Messages.sendMessage(POINTER_DISABLE_CHANNEL, JSON.stringify({ + pointerEnabled: bool + }), true); + Messages.sendMessage(POINT_INDEX_CHANNEL, JSON.stringify({ + pointIndex: !bool + }), true); + } + + var _switch = true; + + function bSwitch() { + if (_switch) { + Script.update.connect(triggerWatcher); + Messages.subscribe(POINT_INDEX_CHANNEL); + Messages.subscribe(GRAB_DISABLE_CHANNEL); + Messages.subscribe(POINTER_DISABLE_CHANNEL); + } else { + Script.update.disconnect(triggerWatcher); + Messages.unsubscribe(POINT_INDEX_CHANNEL); + Messages.unsubscribe(GRAB_DISABLE_CHANNEL); + Messages.unsubscribe(POINTER_DISABLE_CHANNEL); + } + button.editProperties({isActive: _switch}); + + selectionBeamSwitch(!_switch); + + _switch = !_switch; + } + + button.clicked.connect(bSwitch); + + function clean() { + tablet.removeButton(button); + Script.update.disconnect(triggerWatcher); + + Messages.unsubscribe(POINT_INDEX_CHANNEL); + Messages.unsubscribe(GRAB_DISABLE_CHANNEL); + Messages.unsubscribe(POINTER_DISABLE_CHANNEL); + rayExclusionList = []; + } + + Script.scriptEnding.connect(clean); +}()); \ No newline at end of file From fb869034f2ce8a39e68c6495d4ece469f34346d2 Mon Sep 17 00:00:00 2001 From: Mike Moody Date: Sun, 4 Jun 2017 21:05:30 -0700 Subject: [PATCH 2/6] fixing magic numbers changing to local entity making var names more friendly --- .../marketplace/laser/laserPointerApp.js | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/unpublishedScripts/marketplace/laser/laserPointerApp.js b/unpublishedScripts/marketplace/laser/laserPointerApp.js index e77f7fae67..1180278103 100644 --- a/unpublishedScripts/marketplace/laser/laserPointerApp.js +++ b/unpublishedScripts/marketplace/laser/laserPointerApp.js @@ -7,11 +7,12 @@ APP_ICON = 'https://binaryrelay.com/files/public-docs/hifi/laser/laser.svg', APP_ICON_ACTIVE = 'https://binaryrelay.com/files/public-docs/hifi/laser/laser-a.svg'; - var POINT_INDEX_CHANNEL = "Hifi-Point-Index", GRAB_DISABLE_CHANNEL = "Hifi-Grab-Disable", POINTER_DISABLE_CHANNEL = "Hifi-Pointer-Disable"; + var TRIGGER_PRESSURE = 0.95; + var tablet = Tablet.getTablet('com.highfidelity.interface.tablet.system'); var button = tablet.addButton({ @@ -29,8 +30,8 @@ beam: null, sphere: null } - }; + var rayExclusionList = []; @@ -40,14 +41,14 @@ var isNewEntityNeeded = (laserEntities[hand].beam === null); - var _hand = hand === 'right' ? Controller.Standard.RightHand : Controller.Standard.LeftHand; - var _joint = hand === 'right' ? 'RightHand' : 'LeftHand'; //'RightHandIndex4' : 'LeftHandIndex4' - var controllerLocation = getControllerWorldLocation(_hand, true); + var currentHand = hand === 'right' ? Controller.Standard.RightHand : Controller.Standard.LeftHand; + var jointName = hand === 'right' ? 'RightHand' : 'LeftHand'; //'RightHandIndex4' : 'LeftHandIndex4' + var controllerLocation = getControllerWorldLocation(currentHand, true); var worldHandRotation = controllerLocation.orientation; var pickRay = { - origin: MyAvatar.getJointPosition(_joint), + origin: MyAvatar.getJointPosition(jointName), direction: Quat.getUp(worldHandRotation), length: PICK_MAX_DISTANCE }; @@ -83,8 +84,7 @@ var sphere = { lifetime: 360, - type: 'Shape', - shape: 'circle3d', + type: 'Sphere', dimensions: {x: sphereSize, y: sphereSize, z: sphereSize}, color: {red: 0, green: 255, blue: 0}, position: intersection, @@ -102,7 +102,7 @@ drawInFront: true, color: {red: 0, green: 255, blue: 0}, parentID: MyAvatar.sessionUUID, - parentJointIndex: MyAvatar.getJointIndex(_joint), + parentJointIndex: MyAvatar.getJointIndex(jointName), localPosition: {x: 0, y: .2, z: 0}, localRotation: Quat.normalize({}), dimensions: Vec3.multiply(PICK_MAX_DISTANCE * 2, Vec3.ONE), @@ -110,11 +110,11 @@ }; - laserEntities[hand].beam = Entities.addEntity(beam); + laserEntities[hand].beam = Entities.addEntity(beam,true); rayExclusionList.push(laserEntities[hand].beam); if (ray.intersects || avatarRay.intersects) { - laserEntities[hand].sphere = Entities.addEntity(sphere); + laserEntities[hand].sphere = Entities.addEntity(sphere,true); rayExclusionList.push(laserEntities[hand].sphere); } @@ -123,7 +123,7 @@ Entities.editEntity(laserEntities[hand].beam, { parentID: MyAvatar.sessionUUID, - parentJointIndex: MyAvatar.getJointIndex(_joint), + parentJointIndex: MyAvatar.getJointIndex(jointName), localPosition: {x: 0, y: .2, z: 0}, localRotation: Quat.normalize({}), dimensions: Vec3.multiply(PICK_MAX_DISTANCE * 2, Vec3.ONE), @@ -138,7 +138,7 @@ } else { Entities.editEntity(laserEntities[hand].beam, { parentID: MyAvatar.sessionUUID, - parentJointIndex: MyAvatar.getJointIndex(_joint), + parentJointIndex: MyAvatar.getJointIndex(jointName), localPosition: {x: 0, y: .2, z: 0}, localRotation: Quat.normalize({}), dimensions: Vec3.multiply(PICK_MAX_DISTANCE * 2, Vec3.ONE), @@ -155,14 +155,14 @@ var deleteBeamLeft = true, deleteBeamRight = true; - if (Controller.getValue(Controller.Standard.LT) > .95) { + if (Controller.getValue(Controller.Standard.LT) > TRIGGER_PRESSURE) { deleteBeamLeft = false; - laser('left', deltaTime); + laser('left'); } - if (Controller.getValue(Controller.Standard.RT) > .95) { + if (Controller.getValue(Controller.Standard.RT) > TRIGGER_PRESSURE) { deleteBeamRight = false; - laser('right', deltaTime); + laser('right'); } if (deleteBeamLeft && laserEntities.left.beam !== null) { @@ -181,7 +181,7 @@ laserEntities.right.sphere = null; } - if (deleteBeamRight && laserEntities.right.beam !== null && deleteBeamLeft && laserEntities.left.beam !== null) { + if (deleteBeamRight && deleteBeamLeft) { rayExclusionList = []; } } @@ -202,7 +202,7 @@ var _switch = true; - function bSwitch() { + function buttonSwitch() { if (_switch) { Script.update.connect(triggerWatcher); Messages.subscribe(POINT_INDEX_CHANNEL); @@ -221,7 +221,7 @@ _switch = !_switch; } - button.clicked.connect(bSwitch); + button.clicked.connect(buttonSwitch); function clean() { tablet.removeButton(button); From 9f669748f43bae51201f21faa08b83f89dc578c2 Mon Sep 17 00:00:00 2001 From: Mike Moody Date: Sun, 4 Jun 2017 21:34:56 -0700 Subject: [PATCH 3/6] header and cleanup. --- unpublishedScripts/marketplace/laser/laserPointerApp.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/unpublishedScripts/marketplace/laser/laserPointerApp.js b/unpublishedScripts/marketplace/laser/laserPointerApp.js index 1180278103..7c8e606658 100644 --- a/unpublishedScripts/marketplace/laser/laserPointerApp.js +++ b/unpublishedScripts/marketplace/laser/laserPointerApp.js @@ -1,3 +1,7 @@ +// +// Created by Alan-Michael Moody on 6/4/2017 +// + 'use strict'; (function () { @@ -34,7 +38,6 @@ var rayExclusionList = []; - function laser(hand) { var PICK_MAX_DISTANCE = 500; @@ -53,7 +56,6 @@ length: PICK_MAX_DISTANCE }; - var ray = Entities.findRayIntersection(pickRay, true, [], rayExclusionList, true); var avatarRay = AvatarManager.findRayIntersection(pickRay, true, [], rayExclusionList, true); @@ -109,7 +111,6 @@ linePoints: [Vec3.ZERO, {x: 0, y: dist, z: 0}] }; - laserEntities[hand].beam = Entities.addEntity(beam,true); rayExclusionList.push(laserEntities[hand].beam); From 6e34971889ca52c72766101028fd664d571731a9 Mon Sep 17 00:00:00 2001 From: Mike Moody Date: Wed, 21 Jun 2017 20:43:16 -0700 Subject: [PATCH 4/6] Changed to use with Index finger and minor cleanup. --- .../marketplace/laser/laserPointerApp.js | 42 ++++++++----------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/unpublishedScripts/marketplace/laser/laserPointerApp.js b/unpublishedScripts/marketplace/laser/laserPointerApp.js index 7c8e606658..e048d4126a 100644 --- a/unpublishedScripts/marketplace/laser/laserPointerApp.js +++ b/unpublishedScripts/marketplace/laser/laserPointerApp.js @@ -44,15 +44,11 @@ var isNewEntityNeeded = (laserEntities[hand].beam === null); - var currentHand = hand === 'right' ? Controller.Standard.RightHand : Controller.Standard.LeftHand; - var jointName = hand === 'right' ? 'RightHand' : 'LeftHand'; //'RightHandIndex4' : 'LeftHandIndex4' - var controllerLocation = getControllerWorldLocation(currentHand, true); - - var worldHandRotation = controllerLocation.orientation; + var jointName = hand === 'right' ? 'RightHandIndex4' : 'LeftHandIndex4'; //'RightHand' : 'LeftHand'; var pickRay = { origin: MyAvatar.getJointPosition(jointName), - direction: Quat.getUp(worldHandRotation), + direction: MyAvatar.jointToWorldDirection(Vec3.UP, MyAvatar.getJointIndex(jointName)), length: PICK_MAX_DISTANCE }; @@ -70,16 +66,6 @@ dist = Vec3.distance(pickRay.origin, ray.intersection); } - if (!ray.intersects && !avatarRay.intersects && laserEntities[hand].sphere !== null) { - Entities.editEntity(laserEntities[hand].sphere, { - visible: false - }); - } else { - Entities.editEntity(laserEntities[hand].sphere, { - visible: true - }); - } - var sphereSize = dist * 0.01; if (isNewEntityNeeded) { @@ -90,8 +76,8 @@ dimensions: {x: sphereSize, y: sphereSize, z: sphereSize}, color: {red: 0, green: 255, blue: 0}, position: intersection, - collisionless: true - + collisionless: true, + visible: false }; var beam = { @@ -105,7 +91,7 @@ color: {red: 0, green: 255, blue: 0}, parentID: MyAvatar.sessionUUID, parentJointIndex: MyAvatar.getJointIndex(jointName), - localPosition: {x: 0, y: .2, z: 0}, + localPosition: {x: 0, y: .05, z: 0}, localRotation: Quat.normalize({}), dimensions: Vec3.multiply(PICK_MAX_DISTANCE * 2, Vec3.ONE), linePoints: [Vec3.ZERO, {x: 0, y: dist, z: 0}] @@ -114,9 +100,13 @@ laserEntities[hand].beam = Entities.addEntity(beam,true); rayExclusionList.push(laserEntities[hand].beam); + laserEntities[hand].sphere = Entities.addEntity(sphere,true); + rayExclusionList.push(laserEntities[hand].sphere); + if (ray.intersects || avatarRay.intersects) { - laserEntities[hand].sphere = Entities.addEntity(sphere,true); - rayExclusionList.push(laserEntities[hand].sphere); + Entities.editEntity(laserEntities[hand].sphere, { + visible: true + }); } } else { @@ -125,7 +115,7 @@ Entities.editEntity(laserEntities[hand].beam, { parentID: MyAvatar.sessionUUID, parentJointIndex: MyAvatar.getJointIndex(jointName), - localPosition: {x: 0, y: .2, z: 0}, + localPosition: {x: 0, y: .05, z: 0}, localRotation: Quat.normalize({}), dimensions: Vec3.multiply(PICK_MAX_DISTANCE * 2, Vec3.ONE), linePoints: [Vec3.ZERO, {x: 0, y: dist, z: 0}] @@ -133,18 +123,22 @@ Entities.editEntity(laserEntities[hand].sphere, { dimensions: {x: sphereSize, y: sphereSize, z: sphereSize}, - position: intersection + position: intersection, + visible: true }); } else { Entities.editEntity(laserEntities[hand].beam, { parentID: MyAvatar.sessionUUID, parentJointIndex: MyAvatar.getJointIndex(jointName), - localPosition: {x: 0, y: .2, z: 0}, + localPosition: {x: 0, y: .05, z: 0}, localRotation: Quat.normalize({}), dimensions: Vec3.multiply(PICK_MAX_DISTANCE * 2, Vec3.ONE), linePoints: [Vec3.ZERO, {x: 0, y: dist, z: 0}] }); + Entities.editEntity(laserEntities[hand].sphere, { + visible: false + }); } } From ae960b840af0056605d71cfcd10cfa5b490e5c2d Mon Sep 17 00:00:00 2001 From: Mike Moody Date: Wed, 21 Jun 2017 21:24:34 -0700 Subject: [PATCH 5/6] accounted for the forward offset --- .../marketplace/laser/laserPointerApp.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/unpublishedScripts/marketplace/laser/laserPointerApp.js b/unpublishedScripts/marketplace/laser/laserPointerApp.js index e048d4126a..282fe4dc8d 100644 --- a/unpublishedScripts/marketplace/laser/laserPointerApp.js +++ b/unpublishedScripts/marketplace/laser/laserPointerApp.js @@ -41,6 +41,7 @@ function laser(hand) { var PICK_MAX_DISTANCE = 500; + var FORWARD_OFFSET = 0.05; var isNewEntityNeeded = (laserEntities[hand].beam === null); @@ -91,7 +92,7 @@ color: {red: 0, green: 255, blue: 0}, parentID: MyAvatar.sessionUUID, parentJointIndex: MyAvatar.getJointIndex(jointName), - localPosition: {x: 0, y: .05, z: 0}, + localPosition: {x: 0, y: FORWARD_OFFSET, z: 0}, localRotation: Quat.normalize({}), dimensions: Vec3.multiply(PICK_MAX_DISTANCE * 2, Vec3.ONE), linePoints: [Vec3.ZERO, {x: 0, y: dist, z: 0}] @@ -115,26 +116,25 @@ Entities.editEntity(laserEntities[hand].beam, { parentID: MyAvatar.sessionUUID, parentJointIndex: MyAvatar.getJointIndex(jointName), - localPosition: {x: 0, y: .05, z: 0}, + localPosition: {x: 0, y: FORWARD_OFFSET, z: 0}, localRotation: Quat.normalize({}), dimensions: Vec3.multiply(PICK_MAX_DISTANCE * 2, Vec3.ONE), - linePoints: [Vec3.ZERO, {x: 0, y: dist, z: 0}] + linePoints: [Vec3.ZERO, {x: 0, y: dist - FORWARD_OFFSET, z: 0}] }); Entities.editEntity(laserEntities[hand].sphere, { dimensions: {x: sphereSize, y: sphereSize, z: sphereSize}, position: intersection, visible: true - }); } else { Entities.editEntity(laserEntities[hand].beam, { parentID: MyAvatar.sessionUUID, parentJointIndex: MyAvatar.getJointIndex(jointName), - localPosition: {x: 0, y: .05, z: 0}, + localPosition: {x: 0, y: FORWARD_OFFSET, z: 0}, localRotation: Quat.normalize({}), dimensions: Vec3.multiply(PICK_MAX_DISTANCE * 2, Vec3.ONE), - linePoints: [Vec3.ZERO, {x: 0, y: dist, z: 0}] + linePoints: [Vec3.ZERO, {x: 0, y: dist - FORWARD_OFFSET, z: 0}] }); Entities.editEntity(laserEntities[hand].sphere, { visible: false From 4b593d1ab43edf3725af8d68f77f257652f4a5ac Mon Sep 17 00:00:00 2001 From: Mike Moody Date: Sat, 8 Jul 2017 15:18:27 -0700 Subject: [PATCH 6/6] Added fallback method using controllers if joints do not exist. --- .../marketplace/laser/laserPointerApp.js | 92 ++++++++++++++----- 1 file changed, 67 insertions(+), 25 deletions(-) diff --git a/unpublishedScripts/marketplace/laser/laserPointerApp.js b/unpublishedScripts/marketplace/laser/laserPointerApp.js index 282fe4dc8d..515a2c3a76 100644 --- a/unpublishedScripts/marketplace/laser/laserPointerApp.js +++ b/unpublishedScripts/marketplace/laser/laserPointerApp.js @@ -8,8 +8,8 @@ Script.include("/~/system/libraries/controllers.js"); var APP_NAME = 'LASER', - APP_ICON = 'https://binaryrelay.com/files/public-docs/hifi/laser/laser.svg', - APP_ICON_ACTIVE = 'https://binaryrelay.com/files/public-docs/hifi/laser/laser-a.svg'; + APP_ICON = Script.resolvePath('laser.svg'), + APP_ICON_ACTIVE = Script.resolvePath('laser-a.svg'); var POINT_INDEX_CHANNEL = "Hifi-Point-Index", GRAB_DISABLE_CHANNEL = "Hifi-Grab-Disable", @@ -47,12 +47,26 @@ var jointName = hand === 'right' ? 'RightHandIndex4' : 'LeftHandIndex4'; //'RightHand' : 'LeftHand'; + var _hand = hand === 'right' ? Controller.Standard.RightHand : Controller.Standard.LeftHand; + var controllerLocation = getControllerWorldLocation(_hand, true); + + var worldControllerPosition = controllerLocation.position; + var worldControllerRotation = controllerLocation.orientation; + + var jointExists = (MyAvatar.getJointIndex(jointName) > 0) ; + var CONTROLLER_FORWARD_OFFSET = Vec3.multiply(Quat.getUp(worldControllerRotation), FORWARD_OFFSET); + var pickRay = { - origin: MyAvatar.getJointPosition(jointName), - direction: MyAvatar.jointToWorldDirection(Vec3.UP, MyAvatar.getJointIndex(jointName)), + origin: worldControllerPosition, + direction: Quat.getUp(worldControllerRotation), length: PICK_MAX_DISTANCE }; + if (jointExists) { + pickRay.origin = MyAvatar.getJointPosition(jointName); + pickRay.direction = MyAvatar.jointToWorldDirection(Vec3.UP, MyAvatar.getJointIndex(jointName)); + } + var ray = Entities.findRayIntersection(pickRay, true, [], rayExclusionList, true); var avatarRay = AvatarManager.findRayIntersection(pickRay, true, [], rayExclusionList, true); @@ -91,13 +105,19 @@ drawInFront: true, color: {red: 0, green: 255, blue: 0}, parentID: MyAvatar.sessionUUID, - parentJointIndex: MyAvatar.getJointIndex(jointName), - localPosition: {x: 0, y: FORWARD_OFFSET, z: 0}, - localRotation: Quat.normalize({}), dimensions: Vec3.multiply(PICK_MAX_DISTANCE * 2, Vec3.ONE), - linePoints: [Vec3.ZERO, {x: 0, y: dist, z: 0}] + linePoints: [Vec3.ZERO, {x: 0, y: dist - FORWARD_OFFSET, z: 0}] }; + if(jointExists) { + beam.parentJointIndex = MyAvatar.getJointIndex(jointName); + beam.localPosition = {x: 0, y: FORWARD_OFFSET, z: 0}; + beam.localRotation = Quat.normalize({}); + } else { + beam.position = Vec3.sum(pickRay.origin, CONTROLLER_FORWARD_OFFSET); + beam.rotation = worldControllerRotation; + } + laserEntities[hand].beam = Entities.addEntity(beam,true); rayExclusionList.push(laserEntities[hand].beam); @@ -112,15 +132,25 @@ } else { if (ray.intersects || avatarRay.intersects) { - - Entities.editEntity(laserEntities[hand].beam, { - parentID: MyAvatar.sessionUUID, - parentJointIndex: MyAvatar.getJointIndex(jointName), - localPosition: {x: 0, y: FORWARD_OFFSET, z: 0}, - localRotation: Quat.normalize({}), - dimensions: Vec3.multiply(PICK_MAX_DISTANCE * 2, Vec3.ONE), - linePoints: [Vec3.ZERO, {x: 0, y: dist - FORWARD_OFFSET, z: 0}] - }); + if(jointExists) { + Entities.editEntity(laserEntities[hand].beam, { + parentID: MyAvatar.sessionUUID, + parentJointIndex: MyAvatar.getJointIndex(jointName), + localPosition: {x: 0, y: FORWARD_OFFSET, z: 0}, + localRotation: Quat.normalize({}), + dimensions: Vec3.multiply(PICK_MAX_DISTANCE * 2, Vec3.ONE), + linePoints: [Vec3.ZERO, {x: 0, y: dist - FORWARD_OFFSET, z: 0}] + }); + } else { + Entities.editEntity(laserEntities[hand].beam, { + parentID: MyAvatar.sessionUUID, + parentJointIndex: MyAvatar.getJointIndex(jointName), + position: Vec3.sum(pickRay.origin, CONTROLLER_FORWARD_OFFSET), + rotation: worldControllerRotation, + dimensions: Vec3.multiply(PICK_MAX_DISTANCE * 2, Vec3.ONE), + linePoints: [Vec3.ZERO, {x: 0, y: dist - FORWARD_OFFSET, z: 0}] + }); + } Entities.editEntity(laserEntities[hand].sphere, { dimensions: {x: sphereSize, y: sphereSize, z: sphereSize}, @@ -128,14 +158,26 @@ visible: true }); } else { - Entities.editEntity(laserEntities[hand].beam, { - parentID: MyAvatar.sessionUUID, - parentJointIndex: MyAvatar.getJointIndex(jointName), - localPosition: {x: 0, y: FORWARD_OFFSET, z: 0}, - localRotation: Quat.normalize({}), - dimensions: Vec3.multiply(PICK_MAX_DISTANCE * 2, Vec3.ONE), - linePoints: [Vec3.ZERO, {x: 0, y: dist - FORWARD_OFFSET, z: 0}] - }); + if(jointExists) { + Entities.editEntity(laserEntities[hand].beam, { + parentID: MyAvatar.sessionUUID, + parentJointIndex: MyAvatar.getJointIndex(jointName), + localPosition: {x: 0, y: FORWARD_OFFSET, z: 0}, + localRotation: Quat.normalize({}), + dimensions: Vec3.multiply(PICK_MAX_DISTANCE * 2, Vec3.ONE), + linePoints: [Vec3.ZERO, {x: 0, y: dist - FORWARD_OFFSET, z: 0}] + }); + } else { + Entities.editEntity(laserEntities[hand].beam, { + parentID: MyAvatar.sessionUUID, + parentJointIndex: MyAvatar.getJointIndex(jointName), + position: Vec3.sum(pickRay.origin, CONTROLLER_FORWARD_OFFSET), + rotation: worldControllerRotation, + dimensions: Vec3.multiply(PICK_MAX_DISTANCE * 2, Vec3.ONE), + linePoints: [Vec3.ZERO, {x: 0, y: dist - FORWARD_OFFSET, z: 0}] + }); + } + Entities.editEntity(laserEntities[hand].sphere, { visible: false });