-
Notifications
You must be signed in to change notification settings - Fork 1
/
axes.em
290 lines (245 loc) · 10.8 KB
/
axes.em
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* axes.em
*
* Author: Will Monroe
*
* Restrict an object's motion to take place along a certain axis while
* dragging.
*
* [currently: creates a single axis as its own entity, which can be dragged
* in any direction and is not attached to another presence]
*/
system.require('std/graphics/graphics.em');
(function() {
var redAxisMesh = 'meerkat:///wmonroe4/red_axis.dae/optimized/red_axis.dae';
function axesScript(args) {
system.require('std/movement/movableremote.em');
system.require('std/movement/units.em');
system.require('std/core/repeatingTimer.em');
var moveMeshes = {x: 'meerkat:///wmonroe4/red_axis.dae/optimized/red_axis.dae',
y: 'meerkat:///wmonroe4/green_axis.dae/optimized/green_axis.dae',
z: 'meerkat:///wmonroe4/blue_axis.dae/optimized/blue_axis.dae'};
var rotateMeshes = {x: 'meerkat:///wmonroe4/red_gimbal.dae/optimized/red_gimbal.dae',
y: 'meerkat:///wmonroe4/green_gimbal.dae/optimized/green_gimbal.dae',
z: 'meerkat:///wmonroe4/blue_gimbal.dae/optimized/blue_gimbal.dae'};
var offMeshes = {x: '', y: '', z: ''};
var orientations = {x: new util.Quaternion(<0, 0, -1>, Math.PI / 2),
y: new util.Quaternion(),
z: new util.Quaternion(<1, 0, 0>, Math.PI / 2)};
var axisMeshes = offMeshes;
var mode = 'off';
var axes = {};
var axisIDs = {};
var movable = null;
var updateTimer;
var UPDATE_PERIOD = 1 * u.s;
var ROTATE_FACTOR = 0.02;
function onPresenceConnected(presence) {
if(presence.mesh.slice(-12) != 'red_axis.dae')
return;
setPosition << [{'request':'movable':},
{'position'::}];
setOrientation << [{'request':'movable':},
{'orient'::}];
setScale << [{'request':'movable':},
{'scale'::}];
follow << [{'request':'axes':},
{'id'::}];
rotateMode << [{'request':'axes':},
{'mode':'rotate':}];
moveMode << [{'request':'axes':},
{'mode':'move':}];
offMode << [{'request':'axes':},
{'mode':'off':}];
snapLocal << [{'request':'axes':},
{'snap':'local':}];
snapGlobal << [{'request':'axes':},
{'snap':'global':}];
forwardScriptRequest << [{'request':'script':}];
onRedConnected(presence);
}
function onRedConnected(presence) {
presence.modelOrientation = orientations.x;
axes.x = presence;
axisIDs[presence.toString()] = true;
system.createPresence(axisMeshes.y, onGreenConnected);
}
function onGreenConnected(presence) {
presence.modelOrientation = orientations.y;
axes.y = presence;
axisIDs[presence.toString()] = true;
system.createPresence(axisMeshes.z, onBlueConnected);
}
function onBlueConnected(presence) {
presence.modelOrientation = orientations.z;
axes.z = presence;
axisIDs[presence.toString()] = true;
follow();
updateTimer = new std.core.RepeatingTimer(UPDATE_PERIOD,
function() { follow(); });
{axes: 'created'} >> args.avatar >> [];
}
function setPosition(msg, sender) {
var axis = system.self.orientation.mul(
system.self.modelOrientation.mul(<0, 1, 0>));
var delta = axis.scale(axis.dot(msg.position - system.self.position));
if(mode == 'rotate') {
var quatDelta = new util.Quaternion(delta.normal(),
delta.length() * ROTATE_FACTOR /
system.self.scale);
system.self.orientation = quatDelta.mul(system.self.orientation);
for(var a in axes)
axes[a].orientation = system.self.orientation;
if(movable)
movable.setOrientation(system.self.orientation);
} else /* mode == 'move' (or rarely, mode == 'off') */ {
system.self.position = system.self.position + delta;
for(var a in axes)
axes[a].position = system.self.position;
if(movable)
movable.setPosition(system.self.position);
}
msg.makeReply({}) >> [];
}
function setOrientation(msg, sender) {
/* Quaternions mess everything up! By all means, try to fix this
* code if you're brave. Right now I've just made moving become
* rotating when in rotate mode. (See above).
*/
/*
var axis = system.self.orientation.mul(
system.self.modelOrientation.mul(<0, 1, 0>));
//system.__debugPrint('axis: ' + std.core.pretty(axis) + '\n');
var target = msg.orient.mul(<0, 1, 0>);
//system.__debugPrint('target: ' + std.core.pretty(target) + '\n');
var corrAxis = target.cross(axis);
//system.__debugPrint('corr: ' + std.core.pretty(corrAxis) + '\n');
var correction = (new util.Quaternion(corrAxis.x, corrAxis.y,
corrAxis.z, 1 +
target.dot(axis))).normal();
var newOrient = correction.mul(msg.orient);
//system.__debugPrint('new y: ' + std.core.pretty(newOrient.mul(<0, 1, 0>)) + '\n\n');
system.self.orientation = newOrient.mul(system.self.
modelOrientation.inverse());
*/
system.self.orientation = msg.orient.mul(system.self.
modelOrientation.inverse());
for(a in axes)
axes[a].orientation = system.self.orientation;
if(movable)
movable.setOrientation(system.self.orientation);
msg.makeReply({}) >> [];
}
function setScale(msg, sender) {
system.self.scale = msg.scale;
for(var a in axes)
axes[a].scale = system.self.scale;
if(movable)
movable.setScale(system.self.scale / 2);
msg.makeReply({}) >> [];
}
function follow(msg, sender) {
if(typeof(msg) === 'undefined') {
if(movable) {
msg = {id: movable._remote.toString()};
} else {
msg = {id: ''};
}
}
if(msg.id !== '') {
if(msg.id in axisIDs)
// selecting an axis shouldn't make it follow itself!
return;
if(!movable || movable._remote.toString() != msg.id)
movable = new std.movement.MovableRemote(
system.createVisible(msg.id));
for(var a in axes) {
axes[a].mesh = axisMeshes[a];
axes[a].position = movable.getPosition();
axes[a].orientation = movable.getOrientation();
axes[a].scale = movable.getScale() * 2;
}
} else {
movable = null;
for(var a in axes) {
axes[a].mesh = '';
}
}
}
function setMode(modeName) {
mode = modeName;
axisMeshes = {
move: moveMeshes,
rotate: rotateMeshes,
off: offMeshes
}[mode];
for(var a in axes)
if(axes[a].mesh != '')
axes[a].mesh = axisMeshes[a];
}
function rotateMode(msg, sender) {
setMode('rotate');
}
function moveMode(msg, sender) {
setMode('move');
}
function offMode(msg, sender) {
setMode('off');
}
function snapLocal(msg, sender) {
for(var a in axes)
axes[a].modelOrientation = orientations[a];
follow();
}
function snapGlobal(msg, sender) {
if(!movable)
return;
for(var a in axes)
axes[a].modelOrientation = movable.getOrientation().inverse().
mul(orientations[a]);
follow();
}
function forwardScriptRequest(msg, sender) {
if(!movable)
return;
msg >> movable._remote >> [];
}
system.onPresenceConnected(onPresenceConnected);
}
std.graphics.Graphics.prototype.onAxesCreated = function(msg, sender) {
this._axes._axesVisible = sender;
};
std.graphics.Graphics.prototype.createAxes = function(self) {
std.core.bind(this.onAxesCreated, this) << [{'axes':'created':}];
system.createEntityScript(<0, 0, 0>, axesScript,
{avatar: self},
self.queryAngle, redAxisMesh, 1);
};
std.graphics.Graphics.prototype._axes = {};
std.graphics.Graphics.prototype._axes.follow = function(vis) {
if (!this._axesVisible) return;
if(vis)
{request: 'axes', id: vis.toString()} >> this._axesVisible >> [];
else
{request: 'axes', id: ''} >> this._axesVisible >> [];
};
std.graphics.Graphics.prototype._axes.rotateMode = function() {
if (!this._axesVisible) return;
{request: 'axes', mode: 'rotate'} >> this._axesVisible >> [];
};
std.graphics.Graphics.prototype._axes.moveMode = function() {
if (!this._axesVisible) return;
{request: 'axes', mode: 'move'} >> this._axesVisible >> [];
};
std.graphics.Graphics.prototype._axes.offMode = function() {
if (!this._axesVisible) return;
{request: 'axes', mode: 'off'} >> this._axesVisible >> [];
};
std.graphics.Graphics.prototype._axes.snapLocal = function() {
if (!this._axesVisible) return;
{request: 'axes', snap: 'local'} >> this._axesVisible >> [];
};
std.graphics.Graphics.prototype._axes.snapGlobal = function() {
if (!this._axesVisible) return;
{request: 'axes', snap: 'global'} >> this._axesVisible >> [];
};
})();