-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform.html
386 lines (318 loc) · 11.8 KB
/
transform.html
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<script>
var settings = {
// 0 tied to cam, 1 tied to world
target: 1,
// three is my guess-best, others can be found in the docs or the switch below
transformation: 3,
// true is on, false is off
building: false,
// true is on, false is off
pillars: true,
// true is on, false is off
fog: true
}
var scene = document.querySelector('a-scene');
scene.addEventListener('loaded', function(){
// Initially set the camera object
setCameraAttributes();
// Turns the active target on
toggleTarget(activeTarget);
// Setup building
toggleBuilding(building.state);
// Setup pillars
togglePillars(pillars.state);
// Setup observer
setObserver();
});
var fog = {
state: settings.fog,
density: scene.getAttribute('fog').density
}
console.log(fog.density);
// Create a camera object
var camera = {
el: document.querySelector('#human-camera'),
x: 0,
y: 0,
z: 0,
a: 0,
b: 0,
c: 0
}
// Set target object
var targets = document.getElementsByClassName('target');
var newPositionCompInit = undefined;
//Set defaults
var defaultTarget = targets[settings.target];
var defaultTransformation = settings.transformation;
var defaultPositions = [];
defaultPositions[0] = targets[0].getAttribute('position');
defaultPositions[1] = targets[1].getAttribute('position');
var defaultRotations = [];
defaultRotations[0] = targets[0].getAttribute('rotation');
defaultRotations[1] = targets[1].getAttribute('rotation');
var building = {
el: document.querySelector('#building'),
state: settings.building
}
var pillars = {
el: document.querySelector('#pillars'),
state: settings.pillars
}
// Set active target
var activeTarget = defaultTarget;
// KeyListener
var keyListener = new window.keypress.Listener();
keyListener.simple_combo('c', function(){
setCameraAttributes();
console.log(camera);
console.log(activeTarget);
});
keyListener.simple_combo('t', function(){
toggleTarget();
});
keyListener.simple_combo('b', function(){
toggleBuilding();
});
keyListener.simple_combo('p', function(){
togglePillars();
});
keyListener.simple_combo('f', function(){
toggleFog();
});
keyListener.simple_combo('0', function(){
setObserver(defaultTransformation);
});
keyListener.simple_combo('1', function(e){
setObserver(1);
});
keyListener.simple_combo('2', function(){
setObserver(2);
});
keyListener.simple_combo('3', function(){
setObserver(3);
});
keyListener.simple_combo('4', function(){
setObserver(4);
});
keyListener.simple_combo('5', function(){
setObserver(5);
});
keyListener.simple_combo('6', function(){
setObserver(6);
});
keyListener.simple_combo('7', function(){
setObserver(7);
});
keyListener.simple_combo('8', function(){
setObserver(8);
});
// Set global observer and creation function
var observer = undefined;
function setObserver(transformationType){
// Disconnect existing observer
if(observer != undefined)
observer.disconnect();
// Set transformation type to default
if(transformationType == undefined)
transformationType = defaultTransformation;
// Observe the changes to the camera
observer = new MutationObserver(function(transformations){
// For each element in transformations (which comes from the MutationObserver), set camera attributes and update the target
transformations.forEach(function(transformation){
setCameraAttributes();
updateTarget(activeTarget, transformationType);
});
});
// I don't know what I'm doing here, Mozilla's docs told me to do it
var observerConfig = {
attributes: true,
childList: true,
characterData: true
}
// Set up the observer
observer.observe(camera.el, observerConfig);
// Pass it on up so it is available next time
return observer;
}
// Set the camera attributes
function setCameraAttributes(){
var humanCameraPosition = camera.el.getAttribute('position');
camera.x = humanCameraPosition.x;
camera.y = humanCameraPosition.y;
camera.z = humanCameraPosition.z;
var humanCameraRotation = camera.el.getAttribute('rotation');
camera.a = humanCameraRotation.x;
camera.b = humanCameraRotation.y;
camera.c = humanCameraRotation.z;
}
// Sets opacity
function setOpacityOfChildren(el, opacity){
// for each child element in the el[]
for(var activeChild in el){
// if it is an object
if(typeof el[activeChild] == 'object'){
// take the material{}
var material = el[activeChild].getAttribute('material');
// change its opacity
material.opacity = opacity;
// set the new material{}
el[activeChild].setAttribute('material', material);
}
}
}
// Toggles the rotation target
function toggleTarget(toggleTo){
// Turn off active target
setOpacityOfChildren(activeTarget.children, 0);
// If nothing is being toggled to, then just swap states
if(toggleTo == undefined){
if(activeTarget == targets[0]){
activeTarget = targets[1];
}else if(activeTarget == targets[1]){
activeTarget = targets[0];
}
else{
console.log("halp");
}
}else{
// otherwise, set the active target to what we are toggling to
activeTarget = toggleTo;
}
// Turn on the new active target
setOpacityOfChildren(activeTarget.children, 1);
}
function toggleBuilding(ioState){
if(ioState == undefined){
if(building.state == false){
building.state = true;
}else{
building.state = false;
}
}else{
building.state = ioState;
}
if(building.state == true){
setOpacityOfChildren(building.el.children, 1);
}else{
setOpacityOfChildren(building.el.children, 0);
}
}
function togglePillars(ioState){
if(ioState == undefined){
if(pillars.state == false){
pillars.state = true;
}else{
pillars.state = false;
}
}else{
pillars.state = ioState;
}
if(pillars.state == true){
setOpacityOfChildren(pillars.el.children, 1);
}else{
setOpacityOfChildren(pillars.el.children, 0);
}
}
function cameraPositionScalar(camera, cameraScalar){
return camera.x*cameraScalar[0] + " " + camera.y*cameraScalar[1] + " " + camera.z*cameraScalar[2];
}
function cameraRotationScalar(camera, cameraScalar){
return camera.a*cameraScalar[0] + " " + camera.b*cameraScalar[1] + " " + camera.c*cameraScalar[2];
}
function equalScalarString(scalar){
return scalar + ' ' + scalar + ' ' + scalar;
}
function resetTargetPositions(){
targets[0].setAttribute('position', defaultPositions[0]);
targets[1].setAttribute('position', defaultPositions[1]);
}
function resetTargetRotations(){
targets[0].setAttribute('rotation', defaultPositions[0]);
targets[1].setAttribute('rotation', defaultPositions[1]);
}
function toggleFog(){
if(fog.state){
scene.setAttribute('fog', 'density', 0);
fog.state = false;
}else{
scene.setAttribute('fog', 'density', fog.density);
fog.state = true;
}
}
function updateTarget(target, transformationType){
if(transformationType == undefined)
transformationType = defaultTransformation;
resetTargetPositions();
resetTargetRotations();
switch (transformationType){
// Rotate target with human camera rotation
case 1:
target.setAttribute('rotation', cameraRotationScalar(camera, [1, 1, 1]));
break;
// Rotate target with human camera rotation
case 8:
target.setAttribute('rotation', cameraRotationScalar(camera, [-1, -1, 1]));
break;
// Rotate target with human camera rotation
// includes reasonable magic numbers: 4x of horizontal rotation, 3x of vertical rotation
case 2:
target.setAttribute('rotation', cameraRotationScalar(camera, [4, 3, 1]));
break;
// Rotate target inversely with human camera rotation
// includes reasonable magic numbers: -4x of horizontal rotation, -3x of vertical rotation
case 3:
target.setAttribute('position', target.getAttribute('position'));
target.setAttribute('rotation', cameraRotationScalar(camera, [-4, -3, 1]));
break;
// Move target in x-axis based on z-position of human camera
case 4:
newPosition = target.getAttribute('position');
// Offset position by one meter
newPosition.x = camera.z - 1;
target.setAttribute('position', newPosition);
break;
// Rotate target in x-axis based on z-position of human camera
case 5:
target.setAttribute('rotation', camera.z*60 + " 0 0");
break;
// Move target in z-axis based on xy-axis rotation of camera
case 6:
newPosition = target.getAttribute('position');
// Right or down make the target come towards the initial camera
newPosition.z = -1 * (camera.a/10 + camera.b/60);
target.setAttribute('position', newPosition);
break;
// Scale target based on x-axis rotation
case 7:
newScaleBase = camera.a + 1;
if(newScaleBase > .1){
newScale = equalScalarString(camera.a + 1);
}else{
newScale = equalScalarString(.1);
}
target.setAttribute('scale', newScale);
if(newPositionCompInit == undefined){
newPositionCompInit = target.getAttribute('position');
newPositionComp = newPositionCompInit;
centerPoint = {
x: 1,
y: 1,
z: 1
};
}
centerPoint.z = -.1;
centerPoint.y = .3;
newPositionComp.z = camera.a*centerPoint.z - 2.5;
newPositionComp.y = camera.a*centerPoint.y + 1.4;
if(newPositionComp.z > -2.5)
newPositionComp.z = -2.5;
if(newPositionComp.y < .9)
newPositionComp.y = .9;
target.setAttribute('position', newPositionComp);
break;
default:
break;
}
}
</script>