-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathwebgl-scenegraph.js
641 lines (559 loc) · 26.1 KB
/
webgl-scenegraph.js
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
module("WebGL Scenegraph", {});
test("Test mesh.getLocalBoundingBox", 2, function () {
stop();
var frameLoaded = Q.fcall(promiseIFrameLoaded, "scenes/webgl-rendering01.html");
var test = frameLoaded.then(function (doc) {
var group = doc.getElementById("myTransformedGroup");
group.style.display = 'inherit';
return doc.getElementById("xml3DElem");
}).then(promiseSceneRendered).then(function (s) {
var doc = s.ownerDocument;
var mesh = doc.getElementById("myTransformedMesh");
var bb = mesh.getLocalBoundingBox();
QUnit.closeArray(bb.data, [1, 1, 0, -1, -1, 0], "Local box is has max [1, 1, 0] and min [-1, -1, 0]");
return s;
});
test.fin(QUnit.start).done();
});
test("Test mesh.getWorldBoundingBox", 2, function () {
stop();
var frameLoaded = Q.fcall(promiseIFrameLoaded, "scenes/webgl-rendering01.html");
var test = frameLoaded.then(function (doc) {
var group = doc.getElementById("myTransformedGroup");
group.style.display = 'inherit';
return doc.getElementById("xml3DElem");
}).then(promiseSceneRendered).then(function (s) {
var doc = s.ownerDocument;
var mesh = doc.getElementById("myTransformedMesh");
var bb = mesh.getWorldBoundingBox();
QUnit.closeArray(bb.data, [1, 1, -3, -1, -1, -3], "World box has max [1, 1, -3] and min [-1, -1, -3]");
return s;
});
test.fin(QUnit.start).done();
});
test("Change active view via script", 6, function () {
stop();
var frameLoaded = Q.fcall(promiseIFrameLoaded, "scenes/webgl-rendering01.html");
var test = frameLoaded.then(function (doc) {
var s = doc.getElementById("xml3DElem");
equal(s.view, "#defaultView", "Initial active view is #default"); // 3
var v = doc.getElementById("defaultView");
var m = XML3D.math.mat4.create();
QUnit.closeMatrix(v.getViewMatrix(), XML3D.math.mat4.translate(m, m, [0, 0, -3]), EPSILON, "Check view matrix"); // 4
s.view = "#viewOrientationTest";
XML3D.flushDOMChanges();
equal(s.view, "#viewOrientationTest", "New active view is #viewOrientationTest"); // 5
var h = getHandler(s);
ok(h.renderer.needsDraw, "Redraw required");
return s;
}).then(promiseSceneRendered).then(function (s) {
var h = getHandler(s);
ok(!h.renderer.needsDraw, "Redraw not required");
});
test.fin(QUnit.start).done();
});
test("Simple add/remove mesh", 11, function () {
stop();
var frameLoaded = Q.fcall(promiseIFrameLoaded, "scenes/webgl-rendering01.html");
var scene, mesh, renderNode;
var test = frameLoaded.then(function (doc) {
var s = doc.getElementById("xml3DElem");
mesh = doc.createElement("mesh");
mesh.setAttribute("src", "#meshdata");
// Add a mesh
s.appendChild(mesh);
renderNode = getWebGLAdapter(mesh).renderNode;
scene = renderNode.scene;
equal(scene.ready.indexOf(renderNode), -1, "renderNode not in 'Ready' before draw");
return s;
}).then(promiseSceneRendered).then(function (s) {
notEqual(scene.ready.indexOf(renderNode), -1, "renderNode in 'Ready' after draw");
var actual = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 40, 40);
deepEqual(actual, [255, 0, 0, 255], "Red at 40,40 [add mesh]");
// Remove the mesh
s.removeChild(mesh);
return s;
}).then(promiseSceneRendered).then(function (s) {
var adapter = getWebGLAdapter(mesh);
ok(!getWebGLAdapter(mesh), "Mesh adapter is removed");
equal(scene.ready.indexOf(renderNode), -1, "renderNode not in ready list after dispose");
equal(scene.queue.indexOf(renderNode), -1, "renderNode not in queue list after dispose");
var actual = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 40, 40);
deepEqual(actual, [0, 0, 0, 0], "Transparent at 40,40 [remove mesh]");
s.appendChild(mesh);
equal(scene.ready.indexOf(renderNode), -1, "renderNode not in 'Ready' before draw");
return s;
}).then(promiseSceneRendered).then(function (s) {
notEqual(scene.ready.indexOf(getWebGLAdapter(mesh).renderNode), -1, "renderNode in 'Ready' after draw");
var actual = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 40, 40);
deepEqual(actual, [255, 0, 0, 255], "Red at 40,40 [re-add mesh]");
return s;
});
test.fin(QUnit.start).done();
});
module("WebGL Scenegraph", {
setup: function () {
stop();
var that = this;
this.cb = function (e) {
ok(true, "Scene loaded");
that.win = document.getElementById("xml3dframe").contentWindow;
that.doc = document.getElementById("xml3dframe").contentDocument;
start();
};
loadDocument("scenes/webgl-rendering01.html" + window.location.search, this.cb);
}, teardown: function () {
var v = document.getElementById("xml3dframe");
v.removeEventListener("load", this.cb, true);
}, isRenderNodeInScene: function (name, scene) {
var found = false;
scene.traverse(function (node) {
found = found || node.name == name;
});
return found;
}
});
function getCanvasId(x) {
var factory = getWebGLFactory(x);
return factory ? factory.canvasId : null;
}
function getRenderer(x) {
//console.dir(x);
var factory = getWebGLFactory(x);
return factory ? factory.getRenderer() : null;
};
function getHandler(x) {
//console.dir(x);
var renderer = getRenderer(x);
return renderer ? renderer._canvasHandler : null;
};
function getContextForXml3DElement(x) {
var renderer = getRenderer(x);
return renderer ? renderer.context.gl : null;
};
test("Simple add/remove group with mesh", 10, function () {
var x = this.doc.getElementById("xml3DElem"), actual, win = this.doc.defaultView;
var gl = getContextForXml3DElement(x);
var h = getHandler(x);
var mesh = this.doc.createElementNS("http://www.xml3d.org/2009/xml3d", "mesh");
mesh.setAttribute("src", "#meshdata");
var group = this.doc.createElementNS("http://www.xml3d.org/2009/xml3d", "group");
group.appendChild(mesh);
//console.log(getWebGLAdapter(mesh).renderNode.is("NoLights"));
// Add group
x.appendChild(group);
var renderNode = getWebGLAdapter(mesh).renderNode;
var scene = renderNode.scene;
equal(scene.ready.indexOf(renderNode), -1, "renderNode not in 'Ready' before draw");
h.draw();
notEqual(scene.ready.indexOf(renderNode), -1, "renderNode not in 'Ready' before draw");
actual = win.getPixelValue(gl, 40, 40);
deepEqual(actual, [255, 0, 0, 255], "Red at 40,40 [add group with mesh]");
var adapter = getWebGLAdapter(mesh);
// Remove group
x.removeChild(group);
ok(!getWebGLAdapter(group), "Group adapter is removed");
h.draw();
actual = win.getPixelValue(gl, 40, 40);
deepEqual(actual, [0, 0, 0, 0], "Transparent at 40,40 [remove group]");
// Re-add group
x.appendChild(group);
equal(scene.ready.indexOf(renderNode), -1, "renderNode not in 'Ready' before draw");
h.draw();
notEqual(scene.ready.indexOf(renderNode), 1, "renderNode not in 'Ready' before draw");
actual = win.getPixelValue(gl, 40, 40);
deepEqual(actual, [255, 0, 0, 255], "Red at 40,40 [add group with mesh]");
});
test("Simple add/remove transformed group with mesh", 17, function () {
var x = this.doc.getElementById("xml3DElem"), actual, win = this.doc.defaultView;
var gl = getContextForXml3DElement(x);
var h = getHandler(x);
x.setAttribute("view", "#identView");
var mesh = this.doc.createElementNS("http://www.xml3d.org/2009/xml3d", "mesh");
mesh.setAttribute("id", "addedMesh");
mesh.setAttribute("src", "#meshdata");
var group = this.doc.createElementNS("http://www.xml3d.org/2009/xml3d", "group");
group.setAttribute("id", "addedGroup");
group.setAttribute("transform", "#t_grouptransformed");
group.appendChild(mesh);
//console.log(getWebGLAdapter(mesh).renderNode.is("NoLights"));
// Add group
x.appendChild(group);
var renderNode = getWebGLAdapter(mesh).renderNode;
var scene = renderNode.scene;
ok(this.isRenderNodeInScene("addedMesh", scene), "Render node in scene");
ok(this.isRenderNodeInScene("addedGroup", scene), "Group in scene");
equal(scene.ready.indexOf(renderNode), -1, "renderNode not in 'Ready' before draw");
h.draw();
notEqual(scene.ready.indexOf(renderNode), -1, "renderNode in 'Ready' after draw");
actual = win.getPixelValue(gl, 40, 40);
deepEqual(actual, [255, 0, 0, 255], "Red at 40,40 [add group with mesh]");
// Remove group
var adapter = getWebGLAdapter(mesh);
x.removeChild(group);
this.win.XML3D.flushDOMChanges();
ok(!this.isRenderNodeInScene("addedMesh", scene), "Mesh not in scene");
ok(!this.isRenderNodeInScene("addedGroup", scene), "Group not in scene");
ok(!getWebGLAdapter(group), "Group adapter is removed");
ok(!getWebGLAdapter(mesh), "Mesh adapter is removed");
//ok(adapter.renderNode.is("Disposed"), "renderNode in 'Disposed' after removal");
h.draw();
actual = win.getPixelValue(gl, 40, 40);
deepEqual(actual, [0, 0, 0, 0], "Transparent at 40,40 [remove group]");
// Re-add group
x.appendChild(group);
this.win.XML3D.flushDOMChanges();
ok(this.isRenderNodeInScene("addedMesh", scene), "Render node in scene");
ok(this.isRenderNodeInScene("addedGroup", scene), "Group in scene");
var renderNode = getWebGLAdapter(mesh).renderNode;
equal(scene.ready.indexOf(renderNode), -1, "renderNode not in 'Ready' before draw");
h.draw();
notEqual(scene.ready.indexOf(renderNode), -1, "renderNode not in 'Ready' before draw");
actual = win.getPixelValue(gl, 40, 40);
deepEqual(actual, [255, 0, 0, 255], "Red at 40,40 [add group with mesh]");
});
module("WebGL Scenegraph", {});
test("Add invisible groups and meshes", 6, function () {
stop();
var frameLoaded = Q.fcall(promiseIFrameLoaded, "scenes/webgl-rendering01.html");
var mesh, group;
var test = frameLoaded.then(function (doc) {
var scene = doc.querySelector("#xml3DElem");
mesh = doc.createElement("mesh");
mesh.setAttribute("src", "#meshdata");
mesh.setAttribute("style", "display: none;");
// Add a mesh
scene.appendChild(mesh);
return scene;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 40, 40);
QUnit.closeArray(pick, [0, 0, 0, 0], PIXEL_EPSILON, "Mesh is invisible");
return s;
}).then(function (s) {
mesh.style.display = "inherit";
return s;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 40, 40);
QUnit.closeArray(pick, [255, 0, 0, 255], PIXEL_EPSILON, "Mesh is now visible");
return s;
}).then(function (s) {
var doc = s.ownerDocument;
s.removeChild(mesh);
group = doc.createElement("group");
group.setAttribute("style", "display: none;");
group.appendChild(mesh);
s.appendChild(group);
return s;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 40, 40);
QUnit.closeArray(pick, [0, 0, 0, 0], PIXEL_EPSILON, "Group is invisible");
return s;
}).then(function (s) {
group.style.display = "inherit";
return s;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 40, 40);
QUnit.closeArray(pick, [255, 0, 0, 255], PIXEL_EPSILON, "Group is visible");
return s;
}).then(function (s) {
mesh.style.display = "none";
return s;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 40, 40);
QUnit.closeArray(pick, [0, 0, 0, 0], PIXEL_EPSILON, "Mesh is invisible");
return s;
});
test.fin(QUnit.start).done();
});
test("Change material via script", 5, function () {
stop();
var frameLoaded = Q.fcall(promiseIFrameLoaded, "scenes/webgl-rendering01.html");
var group;
var test = frameLoaded.then(function (doc) {
var scene = doc.querySelector("#xml3DElem");
group = doc.getElementById("myGroup");
group.style.display = 'inherit';
return scene;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 40, 40);
QUnit.closeArray(pick, [255, 0, 0, 255], PIXEL_EPSILON, "Red at 40,40 [visible true]");
return s;
}).then(function (s) {
group.setAttribute("material", "#flatblue");
return s;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 40, 40);
QUnit.closeArray(pick, [0, 0, 255, 255], PIXEL_EPSILON, "Blue at 40,40 [flat material]");
return s;
}).then(function (s) {
group.setAttribute("material", "#phonggreen");
return s;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 40, 40);
QUnit.closeArray(pick, [0, 0, 0, 255], PIXEL_EPSILON, "[phong material, no light, no emission]");
return s;
}).then(function (s) {
var doc = s.ownerDocument;
var materialColor = doc.getElementById("phonggreen_emissive");
materialColor.textContent = "0 0 1";
return s;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 40, 40);
QUnit.closeArray(pick, [0, 0, 255, 255], PIXEL_EPSILON, "Blue at 40,40 [set emissive color]");
return s;
});
test.fin(QUnit.start).done();
});
test("Change visible/material for nested groups", 7, function () {
stop();
var frameLoaded = Q.fcall(promiseIFrameLoaded, "scenes/webgl-rendering01.html");
var outerGroup, innerGroup;
var test = frameLoaded.then(function (doc) {
var scene = doc.querySelector("#xml3DElem");
outerGroup = doc.getElementById("group1");
innerGroup = doc.getElementById("group2");
innerGroup.setAttribute("style", "display: inherit;");
return scene;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 40, 40);
QUnit.closeArray(pick, [0, 0, 0, 0], PIXEL_EPSILON, "Transparent");
return s;
}).then(function (s) {
innerGroup.setAttribute("style", "display: none;");
outerGroup.setAttribute("style", "display: inherit;");
return s;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 40, 40);
QUnit.closeArray(pick, [0, 0, 0, 0], PIXEL_EPSILON, "Transparent at 40,40 [child group's visible overrides parent]");
return s;
}).then(function (s) {
innerGroup.setAttribute("style", "display: inherit;");
return s;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 40, 40);
QUnit.closeArray(pick, [0, 0, 255, 255], PIXEL_EPSILON, "Blue at 40,40 [both visible, child material overrides parent]");
return s;
}).then(function (s) {
innerGroup.setAttribute("material", "");
return s;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 40, 40);
QUnit.closeArray(pick, [255, 255, 0, 255], PIXEL_EPSILON, "Yellow at 40,40 [remove child material, #flatYellow active]");
return s;
}).then(function (s) {
innerGroup.setAttribute("material", "#flatblue");
return s;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 40, 40);
QUnit.closeArray(pick, [0, 0, 255, 255], PIXEL_EPSILON, "Blue at 40,40 [re-add child material]");
return s;
}).then(function (s) {
outerGroup.setAttribute("material", "#flatblack");
return s;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 40, 40);
QUnit.closeArray(pick, [0, 0, 255, 255], PIXEL_EPSILON, "Blue at 40,40 [child material overrides new parent material]");
return s;
});
test.fin(QUnit.start).done();
});
test("Camera with group transforms", 6, function () {
stop();
var frameLoaded = Q.fcall(promiseIFrameLoaded, "scenes/webgl-rendering01.html");
var camtest, group5, camTransform, view;
var test = frameLoaded.then(function (doc) {
var scene = doc.querySelector("#xml3DElem");
camTransform = doc.getElementById("t_camera1");
view = doc.getElementById("transformTestView");
camtest = doc.getElementById("viewTest");
group5 = doc.getElementById("group5");
//Simple test of camera orientation to check that view matrix is built correctly
camtest.style.display = 'inherit';
scene.setAttribute("view", "#viewOrientationTest");
return scene;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 90, 90);
QUnit.closeArray(pick, [255, 0, 0, 255], PIXEL_EPSILON, "Simple camera orientation check, Red");
return s;
}).then(function (s) {
camtest.style.display = 'none';
group5.style.display = 'inherit';
s.setAttribute("view", "#transformTestView");
return s;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 90, 90);
QUnit.closeArray(pick, [255, 255, 0, 255], PIXEL_EPSILON, "Camera facing back of cube, Yellow");
return s;
}).then(function (s) {
camTransform.setAttribute("rotation", "1 0 0 1.57");
camTransform.setAttribute("translation", "0 3 0");
return s;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 90, 90);
QUnit.closeArray(pick, [0, 0, 255, 255], PIXEL_EPSILON, "Camera facing top of cube, Blue")
return s;
}).then(function (s) {
view.setAttribute("style", "transform: rotate3d(0, 1, 0, 0deg)");
camTransform.setAttribute("translation", "0 -6 0");
return s;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 90, 90);
QUnit.closeArray(pick, [255, 0, 255, 255], PIXEL_EPSILON, "Camera facing bottom of cube, Purple")
return s;
}).then(function (s) {
view.setAttribute("style", "");
return s;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 90, 90);
QUnit.closeArray(pick, [255, 0, 255, 255], PIXEL_EPSILON, "Camera with orientation '0 0 0 0', Purple")
return s;
});
test.fin(QUnit.start).done();
});
test("Nested transforms", 7, function () {
stop();
var frameLoaded = Q.fcall(promiseIFrameLoaded, "scenes/webgl-rendering01.html");
var test = frameLoaded.then(function (doc) {
var scene = doc.querySelector("#xml3DElem");
var group3 = doc.getElementById("group3");
var group4 = doc.getElementById("group4");
group3.style.display = 'inherit';
return scene;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 90, 90);
QUnit.closeArray(pick, [255, 0, 0, 255], PIXEL_EPSILON, "Red at 90,90 [nested y-axis rotations]");
return s;
}).then(function (s) {
var doc = s.ownerDocument;
var t3 = doc.getElementById("t_group3");
t3.setAttribute("rotation", "0 1 0 0");
return s;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 90, 90);
QUnit.closeArray(pick, [0, 0, 0, 0], PIXEL_EPSILON, "Transparent at 90,90 [outer rotation 0, inner 90]");
return s;
}).then(function (s) {
// Rotate both inner and outer around y-axis 90 degrees, should flip the shape around
var doc = s.ownerDocument;
var t3 = doc.getElementById("t_group3");
t3.setAttribute("rotation", "0 1 0 1.57");
return s;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 90, 90);
QUnit.closeArray(pick, [255, 0, 0, 255], PIXEL_EPSILON, "Red at 90,90 [set outer rotation 90]");
return s;
}).then(function (s) {
// Rotate both inner and outer around y-axis 90 degrees, should flip the shape around
var doc = s.ownerDocument;
var t4 = doc.getElementById("t_group4");
// Rotate inner -90 around local x-axis (actually around z since we've rotated 90 around x first)
t4.setAttribute("rotation", "1 0 0 -1.57");
return s;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 90, 90);
QUnit.closeArray(pick, [0, 0, 0, 0], PIXEL_EPSILON, "Transparent at 90,90 [inner x-axis rotation -90]");
return s;
}).then(function (s) {
// Rotate both inner and outer around y-axis 90 degrees, should flip the shape around
var doc = s.ownerDocument;
var t3 = doc.getElementById("t_group3");
// Rotate outer 90 around x-axis, this should undo the -90 x-axis rotation of inner
t3.setAttribute("rotation", "1 0 0 1.57");
return s;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 90, 90);
QUnit.closeArray(pick, [255, 0, 0, 255], PIXEL_EPSILON, "Red at 90,90 [cancel inner rotation with outer]");
return s;
}).then(function (s) {
// Rotate both inner and outer around y-axis 90 degrees, should flip the shape around
var doc = s.ownerDocument;
var t4 = doc.getElementById("t_group4");
// Rotate outer 90 around x-axis, this should undo the -90 x-axis rotation of inner
t4.setAttribute("translation", "3 0 0");
return s;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 90, 90);
QUnit.closeArray(pick, [0, 0, 0, 0], PIXEL_EPSILON, "Transparent at 90,90 [add inner translation]");
return s;
});
test.fin(QUnit.start).done();
});
test("Camera setDirection/upVector", 2, function () {
stop();
var frameLoaded = Q.fcall(promiseIFrameLoaded, "scenes/webgl-rendering01.html");
var view;
var test = frameLoaded.then(function (doc) {
var scene = doc.querySelector("#xml3DElem");
var group5 = doc.getElementById("group5");
var camtest = doc.getElementById("viewTest");
view = doc.getElementById("viewOrientationTest");
camtest.style.display = 'inherit';
view.setAttribute("style", "transform: rotate3d(0, 0, 1, 0deg)");
scene.setAttribute("view", "#viewOrientationTest");
return scene;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 90, 90);
QUnit.closeArray(pick, [0, 0, 0, 0], PIXEL_EPSILON, "Camera looking down z axis, transparent");
return s;
});/*.then(function (s) {
view.setDirection(XML3D.Vec3.fromValues(1, 0, 0));
return s;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 90, 90);
QUnit.closeArray(pick, [255, 0, 0, 255], PIXEL_EPSILON, "Camera looking to the right, red");
return s;
}).then(function (s) {
view.setUpVector(XML3D.Vec3.fromValues(0, -1, 0));
view.setDirection(XML3D.Vec3.fromValues(-1, 0, 0));
return s;
}).then(promiseSceneRendered).then(function (s) {
var pick = XML3DUnit.getPixelValue(getContextForXml3DElement(s), 90, 90);
QUnit.closeArray(pick, [255, 255, 0, 255], PIXEL_EPSILON, "Camera looking left, yellow");
return s;
});*/
test.fin(QUnit.start).done();
});
test("Add a mesh dynamically", 3, function () {
stop();
var frameLoaded = Q.fcall(promiseIFrameLoaded, "scenes/webgl-rendering01.html");
var d, g;
var test = frameLoaded.then(function (doc) {
d = doc;
var scene = doc.querySelector("#xml3DElem");
g = doc.getElementById("myGroup");
g.style.display = 'inherit';
return scene;
}).then(promiseSceneRenderedEvent).then(function (evt) {
equal(evt.detail.count.objects, 1, "Initially one drawable object");
return evt.target;
}).then(function (s) {
var group = d.createElement("group");
var mesh = d.createElement("mesh");
mesh.setAttribute('id', "new_mesh");
mesh.setAttribute('src', "#meshdata");
g.appendChild(group);
group.appendChild(mesh);
return s;
}).then(promiseSceneRenderedEvent).then(function (evt) {
equal(evt.detail.count.objects, 2, "Now two drawable objects");
});
test.fin(QUnit.start).done();
});
test("Add xml3d element inside a div", 2, function() {
stop();
var frameLoaded = Q.fcall(promiseIFrameLoaded, "scenes/webgl-rendering01.html");
var test = frameLoaded.then(function (doc) {
var x = doc.getElementById("xml3DElem");
x.parentNode.removeChild(x);
var div = doc.createElement("div");
var xml3d = doc.createElement("xml3d");
xml3d.setAttribute("style", "background-color:green; width: 200px; height: 200px;");
div.appendChild(xml3d);
doc.querySelector("body").appendChild(div);
xml3d.addEventListener("framedrawn", function() {
ok(true, "New XML3D element was initialized");
});
return xml3d;
});
test.fin(QUnit.start).done();
});