forked from aframevr/aframe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaterial.test.js
More file actions
426 lines (378 loc) · 17 KB
/
material.test.js
File metadata and controls
426 lines (378 loc) · 17 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
/* global assert, setup, suite, test, sinon, AFRAME */
import { entityFactory } from '../helpers.js';
import { shaders } from 'core/shader.js';
import THREE from 'lib/three.js';
var IMG_SRC = '/base/tests/assets/test.png';
suite('material', function () {
var el;
setup(function (done) {
el = entityFactory();
this.sinon = sinon.createSandbox();
el.setAttribute('geometry', '');
el.setAttribute('material', 'shader: flat');
if (el.hasLoaded) { done(); }
el.addEventListener('loaded', function (evt) {
done();
});
});
suite('update', function () {
test('creates material', function () {
assert.ok(el.getObject3D('mesh').material);
});
test('updates material', function () {
el.setAttribute('material', 'color: #F0F; side: double');
assert.shallowDeepEqual(el.getObject3D('mesh').material.color,
{r: 1, g: 0, b: 1});
assert.shallowDeepEqual(el.getObject3D('mesh').material.side, THREE.DoubleSide);
});
test('updates material shader', function () {
assert.equal(el.getObject3D('mesh').material.type, 'MeshBasicMaterial');
el.setAttribute('material', 'shader', 'standard');
assert.equal(el.getObject3D('mesh').material.type, 'MeshStandardMaterial');
});
test('disposes material when changing to new material', function () {
var material = el.getObject3D('mesh').material;
var disposeSpy = this.sinon.spy(material, 'dispose');
el.setAttribute('material', 'shader', 'standard');
assert.ok(disposeSpy.called);
});
test('disposes material when removing material', function () {
var material = el.getObject3D('mesh').material;
var disposeSpy = this.sinon.spy(material, 'dispose');
el.removeAttribute('material');
assert.ok(disposeSpy.called);
});
test('disposes textures when removing material', function () {
var material = el.getObject3D('mesh').material;
var texture1 = {uuid: 'tex1', isTexture: true, dispose: sinon.spy()};
var texture2 = {uuid: 'tex2', isTexture: true, dispose: sinon.spy()};
material.map = texture1;
material.normalMap = texture2;
el.removeAttribute('material');
assert.ok(texture1.dispose.called);
assert.ok(texture2.dispose.called);
});
test('disposes texture when removing texture', function (done) {
var imageUrl = 'base/tests/assets/test.png';
el.setAttribute('material', 'src: url(' + imageUrl + ')');
el.addEventListener('materialtextureloaded', function (evt) {
var loadedTexture = evt.detail.texture;
var disposeSpy = sinon.spy(loadedTexture, 'dispose');
el.setAttribute('material', 'src', '');
assert.ok(disposeSpy.called);
done();
});
});
test('defaults to standard material', function () {
el.removeAttribute('material'); // setup creates a non-default component
el.setAttribute('material', '');
assert.equal(el.getObject3D('mesh').material.type, 'MeshStandardMaterial');
});
test('does not recreate material for basic updates', function () {
var uuid = el.getObject3D('mesh').material.uuid;
el.setAttribute('material', 'color', '#F0F');
assert.equal(el.getObject3D('mesh').material.uuid, uuid);
});
test('can toggle material to flat shading', function () {
el.setAttribute('material', 'shader: flat');
el.setAttribute('material', 'shader: standard');
assert.equal(el.getObject3D('mesh').material.type, 'MeshStandardMaterial');
});
test('can unset fog', function () {
assert.ok(el.getObject3D('mesh').material.fog);
el.setAttribute('material', 'fog', false);
assert.notOk(el.getObject3D('mesh').material.fog);
});
test('emits event when loading texture', function (done) {
var imageUrl = 'base/tests/assets/test.png';
el.setAttribute('material', '');
assert.notOk(el.components.material.material.texture);
el.setAttribute('material', 'src: url(' + imageUrl + ')');
el.addEventListener('materialtextureloaded', function (evt) {
var loadedTexture = evt.detail.texture;
assert.ok(el.components.material.material.map === loadedTexture);
done();
});
});
test('can load canvas texture', function (done) {
var canvas = document.createElement('canvas');
canvas.setAttribute('id', 'canvas');
el.sceneEl.appendChild(canvas);
el.addEventListener('materialtextureloaded', function (evt) {
assert.equal(el.components.material.material.map.image, canvas);
done();
});
setTimeout(() => {
el.setAttribute('material', {src: '#canvas'});
});
});
test('updates texture when repeat changes', function (done) {
var imageUrl = 'base/tests/assets/test.png';
el.setAttribute('material', '');
assert.notOk(el.components.material.material.texture);
el.setAttribute('material', 'src: url(' + imageUrl + ')');
el.addEventListener('materialtextureloaded', function (evt) {
var loadedTexture = evt.detail.texture;
assert.ok(el.components.material.material.map === loadedTexture);
var version = loadedTexture.version;
el.setAttribute('material', 'repeat', '2 2');
assert.ok(el.components.material.material.map.version > version);
done();
});
});
test('removes texture when src attribute removed', function (done) {
var imageUrl = 'base/tests/assets/test.png';
el.setAttribute('material', '');
assert.notOk(el.components.material.material.texture);
el.setAttribute('material', 'src: url(' + imageUrl + ')');
el.addEventListener('materialtextureloaded', function (evt) {
var loadedTexture = evt.detail.texture;
assert.ok(el.components.material.material.map === loadedTexture);
el.removeAttribute('material', 'src');
assert.notOk(el.components.material.material.map);
done();
});
});
test('removes texture when src attribute is empty string', function (done) {
var imageUrl = 'base/tests/assets/test.png';
el.setAttribute('material', '');
assert.notOk(el.components.material.material.texture);
el.setAttribute('material', 'src: url(' + imageUrl + ')');
el.addEventListener('materialtextureloaded', function (evt) {
var loadedTexture = evt.detail.texture;
assert.ok(el.components.material.material.map === loadedTexture);
el.setAttribute('material', 'src', '');
assert.notOk(el.components.material.material.map);
done();
});
});
test('does not invoke XHR if passing <img>', function (done) {
var assetsEl = document.createElement('a-assets');
var img = document.createElement('img');
var imageLoaderSpy = this.sinon.spy(THREE.ImageLoader.prototype, 'load');
var textureLoaderSpy = this.sinon.spy(THREE.TextureLoader.prototype, 'load');
img.setAttribute('src', IMG_SRC);
img.setAttribute('id', 'foo');
THREE.Cache.clear();
assetsEl.appendChild(img);
el.sceneEl.appendChild(assetsEl);
// Adding the asset will add image:${IMG_SRC} in THREE.Cache when the img
// loading is complete with img.onload, without going through THREE.ImageLoader
assert.notOk(!!THREE.Cache.get(`image:${IMG_SRC}`));
el.addEventListener('materialtextureloaded', function () {
assert.notOk(imageLoaderSpy.called);
assert.notOk(textureLoaderSpy.called);
THREE.ImageLoader.prototype.load.restore();
THREE.TextureLoader.prototype.load.restore();
// load event is triggered after this materialtextureloaded callback
img.addEventListener('load', function () {
assert.equal(THREE.Cache.get(`image:${IMG_SRC}`), img);
THREE.Cache.clear();
done();
}, {once: true});
});
el.setAttribute('material', 'src', '#foo');
});
test('invokes XHR if <img> not cached', function (done) {
var imageLoaderSpy = this.sinon.spy(THREE.ImageLoader.prototype, 'load');
el.addEventListener('materialtextureloaded', function () {
assert.ok(imageLoaderSpy.called);
assert.ok(!!THREE.Cache.get(`image:${IMG_SRC}`));
THREE.ImageLoader.prototype.load.restore();
done();
});
// The image is loaded via the material system using THREE.ImageLoader
el.setAttribute('material', 'src', IMG_SRC);
});
test('sets material to MeshShaderMaterial for custom shaders', function () {
delete shaders.test;
AFRAME.registerShader('test', {});
assert.equal(el.getObject3D('mesh').material.type, 'MeshBasicMaterial');
el.setAttribute('material', 'shader', 'test');
assert.equal(el.getObject3D('mesh').material.type, 'ShaderMaterial');
});
});
suite('updateSchema', function () {
test('updates schema for flat shader', function () {
el.components.material.updateSchema({shader: 'flat'});
assert.ok(el.components.material.schema.color);
assert.ok(el.components.material.schema.fog);
assert.ok(el.components.material.schema.repeat);
assert.ok(el.components.material.schema.src);
assert.notOk(el.components.material.schema.metalness);
assert.notOk(el.components.material.schema.roughness);
assert.notOk(el.components.material.schema.envMap);
});
test('updates schema for custom shader', function () {
delete shaders.test;
AFRAME.registerShader('test', {
schema: {
color: {type: 'color'},
luminance: {default: 1}
}
});
el.setAttribute('material', 'shader', 'test');
assert.ok(el.components.material.schema.opacity);
assert.ok(el.components.material.schema.color);
assert.ok(el.components.material.schema.luminance);
assert.notOk(el.components.material.schema.src);
});
});
suite('remove', function () {
test('removes material', function () {
assert.ok(el.getObject3D('mesh').material);
el.removeAttribute('material');
assert.equal(el.getObject3D('mesh').material.type, 'MeshBasicMaterial');
});
test('disposes material', function () {
var material = el.getObject3D('mesh').material;
var disposeSpy = this.sinon.spy(material, 'dispose');
el.removeAttribute('material');
assert.ok(disposeSpy.called);
});
});
suite('side', function () {
test('can be set with initial material', function (done) {
var el = entityFactory();
el.setAttribute('geometry', '');
el.setAttribute('material', 'side: double');
el.addEventListener('loaded', function () {
assert.ok(el.getObject3D('mesh').material.side, THREE.DoubleSide);
done();
});
});
test('defaults to front side', function () {
assert.equal(el.getObject3D('mesh').material.side, THREE.FrontSide);
});
test('can be set to back', function () {
el.setAttribute('material', 'side: back');
assert.equal(el.getObject3D('mesh').material.side, THREE.BackSide);
});
test('can be set to double', function () {
el.setAttribute('material', 'side: double');
assert.equal(el.getObject3D('mesh').material.side, THREE.DoubleSide);
});
test('sets material.needsUpdate true if side switches from/to double', function () {
var oldMaterialVersion = el.getObject3D('mesh').material.version;
el.setAttribute('material', 'side: front');
assert.equal(el.getObject3D('mesh').material.version, oldMaterialVersion);
el.setAttribute('material', 'side: double');
assert.equal(el.getObject3D('mesh').material.version, oldMaterialVersion + 1);
el.setAttribute('material', 'side: front');
assert.equal(el.getObject3D('mesh').material.version, oldMaterialVersion + 2);
});
});
suite('transparent', function () {
test('can set transparent', function () {
assert.notOk(el.getObject3D('mesh').material.transparent);
el.setAttribute('material', 'opacity: 1; transparent: true');
assert.equal(el.getObject3D('mesh').material.opacity, 1);
assert.ok(el.getObject3D('mesh').material.transparent);
});
test('can be set to false', function () {
el.setAttribute('material', 'opacity: 1; transparent: false');
assert.equal(el.getObject3D('mesh').material.opacity, 1);
assert.notOk(el.getObject3D('mesh').material.transparent);
});
test('is inferred if opacity is less than 1', function () {
assert.notOk(el.getObject3D('mesh').material.transparent);
el.setAttribute('material', 'opacity: 0.75');
assert.equal(el.getObject3D('mesh').material.opacity, 0.75);
assert.ok(el.getObject3D('mesh').material.transparent);
});
});
suite('depthTest', function () {
test('can be set to false', function () {
assert.ok(el.getObject3D('mesh').material.depthTest);
el.setAttribute('material', 'depthTest: false');
assert.equal(el.getObject3D('mesh').material.depthTest, 0);
});
});
suite('depthWrite', function () {
test('can be set to false', function () {
assert.ok(el.getObject3D('mesh').material.depthWrite);
el.setAttribute('material', 'depthWrite: false');
assert.equal(el.getObject3D('mesh').material.depthWrite, 0);
});
});
suite('alphaTest', function () {
test('can be updated', function () {
assert.equal(el.getObject3D('mesh').material.alphaTest, 0);
el.setAttribute('material', 'alphaTest: 1.0');
assert.equal(el.getObject3D('mesh').material.alphaTest, 1);
});
test('sets material.needsUpdate true if alphaTest is updated', function () {
var oldMaterialVersion = el.getObject3D('mesh').material.version;
el.setAttribute('material', 'alphaTest: 0.0');
assert.equal(el.getObject3D('mesh').material.version, oldMaterialVersion);
el.setAttribute('material', 'alphaTest: 1.0');
// A-Frame sets needsUpdate once and THREE one more internally when setting alphaTest.
assert.equal(el.getObject3D('mesh').material.version, oldMaterialVersion + 2);
});
});
suite('vertexColors', function () {
test('defaults to no color', function () {
assert.equal(el.getAttribute('material').vertexColorsEnabled, false);
assert.equal(el.components.material.material.vertexColors, false);
});
test('can set vertex colors using vertexColorsEnabled property', function () {
var oldMaterialVersion = el.getObject3D('mesh').material.version;
el.setAttribute('material', 'vertexColorsEnabled', true);
assert.equal(el.components.material.material.vertexColors, true);
assert.equal(el.components.material.material.version, oldMaterialVersion + 1);
});
});
test('can set visible to false', function () {
assert.ok(el.getObject3D('mesh').material.visible);
el.setAttribute('material', 'visible: false');
assert.notOk(el.getObject3D('mesh').material.visible);
});
suite('blending', function () {
test('defaults to normal', function () {
assert.equal(el.getAttribute('material').blending, 'normal');
assert.equal(el.components.material.material.blending, THREE.NormalBlending);
});
test('can set to no blending', function () {
el.setAttribute('material', 'blending', 'none');
assert.equal(el.components.material.material.blending, THREE.NoBlending);
});
test('can set to additive', function () {
el.setAttribute('material', 'blending', 'additive');
assert.equal(el.components.material.material.blending, THREE.AdditiveBlending);
});
test('can set to subtractive', function () {
el.setAttribute('material', 'blending', 'subtractive');
assert.equal(el.components.material.material.blending, THREE.SubtractiveBlending);
});
test('can set to multiply', function () {
el.setAttribute('material', 'blending', 'multiply');
assert.equal(el.components.material.material.blending, THREE.MultiplyBlending);
});
});
suite('anisotropy', function () {
test('defaults to THREE.Texture.DEFAULT_ANISOTROPY', function (done) {
var imageUrl = 'base/tests/assets/test.png';
el.setAttribute('material', '');
assert.notOk(el.components.material.material.texture);
el.setAttribute('material', 'src: url(' + imageUrl + ')');
el.addEventListener('materialtextureloaded', function (evt) {
var loadedTexture = evt.detail.texture;
assert.ok(el.components.material.material.map === loadedTexture);
assert.strictEqual(loadedTexture.anisotropy, THREE.Texture.DEFAULT_ANISOTROPY);
done();
});
});
test('can set specific anisotropic filtering sample rate', function (done) {
var imageUrl = 'base/tests/assets/test.png';
el.setAttribute('material', '');
assert.notOk(el.components.material.material.texture);
el.setAttribute('material', 'src: url(' + imageUrl + '); anisotropy: 8');
el.addEventListener('materialtextureloaded', function (evt) {
var loadedTexture = evt.detail.texture;
assert.ok(el.components.material.material.map === loadedTexture);
assert.strictEqual(loadedTexture.anisotropy, 8);
done();
});
});
});
});