LayerGL.js
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
673
674
675
676
677
678
/**
* Provide WebGL layer to zrender. Which is rendered on top of clay.
*
*
* Relationship between zrender, LayerGL(renderer) and ViewGL(Scene, Camera, Viewport)
* zrender
* / \
* LayerGL LayerGL
* (renderer) (renderer)
* / \
* ViewGL ViewGL
*/
import * as echarts from 'echarts/lib/echarts';
import Renderer from 'claygl/src/Renderer';
import RayPicking from 'claygl/src/picking/RayPicking';
import Texture from 'claygl/src/Texture';
import graphicGL from '../util/graphicGL';
// PENDING, clay. notifier is same with zrender Eventful
import notifier from 'claygl/src/core/mixin/notifier';
import requestAnimationFrame from 'zrender/lib/animation/requestAnimationFrame';
/**
* @constructor
* @alias module:echarts-gl/core/LayerGL
* @param {string} id Layer ID
* @param {module:zrender/ZRender} zr
*/
var LayerGL = function (id, zr) {
/**
* Layer ID
* @type {string}
*/
this.id = id;
/**
* @type {module:zrender/ZRender}
*/
this.zr = zr;
/**
* @type {clay.Renderer}
*/
try {
this.renderer = new Renderer({
clearBit: 0,
devicePixelRatio: zr.painter.dpr,
preserveDrawingBuffer: true,
// PENDING
premultipliedAlpha: true
});
this.renderer.resize(zr.painter.getWidth(), zr.painter.getHeight());
}
catch (e) {
this.renderer = null;
this.dom = document.createElement('div');
this.dom.style.cssText = 'position:absolute; left: 0; top: 0; right: 0; bottom: 0;';
this.dom.className = 'ecgl-nowebgl';
this.dom.innerHTML = 'Sorry, your browser does not support WebGL';
console.error(e);
return;
}
this.onglobalout = this.onglobalout.bind(this);
zr.on('globalout', this.onglobalout);
/**
* Canvas dom for webgl rendering
* @type {HTMLCanvasElement}
*/
this.dom = this.renderer.canvas;
var style = this.dom.style;
style.position = 'absolute';
style.left = '0';
style.top = '0';
/**
* @type {Array.<clay.Scene>}
*/
this.views = [];
this._picking = new RayPicking({
renderer: this.renderer
});
this._viewsToDispose = [];
/**
* Current accumulating id.
*/
this._accumulatingId = 0;
this._zrEventProxy = new echarts.graphic.Rect({
shape: {x: -1, y: -1, width: 2, height: 2},
// FIXME Better solution.
__isGLToZRProxy: true
});
this._backgroundColor = null;
this._disposed = false;
};
LayerGL.prototype.setUnpainted = function () {};
/**
* @param {module:echarts-gl/core/ViewGL} view
*/
LayerGL.prototype.addView = function (view) {
if (view.layer === this) {
return;
}
// If needs to dispose in this layer. unmark it.
var idx = this._viewsToDispose.indexOf(view);
if (idx >= 0) {
this._viewsToDispose.splice(idx, 1);
}
this.views.push(view);
view.layer = this;
var zr = this.zr;
view.scene.traverse(function (node) {
node.__zr = zr;
if (node.addAnimatorsToZr) {
node.addAnimatorsToZr(zr);
}
});
};
function removeFromZr(node) {
var zr = node.__zr;
node.__zr = null;
if (zr && node.removeAnimatorsFromZr) {
node.removeAnimatorsFromZr(zr);
}
}
/**
* @param {module:echarts-gl/core/ViewGL} view
*/
LayerGL.prototype.removeView = function (view) {
if (view.layer !== this) {
return;
}
var idx = this.views.indexOf(view);
if (idx >= 0) {
this.views.splice(idx, 1);
view.scene.traverse(removeFromZr, this);
view.layer = null;
// Mark to dispose in this layer.
this._viewsToDispose.push(view);
}
};
/**
* Remove all views
*/
LayerGL.prototype.removeViewsAll = function () {
this.views.forEach(function (view) {
view.scene.traverse(removeFromZr, this);
view.layer = null;
// Mark to dispose in this layer.
this._viewsToDispose.push(view);
}, this);
this.views.length = 0;
};
/**
* Resize the canvas and viewport, will be invoked by zrender
* @param {number} width
* @param {number} height
*/
LayerGL.prototype.resize = function (width, height) {
var renderer = this.renderer;
renderer.resize(width, height);
};
/**
* Clear color and depth
* @return {[type]} [description]
*/
LayerGL.prototype.clear = function () {
var gl = this.renderer.gl;
var clearColor = this._backgroundColor || [0, 0, 0, 0];
gl.clearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
gl.depthMask(true);
gl.colorMask(true, true, true, true);
gl.clear(gl.DEPTH_BUFFER_BIT | gl.COLOR_BUFFER_BIT);
};
/**
* Clear depth
*/
LayerGL.prototype.clearDepth = function () {
var gl = this.renderer.gl;
gl.clear(gl.DEPTH_BUFFER_BIT);
};
/**
* Clear color
*/
LayerGL.prototype.clearColor = function () {
var gl = this.renderer.gl;
gl.clearColor(0, 0, 0, 0);
gl.clear(gl.COLOR_BUFFER_BIT);
};
/**
* Mark layer to refresh next tick
*/
LayerGL.prototype.needsRefresh = function () {
this.zr.refresh();
};
/**
* Refresh the layer, will be invoked by zrender
*/
LayerGL.prototype.refresh = function (bgColor) {
this._backgroundColor = bgColor ? graphicGL.parseColor(bgColor) : [0, 0, 0, 0];
this.renderer.clearColor = this._backgroundColor;
for (var i = 0; i < this.views.length; i++) {
this.views[i].prepareRender(this.renderer);
}
this._doRender(false);
// Auto dispose unused resources on GPU, like program(shader), texture, geometry(buffers)
this._trackAndClean();
// Dispose trashed views
for (var i = 0; i < this._viewsToDispose.length; i++) {
this._viewsToDispose[i].dispose(this.renderer);
}
this._viewsToDispose.length = 0;
this._startAccumulating();
};
LayerGL.prototype.renderToCanvas = function (ctx) {
// PENDING will block the page
this._startAccumulating(true);
ctx.drawImage(this.dom, 0, 0, ctx.canvas.width, ctx.canvas.height);
};
LayerGL.prototype._doRender = function (accumulating) {
this.clear();
this.renderer.saveViewport();
for (var i = 0; i < this.views.length; i++) {
this.views[i].render(this.renderer, accumulating);
}
this.renderer.restoreViewport();
};
/**
* Stop accumulating
*/
LayerGL.prototype._stopAccumulating = function () {
this._accumulatingId = 0;
clearTimeout(this._accumulatingTimeout);
};
var accumulatingId = 1;
/**
* Start accumulating all the views.
* Accumulating is for antialising and have more sampling in SSAO
* @private
*/
LayerGL.prototype._startAccumulating = function (immediate) {
var self = this;
this._stopAccumulating();
var needsAccumulate = false;
for (var i = 0; i < this.views.length; i++) {
needsAccumulate = this.views[i].needsAccumulate() || needsAccumulate;
}
if (!needsAccumulate) {
return;
}
function accumulate(id) {
if (!self._accumulatingId || id !== self._accumulatingId) {
return;
}
var isFinished = true;
for (var i = 0; i < self.views.length; i++) {
isFinished = self.views[i].isAccumulateFinished() && needsAccumulate;
}
if (!isFinished) {
self._doRender(true);
if (immediate) {
accumulate(id);
}
else {
requestAnimationFrame(function () {
accumulate(id);
});
}
}
}
this._accumulatingId = accumulatingId++;
if (immediate) {
accumulate(self._accumulatingId);
}
else {
this._accumulatingTimeout = setTimeout(function () {
accumulate(self._accumulatingId);
}, 50);
}
};
LayerGL.prototype._trackAndClean = function () {
var textureList = [];
var geometriesList = [];
// Mark all resources unused;
if (this._textureList) {
markUnused(this._textureList);
markUnused(this._geometriesList);
}
for (var i = 0; i < this.views.length; i++) {
collectResources(this.views[i].scene, textureList, geometriesList);
}
// Dispose those unsed resources.
if (this._textureList) {
checkAndDispose(this.renderer, this._textureList);
checkAndDispose(this.renderer, this._geometriesList);
}
this._textureList = textureList;
this._geometriesList = geometriesList;
};
function markUnused(resourceList) {
for (var i = 0; i < resourceList.length; i++) {
resourceList[i].__used__ = 0;
}
}
function checkAndDispose(renderer, resourceList) {
for (var i = 0; i < resourceList.length; i++) {
if (!resourceList[i].__used__) {
resourceList[i].dispose(renderer);
}
}
}
function updateUsed(resource, list) {
resource.__used__ = resource.__used__ || 0;
resource.__used__++;
if (resource.__used__ === 1) {
// Don't push to the list twice.
list.push(resource);
}
}
function collectResources(scene, textureResourceList, geometryResourceList) {
var prevMaterial;
var prevGeometry;
scene.traverse(function (renderable) {
if (renderable.isRenderable()) {
var geometry = renderable.geometry;
var material = renderable.material;
// TODO optimize!!
if (material !== prevMaterial) {
var textureUniforms = material.getTextureUniforms();
for (var u = 0; u < textureUniforms.length; u++) {
var uniformName = textureUniforms[u];
var val = material.uniforms[uniformName].value;
if (!val) {
continue;
}
if (val instanceof Texture) {
updateUsed(val, textureResourceList);
}
else if (val instanceof Array) {
for (var k = 0; k < val.length; k++) {
if (val[k] instanceof Texture) {
updateUsed(val[k], textureResourceList);
}
}
}
}
}
if (geometry !== prevGeometry) {
updateUsed(geometry, geometryResourceList);
}
prevMaterial = material;
prevGeometry = geometry;
}
});
for (var k = 0; k < scene.lights.length; k++) {
// Track AmbientCubemap
if (scene.lights[k].cubemap) {
updateUsed(scene.lights[k].cubemap, textureResourceList);
}
}
}
/**
* Dispose the layer
*/
LayerGL.prototype.dispose = function () {
if (this._disposed) {
return;
}
this._stopAccumulating();
if (this._textureList) {
markUnused(this._textureList);
markUnused(this._geometriesList);
checkAndDispose(this.renderer, this._textureList);
checkAndDispose(this.renderer, this._geometriesList);
}
this.zr.off('globalout', this.onglobalout);
this._disposed = true;
};
// Event handlers
LayerGL.prototype.onmousedown = function (e) {
if (e.target && e.target.__isGLToZRProxy) {
return;
}
e = e.event;
var obj = this.pickObject(e.offsetX, e.offsetY);
if (obj) {
this._dispatchEvent('mousedown', e, obj);
this._dispatchDataEvent('mousedown', e, obj);
}
this._downX = e.offsetX;
this._downY = e.offsetY;
};
LayerGL.prototype.onmousemove = function (e) {
if (e.target && e.target.__isGLToZRProxy) {
return;
}
e = e.event;
var obj = this.pickObject(e.offsetX, e.offsetY);
var target = obj && obj.target;
var lastHovered = this._hovered;
this._hovered = obj;
if (lastHovered && target !== lastHovered.target) {
lastHovered.relatedTarget = target;
this._dispatchEvent('mouseout', e, lastHovered);
// this._dispatchDataEvent('mouseout', e, lastHovered);
this.zr.setCursorStyle('default');
}
this._dispatchEvent('mousemove', e, obj);
if (obj) {
this.zr.setCursorStyle('pointer');
if (!lastHovered || (target !== lastHovered.target)) {
this._dispatchEvent('mouseover', e, obj);
// this._dispatchDataEvent('mouseover', e, obj);
}
}
this._dispatchDataEvent('mousemove', e, obj);
};
LayerGL.prototype.onmouseup = function (e) {
if (e.target && e.target.__isGLToZRProxy) {
return;
}
e = e.event;
var obj = this.pickObject(e.offsetX, e.offsetY);
if (obj) {
this._dispatchEvent('mouseup', e, obj);
this._dispatchDataEvent('mouseup', e, obj);
}
this._upX = e.offsetX;
this._upY = e.offsetY;
};
LayerGL.prototype.onclick = LayerGL.prototype.dblclick = function (e) {
if (e.target && e.target.__isGLToZRProxy) {
return;
}
// Ignore click event if mouse moved
var dx = this._upX - this._downX;
var dy = this._upY - this._downY;
if (Math.sqrt(dx * dx + dy * dy) > 20) {
return;
}
e = e.event;
var obj = this.pickObject(e.offsetX, e.offsetY);
if (obj) {
this._dispatchEvent(e.type, e, obj);
this._dispatchDataEvent(e.type, e, obj);
}
// Try set depth of field onclick
var result = this._clickToSetFocusPoint(e);
if (result) {
var success = result.view.setDOFFocusOnPoint(result.distance);
if (success) {
this.zr.refresh();
}
}
};
LayerGL.prototype._clickToSetFocusPoint = function (e) {
var renderer = this.renderer;
var oldViewport = renderer.viewport;
for (var i = this.views.length - 1; i >= 0; i--) {
var viewGL = this.views[i];
if (viewGL.hasDOF() && viewGL.containPoint(e.offsetX, e.offsetY)) {
this._picking.scene = viewGL.scene;
this._picking.camera = viewGL.camera;
// Only used for picking, renderer.setViewport will also invoke gl.viewport.
// Set directly, PENDING.
renderer.viewport = viewGL.viewport;
var result = this._picking.pick(e.offsetX, e.offsetY, true);
if (result) {
result.view = viewGL;
return result;
}
}
}
renderer.viewport = oldViewport;
};
LayerGL.prototype.onglobalout = function (e) {
var lastHovered = this._hovered;
if (lastHovered) {
this._dispatchEvent('mouseout', e, {
target: lastHovered.target
});
}
};
LayerGL.prototype.pickObject = function (x, y) {
var output = [];
var renderer = this.renderer;
var oldViewport = renderer.viewport;
for (var i = 0; i < this.views.length; i++) {
var viewGL = this.views[i];
if (viewGL.containPoint(x, y)) {
this._picking.scene = viewGL.scene;
this._picking.camera = viewGL.camera;
// Only used for picking, renderer.setViewport will also invoke gl.viewport.
// Set directly, PENDING.
renderer.viewport = viewGL.viewport;
this._picking.pickAll(x, y, output);
}
}
renderer.viewport = oldViewport;
output.sort(function (a, b) {
return a.distance - b.distance;
});
return output[0];
};
LayerGL.prototype._dispatchEvent = function (eveName, originalEvent, newEvent) {
if (!newEvent) {
newEvent = {};
}
var current = newEvent.target;
newEvent.cancelBubble = false;
newEvent.event = originalEvent;
newEvent.type = eveName;
newEvent.offsetX = originalEvent.offsetX;
newEvent.offsetY = originalEvent.offsetY;
while (current) {
current.trigger(eveName, newEvent);
current = current.getParent();
if (newEvent.cancelBubble) {
break;
}
}
this._dispatchToView(eveName, newEvent);
};
LayerGL.prototype._dispatchDataEvent = function (eveName, originalEvent, newEvent) {
var mesh = newEvent && newEvent.target;
var dataIndex = mesh && mesh.dataIndex;
var seriesIndex = mesh && mesh.seriesIndex;
// Custom event data
var eventData = mesh && mesh.eventData;
var elChangedInMouseMove = false;
var eventProxy = this._zrEventProxy;
eventProxy.x = originalEvent.offsetX;
eventProxy.y = originalEvent.offsetY;
eventProxy.update();
var targetInfo = {
target: eventProxy
};
const ecData = echarts.helper.getECData(eventProxy);
if (eveName === 'mousemove') {
if (dataIndex != null) {
if (dataIndex !== this._lastDataIndex) {
if (parseInt(this._lastDataIndex, 10) >= 0) {
ecData.dataIndex = this._lastDataIndex;
ecData.seriesIndex = this._lastSeriesIndex;
// FIXME May cause double events.
this.zr.handler.dispatchToElement(targetInfo, 'mouseout', originalEvent);
}
elChangedInMouseMove = true;
}
}
else if (eventData != null) {
if (eventData !== this._lastEventData) {
if (this._lastEventData != null) {
ecData.eventData = this._lastEventData;
// FIXME May cause double events.
this.zr.handler.dispatchToElement(targetInfo, 'mouseout', originalEvent);
}
elChangedInMouseMove = true;
}
}
this._lastEventData = eventData;
this._lastDataIndex = dataIndex;
this._lastSeriesIndex = seriesIndex;
}
ecData.eventData = eventData;
ecData.dataIndex = dataIndex;
ecData.seriesIndex = seriesIndex;
if (eventData != null || (parseInt(dataIndex, 10) >= 0 && parseInt(seriesIndex, 10) >= 0)) {
this.zr.handler.dispatchToElement(targetInfo, eveName, originalEvent);
if (elChangedInMouseMove) {
this.zr.handler.dispatchToElement(targetInfo, 'mouseover', originalEvent);
}
}
};
LayerGL.prototype._dispatchToView = function (eventName, e) {
for (var i = 0; i < this.views.length; i++) {
if (this.views[i].containPoint(e.offsetX, e.offsetY)) {
this.views[i].trigger(eventName, e);
}
}
};
Object.assign(LayerGL.prototype, notifier);
export default LayerGL;