-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathMesh.js
More file actions
672 lines (597 loc) · 18.1 KB
/
Mesh.js
File metadata and controls
672 lines (597 loc) · 18.1 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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/**
* 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 Geometry = require('../webgl-geometries');
var Commands = require('../core/Commands');
var TransformSystem = require('../core/TransformSystem');
var defaultGeometry = new Geometry.Plane();
/**
* The Mesh class is responsible for providing the API for how
* a RenderNode will interact with the WebGL API by adding
* a set of commands to the renderer.
*
* @class Mesh
* @constructor
* @renderable
*
* @param {Node} node Dispatch LocalDispatch to be retrieved
* @param {Object} options Optional params for configuring Mesh
*
* @return {undefined} undefined
*/
function Mesh (node, options) {
this._node = node;
this._changeQueue = [];
this._initialized = false;
this._requestingUpdate = false;
this._inDraw = false;
this.value = {
geometry: defaultGeometry,
drawOptions: null,
color: null,
expressions: {},
flatShading: null,
positionOffset: null,
normals: null,
glossiness: {
strength: null,
color: [0, 0, 0]
}
};
if (options) this.setDrawOptions(options);
this._id = node.addComponent(this);
}
/**
* Pass custom options to Mesh, such as a 3 element map
* which displaces the position of each vertex in world space.
*
* @method
*
* @param {Object} options Draw options
* @return {Mesh} Current mesh
*/
Mesh.prototype.setDrawOptions = function setOptions (options) {
this._changeQueue.push(Commands.GL_SET_DRAW_OPTIONS);
this._changeQueue.push(options);
this.value.drawOptions = options;
return this;
};
/**
* Get the mesh's custom options.
*
* @method
*
* @returns {Object} Options
*/
Mesh.prototype.getDrawOptions = function getDrawOptions () {
return this.value.drawOptions;
};
/**
* Assigns a geometry to be used for this mesh. Sets the geometry from either
* a 'Geometry' or a valid primitive (string) name. Queues the set command for this
* geometry and looks for buffers to send to the renderer to update geometry.
*
* @method
*
* @param {Geometry|String} geometry Geometry to be associated with the mesh.
* @param {Object} options Various configurations for geometries.
*
* @return {Mesh} Mesh
*/
Mesh.prototype.setGeometry = function setGeometry (geometry, options) {
if (typeof geometry === 'string') {
if (!Geometry[geometry]) throw 'Invalid geometry: "' + geometry + '".';
else geometry = new Geometry[geometry](options);
}
if (this.value.geometry !== geometry || this._inDraw) {
if (this._initialized) {
this._changeQueue.push(Commands.GL_SET_GEOMETRY);
this._changeQueue.push(geometry.spec.id);
this._changeQueue.push(geometry.spec.type);
this._changeQueue.push(geometry.spec.dynamic);
}
this._requestUpdate();
this.value.geometry = geometry;
}
if (this._initialized) {
if (this._node) {
var i = this.value.geometry.spec.invalidations.length;
while (i--) {
this.value.geometry.spec.invalidations.pop();
this._changeQueue.push(Commands.GL_BUFFER_DATA);
this._changeQueue.push(this.value.geometry.spec.id);
this._changeQueue.push(this.value.geometry.spec.bufferNames[i]);
this._changeQueue.push(this.value.geometry.spec.bufferValues[i]);
this._changeQueue.push(this.value.geometry.spec.bufferSpacings[i]);
this._changeQueue.push(this.value.geometry.spec.dynamic);
}
if (i) this._requestUpdate();
}
}
return this;
};
/**
* Gets the geometry of a mesh.
*
* @method
*
* @returns {Geometry} Geometry
*/
Mesh.prototype.getGeometry = function getGeometry () {
return this.value.geometry;
};
/**
* Changes the color of Mesh, passing either a material expression or
* color using the 'Color' utility component.
*
* @method
*
* @param {Object|Color} color Material, image, vec3, or Color instance
*
* @return {Mesh} Mesh
*/
Mesh.prototype.setBaseColor = function setBaseColor (color) {
var uniformValue;
var isMaterial = color.__isAMaterial__;
var isColor = !!color.getNormalizedRGBA;
if (isMaterial) {
this.value.color = null;
this.value.expressions.baseColor = color;
uniformValue = color;
}
else if (isColor) {
this.value.expressions.baseColor = null;
this.value.color = color;
uniformValue = color.getNormalizedRGBA();
}
if (this._initialized) {
// If a material expression
if (color.__isAMaterial__) {
this._changeQueue.push(Commands.MATERIAL_INPUT);
}
// If a color component
else if (color.getNormalizedRGB) {
this._changeQueue.push(Commands.GL_UNIFORMS);
}
this._changeQueue.push('u_baseColor');
this._changeQueue.push(uniformValue);
}
this._requestUpdate();
return this;
};
/**
* Returns either the material expression or the color instance of Mesh.
*
* @method
*
* @returns {MaterialExpress|Color} MaterialExpress or Color instance
*/
Mesh.prototype.getBaseColor = function getBaseColor () {
return this.value.expressions.baseColor || this.value.color;
};
/**
* Change whether the Mesh is affected by light. Default is true.
*
* @method
*
* @param {boolean} bool Boolean for setting flat shading
*
* @return {Mesh} Mesh
*/
Mesh.prototype.setFlatShading = function setFlatShading (bool) {
if (this._inDraw || this.value.flatShading !== bool) {
this.value.flatShading = bool;
if (this._initialized) {
this._changeQueue.push(Commands.GL_UNIFORMS);
this._changeQueue.push('u_flatShading');
this._changeQueue.push(bool ? 1 : 0);
}
this._requestUpdate();
}
return this;
};
/**
* Returns a boolean for whether Mesh is affected by light.
*
* @method
*
* @returns {Boolean} Boolean
*/
Mesh.prototype.getFlatShading = function getFlatShading () {
return this.value.flatShading;
};
/**
* Defines a 3-element map which is used to provide significant physical
* detail to the surface by perturbing the facing direction of each individual
* pixel.
*
* @method
*
* @param {Object|Array} materialExpression Material, Image or vec3
*
* @return {Mesh} Mesh
*/
Mesh.prototype.setNormals = function setNormals (materialExpression) {
var isMaterial = materialExpression.__isAMaterial__;
if (isMaterial) {
this.value.expressions.normals = materialExpression;
}
if (this._initialized) {
this._changeQueue.push(materialExpression.__isAMaterial__ ? Commands.MATERIAL_INPUT : Commands.GL_UNIFORMS);
this._changeQueue.push('u_normals');
this._changeQueue.push(materialExpression);
}
this._requestUpdate();
return this;
};
/**
* Returns the Normals expression of Mesh
*
* @method
*
* @param {materialExpression} materialExpression Normals Material Expression
*
* @returns {Array} The normals expression for Mesh
*/
Mesh.prototype.getNormals = function getNormals (materialExpression) {
return this.value.expressions.normals;
};
/**
* Defines the glossiness of the mesh from either a material expression or a
* scalar value
*
* @method
*
* @param {MaterialExpression|Number} glossiness Accepts either a material expression or a number setting its strength
* @param {Color} specularColor Optional value for being able to change the specular color for glossiness.
*
* @return {Mesh} Mesh
*/
Mesh.prototype.setGlossiness = function setGlossiness(glossiness, specularColor) {
var isMaterial = glossiness.__isAMaterial__;
var hasSpecularColor = specularColor && specularColor.getNormalizedRGB;
if (isMaterial) {
this.value.glossiness.strength = null;
this.value.expressions.glossiness = glossiness;
}
else {
this.value.expressions.glossiness = null;
this.value.glossiness.strength = glossiness;
var glossinessValue = this.value.glossiness.color;
if (hasSpecularColor) {
this.value.glossiness.color = specularColor;
glossinessValue = this.value.glossiness.color.getNormalizedRGB();
}
glossinessValue.push(this.value.glossiness.strength);
glossiness = glossinessValue;
}
if (this._initialized) {
this._changeQueue.push(glossiness.__isAMaterial__ ? Commands.MATERIAL_INPUT : Commands.GL_UNIFORMS);
this._changeQueue.push('u_glossiness');
this._changeQueue.push(glossiness);
}
this._requestUpdate();
return this;
};
/**
* Returns material expression or scalar value for glossiness.
*
* @method
*
* @returns {MaterialExpress|Object} MaterialExpression or Glossiness
*/
Mesh.prototype.getGlossiness = function getGlossiness() {
return this.value.expressions.glossiness || this.value.glossiness;
};
/**
* Defines 3 element map which displaces the position of each vertex in world
* space.
*
* @method
*
* @param {MaterialExpression|Array} materialExpression Position offset expression
*
* @return {Mesh} Mesh
*/
Mesh.prototype.setPositionOffset = function positionOffset(materialExpression) {
var uniformValue;
var isMaterial = materialExpression.__isAMaterial__;
if (isMaterial) {
this.value.expressions.positionOffset = materialExpression;
uniformValue = materialExpression;
}
else {
this.value.expressions.positionOffset = null;
this.value.positionOffset = materialExpression;
uniformValue = this.value.positionOffset;
}
if (this._initialized) {
this._changeQueue.push(materialExpression.__isAMaterial__ ? Commands.MATERIAL_INPUT : Commands.GL_UNIFORMS);
this._changeQueue.push('u_positionOffset');
this._changeQueue.push(uniformValue);
}
this._requestUpdate();
return this;
};
/**
* Returns position offset.
*
* @method
*
* @returns {MaterialExpress|Number} MaterialExpress or Number
*/
Mesh.prototype.getPositionOffset = function getPositionOffset () {
return this.value.expressions.positionOffset || this.value.positionOffset;
};
/**
* Get the mesh's expressions
*
* @method
*
* @returns {Object} Object
*/
Mesh.prototype.getMaterialExpressions = function getMaterialExpressions () {
return this.value.expressions;
};
/**
* Get the mesh's value
*
* @method
*
* @returns {Number} Value
*/
Mesh.prototype.getValue = function getValue () {
return this.value;
};
/**
* Queues the invalidations for Mesh
*
* @param {String} expressionName Expression Name
*
* @return {Mesh} Mesh
*/
Mesh.prototype._pushInvalidations = function _pushInvalidations (expressionName) {
var uniformKey;
var expression = this.value.expressions[expressionName];
if (expression) {
var i = expression.invalidations.length;
while (i--) {
uniformKey = expression.invalidations.pop();
this._node.sendDrawCommand(Commands.GL_UNIFORMS);
this._node.sendDrawCommand(uniformKey);
this._node.sendDrawCommand(expression.uniforms[uniformKey]);
}
}
return this;
};
/**
* Sends draw commands to the renderer
*
* @private
* @method
*
* @return {undefined} undefined
*/
Mesh.prototype.onUpdate = function onUpdate() {
var node = this._node;
var queue = this._changeQueue;
if (node) {
node.sendDrawCommand(Commands.WITH);
node.sendDrawCommand(node.getLocation());
// If any invalidations exist, push them into the queue
if (this.value.color && this.value.color.isActive()) {
this._node.sendDrawCommand(Commands.GL_UNIFORMS);
this._node.sendDrawCommand('u_baseColor');
this._node.sendDrawCommand(this.value.color.getNormalizedRGBA());
this._node.requestUpdateOnNextTick(this._id);
}
if (this.value.glossiness.color.isActive && this.value.glossiness.color.isActive()) {
this._node.sendDrawCommand('GL_UNIFORMS');
this._node.sendDrawCommand('u_glossiness');
var glossiness = this.value.glossiness.color.getNormalizedRGB();
glossiness.push(this.value.glossiness.strength);
this._node.sendDrawCommand(glossiness);
this._node.requestUpdateOnNextTick(this._id);
}
else {
this._requestingUpdate = false;
}
// If any invalidations exist, push them into the queue
this._pushInvalidations('baseColor');
this._pushInvalidations('positionOffset');
for (var i = 0; i < queue.length; i++) {
node.sendDrawCommand(queue[i]);
}
queue.length = 0;
}
};
/**
* Save reference to node, set its ID and call draw on Mesh.
*
* @method
*
* @param {Node} node Node
* @param {Number} id Identifier for Mesh
*
* @return {undefined} undefined
*/
Mesh.prototype.onMount = function onMount (node, id) {
this._node = node;
this._id = id;
TransformSystem.makeCalculateWorldMatrixAt(node.getLocation());
this.draw();
};
/**
* Queues the command for dismounting Mesh
*
* @method
*
* @return {undefined} undefined
*/
Mesh.prototype.onDismount = function onDismount () {
this._initialized = false;
this._changeQueue.push(Commands.GL_REMOVE_MESH);
this._requestUpdate();
};
/**
* Makes Mesh visible
*
* @method
*
* @return {undefined} undefined
*/
Mesh.prototype.onShow = function onShow () {
this._changeQueue.push(Commands.GL_MESH_VISIBILITY, true);
this._requestUpdate();
};
/**
* Makes Mesh hidden
*
* @method
*
* @return {undefined} undefined
*/
Mesh.prototype.onHide = function onHide () {
this._changeQueue.push(Commands.GL_MESH_VISIBILITY, false);
this._requestUpdate();
};
/**
* Receives transform change updates from the scene graph.
*
* @method
* @private
*
* @param {Array} transform Transform matric
*
* @return {undefined} undefined
*/
Mesh.prototype.onTransformChange = function onTransformChange (transform) {
if (this._initialized) {
this._changeQueue.push(Commands.GL_UNIFORMS);
this._changeQueue.push('u_transform');
this._changeQueue.push(transform.getWorldTransform());
}
this._requestUpdate();
};
/**
* Receives size change updates from the scene graph.
*
* @method
* @private
*
* @param {Number} x width of the Node the Mesh is attached to
* @param {Number} y height of the Node the Mesh is attached to
* @param {Number} z depth of the Node the Mesh is attached to
*
* @return {undefined} undefined
*/
Mesh.prototype.onSizeChange = function onSizeChange (x, y, z) {
if (this._initialized) {
this._changeQueue.push(Commands.GL_UNIFORMS);
this._changeQueue.push('u_size');
this._changeQueue.push([x, y, z]);
}
this._requestUpdate();
};
/**
* Receives opacity change updates from the scene graph.
*
* @method
* @private
*
* @param {Number} opacity Opacity
*
* @return {undefined} undefined
*/
Mesh.prototype.onOpacityChange = function onOpacityChange (opacity) {
if (this._initialized) {
this._changeQueue.push(Commands.GL_UNIFORMS);
this._changeQueue.push('u_opacity');
this._changeQueue.push(opacity);
}
this._requestUpdate();
};
/**
* Adds functionality for UI events (TODO)
*
* @method
*
* @param {String} UIEvent UI Event
*
* @return {undefined} undefined
*/
Mesh.prototype.onAddUIEvent = function onAddUIEvent (UIEvent) {
//TODO
};
/**
* Queues instance to be updated.
*
* @method
*
* @return {undefined} undefined
*/
Mesh.prototype._requestUpdate = function _requestUpdate () {
if (!this._requestingUpdate) {
this._node.requestUpdate(this._id);
this._requestingUpdate = true;
}
};
/**
* Initializes the mesh with appropriate listeners.
*
* @method
*
* @return {undefined} undefined
*/
Mesh.prototype.init = function init () {
this._initialized = true;
this.onTransformChange(TransformSystem.get(this._node.getLocation()));
var size = this._node.getSize();
this.onSizeChange(size[0], size[1], size[2]);
this.onOpacityChange(this._node.getOpacity());
this._requestUpdate();
};
/**
* Draws given Mesh's current state.
*
* @method
*
* @return {undefined} undefined
*/
Mesh.prototype.draw = function draw () {
this._inDraw = true;
this.init();
var value = this.getValue();
if (value.geometry != null) this.setGeometry(value.geometry);
if (value.color != null) this.setBaseColor(value.color);
if (value.glossiness.strength != null) this.setGlossiness.apply(this, value.glossiness.strength);
if (value.drawOptions != null) this.setDrawOptions(value.drawOptions);
if (value.flatShading != null) this.setFlatShading(value.flatShading);
if (value.expressions.normals != null) this.setNormals(value.expressions.normals);
if (value.expressions.baseColor != null) this.setBaseColor(value.expressions.baseColor);
if (value.expressions.glossiness != null) this.setGlossiness(value.expressions.glossiness);
if (value.expressions.positionOffset != null) this.setPositionOffset(value.expressions.positionOffset);
this._inDraw = false;
};
module.exports = Mesh;