Showing the changes to scripts/developer (1 of 1 file). Show all
Unified · Split
| @@ -0,0 +1,131 @@ | |||
| 1 | // | ||
| 2 | // debugatar.js | ||
| 3 | // | ||
| 4 | /* eslint indent: ["error", 4, { "outerIIFEBody": 0 }] */ | ||
| 5 | /* global Xform */ | ||
| 6 | Script.include("/~/system/libraries/Xform.js"); | ||
| 7 | |||
| 8 | (function() { // BEGIN LOCAL_SCOPE | ||
| 9 | |||
| 10 | var TABLET_BUTTON_NAME = "PUCKATTACH"; | ||
| 11 | var HTML_URL = "https://s3.amazonaws.com/hifi-public/tony/html/puck-attach.html"; | ||
| 12 | |||
| 13 | var tablet = Tablet.getTablet("com.highfidelity.interface.tablet.system"); | ||
| 14 | var tabletButton = tablet.addButton({ | ||
| 15 | text: TABLET_BUTTON_NAME, | ||
| 16 | icon: "https://s3.amazonaws.com/hifi-public/tony/icons/tpose-i.svg", | ||
| 17 | activeIcon: "https://s3.amazonaws.com/hifi-public/tony/icons/tpose-a.svg" | ||
| 18 | }); | ||
| 19 | |||
| 20 | tabletButton.clicked.connect(function () { | ||
| 21 | if (shown) { | ||
| 22 | tablet.gotoHomeScreen(); | ||
| 23 | } else { | ||
| 24 | tablet.gotoWebScreen(HTML_URL); | ||
| 25 | } | ||
| 26 | }); | ||
| 27 | |||
| 28 | var shown = false; | ||
| 29 | var attachedEntity; | ||
| 30 | var attachedObj; | ||
| 31 | |||
| 32 | function onScreenChanged(type, url) { | ||
| 33 | if (type === "Web" && url === HTML_URL) { | ||
| 34 | tabletButton.editProperties({isActive: true}); | ||
| 35 | if (!shown) { | ||
| 36 | // hook up to event bridge | ||
| 37 | tablet.webEventReceived.connect(onWebEventReceived); | ||
| 38 | } | ||
| 39 | shown = true; | ||
| 40 | } else { | ||
| 41 | tabletButton.editProperties({isActive: false}); | ||
| 42 | if (shown) { | ||
| 43 | // disconnect from event bridge | ||
| 44 | tablet.webEventReceived.disconnect(onWebEventReceived); | ||
| 45 | } | ||
| 46 | shown = false; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | tablet.screenChanged.connect(onScreenChanged); | ||
| 51 | |||
| 52 | function attach(obj) { | ||
| 53 | attachedEntity = Entities.addEntity({ | ||
| 54 | type: "Model", | ||
| 55 | name: "puck-attach-entity", | ||
| 56 | modelURL: obj.modelurl | ||
| 57 | }); | ||
| 58 | attachedObj = obj; | ||
| 59 | var localPos = {x: Number(obj.posx), y: Number(obj.posy), z: Number(obj.posz)}; | ||
| 60 | var localRot = Quat.fromVec3Degrees({x: Number(obj.rotx), y: Number(obj.roty), z: Number(obj.rotz)}); | ||
| 61 | attachedObj.localXform = new Xform(localRot, localPos); | ||
| 62 | var key = "TrackedObject" + pad(attachedObj.puckno, 2); | ||
| 63 | attachedObj.key = key; | ||
| 64 | |||
| 65 | print("AJT: attachedObj = " + JSON.stringify(attachedObj)); | ||
| 66 | |||
| 67 | Script.update.connect(update); | ||
| 68 | update(0.001); | ||
| 69 | } | ||
| 70 | |||
| 71 | function remove() { | ||
| 72 | if (attachedEntity) { | ||
| 73 | Script.update.disconnect(update); | ||
| 74 | Entities.deleteEntity(attachedEntity); | ||
| 75 | attachedEntity = undefined; | ||
| 76 | } | ||
| 77 | attachedObj = undefined; | ||
| 78 | } | ||
| 79 | |||
| 80 | function pad(num, size) { | ||
| 81 | var s = "000000000" + num; | ||
| 82 | return s.substr(s.length-size); | ||
| 83 | } | ||
| 84 | |||
| 85 | function update(dt) { | ||
| 86 | if (attachedEntity && attachedObj && Controller.Hardware.Vive) { | ||
| 87 | var pose = Controller.getPoseValue(Controller.Hardware.Vive[attachedObj.key]); | ||
| 88 | var avatarXform = new Xform(MyAvatar.orientation, MyAvatar.position); | ||
| 89 | var puckXform = new Xform(pose.rotation, pose.translation); | ||
| 90 | var finalXform = Xform.mul(avatarXform, Xform.mul(puckXform, attachedObj.localXform)); | ||
| 91 | if (pose && pose.valid) { | ||
| 92 | Entities.editEntity(attachedEntity, { | ||
| 93 | position: finalXform.pos, | ||
| 94 | rotation: finalXform.rot | ||
| 95 | }); | ||
| 96 | } else { | ||
| 97 | if (pose) { | ||
| 98 | print("AJT: WARNING: invalid pose for " + attachedObj.key); | ||
| 99 | } else { | ||
| 100 | print("AJT: WARNING: could not find key " + attachedObj.key); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | function onWebEventReceived(msg) { | ||
| 107 | var obj = {}; | ||
| 108 | try { | ||
| 109 | obj = JSON.parse(msg); | ||
| 110 | } catch (err) { | ||
| 111 | return; | ||
| 112 | } | ||
| 113 | if (obj.cmd === "attach") { | ||
| 114 | remove(); | ||
| 115 | attach(obj); | ||
| 116 | } else if (obj.cmd === "detach") { | ||
| 117 | remove(); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | Script.scriptEnding.connect(function () { | ||
| 122 | remove(); | ||
| 123 | tablet.removeButton(tabletButton); | ||
| 124 | if (shown) { | ||
| 125 | tablet.webEventReceived.disconnect(onWebEventReceived); | ||
| 126 | tablet.gotoHomeScreen(); | ||
| 127 | } | ||
| 128 | tablet.screenChanged.disconnect(onScreenChanged); | ||
| 129 | }); | ||
| 130 | |||
| 131 | }()); // END LOCAL_SCOPE | ||