-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathMesh.js
More file actions
434 lines (311 loc) · 12.5 KB
/
Mesh.js
File metadata and controls
434 lines (311 loc) · 12.5 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
/**
* 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 test = require('tape');
var Mesh = require('../Mesh');
var MockDispatch = require('./MockDispatch');
var MockColor = require('./MockColor');
var Commands = require('../../core/Commands');
var time = 0;
var node;
var mesh;
/*
* Helper function for creating a mesh using a dummy dispatch
*/
function createMesh(options) {
var dispatch = new MockDispatch();
return {
mesh: new Mesh(dispatch, options),
dispatch: dispatch
};
}
/*
* Dummy material expression
*/
var materialExpression = {
__isAMaterial__: true
};
/*
* Helper function for checking whether N number of strings (checkList)
* are contained in an array list (containerList)
*/
function contains(checkList, containerList) {
for(var i = 0, len = checkList.length; i < len; i++) {
if (!~containerList.indexOf(checkList[i])) return false;
}
return true;
}
test('Mesh', function(t) {
t.test('Time setup', function(t) {
time = 0;
Date.now = function() {
return time;
};
t.equal(typeof Date.now, 'function',
'should be a function');
time = 50;
t.equal(Date.now(), 50,
'should manipulate current time for testing');
time = 0;
t.equal(Date.now(), 0,
'should be able to set time to 0');
t.end();
});
t.test('createMesh helper', function(t) {
t.doesNotThrow(createMesh,
'should not throw an error');
t.end();
});
t.test('contains helper', function(t) {
var checkList = ['one', 'two'];
var notIncluded = ['notIncluded'];
var containerList = ['one', 'two', 'three', 'four'];
t.true(contains(checkList, containerList),
'should pass for all string lists that are included');
t.false(contains(notIncluded, containerList),
'should fail for string lists that are not included');
t.end();
});
t.test('Mesh constructor', function(t) {
t.equal(typeof Mesh, 'function',
'should be a function');
t.throws(function() {
mesh = new Mesh();
}, 'should throw an error if a node is not provided');
mesh = createMesh().mesh;
t.equal(typeof mesh.setBaseColor, 'function',
'mesh should be instantiated without errors');
mesh = createMesh('randomOption').mesh;
t.true(contains([Commands.GL_SET_DRAW_OPTIONS, 'randomOption'], mesh._changeQueue),
'should take options');
t.end();
});
t.test('Mesh.prototype.setDrawOptions', function(t) {
mesh = createMesh().mesh;
t.equal(typeof mesh.setDrawOptions, 'function',
'should be a function');
t.false(~mesh._changeQueue.indexOf('notPassedIn'),
'should not contain false options');
mesh.setDrawOptions('randomOption');
t.true(contains([Commands.GL_SET_DRAW_OPTIONS, 'randomOption'], mesh._changeQueue),
'should take options');
t.false(contains([Commands.GL_SET_DRAW_OPTIONS, 'notIncluded'], mesh._changeQueue),
'should not pass fake options');
t.end();
});
t.test('Mesh.prototype.onTransformChange', function(t) {
node = createMesh();
mesh = node.mesh;
t.equal(typeof mesh.onTransformChange, 'function',
'should be a function');
var transform = new Array(16);
mesh.onSizeChange(transform);
t.ok(contains(mesh._changeQueue, [Commands.GL_UNIFORMS, 'u_transform', transform]),
'should enqueue transform changes');
t.end();
});
t.test('Mesh.prototype.onSizeChange', function(t) {
node = createMesh();
mesh = node.mesh;
t.equal(typeof mesh.onSizeChange, 'function',
'should be a function');
mesh.onSizeChange('none');
t.ok(contains(mesh._changeQueue, [Commands.GL_UNIFORMS, 'u_size', 'none']),
'should enqueue size changes');
t.end();
});
t.test('Mesh.prototype.onOpacityChange', function(t) {
node = createMesh();
mesh = node.mesh;
t.equal(typeof mesh.onOpacityChange, 'function',
'should be a function');
mesh.onOpacityChange(5);
t.ok(contains(mesh._changeQueue, [Commands.GL_UNIFORMS, 'u_opacity', 5]),
'should enqueue opacity changes');
t.end();
});
t.test('Mesh.prototype.setGeometry', function(t) {
mesh = createMesh().mesh;
t.equal(typeof mesh.setGeometry, 'function',
'should be a function');
t.throws(function() {
mesh.setGeometry('notAGeometry');
}, 'throws an error for geometry string references that are not included');
t.doesNotThrow(function() {
mesh._initialized = true;
mesh.setGeometry('Sphere');
}, 'accepts string references for geometries');
var geometry = mesh.value.geometry;
t.true(contains([Commands.GL_SET_GEOMETRY, geometry.spec.id, geometry.spec.type, geometry.spec.dynamic], mesh._changeQueue),
'sends the appropriate commands for geometry');
t.end();
});
t.test('Mesh.prototype.getGeometry', function(t) {
mesh = createMesh().mesh;
t.equal(typeof mesh.getGeometry, 'function',
'should be a function');
t.true(mesh.getGeometry(),
'should return a default geometry of plane if none was set');
mesh.setGeometry('Box');
t.true(mesh.getGeometry(),
'should return a geometry if one has been set');
t.end();
});
t.test('Mesh.prototype._pushInvalidations', function(t) {
mesh = createMesh().mesh;
t.equal(typeof mesh._pushInvalidations, 'function',
'should be a function');
t.end();
});
t.test('Mesh.prototype.setBaseColor', function(t) {
mesh = createMesh().mesh;
mesh._initialized = true;
t.equal(typeof mesh.setBaseColor, 'function',
'should be a function');
t.false(mesh.value.expressions.baseColor,
'should not have a material expression by default');
mesh.setBaseColor(materialExpression);
t.true(contains([materialExpression], mesh._changeQueue),
'should be able to take a material expression');
t.false(mesh._color,
'should not have a color by default');
var length = mesh._changeQueue.length;
var c = new MockColor();
mesh.setBaseColor(c);
t.true(length < mesh._changeQueue.length, 'should be able to take a Color instance');
t.end();
});
t.test('Mesh.prototype.getBaseColor', function(t) {
mesh = createMesh().mesh;
t.equal(typeof mesh.getBaseColor, 'function',
'should be a function');
mesh.setBaseColor(materialExpression);
t.true(mesh.getBaseColor().__isAMaterial__,
'should be able to take a material expression');
t.end();
});
t.test('Mesh.prototype.setFlatShading', function(t) {
mesh = createMesh().mesh;
t.equal(typeof mesh.setFlatShading, 'function',
'should be a function');
t.false(mesh._flatShading,
'should be false by default');
mesh.setFlatShading(true);
t.true(mesh.value.flatShading,
'should be true when set to true');
mesh.setFlatShading(false);
t.false(mesh.value.flatShading,
'should be false when set to false');
t.end();
});
t.test('Mesh.prototype.getFlatshading', function(t) {
mesh = createMesh().mesh;
t.equal(typeof mesh.getFlatShading, 'function',
'should be a function');
t.false(mesh._flatShading,
'should be false by default');
mesh.setFlatShading(true);
t.true(mesh.getFlatShading(),
'should return true when set to true');
mesh.setFlatShading(false);
t.false(mesh.getFlatShading(),
'should return false when set to false');
t.end();
});
t.test('Mesh.prototype.setNormals', function(t) {
mesh = createMesh().mesh;
mesh._initialized = true;
t.equal(typeof mesh.setNormals, 'function',
'should be a function');
mesh.setNormals(materialExpression);
t.true(contains([Commands.MATERIAL_INPUT, 'u_normals', materialExpression], mesh._changeQueue),
'should be able to take a normal material expression');
t.end();
});
t.test('Mesh.prototype.getNormals', function(t) {
mesh = createMesh().mesh;
t.equal(typeof mesh.getNormals, 'function',
'should be a function');
mesh.setNormals(materialExpression);
t.true(mesh.getNormals().__isAMaterial__,
'should be able to return the Normals expression');
t.end();
});
t.test('Mesh.prototype.setGlossiness', function(t) {
time = 0;
mesh = createMesh().mesh;
mesh._initialized = true;
t.equal(typeof mesh.setGlossiness, 'function',
'should be a function');
mesh.setGlossiness(materialExpression);
t.true(contains([Commands.MATERIAL_INPUT, 'u_glossiness', materialExpression], mesh._changeQueue),
'should take a material expression for glossiness');
mesh.setGlossiness(new MockColor(), 10);
t.equal(typeof mesh.value.glossiness, 'object',
'should take constants for setting glossiness');
t.end();
});
t.test('Mesh.prototype.getGlossiness', function(t) {
mesh = createMesh().mesh;
t.equal(typeof mesh.getGlossiness, 'function',
'should be a function');
mesh.setGlossiness(materialExpression);
t.true(mesh.getGlossiness().__isAMaterial__,
'should be able to return the glossiness expression');
var x = new MockColor();
mesh.setGlossiness(10, x);
t.equal(mesh.getGlossiness().strength, 10,
'should be able to return the glossiness strength');
t.equal(mesh.getGlossiness().color, x,
'should be able to return the glossiness color');
t.end();
});
t.test('Mesh.prototype.setPositionOffset', function(t) {
time = 0;
mesh = createMesh().mesh;
mesh._initialized = true;
t.equal(typeof mesh.setPositionOffset, 'function',
'should be a function');
mesh.setPositionOffset(materialExpression);
t.true(contains([Commands.MATERIAL_INPUT, 'u_positionOffset', materialExpression], mesh._changeQueue),
'should take a material expression for positionOffset');
mesh.setPositionOffset([.5, .2, .1]);
t.equal(typeof mesh.value.positionOffset, 'object',
'should take a constant for setting positionOffset');
t.end();
});
t.test('Mesh.prototype.getPositionOffset', function(t) {
time = 0;
mesh = createMesh().mesh;
t.equal(typeof mesh.getPositionOffset, 'function',
'should be a function');
mesh.setPositionOffset(materialExpression);
t.true(mesh.getPositionOffset().__isAMaterial__,
'should be able to return the glossiness expression');
mesh.setPositionOffset([10, 10, 10]);
t.deepEqual(mesh.getPositionOffset(), [10, 10, 10],
'should be able to return the glossiness value');
t.end();
});
});