-
Notifications
You must be signed in to change notification settings - Fork 13
/
scene.js
853 lines (741 loc) · 20.4 KB
/
scene.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
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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
'use strict'
var createCamera = require('./camera.js')
var createAxes = require('gl-axes3d')
var axesRanges = require('gl-axes3d/properties')
var createSpikes = require('gl-spikes3d')
var createSelect = require('gl-select-static')
var createFBO = require('gl-fbo')
var drawTriangle = require('a-big-triangle')
var mouseChange = require('mouse-change')
var perspective = require('gl-mat4/perspective')
var ortho = require('gl-mat4/ortho')
var createShader = require('./lib/shader')
var isMobile = require('is-mobile')({ tablet: true, featureDetect: true })
module.exports = {
createScene: createScene,
createCamera: createCamera
}
function MouseSelect() {
this.mouse = [-1,-1]
this.screen = null
this.distance = Infinity
this.index = null
this.dataCoordinate = null
this.dataPosition = null
this.object = null
this.data = null
}
function getContext(canvas, options) {
var gl = null
try {
gl = canvas.getContext('webgl', options)
if(!gl) {
gl = canvas.getContext('experimental-webgl', options)
}
} catch(e) {
return null
}
return gl
}
function roundUpPow10(x) {
var y = Math.round(Math.log(Math.abs(x)) / Math.log(10))
if(y < 0) {
var base = Math.round(Math.pow(10, -y))
return Math.ceil(x*base) / base
} else if(y > 0) {
var base = Math.round(Math.pow(10, y))
return Math.ceil(x/base) * base
}
return Math.ceil(x)
}
function defaultBool(x) {
if(typeof x === 'boolean') {
return x
}
return true
}
function createScene(options) {
options = options || {}
options.camera = options.camera || {}
var canvas = options.canvas
if(!canvas) {
canvas = document.createElement('canvas')
if(options.container) {
var container = options.container
container.appendChild(canvas)
} else {
document.body.appendChild(canvas)
}
}
var gl = options.gl
if(!gl) {
if(options.glOptions) {
isMobile = !!options.glOptions.preserveDrawingBuffer
}
gl = getContext(canvas,
options.glOptions || {
premultipliedAlpha: true,
antialias: true,
preserveDrawingBuffer: isMobile
})
}
if(!gl) {
throw new Error('webgl not supported')
}
//Initial bounds
var bounds = options.bounds || [[-10,-10,-10], [10,10,10]]
//Create selection
var selection = new MouseSelect()
//Accumulation buffer
var accumBuffer = createFBO(gl,
gl.drawingBufferWidth, gl.drawingBufferHeight, {
preferFloat: !isMobile
})
var accumShader = createShader(gl)
var isOrtho =
(options.cameraObject && options.cameraObject._ortho === true) ||
(options.camera.projection && options.camera.projection.type === 'orthographic') ||
false
//Create a camera
var cameraOptions = {
eye: options.camera.eye || [2,0,0],
center: options.camera.center || [0,0,0],
up: options.camera.up || [0,1,0],
zoomMin: options.camera.zoomMax || 0.1,
zoomMax: options.camera.zoomMin || 100,
mode: options.camera.mode || 'turntable',
_ortho: isOrtho
}
//Create axes
var axesOptions = options.axes || {}
var axes = createAxes(gl, axesOptions)
axes.enable = !axesOptions.disable
//Create spikes
var spikeOptions = options.spikes || {}
var spikes = createSpikes(gl, spikeOptions)
//Object list is empty initially
var objects = []
var pickBufferIds = []
var pickBufferCount = []
var pickBuffers = []
//Dirty flag, skip redraw if scene static
var dirty = true
var pickDirty = true
var projection = new Array(16)
var model = new Array(16)
var cameraParams = {
view: null,
projection: projection,
model: model,
_ortho: false
}
var pickDirty = true
var viewShape = [ gl.drawingBufferWidth, gl.drawingBufferHeight ]
var camera = options.cameraObject || createCamera(canvas, cameraOptions)
//Create scene object
var scene = {
gl: gl,
contextLost: false,
pixelRatio: options.pixelRatio || 1,
canvas: canvas,
selection: selection,
camera: camera,
axes: axes,
axesPixels: null,
spikes: spikes,
bounds: bounds,
objects: objects,
shape: viewShape,
aspect: options.aspectRatio || [1,1,1],
pickRadius: options.pickRadius || 10,
zNear: options.zNear || 0.01,
zFar: options.zFar || 1000,
fovy: options.fovy || Math.PI/4,
clearColor: options.clearColor || [0,0,0,0],
autoResize: defaultBool(options.autoResize),
autoBounds: defaultBool(options.autoBounds),
autoScale: !!options.autoScale,
autoCenter: defaultBool(options.autoCenter),
clipToBounds: defaultBool(options.clipToBounds),
snapToData: !!options.snapToData,
onselect: options.onselect || null,
onrender: options.onrender || null,
onclick: options.onclick || null,
cameraParams: cameraParams,
oncontextloss: null,
mouseListener: null,
_stopped: false,
getAspectratio: function() {
return {
x: this.aspect[0],
y: this.aspect[1],
z: this.aspect[2]
}
},
setAspectratio: function(aspectratio) {
this.aspect[0] = aspectratio.x
this.aspect[1] = aspectratio.y
this.aspect[2] = aspectratio.z
pickDirty = true
},
setBounds: function(axisIndex, range) {
this.bounds[0][axisIndex] = range.min
this.bounds[1][axisIndex] = range.max
},
setClearColor: function(clearColor) {
this.clearColor = clearColor
},
clearRGBA: function() {
this.gl.clearColor(
this.clearColor[0],
this.clearColor[1],
this.clearColor[2],
this.clearColor[3]
)
this.gl.clear(
this.gl.COLOR_BUFFER_BIT |
this.gl.DEPTH_BUFFER_BIT
)
}
}
var pickShape = [ (gl.drawingBufferWidth/scene.pixelRatio)|0, (gl.drawingBufferHeight/scene.pixelRatio)|0 ]
function resizeListener() {
if(scene._stopped) {
return
}
if(!scene.autoResize) {
return
}
var parent = canvas.parentNode
var width = 1
var height = 1
if(parent && parent !== document.body) {
width = parent.clientWidth
height = parent.clientHeight
} else {
width = window.innerWidth
height = window.innerHeight
}
var nextWidth = Math.ceil(width * scene.pixelRatio)|0
var nextHeight = Math.ceil(height * scene.pixelRatio)|0
if(nextWidth !== canvas.width || nextHeight !== canvas.height) {
canvas.width = nextWidth
canvas.height = nextHeight
var style = canvas.style
style.position = style.position || 'absolute'
style.left = '0px'
style.top = '0px'
style.width = width + 'px'
style.height = height + 'px'
dirty = true
}
}
if(scene.autoResize) {
resizeListener()
}
window.addEventListener('resize', resizeListener)
function reallocPickIds() {
var numObjs = objects.length
var numPick = pickBuffers.length
for(var i=0; i<numPick; ++i) {
pickBufferCount[i] = 0
}
obj_loop:
for(var i=0; i<numObjs; ++i) {
var obj = objects[i]
var pickCount = obj.pickSlots
if(!pickCount) {
pickBufferIds[i] = -1
continue
}
for(var j=0; j<numPick; ++j) {
if(pickBufferCount[j] + pickCount < 255) {
pickBufferIds[i] = j
obj.setPickBase(pickBufferCount[j]+1)
pickBufferCount[j] += pickCount
continue obj_loop
}
}
//Create new pick buffer
var nbuffer = createSelect(gl, viewShape)
pickBufferIds[i] = numPick
pickBuffers.push(nbuffer)
pickBufferCount.push(pickCount)
obj.setPickBase(1)
numPick += 1
}
while(numPick > 0 && pickBufferCount[numPick-1] === 0) {
pickBufferCount.pop()
pickBuffers.pop().dispose()
}
}
scene.update = function(options) {
if(scene._stopped) {
return
}
options = options || {}
dirty = true
pickDirty = true
}
scene.add = function(obj) {
if(scene._stopped) {
return
}
obj.axes = axes
objects.push(obj)
pickBufferIds.push(-1)
dirty = true
pickDirty = true
reallocPickIds()
}
scene.remove = function(obj) {
if(scene._stopped) {
return
}
var idx = objects.indexOf(obj)
if(idx < 0) {
return
}
objects.splice(idx, 1)
pickBufferIds.pop()
dirty = true
pickDirty = true
reallocPickIds()
}
scene.dispose = function() {
if(scene._stopped) {
return
}
scene._stopped = true
window.removeEventListener('resize', resizeListener)
canvas.removeEventListener('webglcontextlost', checkContextLoss)
scene.mouseListener.enabled = false
if(scene.contextLost) {
return
}
//Destroy objects
axes.dispose()
spikes.dispose()
for(var i=0; i<objects.length; ++i) {
objects[i].dispose()
}
//Clean up buffers
accumBuffer.dispose()
for(var i=0; i<pickBuffers.length; ++i) {
pickBuffers[i].dispose()
}
//Clean up shaders
accumShader.dispose()
//Release all references
gl = null
axes = null
spikes = null
objects = []
}
//Update mouse position
scene._mouseRotating = false
scene._prevButtons = 0
scene.enableMouseListeners = function() {
scene.mouseListener = mouseChange(canvas, function(buttons, x, y) {
if(scene._stopped) {
return
}
var numPick = pickBuffers.length
var numObjs = objects.length
var prevObj = selection.object
selection.distance = Infinity
selection.mouse[0] = x
selection.mouse[1] = y
selection.object = null
selection.screen = null
selection.dataCoordinate = selection.dataPosition = null
var change = false
if(buttons && scene._prevButtons) {
scene._mouseRotating = true
} else {
if(scene._mouseRotating) {
pickDirty = true
}
scene._mouseRotating = false
for(var i=0; i<numPick; ++i) {
var result = pickBuffers[i].query(x, pickShape[1] - y - 1, scene.pickRadius)
if(result) {
if(result.distance > selection.distance) {
continue
}
for(var j=0; j<numObjs; ++j) {
var obj = objects[j]
if(pickBufferIds[j] !== i) {
continue
}
var objPick = obj.pick(result)
if(objPick) {
selection.buttons = buttons
selection.screen = result.coord
selection.distance = result.distance
selection.object = obj
selection.index = objPick.distance
selection.dataPosition = objPick.position
selection.dataCoordinate = objPick.dataCoordinate
selection.data = objPick
change = true
}
}
}
}
}
if(prevObj && prevObj !== selection.object) {
if(prevObj.highlight) {
prevObj.highlight(null)
}
dirty = true
}
if(selection.object) {
if(selection.object.highlight) {
selection.object.highlight(selection.data)
}
dirty = true
}
change = change || (selection.object !== prevObj)
if(change && scene.onselect) {
scene.onselect(selection)
}
if((buttons & 1) && !(scene._prevButtons & 1) && scene.onclick) {
scene.onclick(selection)
}
scene._prevButtons = buttons
})
}
function checkContextLoss() {
if(scene.contextLost) {
return true
}
if(gl.isContextLost()) {
scene.contextLost = true
scene.mouseListener.enabled = false
scene.selection.object = null
if(scene.oncontextloss) {
scene.oncontextloss()
}
}
}
canvas.addEventListener('webglcontextlost', checkContextLoss)
//Render the scene for mouse picking
function renderPick() {
if(checkContextLoss()) {
return
}
gl.colorMask(true, true, true, true)
gl.depthMask(true)
gl.disable(gl.BLEND)
gl.enable(gl.DEPTH_TEST)
gl.depthFunc(gl.LEQUAL)
var numObjs = objects.length
var numPick = pickBuffers.length
for(var j=0; j<numPick; ++j) {
var buf = pickBuffers[j]
buf.shape = pickShape
buf.begin()
for(var i=0; i<numObjs; ++i) {
if(pickBufferIds[i] !== j) {
continue
}
var obj = objects[i]
if(obj.drawPick) {
obj.pixelRatio = 1
obj.drawPick(cameraParams)
}
}
buf.end()
}
}
var nBounds = [
[ Infinity, Infinity, Infinity],
[-Infinity,-Infinity,-Infinity]]
var prevBounds = [nBounds[0].slice(), nBounds[1].slice()]
function redraw() {
if(checkContextLoss()) {
return
}
resizeListener()
//Tick camera
var cameraMoved = scene.camera.tick()
cameraParams.view = scene.camera.matrix
dirty = dirty || cameraMoved
pickDirty = pickDirty || cameraMoved
//Set pixel ratio
axes.pixelRatio = scene.pixelRatio
spikes.pixelRatio = scene.pixelRatio
//Check if any objects changed, recalculate bounds
var numObjs = objects.length
var lo = nBounds[0]
var hi = nBounds[1]
lo[0] = lo[1] = lo[2] = Infinity
hi[0] = hi[1] = hi[2] = -Infinity
for(var i=0; i<numObjs; ++i) {
var obj = objects[i]
//Set the axes properties for each object
obj.pixelRatio = scene.pixelRatio
obj.axes = scene.axes
dirty = dirty || !!obj.dirty
pickDirty = pickDirty || !!obj.dirty
var obb = obj.bounds
if(obb) {
var olo = obb[0]
var ohi = obb[1]
for(var j=0; j<3; ++j) {
lo[j] = Math.min(lo[j], olo[j])
hi[j] = Math.max(hi[j], ohi[j])
}
}
}
//Recalculate bounds
var bounds = scene.bounds
if(scene.autoBounds) {
for(var j=0; j<3; ++j) {
if(hi[j] < lo[j]) {
lo[j] = -1
hi[j] = 1
} else {
if(lo[j] === hi[j]) {
lo[j] -= 1
hi[j] += 1
}
var padding = 0.05 * (hi[j] - lo[j])
lo[j] = lo[j] - padding
hi[j] = hi[j] + padding
}
bounds[0][j] = lo[j]
bounds[1][j] = hi[j]
}
}
var boundsChanged = false
for(var j=0; j<3; ++j) {
boundsChanged = boundsChanged ||
(prevBounds[0][j] !== bounds[0][j]) ||
(prevBounds[1][j] !== bounds[1][j])
prevBounds[0][j] = bounds[0][j]
prevBounds[1][j] = bounds[1][j]
}
//Recalculate bounds
pickDirty = pickDirty || boundsChanged
dirty = dirty || boundsChanged
if(!dirty) {
return
}
if(boundsChanged) {
var tickSpacing = [0,0,0]
for(var i=0; i<3; ++i) {
tickSpacing[i] = roundUpPow10((bounds[1][i]-bounds[0][i]) / 10.0)
}
if(axes.autoTicks) {
axes.update({
bounds: bounds,
tickSpacing: tickSpacing
})
} else {
axes.update({
bounds: bounds
})
}
}
//Get scene
var width = gl.drawingBufferWidth
var height = gl.drawingBufferHeight
viewShape[0] = width
viewShape[1] = height
pickShape[0] = Math.max(width/scene.pixelRatio, 1)|0
pickShape[1] = Math.max(height/scene.pixelRatio, 1)|0
//Compute camera parameters
calcCameraParams(scene, isOrtho)
//Apply axes/clip bounds
for(var i=0; i<numObjs; ++i) {
var obj = objects[i]
//Set axes bounds
obj.axesBounds = bounds
//Set clip bounds
if(scene.clipToBounds) {
obj.clipBounds = bounds
}
}
//Set spike parameters
if(selection.object) {
if(scene.snapToData) {
spikes.position = selection.dataCoordinate
} else {
spikes.position = selection.dataPosition
}
spikes.bounds = bounds
}
//If state changed, then redraw pick buffers
if(pickDirty) {
pickDirty = false
renderPick()
}
//Recalculate pixel data
scene.axesPixels = axesRanges(scene.axes, cameraParams, width, height)
//Call render callback
if(scene.onrender) {
scene.onrender()
}
//Read value
gl.bindFramebuffer(gl.FRAMEBUFFER, null)
gl.viewport(0, 0, width, height)
//General strategy: 3 steps
// 1. render non-transparent objects
// 2. accumulate transparent objects into separate fbo
// 3. composite final scene
//Clear FBO
scene.clearRGBA()
gl.depthMask(true)
gl.colorMask(true, true, true, true)
gl.enable(gl.DEPTH_TEST)
gl.depthFunc(gl.LEQUAL)
gl.disable(gl.BLEND)
gl.disable(gl.CULL_FACE) //most visualization surfaces are 2 sided
//Render opaque pass
var hasTransparent = false
if(axes.enable) {
hasTransparent = hasTransparent || axes.isTransparent()
axes.draw(cameraParams)
}
spikes.axes = axes
if(selection.object) {
spikes.draw(cameraParams)
}
gl.disable(gl.CULL_FACE) //most visualization surfaces are 2 sided
for(var i=0; i<numObjs; ++i) {
var obj = objects[i]
obj.axes = axes
obj.pixelRatio = scene.pixelRatio
if(obj.isOpaque && obj.isOpaque()) {
obj.draw(cameraParams)
}
if(obj.isTransparent && obj.isTransparent()) {
hasTransparent = true
}
}
if(hasTransparent) {
//Render transparent pass
accumBuffer.shape = viewShape
accumBuffer.bind()
gl.clear(gl.DEPTH_BUFFER_BIT)
gl.colorMask(false, false, false, false)
gl.depthMask(true)
gl.depthFunc(gl.LESS)
//Render forward facing objects
if(axes.enable && axes.isTransparent()) {
axes.drawTransparent(cameraParams)
}
for(var i=0; i<numObjs; ++i) {
var obj = objects[i]
if(obj.isOpaque && obj.isOpaque()) {
obj.draw(cameraParams)
}
}
//Render transparent pass
gl.enable(gl.BLEND)
gl.blendEquation(gl.FUNC_ADD)
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
gl.colorMask(true, true, true, true)
gl.depthMask(false)
gl.clearColor(0,0,0,0)
gl.clear(gl.COLOR_BUFFER_BIT)
if(axes.isTransparent()) {
axes.drawTransparent(cameraParams)
}
for(var i=0; i<numObjs; ++i) {
var obj = objects[i]
if(obj.isTransparent && obj.isTransparent()) {
obj.drawTransparent(cameraParams)
}
}
//Unbind framebuffer
gl.bindFramebuffer(gl.FRAMEBUFFER, null)
//Draw composite pass
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
gl.disable(gl.DEPTH_TEST)
accumShader.bind()
accumBuffer.color[0].bind(0)
accumShader.uniforms.accumBuffer = 0
drawTriangle(gl)
//Turn off blending
gl.disable(gl.BLEND)
}
//Clear dirty flags
dirty = false
for(var i=0; i<numObjs; ++i) {
objects[i].dirty = false
}
}
//Draw the whole scene
function render() {
if(scene._stopped || scene.contextLost) {
return
}
// this order is important: ios safari sometimes has sync raf
redraw()
requestAnimationFrame(render)
}
scene.enableMouseListeners()
render()
//Force redraw of whole scene
scene.redraw = function() {
if(scene._stopped) {
return
}
dirty = true
redraw()
}
return scene
}
function calcCameraParams(scene, isOrtho) {
var bounds = scene.bounds
var cameraParams = scene.cameraParams
var projection = cameraParams.projection
var model = cameraParams.model
var width = scene.gl.drawingBufferWidth
var height = scene.gl.drawingBufferHeight
var zNear = scene.zNear
var zFar = scene.zFar
var fovy = scene.fovy
var r = width / height
if(isOrtho) {
ortho(projection,
-r,
r,
-1,
1,
zNear,
zFar
)
cameraParams._ortho = true
} else {
perspective(projection,
fovy,
r,
zNear,
zFar
)
cameraParams._ortho = false
}
//Compute model matrix
for(var i=0; i<16; ++i) {
model[i] = 0
}
model[15] = 1
var maxS = 0
for(var i=0; i<3; ++i) {
maxS = Math.max(maxS, bounds[1][i] - bounds[0][i])
}
for(var i=0; i<3; ++i) {
if(scene.autoScale) {
model[5*i] = scene.aspect[i] / (bounds[1][i] - bounds[0][i])
} else {
model[5*i] = 1 / maxS
}
if(scene.autoCenter) {
model[12+i] = -model[5*i] * 0.5 * (bounds[0][i] + bounds[1][i])
}
}
}