-
Notifications
You must be signed in to change notification settings - Fork 6
/
axes.js
672 lines (569 loc) · 16.9 KB
/
axes.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
'use strict'
module.exports = createAxes
var createText = require('./lib/text.js')
var createLines = require('./lib/lines.js')
var createBackground = require('./lib/background.js')
var getCubeProperties = require('./lib/cube.js')
var Ticks = require('./lib/ticks.js')
var identity = new Float32Array([
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1])
var ab = ArrayBuffer
var dv = DataView
function isTypedArray(a) {
return ab.isView(a) && !(a instanceof dv)
}
function isArrayOrTypedArray(a) {
return Array.isArray(a) || isTypedArray(a)
}
function copyVec3(a, b) {
a[0] = b[0]
a[1] = b[1]
a[2] = b[2]
return a
}
function Axes(gl) {
this.gl = gl
this.pixelRatio = 1
this.bounds = [ [-10, -10, -10],
[ 10, 10, 10] ]
this.ticks = [ [], [], [] ]
this.autoTicks = true
this.tickSpacing = [ 1, 1, 1 ]
this.tickEnable = [ true, true, true ]
this.tickFont = [ 'sans-serif', 'sans-serif', 'sans-serif' ]
this.tickFontStyle = [ 'normal', 'normal', 'normal' ]
this.tickFontWeight = [ 'normal', 'normal', 'normal' ]
this.tickFontVariant = [ 'normal', 'normal', 'normal' ]
this.tickSize = [ 12, 12, 12 ]
this.tickAngle = [ 0, 0, 0 ]
this.tickAlign = [ 'auto', 'auto', 'auto' ]
this.tickColor = [ [0,0,0,1], [0,0,0,1], [0,0,0,1] ]
this.tickPad = [ 10, 10, 10 ]
this.lastCubeProps = {
cubeEdges: [0,0,0],
axis: [0,0,0]
}
this.labels = [ 'x', 'y', 'z' ]
this.labelEnable = [ true, true, true ]
this.labelFont = [ 'sans-serif', 'sans-serif', 'sans-serif' ]
this.labelFontStyle = [ 'normal', 'normal', 'normal' ]
this.labelFontWeight = [ 'normal', 'normal', 'normal' ]
this.labelFontVariant = [ 'normal', 'normal', 'normal' ]
this.labelSize = [ 20, 20, 20 ]
this.labelAngle = [ 0, 0, 0 ]
this.labelAlign = [ 'auto', 'auto', 'auto' ]
this.labelColor = [ [0,0,0,1], [0,0,0,1], [0,0,0,1] ]
this.labelPad = [ 10, 10, 10 ]
this.lineEnable = [ true, true, true ]
this.lineMirror = [ false, false, false ]
this.lineWidth = [ 1, 1, 1 ]
this.lineColor = [ [0,0,0,1], [0,0,0,1], [0,0,0,1] ]
this.lineTickEnable = [ true, true, true ]
this.lineTickMirror = [ false, false, false ]
this.lineTickLength = [ 0, 0, 0 ]
this.lineTickWidth = [ 1, 1, 1 ]
this.lineTickColor = [ [0,0,0,1], [0,0,0,1], [0,0,0,1] ]
this.gridEnable = [ true, true, true ]
this.gridWidth = [ 1, 1, 1 ]
this.gridColor = [ [0,0,0,1], [0,0,0,1], [0,0,0,1] ]
this.zeroEnable = [ true, true, true ]
this.zeroLineColor = [ [0,0,0,1], [0,0,0,1], [0,0,0,1] ]
this.zeroLineWidth = [ 2, 2, 2 ]
this.backgroundEnable = [ false, false, false ]
this.backgroundColor = [ [0.8, 0.8, 0.8, 0.5],
[0.8, 0.8, 0.8, 0.5],
[0.8, 0.8, 0.8, 0.5] ]
this._firstInit = true
this._text = null
this._lines = null
this._background = createBackground(gl)
}
var proto = Axes.prototype
proto.update = function(options) {
options = options || {}
//Option parsing helper functions
function parseOption(nest, cons, name) {
if(name in options) {
var opt = options[name]
var prev = this[name]
var next
if(nest ? (isArrayOrTypedArray(opt) && isArrayOrTypedArray(opt[0])) :
isArrayOrTypedArray(opt) ) {
this[name] = next = [ cons(opt[0]), cons(opt[1]), cons(opt[2]) ]
} else {
this[name] = next = [ cons(opt), cons(opt), cons(opt) ]
}
for(var i=0; i<3; ++i) {
if(next[i] !== prev[i]) {
return true
}
}
}
return false
}
var NUMBER = parseOption.bind(this, false, Number)
var BOOLEAN = parseOption.bind(this, false, Boolean)
var STRING = parseOption.bind(this, false, String)
var COLOR = parseOption.bind(this, true, function(v) {
if(isArrayOrTypedArray(v)) {
if(v.length === 3) {
return [ +v[0], +v[1], +v[2], 1.0 ]
} else if(v.length === 4) {
return [ +v[0], +v[1], +v[2], +v[3] ]
}
}
return [ 0, 0, 0, 1 ]
})
//Tick marks and bounds
var nextTicks
var ticksUpdate = false
var boundsChanged = false
if('bounds' in options) {
var bounds = options.bounds
i_loop:
for(var i=0; i<2; ++i) {
for(var j=0; j<3; ++j) {
if(bounds[i][j] !== this.bounds[i][j]) {
boundsChanged = true
}
this.bounds[i][j] = bounds[i][j]
}
}
}
if('ticks' in options) {
nextTicks = options.ticks
ticksUpdate = true
this.autoTicks = false
for(var i=0; i<3; ++i) {
this.tickSpacing[i] = 0.0
}
} else if(NUMBER('tickSpacing')) {
this.autoTicks = true
boundsChanged = true
}
if(this._firstInit) {
if(!('ticks' in options || 'tickSpacing' in options)) {
this.autoTicks = true
}
//Force tick recomputation on first update
boundsChanged = true
ticksUpdate = true
this._firstInit = false
}
if(boundsChanged && this.autoTicks) {
nextTicks = Ticks.create(this.bounds, this.tickSpacing)
ticksUpdate = true
}
//Compare next ticks to previous ticks, only update if needed
if(ticksUpdate) {
for(var i=0; i<3; ++i) {
nextTicks[i].sort(function(a,b) {
return a.x-b.x
})
}
if(Ticks.equal(nextTicks, this.ticks)) {
ticksUpdate = false
} else {
this.ticks = nextTicks
}
}
//Parse tick properties
BOOLEAN('tickEnable')
//If font changes, must rebuild vbo
if(STRING('tickFont')) ticksUpdate = true
if(STRING('tickFontStyle')) ticksUpdate = true
if(STRING('tickFontWeight')) ticksUpdate = true
if(STRING('tickFontVariant')) ticksUpdate = true
NUMBER('tickSize')
NUMBER('tickAngle')
NUMBER('tickPad')
COLOR('tickColor')
//Axis labels
var labelUpdate = STRING('labels')
if(STRING('labelFont')) labelUpdate = true
if(STRING('labelFontStyle')) labelUpdate = true
if(STRING('labelFontWeight')) labelUpdate = true
if(STRING('labelFontVariant')) labelUpdate = true
BOOLEAN('labelEnable')
NUMBER('labelSize')
NUMBER('labelPad')
COLOR('labelColor')
//Axis lines
BOOLEAN('lineEnable')
BOOLEAN('lineMirror')
NUMBER('lineWidth')
COLOR('lineColor')
//Axis line ticks
BOOLEAN('lineTickEnable')
BOOLEAN('lineTickMirror')
NUMBER('lineTickLength')
NUMBER('lineTickWidth')
COLOR('lineTickColor')
//Grid lines
BOOLEAN('gridEnable')
NUMBER('gridWidth')
COLOR('gridColor')
//Zero line
BOOLEAN('zeroEnable')
COLOR('zeroLineColor')
NUMBER('zeroLineWidth')
//Background
BOOLEAN('backgroundEnable')
COLOR('backgroundColor')
var labelFontOpts = [
{
family: this.labelFont[0],
style: this.labelFontStyle[0],
weight: this.labelFontWeight[0],
variant: this.labelFontVariant[0],
},
{
family: this.labelFont[1],
style: this.labelFontStyle[1],
weight: this.labelFontWeight[1],
variant: this.labelFontVariant[1],
},
{
family: this.labelFont[2],
style: this.labelFontStyle[2],
weight: this.labelFontWeight[2],
variant: this.labelFontVariant[2],
}
]
var tickFontOpts = [
{
family: this.tickFont[0],
style: this.tickFontStyle[0],
weight: this.tickFontWeight[0],
variant: this.tickFontVariant[0],
},
{
family: this.tickFont[1],
style: this.tickFontStyle[1],
weight: this.tickFontWeight[1],
variant: this.tickFontVariant[1],
},
{
family: this.tickFont[2],
style: this.tickFontStyle[2],
weight: this.tickFontWeight[2],
variant: this.tickFontVariant[2],
}
]
//Update text if necessary
if(!this._text) {
this._text = createText(
this.gl,
this.bounds,
this.labels,
labelFontOpts,
this.ticks,
tickFontOpts)
} else if(this._text && (labelUpdate || ticksUpdate)) {
this._text.update(
this.bounds,
this.labels,
labelFontOpts,
this.ticks,
tickFontOpts)
}
//Update lines if necessary
if(this._lines && ticksUpdate) {
this._lines.dispose()
this._lines = null
}
if(!this._lines) {
this._lines = createLines(this.gl, this.bounds, this.ticks)
}
}
function OffsetInfo() {
this.primalOffset = [0,0,0]
this.primalMinor = [0,0,0]
this.mirrorOffset = [0,0,0]
this.mirrorMinor = [0,0,0]
}
var LINE_OFFSET = [ new OffsetInfo(), new OffsetInfo(), new OffsetInfo() ]
function computeLineOffset(result, i, bounds, cubeEdges, cubeAxis) {
var primalOffset = result.primalOffset
var primalMinor = result.primalMinor
var dualOffset = result.mirrorOffset
var dualMinor = result.mirrorMinor
var e = cubeEdges[i]
//Calculate offsets
for(var j=0; j<3; ++j) {
if(i === j) {
continue
}
var a = primalOffset,
b = dualOffset,
c = primalMinor,
d = dualMinor
if(e & (1<<j)) {
a = dualOffset
b = primalOffset
c = dualMinor
d = primalMinor
}
a[j] = bounds[0][j]
b[j] = bounds[1][j]
if(cubeAxis[j] > 0) {
c[j] = -1
d[j] = 0
} else {
c[j] = 0
d[j] = +1
}
}
}
var CUBE_ENABLE = [0,0,0]
var DEFAULT_PARAMS = {
model: identity,
view: identity,
projection: identity,
_ortho: false
}
proto.isOpaque = function() {
return true
}
proto.isTransparent = function() {
return false
}
proto.drawTransparent = function(params) {}
var ALIGN_OPTION_AUTO = 0 // i.e. as defined in the shader the text would rotate to stay upwards range: [-90,90]
var PRIMAL_MINOR = [0,0,0]
var MIRROR_MINOR = [0,0,0]
var PRIMAL_OFFSET = [0,0,0]
proto.draw = function(params) {
params = params || DEFAULT_PARAMS
var gl = this.gl
//Geometry for camera and axes
var model = params.model || identity
var view = params.view || identity
var projection = params.projection || identity
var bounds = this.bounds
var isOrtho = params._ortho || false
//Unpack axis info
var cubeParams = getCubeProperties(model, view, projection, bounds, isOrtho)
var cubeEdges = cubeParams.cubeEdges
var cubeAxis = cubeParams.axis
var cx = view[12]
var cy = view[13]
var cz = view[14]
var cw = view[15]
var orthoFix = (isOrtho) ? 2 : 1 // double up padding for orthographic ticks & labels
var pixelScaleF = orthoFix * this.pixelRatio * (projection[3]*cx + projection[7]*cy + projection[11]*cz + projection[15]*cw) / gl.drawingBufferHeight
for(var i=0; i<3; ++i) {
this.lastCubeProps.cubeEdges[i] = cubeEdges[i]
this.lastCubeProps.axis[i] = cubeAxis[i]
}
//Compute axis info
var lineOffset = LINE_OFFSET
for(var i=0; i<3; ++i) {
computeLineOffset(
LINE_OFFSET[i],
i,
this.bounds,
cubeEdges,
cubeAxis)
}
//Set up state parameters
var gl = this.gl
//Draw background first
var cubeEnable = CUBE_ENABLE
for(var i=0; i<3; ++i) {
if(this.backgroundEnable[i]) {
cubeEnable[i] = cubeAxis[i]
} else {
cubeEnable[i] = 0
}
}
this._background.draw(
model,
view,
projection,
bounds,
cubeEnable,
this.backgroundColor)
//Draw lines
this._lines.bind(
model,
view,
projection,
this)
//First draw grid lines and zero lines
for(var i=0; i<3; ++i) {
var x = [0,0,0]
if(cubeAxis[i] > 0) {
x[i] = bounds[1][i]
} else {
x[i] = bounds[0][i]
}
//Draw grid lines
for(var j=0; j<2; ++j) {
var u = (i + 1 + j) % 3
var v = (i + 1 + (j^1)) % 3
if(this.gridEnable[u]) {
this._lines.drawGrid(u, v, this.bounds, x, this.gridColor[u], this.gridWidth[u]*this.pixelRatio)
}
}
//Draw zero lines (need to do this AFTER all grid lines are drawn)
for(var j=0; j<2; ++j) {
var u = (i + 1 + j) % 3
var v = (i + 1 + (j^1)) % 3
if(this.zeroEnable[v]) {
//Check if zero line in bounds
if(Math.min(bounds[0][v], bounds[1][v]) <= 0 && Math.max(bounds[0][v], bounds[1][v]) >= 0) {
this._lines.drawZero(u, v, this.bounds, x, this.zeroLineColor[v], this.zeroLineWidth[v]*this.pixelRatio)
}
}
}
}
//Then draw axis lines and tick marks
for(var i=0; i<3; ++i) {
//Draw axis lines
if(this.lineEnable[i]) {
this._lines.drawAxisLine(i, this.bounds, lineOffset[i].primalOffset, this.lineColor[i], this.lineWidth[i]*this.pixelRatio)
}
if(this.lineMirror[i]) {
this._lines.drawAxisLine(i, this.bounds, lineOffset[i].mirrorOffset, this.lineColor[i], this.lineWidth[i]*this.pixelRatio)
}
//Compute minor axes
var primalMinor = copyVec3(PRIMAL_MINOR, lineOffset[i].primalMinor)
var mirrorMinor = copyVec3(MIRROR_MINOR, lineOffset[i].mirrorMinor)
var tickLength = this.lineTickLength
for(var j=0; j<3; ++j) {
var scaleFactor = pixelScaleF / model[5*j]
primalMinor[j] *= tickLength[j] * scaleFactor
mirrorMinor[j] *= tickLength[j] * scaleFactor
}
//Draw axis line ticks
if(this.lineTickEnable[i]) {
this._lines.drawAxisTicks(i, lineOffset[i].primalOffset, primalMinor, this.lineTickColor[i], this.lineTickWidth[i]*this.pixelRatio)
}
if(this.lineTickMirror[i]) {
this._lines.drawAxisTicks(i, lineOffset[i].mirrorOffset, mirrorMinor, this.lineTickColor[i], this.lineTickWidth[i]*this.pixelRatio)
}
}
this._lines.unbind()
//Draw text sprites
this._text.bind(
model,
view,
projection,
this.pixelRatio)
var alignOpt // options in shader are from this list {-1, 0, 1, 2, 3, ..., n}
// -1: backward compatible
// 0: raw data
// 1: auto align, free angles
// 2: auto align, horizontal or vertical
//3-n: auto align, round to n directions e.g. 12 -> round to angles with 30-degree steps
var hv_ratio = 0.5 // can have an effect on the ratio between horizontals and verticals when using option 2
var enableAlign
var alignDir
function alignTo(i) {
alignDir = [0,0,0]
alignDir[i] = 1
}
function solveTickAlignments(i, minor, major) {
var i1 = (i + 1) % 3
var i2 = (i + 2) % 3
var A = minor[i1]
var B = minor[i2]
var C = major[i1]
var D = major[i2]
if ((A > 0) && (D > 0)) { alignTo(i1); return; }
else if ((A > 0) && (D < 0)) { alignTo(i1); return; }
else if ((A < 0) && (D > 0)) { alignTo(i1); return; }
else if ((A < 0) && (D < 0)) { alignTo(i1); return; }
else if ((B > 0) && (C > 0)) { alignTo(i2); return; }
else if ((B > 0) && (C < 0)) { alignTo(i2); return; }
else if ((B < 0) && (C > 0)) { alignTo(i2); return; }
else if ((B < 0) && (C < 0)) { alignTo(i2); return; }
}
for(var i=0; i<3; ++i) {
var minor = lineOffset[i].primalMinor
var major = lineOffset[i].mirrorMinor
var offset = copyVec3(PRIMAL_OFFSET, lineOffset[i].primalOffset)
for(var j=0; j<3; ++j) {
if(this.lineTickEnable[i]) {
offset[j] += pixelScaleF * minor[j] * Math.max(this.lineTickLength[j], 0) / model[5*j]
}
}
var axis = [0,0,0]
axis[i] = 1
//Draw tick text
if(this.tickEnable[i]) {
if(this.tickAngle[i] === -3600) {
this.tickAngle[i] = 0
this.tickAlign[i] = 'auto'
} else {
this.tickAlign[i] = -1
}
enableAlign = 1;
alignOpt = [this.tickAlign[i], hv_ratio, enableAlign]
if(alignOpt[0] === 'auto') alignOpt[0] = ALIGN_OPTION_AUTO
else alignOpt[0] = parseInt('' + alignOpt[0])
alignDir = [0,0,0]
solveTickAlignments(i, minor, major)
//Add tick padding
for(var j=0; j<3; ++j) {
offset[j] += pixelScaleF * minor[j] * this.tickPad[j] / model[5*j]
}
//Draw axis
this._text.drawTicks(
i,
this.tickSize[i],
this.tickAngle[i],
offset,
this.tickColor[i],
axis,
alignDir,
alignOpt)
}
//Draw labels
if(this.labelEnable[i]) {
enableAlign = 0
alignDir = [0,0,0]
if(this.labels[i].length > 4) { // for large label axis enable alignDir to axis
alignTo(i)
enableAlign = 1
}
alignOpt = [this.labelAlign[i], hv_ratio, enableAlign]
if(alignOpt[0] === 'auto') alignOpt[0] = ALIGN_OPTION_AUTO
else alignOpt[0] = parseInt('' + alignOpt[0])
//Add label padding
for(var j=0; j<3; ++j) {
offset[j] += pixelScaleF * minor[j] * this.labelPad[j] / model[5*j]
}
offset[i] += 0.5 * (bounds[0][i] + bounds[1][i])
//Draw axis
this._text.drawLabel(
i,
this.labelSize[i],
this.labelAngle[i],
offset,
this.labelColor[i],
[0,0,0],
alignDir,
alignOpt)
}
}
this._text.unbind()
}
proto.dispose = function() {
this._text.dispose()
this._lines.dispose()
this._background.dispose()
this._lines = null
this._text = null
this._background = null
this.gl = null
}
function createAxes(gl, options) {
var axes = new Axes(gl)
axes.update(options)
return axes
}