-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathContext.js
More file actions
555 lines (478 loc) · 18.3 KB
/
Context.js
File metadata and controls
555 lines (478 loc) · 18.3 KB
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Famous Industries Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
'use strict';
var WebGLRenderer = require('../webgl-renderers/WebGLRenderer');
var Camera = require('../components/Camera');
var DOMRenderer = require('../dom-renderers/DOMRenderer');
var Commands = require('../core/Commands');
/**
* Context is a render layer with its own WebGLRenderer and DOMRenderer.
* It is the interface between the Compositor which receives commands
* and the renderers that interpret them. It also relays information to
* the renderers about resizing.
*
* The DOMElement at the given query selector is used as the root. A
* new DOMElement is appended to this root element, and used as the
* parent element for all Famous DOM rendering at this context. A
* canvas is added and used for all WebGL rendering at this context.
*
* @class Context
* @constructor
*
* @param {String} selector Query selector used to locate root element of
* context layer.
* @param {Compositor} compositor Compositor reference to pass down to
* WebGLRenderer.
*/
function Context(selector, compositor) {
this._compositor = compositor;
this._rootEl = document.querySelector(selector);
this._selector = selector;
if (this._rootEl === null) {
throw new Error(
'Failed to create Context: ' +
'No matches for "' + selector + '" found.'
);
}
this._selector = selector;
// Initializes the DOMRenderer.
// Every Context has at least a DOMRenderer for now.
this._initDOMRenderer();
// WebGLRenderer will be instantiated when needed.
this._webGLRenderer = null;
this._domRenderer = new DOMRenderer(this._domRendererRootEl, selector, compositor);
this._canvasEl = null;
// State holders
this._renderState = {
projectionType: Camera.ORTHOGRAPHIC_PROJECTION,
perspectiveTransform: new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]),
viewTransform: new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]),
viewDirty: false,
perspectiveDirty: false
};
this._size = [];
this._meshTransform = new Float32Array(16);
this._meshSize = [0, 0, 0];
this._initDOM = false;
this._commandCallbacks = [];
this.initCommandCallbacks();
this.updateSize();
}
/**
* Queries DOMRenderer size and updates canvas size. Relays size information to
* WebGLRenderer.
*
* @method
*
* @return {Context} this
*/
Context.prototype.updateSize = function () {
var width = this._rootEl.offsetWidth;
var height = this._rootEl.offsetHeight;
this._size[0] = width;
this._size[1] = height;
this._size[2] = (width > height) ? width : height;
this._compositor.sendResize(this._selector, this._size);
if (this._webGLRenderer) this._webGLRenderer.updateSize(this._size);
return this;
};
/**
* Draw function called after all commands have been handled for current frame.
* Issues draw commands to all renderers with current renderState.
*
* @method
*
* @return {undefined} undefined
*/
Context.prototype.draw = function draw() {
this._domRenderer.draw(this._renderState);
if (this._webGLRenderer) this._webGLRenderer.draw(this._renderState);
if (this._renderState.perspectiveDirty) this._renderState.perspectiveDirty = false;
if (this._renderState.viewDirty) this._renderState.viewDirty = false;
};
/**
* Initializes the DOMRenderer by creating a root DIV element and appending it
* to the context.
*
* @method
* @private
*
* @return {undefined} undefined
*/
Context.prototype._initDOMRenderer = function _initDOMRenderer() {
this._domRendererRootEl = document.createElement('div');
this._rootEl.appendChild(this._domRendererRootEl);
this._domRendererRootEl.style.visibility = 'hidden';
this._domRenderer = new DOMRenderer(
this._domRendererRootEl,
this._selector,
this._compositor
);
};
Context.prototype.cleanup = function cleanup () {
this._rootEl.removeChild(this._domRendererRootEl);
if (this._webGLRendererRootEl)
this._rootEl.removeChild(this._webGLRendererRootEl);
};
Context.prototype.initCommandCallbacks = function initCommandCallbacks () {
this._commandCallbacks[Commands.INIT_DOM] = initDOM;
this._commandCallbacks[Commands.DOM_RENDER_SIZE] = domRenderSize;
this._commandCallbacks[Commands.CHANGE_TRANSFORM] = changeTransform;
this._commandCallbacks[Commands.CHANGE_SIZE] = changeSize;
this._commandCallbacks[Commands.CHANGE_PROPERTY] = changeProperty;
this._commandCallbacks[Commands.CHANGE_CONTENT] = changeContent;
this._commandCallbacks[Commands.CHANGE_ATTRIBUTE] = changeAttribute;
this._commandCallbacks[Commands.ADD_CLASS] = addClass;
this._commandCallbacks[Commands.REMOVE_CLASS] = removeClass;
this._commandCallbacks[Commands.SUBSCRIBE] = subscribe;
this._commandCallbacks[Commands.UNSUBSCRIBE] = unsubscribe;
this._commandCallbacks[Commands.GL_SET_DRAW_OPTIONS] = glSetDrawOptions;
this._commandCallbacks[Commands.GL_AMBIENT_LIGHT] = glAmbientLight;
this._commandCallbacks[Commands.GL_LIGHT_POSITION] = glLightPosition;
this._commandCallbacks[Commands.GL_LIGHT_COLOR] = glLightColor;
this._commandCallbacks[Commands.MATERIAL_INPUT] = materialInput;
this._commandCallbacks[Commands.GL_SET_GEOMETRY] = glSetGeometry;
this._commandCallbacks[Commands.GL_UNIFORMS] = glUniforms;
this._commandCallbacks[Commands.GL_BUFFER_DATA] = glBufferData;
this._commandCallbacks[Commands.GL_CUTOUT_STATE] = glCutoutState;
this._commandCallbacks[Commands.GL_MESH_VISIBILITY] = glMeshVisibility;
this._commandCallbacks[Commands.GL_REMOVE_MESH] = glRemoveMesh;
this._commandCallbacks[Commands.PINHOLE_PROJECTION] = pinholeProjection;
this._commandCallbacks[Commands.ORTHOGRAPHIC_PROJECTION] = orthographicProjection;
this._commandCallbacks[Commands.CHANGE_VIEW_TRANSFORM] = changeViewTransform;
this._commandCallbacks[Commands.PREVENT_DEFAULT] = preventDefault;
this._commandCallbacks[Commands.ALLOW_DEFAULT] = allowDefault;
this._commandCallbacks[Commands.READY] = ready;
this._commandCallbacks[Commands.REMOVE_SCENE] = removeScene;
};
/**
* Initializes the WebGLRenderer and updates it initial size.
*
* The Initialization process consists of the following steps:
*
* 1. A new `<canvas>` element is being created and appended to the root element.
* 2. The WebGLRenderer is being instantiated.
* 3. The size of the WebGLRenderer is being updated.
*
* @method
* @private
*
* @return {undefined} undefined
*/
Context.prototype._initWebGLRenderer = function _initWebGLRenderer() {
this._webGLRendererRootEl = document.createElement('canvas');
this._rootEl.appendChild(this._webGLRendererRootEl);
this._webGLRenderer = new WebGLRenderer(
this._webGLRendererRootEl,
this._compositor
);
// Don't read offset width and height.
this._webGLRenderer.updateSize(this._size);
};
/**
* Gets the size of the parent element of the DOMRenderer for this context.
*
* @method
*
* @return {undefined} undefined
*/
Context.prototype.getRootSize = function getRootSize() {
return [
this._rootEl.offsetWidth,
this._rootEl.offsetHeight
];
};
/**
* Initializes the context if the `READY` command has been received earlier.
*
* @return {undefined} undefined
*/
Context.prototype.checkInit = function checkInit () {
if (this._initDOM) {
this._domRendererRootEl.style.visibility = 'visible';
this._initDOM = false;
}
};
/**
* Handles delegation of commands to renderers of this context.
*
* @method
*
* @param {String} path String used as identifier of a given node in the
* scene graph.
* @param {Array} commands List of all commands from this frame.
* @param {Number} iterator Number indicating progress through the command
* queue.
*
* @return {Number} iterator indicating progress through the command queue.
*/
Context.prototype.receive = function receive(path, commands, iterator) {
var localIterator = iterator;
var command = commands[++localIterator];
this._domRenderer.loadPath(path);
while (command != null) {
if (command === Commands.WITH || command === Commands.TIME) return localIterator - 1;
else localIterator = this._commandCallbacks[command](this, path, commands, localIterator) + 1;
command = commands[localIterator];
}
return localIterator;
};
/**
* Getter method used for retrieving the used DOMRenderer.
*
* @method
*
* @return {DOMRenderer} The DOMRenderer being used by the Context.
*/
Context.prototype.getDOMRenderer = function getDOMRenderer() {
return this._domRenderer;
};
/**
* Getter method used for retrieving the used WebGLRenderer (if any).
*
* @method
*
* @return {WebGLRenderer|null} The WebGLRenderer being used by the Context.
*/
Context.prototype.getWebGLRenderer = function getWebGLRenderer() {
return this._webGLRenderer;
};
// Command Callbacks
function preventDefault (context, path, commands, iterator) {
if (context._webGLRenderer) context._webGLRenderer.getOrSetCutout(path);
context._domRenderer.preventDefault(commands[++iterator]);
return iterator;
}
function allowDefault (context, path, commands, iterator) {
if (context._webGLRenderer) context._webGLRenderer.getOrSetCutout(path);
context._domRenderer.allowDefault(commands[++iterator]);
return iterator;
}
function ready (context, path, commands, iterator) {
context._initDOM = true;
return iterator;
}
function initDOM (context, path, commands, iterator) {
context._domRenderer.insertEl(commands[++iterator]);
return iterator;
}
function domRenderSize (context, path, commands, iterator) {
context._domRenderer.getSizeOf(commands[++iterator]);
return iterator;
}
function changeTransform (context, path, commands, iterator) {
var temp = context._meshTransform;
temp[0] = commands[++iterator];
temp[1] = commands[++iterator];
temp[2] = commands[++iterator];
temp[3] = commands[++iterator];
temp[4] = commands[++iterator];
temp[5] = commands[++iterator];
temp[6] = commands[++iterator];
temp[7] = commands[++iterator];
temp[8] = commands[++iterator];
temp[9] = commands[++iterator];
temp[10] = commands[++iterator];
temp[11] = commands[++iterator];
temp[12] = commands[++iterator];
temp[13] = commands[++iterator];
temp[14] = commands[++iterator];
temp[15] = commands[++iterator];
context._domRenderer.setMatrix(temp);
if (context._webGLRenderer)
context._webGLRenderer.setCutoutUniform(path, 'u_transform', temp);
return iterator;
}
function changeSize (context, path, commands, iterator) {
var width = commands[++iterator];
var height = commands[++iterator];
context._domRenderer.setSize(width, height);
if (context._webGLRenderer) {
context._meshSize[0] = width;
context._meshSize[1] = height;
context._webGLRenderer.setCutoutUniform(path, 'u_size', context._meshSize);
}
return iterator;
}
function changeProperty (context, path, commands, iterator) {
if (context._webGLRenderer) context._webGLRenderer.getOrSetCutout(path);
context._domRenderer.setProperty(commands[++iterator], commands[++iterator]);
return iterator;
}
function changeContent (context, path, commands, iterator) {
if (context._webGLRenderer) context._webGLRenderer.getOrSetCutout(path);
context._domRenderer.setContent(commands[++iterator]);
return iterator;
}
function changeAttribute (context, path, commands, iterator) {
if (context._webGLRenderer) context._webGLRenderer.getOrSetCutout(path);
context._domRenderer.setAttribute(commands[++iterator], commands[++iterator]);
return iterator;
}
function addClass (context, path, commands, iterator) {
if (context._webGLRenderer) context._webGLRenderer.getOrSetCutout(path);
context._domRenderer.addClass(commands[++iterator]);
return iterator;
}
function removeClass (context, path, commands, iterator) {
if (context._webGLRenderer) context._webGLRenderer.getOrSetCutout(path);
context._domRenderer.removeClass(commands[++iterator]);
return iterator;
}
function subscribe (context, path, commands, iterator) {
if (context._webGLRenderer) context._webGLRenderer.getOrSetCutout(path);
context._domRenderer.subscribe(commands[++iterator]);
return iterator;
}
function unsubscribe (context, path, commands, iterator) {
if (context._webGLRenderer) context._webGLRenderer.getOrSetCutout(path);
context._domRenderer.unsubscribe(commands[++iterator]);
return iterator;
}
function glSetDrawOptions (context, path, commands, iterator) {
if (!context._webGLRenderer) context._initWebGLRenderer();
context._webGLRenderer.setMeshOptions(path, commands[++iterator]);
return iterator;
}
function glAmbientLight (context, path, commands, iterator) {
if (!context._webGLRenderer) context._initWebGLRenderer();
context._webGLRenderer.setAmbientLightColor(
path,
commands[++iterator],
commands[++iterator],
commands[++iterator]
);
return iterator;
}
function glLightPosition (context, path, commands, iterator) {
if (!context._webGLRenderer) context._initWebGLRenderer();
context._webGLRenderer.setLightPosition(
path,
commands[++iterator],
commands[++iterator],
commands[++iterator]
);
return iterator;
}
function glLightColor (context, path, commands, iterator) {
if (!context._webGLRenderer) context._initWebGLRenderer();
context._webGLRenderer.setLightColor(
path,
commands[++iterator],
commands[++iterator],
commands[++iterator]
);
return iterator;
}
function materialInput (context, path, commands, iterator) {
if (!context._webGLRenderer) context._initWebGLRenderer();
context._webGLRenderer.handleMaterialInput(
path,
commands[++iterator],
commands[++iterator]
);
return iterator;
}
function glSetGeometry (context, path, commands, iterator) {
if (!context._webGLRenderer) context._initWebGLRenderer();
context._webGLRenderer.setGeometry(
path,
commands[++iterator],
commands[++iterator],
commands[++iterator]
);
return iterator;
}
function glUniforms (context, path, commands, iterator) {
if (!context._webGLRenderer) context._initWebGLRenderer();
context._webGLRenderer.setMeshUniform(
path,
commands[++iterator],
commands[++iterator]
);
return iterator;
}
function glBufferData (context, path, commands, iterator) {
if (!context._webGLRenderer) context._initWebGLRenderer();
context._webGLRenderer.bufferData(
commands[++iterator],
commands[++iterator],
commands[++iterator],
commands[++iterator],
commands[++iterator]
);
return iterator;
}
function glCutoutState (context, path, commands, iterator) {
if (!context._webGLRenderer) context._initWebGLRenderer();
context._webGLRenderer.setCutoutState(path, commands[++iterator]);
return iterator;
}
function glMeshVisibility (context, path, commands, iterator) {
if (!context._webGLRenderer) context._initWebGLRenderer();
context._webGLRenderer.setMeshVisibility(path, commands[++iterator]);
return iterator;
}
function glRemoveMesh (context, path, commands, iterator) {
if (!context._webGLRenderer) context._initWebGLRenderer();
context._webGLRenderer.removeMesh(path);
return iterator;
}
function pinholeProjection (context, path, commands, iterator) {
context._renderState.projectionType = Camera.PINHOLE_PROJECTION;
context._renderState.perspectiveTransform[11] = -1 / commands[++iterator];
context._renderState.perspectiveDirty = true;
return iterator;
}
function orthographicProjection (context, path, commands, iterator) {
context._renderState.projectionType = Camera.ORTHOGRAPHIC_PROJECTION;
context._renderState.perspectiveTransform[11] = 0;
context._renderState.perspectiveDirty = true;
return iterator;
}
function changeViewTransform (context, path, commands, iterator) {
context._renderState.viewTransform[0] = commands[++iterator];
context._renderState.viewTransform[1] = commands[++iterator];
context._renderState.viewTransform[2] = commands[++iterator];
context._renderState.viewTransform[3] = commands[++iterator];
context._renderState.viewTransform[4] = commands[++iterator];
context._renderState.viewTransform[5] = commands[++iterator];
context._renderState.viewTransform[6] = commands[++iterator];
context._renderState.viewTransform[7] = commands[++iterator];
context._renderState.viewTransform[8] = commands[++iterator];
context._renderState.viewTransform[9] = commands[++iterator];
context._renderState.viewTransform[10] = commands[++iterator];
context._renderState.viewTransform[11] = commands[++iterator];
context._renderState.viewTransform[12] = commands[++iterator];
context._renderState.viewTransform[13] = commands[++iterator];
context._renderState.viewTransform[14] = commands[++iterator];
context._renderState.viewTransform[15] = commands[++iterator];
context._renderState.viewDirty = true;
return iterator;
}
function removeScene (context, path, commands, iterator) {
context._compositor.removeContext(path);
return commands.length - 1;
}
module.exports = Context;