This repository has been archived by the owner on May 28, 2022. It is now read-only.
/
drawAxis.js
69 lines (63 loc) · 2.11 KB
/
drawAxis.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
const mat4 = require('gl-mat4')
const drawAxis = function (regl, params) {
const defaults = {
xColor: [1, 0, 0, 1],
yColor: [0, 1, 0, 1],
zColor: [0, 0, 1, 1],
size: 10,
lineWidth: 3, // FIXME/ linewidth has been "deprecated" in multiple browsers etc, need a workaround,
alwaysVisible: true // to have the widget alway visible 'on top' of the rest of the scene
}
let {size, xColor, yColor, zColor, lineWidth, alwaysVisible} = Object.assign({}, defaults, params)
if (lineWidth > regl.limits.lineWidthDims[1]) {
lineWidth = regl.limits.lineWidthDims[1]
}
const points = [
0, 0, 0,
size, 0, 0
]
const commandParams = {
frag: `precision mediump float;
uniform vec4 color;
void main() {
gl_FragColor = color;
}`,
vert: `
precision mediump float;
attribute vec3 position;
uniform mat4 model, view, projection;
void main() {
gl_Position = projection * view * model * vec4(position, 1);
}`,
uniforms: {
model: (context, props) => props && props.model ? props.model : mat4.identity([]),
color: (context, props) => props.color,
angle: (contet, props) => props.angle
},
attributes: {
position: points
},
count: points.length / 3,
primitive: 'line loop',
lineWidth,
depth: {
enable: !alwaysVisible // disable depth testing to have the axis widget 'alway on top' of other items in the 3d viewer
}
}
const xAxisModel = mat4.identity([])
const yAxisModel = mat4.rotateZ(mat4.create(), mat4.identity([]), Math.PI / 2)
const zAxisModel = mat4.rotateY(mat4.create(), mat4.identity([]), -Math.PI / 2)
let single = regl(commandParams)
return (props) => {
const defaults = {
model: mat4.identity([])
}
props = Object.assign({}, defaults, props)
return single([
{ color: xColor, model: mat4.multiply(mat4.create(), props.model, xAxisModel) }, // X
{ color: yColor, model: mat4.multiply(mat4.create(), props.model, yAxisModel) }, // Y
{ color: zColor, model: mat4.multiply(mat4.create(), props.model, zAxisModel) } // Z
])
}
}
module.exports = drawAxis