From cd1beceb7582583f4c8f5cece2e5948be21a999e Mon Sep 17 00:00:00 2001 From: "Anthony J. Thibault" Date: Tue, 20 Jun 2017 17:02:50 -0700 Subject: [PATCH 1/7] Added script that will attach an entity to a vive sensor. --- scripts/developer/tests/puck-attach.js | 131 +++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 scripts/developer/tests/puck-attach.js diff --git a/scripts/developer/tests/puck-attach.js b/scripts/developer/tests/puck-attach.js new file mode 100644 index 0000000000..ff86d31809 --- /dev/null +++ b/scripts/developer/tests/puck-attach.js @@ -0,0 +1,131 @@ +// +// debugatar.js +// +/* eslint indent: ["error", 4, { "outerIIFEBody": 0 }] */ +/* global Xform */ +Script.include("/~/system/libraries/Xform.js"); + +(function() { // BEGIN LOCAL_SCOPE + +var TABLET_BUTTON_NAME = "PUCKATTACH"; +var HTML_URL = "https://s3.amazonaws.com/hifi-public/tony/html/puck-attach.html"; + +var tablet = Tablet.getTablet("com.highfidelity.interface.tablet.system"); +var tabletButton = tablet.addButton({ + text: TABLET_BUTTON_NAME, + icon: "https://s3.amazonaws.com/hifi-public/tony/icons/tpose-i.svg", + activeIcon: "https://s3.amazonaws.com/hifi-public/tony/icons/tpose-a.svg" +}); + +tabletButton.clicked.connect(function () { + if (shown) { + tablet.gotoHomeScreen(); + } else { + tablet.gotoWebScreen(HTML_URL); + } +}); + +var shown = false; +var attachedEntity; +var attachedObj; + +function onScreenChanged(type, url) { + if (type === "Web" && url === HTML_URL) { + tabletButton.editProperties({isActive: true}); + if (!shown) { + // hook up to event bridge + tablet.webEventReceived.connect(onWebEventReceived); + } + shown = true; + } else { + tabletButton.editProperties({isActive: false}); + if (shown) { + // disconnect from event bridge + tablet.webEventReceived.disconnect(onWebEventReceived); + } + shown = false; + } +} + +tablet.screenChanged.connect(onScreenChanged); + +function attach(obj) { + attachedEntity = Entities.addEntity({ + type: "Model", + name: "puck-attach-entity", + modelURL: obj.modelurl + }); + attachedObj = obj; + var localPos = {x: Number(obj.posx), y: Number(obj.posy), z: Number(obj.posz)}; + var localRot = Quat.fromVec3Degrees({x: Number(obj.rotx), y: Number(obj.roty), z: Number(obj.rotz)}); + attachedObj.localXform = new Xform(localRot, localPos); + var key = "TrackedObject" + pad(attachedObj.puckno, 2); + attachedObj.key = key; + + print("AJT: attachedObj = " + JSON.stringify(attachedObj)); + + Script.update.connect(update); + update(0.001); +} + +function remove() { + if (attachedEntity) { + Script.update.disconnect(update); + Entities.deleteEntity(attachedEntity); + attachedEntity = undefined; + } + attachedObj = undefined; +} + +function pad(num, size) { + var s = "000000000" + num; + return s.substr(s.length-size); +} + +function update(dt) { + if (attachedEntity && attachedObj && Controller.Hardware.Vive) { + var pose = Controller.getPoseValue(Controller.Hardware.Vive[attachedObj.key]); + var avatarXform = new Xform(MyAvatar.orientation, MyAvatar.position); + var puckXform = new Xform(pose.rotation, pose.translation); + var finalXform = Xform.mul(avatarXform, Xform.mul(puckXform, attachedObj.localXform)); + if (pose && pose.valid) { + Entities.editEntity(attachedEntity, { + position: finalXform.pos, + rotation: finalXform.rot + }); + } else { + if (pose) { + print("AJT: WARNING: invalid pose for " + attachedObj.key); + } else { + print("AJT: WARNING: could not find key " + attachedObj.key); + } + } + } +} + +function onWebEventReceived(msg) { + var obj = {}; + try { + obj = JSON.parse(msg); + } catch (err) { + return; + } + if (obj.cmd === "attach") { + remove(); + attach(obj); + } else if (obj.cmd === "detach") { + remove(); + } +} + +Script.scriptEnding.connect(function () { + remove(); + tablet.removeButton(tabletButton); + if (shown) { + tablet.webEventReceived.disconnect(onWebEventReceived); + tablet.gotoHomeScreen(); + } + tablet.screenChanged.disconnect(onScreenChanged); +}); + +}()); // END LOCAL_SCOPE From 287dc3e1b83bd68ba6b85d09d5fc084d84a43c2b Mon Sep 17 00:00:00 2001 From: "Anthony J. Thibault" Date: Tue, 20 Jun 2017 17:10:53 -0700 Subject: [PATCH 2/7] updated license and button name --- scripts/developer/tests/puck-attach.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/scripts/developer/tests/puck-attach.js b/scripts/developer/tests/puck-attach.js index ff86d31809..7ed38eb5d0 100644 --- a/scripts/developer/tests/puck-attach.js +++ b/scripts/developer/tests/puck-attach.js @@ -1,13 +1,18 @@ // -// debugatar.js +// Created by Anthony J. Thibault on 2017/06/20 +// Copyright 2017 High Fidelity, Inc. // +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + /* eslint indent: ["error", 4, { "outerIIFEBody": 0 }] */ /* global Xform */ Script.include("/~/system/libraries/Xform.js"); (function() { // BEGIN LOCAL_SCOPE -var TABLET_BUTTON_NAME = "PUCKATTACH"; +var TABLET_BUTTON_NAME = "PUCKTACH"; var HTML_URL = "https://s3.amazonaws.com/hifi-public/tony/html/puck-attach.html"; var tablet = Tablet.getTablet("com.highfidelity.interface.tablet.system"); From e84d0358cc9d55aef5f35a161be68698b8dd9351 Mon Sep 17 00:00:00 2001 From: "Anthony J. Thibault" Date: Tue, 20 Jun 2017 17:18:35 -0700 Subject: [PATCH 3/7] updated images --- scripts/developer/tests/puck-attach.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/developer/tests/puck-attach.js b/scripts/developer/tests/puck-attach.js index 7ed38eb5d0..69d6c9e39b 100644 --- a/scripts/developer/tests/puck-attach.js +++ b/scripts/developer/tests/puck-attach.js @@ -18,8 +18,8 @@ var HTML_URL = "https://s3.amazonaws.com/hifi-public/tony/html/puck-attach.html" var tablet = Tablet.getTablet("com.highfidelity.interface.tablet.system"); var tabletButton = tablet.addButton({ text: TABLET_BUTTON_NAME, - icon: "https://s3.amazonaws.com/hifi-public/tony/icons/tpose-i.svg", - activeIcon: "https://s3.amazonaws.com/hifi-public/tony/icons/tpose-a.svg" + icon: "https://s3.amazonaws.com/hifi-public/tony/icons/puck-i.svg", + activeIcon: "https://s3.amazonaws.com/hifi-public/tony/icons/puck-a.svg" }); tabletButton.clicked.connect(function () { From b78e2781020c44737c3e406de83b5675361433e9 Mon Sep 17 00:00:00 2001 From: "Anthony J. Thibault" Date: Tue, 20 Jun 2017 17:19:57 -0700 Subject: [PATCH 4/7] adjusted print statements --- scripts/developer/tests/puck-attach.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/developer/tests/puck-attach.js b/scripts/developer/tests/puck-attach.js index 69d6c9e39b..807977794d 100644 --- a/scripts/developer/tests/puck-attach.js +++ b/scripts/developer/tests/puck-attach.js @@ -67,7 +67,7 @@ function attach(obj) { var key = "TrackedObject" + pad(attachedObj.puckno, 2); attachedObj.key = key; - print("AJT: attachedObj = " + JSON.stringify(attachedObj)); + print("PUCK-ATTACH: attachedObj = " + JSON.stringify(attachedObj)); Script.update.connect(update); update(0.001); @@ -100,9 +100,9 @@ function update(dt) { }); } else { if (pose) { - print("AJT: WARNING: invalid pose for " + attachedObj.key); + print("PUCK-ATTACH: WARNING: invalid pose for " + attachedObj.key); } else { - print("AJT: WARNING: could not find key " + attachedObj.key); + print("PUCK-ATTACH: WARNING: could not find key " + attachedObj.key); } } } From 3184d4e9d13ae105e531fffff9a4e61959cb48be Mon Sep 17 00:00:00 2001 From: "Anthony J. Thibault" Date: Thu, 22 Jun 2017 12:17:22 -0700 Subject: [PATCH 5/7] Puck attach only shows available tracked objects. --- scripts/developer/tests/puck-attach.js | 35 +++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/scripts/developer/tests/puck-attach.js b/scripts/developer/tests/puck-attach.js index 807977794d..7fe13e6ba1 100644 --- a/scripts/developer/tests/puck-attach.js +++ b/scripts/developer/tests/puck-attach.js @@ -13,7 +13,8 @@ Script.include("/~/system/libraries/Xform.js"); (function() { // BEGIN LOCAL_SCOPE var TABLET_BUTTON_NAME = "PUCKTACH"; -var HTML_URL = "https://s3.amazonaws.com/hifi-public/tony/html/puck-attach.html"; +// var HTML_URL = "https://s3.amazonaws.com/hifi-public/tony/html/puck-attach.html"; +var HTML_URL = "file:///C:/msys64/home/anthony/code/hifi/examples/html/puck-attach.html"; var tablet = Tablet.getTablet("com.highfidelity.interface.tablet.system"); var tabletButton = tablet.addButton({ @@ -40,6 +41,14 @@ function onScreenChanged(type, url) { if (!shown) { // hook up to event bridge tablet.webEventReceived.connect(onWebEventReceived); + + Script.setTimeout(function () { + // send available tracked objects to the html running in the tablet. + var availableTrackedObjects = getAvailableTrackedObjects(); + tablet.emitScriptEvent(JSON.stringify(availableTrackedObjects)); + + print("PUCK-ATTACH: availableTrackedObjects = " + JSON.stringify(availableTrackedObjects)); + }, 1000); // wait 1 sec before sending.. } shown = true; } else { @@ -54,6 +63,24 @@ function onScreenChanged(type, url) { tablet.screenChanged.connect(onScreenChanged); +function indexToTrackedObjectName(index) { + return "TrackedObject" + pad(index, 2); +} + +function getAvailableTrackedObjects() { + var available = []; + var NUM_TRACKED_OBJECTS = 16; + var i; + for (i = 0; i < NUM_TRACKED_OBJECTS; i++) { + var key = indexToTrackedObjectName(i); + var pose = Controller.getPoseValue(Controller.Hardware.Vive[key]); + if (pose && pose.valid) { + available.push(i); + } + } + return available; +} + function attach(obj) { attachedEntity = Entities.addEntity({ type: "Model", @@ -64,7 +91,7 @@ function attach(obj) { var localPos = {x: Number(obj.posx), y: Number(obj.posy), z: Number(obj.posz)}; var localRot = Quat.fromVec3Degrees({x: Number(obj.rotx), y: Number(obj.roty), z: Number(obj.rotz)}); attachedObj.localXform = new Xform(localRot, localPos); - var key = "TrackedObject" + pad(attachedObj.puckno, 2); + var key = indexToTrackedObjectName(Number(attachedObj.puckno)); attachedObj.key = key; print("PUCK-ATTACH: attachedObj = " + JSON.stringify(attachedObj)); @@ -83,8 +110,8 @@ function remove() { } function pad(num, size) { - var s = "000000000" + num; - return s.substr(s.length-size); + var tempString = "000000000" + num; + return tempString.substr(tempString.length - size); } function update(dt) { From e531468ac7dacbe5b99e5292141a8f0158f13e8a Mon Sep 17 00:00:00 2001 From: "Anthony J. Thibault" Date: Thu, 22 Jun 2017 13:14:47 -0700 Subject: [PATCH 6/7] Fix for html url --- scripts/developer/tests/puck-attach.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/developer/tests/puck-attach.js b/scripts/developer/tests/puck-attach.js index 7fe13e6ba1..00d2ca5fa3 100644 --- a/scripts/developer/tests/puck-attach.js +++ b/scripts/developer/tests/puck-attach.js @@ -13,8 +13,7 @@ Script.include("/~/system/libraries/Xform.js"); (function() { // BEGIN LOCAL_SCOPE var TABLET_BUTTON_NAME = "PUCKTACH"; -// var HTML_URL = "https://s3.amazonaws.com/hifi-public/tony/html/puck-attach.html"; -var HTML_URL = "file:///C:/msys64/home/anthony/code/hifi/examples/html/puck-attach.html"; +var HTML_URL = "https://s3.amazonaws.com/hifi-public/tony/html/puck-attach.html"; var tablet = Tablet.getTablet("com.highfidelity.interface.tablet.system"); var tabletButton = tablet.addButton({ From aabbcfd23dafecd99beb85a6c3ab3cf7a7e6b3dc Mon Sep 17 00:00:00 2001 From: "Anthony J. Thibault" Date: Fri, 23 Jun 2017 16:47:53 -0700 Subject: [PATCH 7/7] code review feedback --- scripts/developer/tests/puck-attach.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/scripts/developer/tests/puck-attach.js b/scripts/developer/tests/puck-attach.js index 00d2ca5fa3..2d0a2e6d02 100644 --- a/scripts/developer/tests/puck-attach.js +++ b/scripts/developer/tests/puck-attach.js @@ -5,6 +5,18 @@ // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +// When this script is running, a new app button, named "PUCKTACH", will be added to the toolbar/tablet. +// Click this app to bring up the puck attachment panel. This panel contains the following fields. +// +// * Tracked Object - A drop down list of all the available pucks found. If no pucks are found this list will only have a single NONE entry. +// Closing and re-opening the app will refresh this list. +// * Model URL - A model url of the model you wish to be attached to the specified puck. +// * Position X, Y, Z - used to apply an offset between the puck and the attached model. +// * Rot X, Y, Z - used to apply euler angle offsets, in degrees, between the puck and the attached model. +// * Create Attachment - when this button is pressed a new Entity will be created at the specified puck's location. +// If a puck atttachment already exists, it will be destroyed before the new entity is created. +// * Destroy Attachmetn - destroies the entity attached to the puck. +// /* eslint indent: ["error", 4, { "outerIIFEBody": 0 }] */ /* global Xform */ @@ -46,7 +58,7 @@ function onScreenChanged(type, url) { var availableTrackedObjects = getAvailableTrackedObjects(); tablet.emitScriptEvent(JSON.stringify(availableTrackedObjects)); - print("PUCK-ATTACH: availableTrackedObjects = " + JSON.stringify(availableTrackedObjects)); + // print("PUCK-ATTACH: availableTrackedObjects = " + JSON.stringify(availableTrackedObjects)); }, 1000); // wait 1 sec before sending.. } shown = true; @@ -93,10 +105,10 @@ function attach(obj) { var key = indexToTrackedObjectName(Number(attachedObj.puckno)); attachedObj.key = key; - print("PUCK-ATTACH: attachedObj = " + JSON.stringify(attachedObj)); + // print("PUCK-ATTACH: attachedObj = " + JSON.stringify(attachedObj)); Script.update.connect(update); - update(0.001); + update(); } function remove() { @@ -113,7 +125,7 @@ function pad(num, size) { return tempString.substr(tempString.length - size); } -function update(dt) { +function update() { if (attachedEntity && attachedObj && Controller.Hardware.Vive) { var pose = Controller.getPoseValue(Controller.Hardware.Vive[attachedObj.key]); var avatarXform = new Xform(MyAvatar.orientation, MyAvatar.position);