History

Added script that will attach an entity to a vive sensor.

Anthony J. Thibault committed 9 years ago

Commit cd1bece1 parent 07fefdc1 file · +131 −0Browse files

Showing the changes to scripts/developer (1 of 1 file). Show all

Unified · Split

scripts/developer/tests/puck-attach.js+131−0
@@ -0,0 +1,131 @@
1//
2// debugatar.js
3//
4/* eslint indent: ["error", 4, { "outerIIFEBody": 0 }] */
5/* global Xform */
6Script.include("/~/system/libraries/Xform.js");
7
8(function() { // BEGIN LOCAL_SCOPE
9
10var TABLET_BUTTON_NAME = "PUCKATTACH";
11var HTML_URL = "https://s3.amazonaws.com/hifi-public/tony/html/puck-attach.html";
12
13var tablet = Tablet.getTablet("com.highfidelity.interface.tablet.system");
14var 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
20tabletButton.clicked.connect(function () {
21 if (shown) {
22 tablet.gotoHomeScreen();
23 } else {
24 tablet.gotoWebScreen(HTML_URL);
25 }
26});
27
28var shown = false;
29var attachedEntity;
30var attachedObj;
31
32function 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
50tablet.screenChanged.connect(onScreenChanged);
51
52function 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
71function remove() {
72 if (attachedEntity) {
73 Script.update.disconnect(update);
74 Entities.deleteEntity(attachedEntity);
75 attachedEntity = undefined;
76 }
77 attachedObj = undefined;
78}
79
80function pad(num, size) {
81 var s = "000000000" + num;
82 return s.substr(s.length-size);
83}
84
85function 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
106function 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
121Script.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

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t