gridviz 2.0.27 → 2.0.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/gridviz.js +34315 -0
- package/package.json +4 -2
- package/src/App.js +651 -0
- package/src/BackgroundLayer.js +182 -0
- package/src/BackgroundLayerWMS.js +131 -0
- package/src/Dataset.js +76 -0
- package/src/DatasetComponent.js +103 -0
- package/src/GeoCanvas.js +419 -0
- package/src/LabelLayer.js +190 -0
- package/src/Layer.js +92 -0
- package/src/Legend.js +86 -0
- package/src/LineLayer.js +150 -0
- package/src/Style.js +114 -0
- package/src/Tooltip.js +150 -0
- package/src/dataset/COGeo.js +24 -0
- package/src/dataset/CSVGrid.js +105 -0
- package/src/dataset/GeoTIFF.js +127 -0
- package/src/dataset/GridTile.js +41 -0
- package/src/dataset/ParquetGrid.js +123 -0
- package/src/dataset/TiledGrid.js +294 -0
- package/src/index.js +176 -0
- package/src/legend/ColorCategoryLegend.js +124 -0
- package/src/legend/ColorDiscreteLegend.js +105 -0
- package/src/legend/ColorLegend.js +125 -0
- package/src/legend/SegmentOrientationLegend.js +87 -0
- package/src/legend/SegmentWidthLegend.js +97 -0
- package/src/legend/SizeLegend.js +141 -0
- package/src/style/CompositionStyle.js +294 -0
- package/src/style/ContourStyle.js +54 -0
- package/src/style/DotDensityStyle.js +156 -0
- package/src/style/JoyPlotStyle.js +135 -0
- package/src/style/KernelSmoothingOldStyle.js +221 -0
- package/src/style/KernelSmoothingStyle.js +115 -0
- package/src/style/KernelSmoothingWGLStyle.js +130 -0
- package/src/style/LegoStyle.js +112 -0
- package/src/style/MosaicStyle.js +107 -0
- package/src/style/NinjaStarStyle.js +124 -0
- package/src/style/PillarStyle.js +230 -0
- package/src/style/SegmentStyle.js +142 -0
- package/src/style/ShapeColorSizeStyle.js +126 -0
- package/src/style/SideCatStyle.js +172 -0
- package/src/style/SideStyle.js +218 -0
- package/src/style/SquareColorCatWGLStyle.js +120 -0
- package/src/style/SquareColorWGLStyle.js +146 -0
- package/src/style/StrokeStyle.js +131 -0
- package/src/style/TanakaStyle.js +160 -0
- package/src/style/TextStyle.js +132 -0
- package/src/utils/Utils.js +35 -0
- package/src/utils/WebGLRectangleColoring.js +106 -0
- package/src/utils/WebGLSquareColoring.js +118 -0
- package/src/utils/WebGLSquareColoringAdvanced.js +230 -0
- package/src/utils/WebGLSquareColoringCatAdvanced.js +133 -0
- package/src/utils/WebGLSquareColoringKS.js +153 -0
- package/src/utils/stretching.js +131 -0
- package/src/utils/webGLUtils.js +71 -0
- package/src/utils/zoomUtils.js +142 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
//@ts-check
|
|
2
|
+
'use strict'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Get the class id from a value and class break values
|
|
6
|
+
*
|
|
7
|
+
* @param {number} v the value
|
|
8
|
+
* @param {Array.<number>} breaks the breaks
|
|
9
|
+
* @returns The class id, from 0 to breaks.length
|
|
10
|
+
*/
|
|
11
|
+
export function getClass(v, breaks) {
|
|
12
|
+
if (!breaks) return
|
|
13
|
+
if (breaks.length == 0) return 0
|
|
14
|
+
if (v <= breaks[0]) return 0
|
|
15
|
+
for (let i = 1; i < breaks.length; i++) if (breaks[i - 1] < v && v <= breaks[i]) return i
|
|
16
|
+
return breaks.length
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export let monitor = false
|
|
20
|
+
|
|
21
|
+
let previousDate
|
|
22
|
+
export function monitorDuration(message) {
|
|
23
|
+
const nowDate = Date.now()
|
|
24
|
+
|
|
25
|
+
//first call
|
|
26
|
+
if (!previousDate) {
|
|
27
|
+
previousDate = nowDate
|
|
28
|
+
console.log(previousDate, message)
|
|
29
|
+
return
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const d = nowDate - previousDate
|
|
33
|
+
previousDate = nowDate
|
|
34
|
+
console.log(d, message)
|
|
35
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
//@ts-check
|
|
2
|
+
'use strict'
|
|
3
|
+
|
|
4
|
+
import { initShaderProgram, createShader } from './webGLUtils'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Everything to easily draw colored rectangles with webGL.
|
|
8
|
+
*/
|
|
9
|
+
export class WebGLRectangleColoring {
|
|
10
|
+
//TODO: add color alpha/opacity
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
*
|
|
14
|
+
* @param {WebGLRenderingContext} gl
|
|
15
|
+
*/
|
|
16
|
+
constructor(gl) {
|
|
17
|
+
this.gl = gl
|
|
18
|
+
|
|
19
|
+
this.program = initShaderProgram(
|
|
20
|
+
gl,
|
|
21
|
+
createShader(
|
|
22
|
+
gl,
|
|
23
|
+
gl.VERTEX_SHADER,
|
|
24
|
+
`
|
|
25
|
+
attribute vec2 pos;
|
|
26
|
+
uniform mat3 mat;
|
|
27
|
+
attribute vec3 color;
|
|
28
|
+
varying vec3 vColor;
|
|
29
|
+
void main() {
|
|
30
|
+
gl_Position = vec4(mat * vec3(pos, 1.0), 1.0);
|
|
31
|
+
vColor = color;
|
|
32
|
+
}
|
|
33
|
+
`
|
|
34
|
+
),
|
|
35
|
+
createShader(
|
|
36
|
+
gl,
|
|
37
|
+
gl.FRAGMENT_SHADER,
|
|
38
|
+
`
|
|
39
|
+
precision mediump float;
|
|
40
|
+
varying vec3 vColor;
|
|
41
|
+
void main(void) {
|
|
42
|
+
gl_FragColor = vec4(vColor, 1.0);
|
|
43
|
+
}`
|
|
44
|
+
)
|
|
45
|
+
)
|
|
46
|
+
gl.useProgram(this.program)
|
|
47
|
+
|
|
48
|
+
//buffer data
|
|
49
|
+
this.verticesBuffer = []
|
|
50
|
+
this.colorsBuffer = []
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Add data to vertices/color buffers for color rectangle drawing */
|
|
54
|
+
addRectangleData(x1, x2, y1, y2, cR = 0, cG = 0, cB = 1, cA = 0) {
|
|
55
|
+
//TODO cA
|
|
56
|
+
//add vertices
|
|
57
|
+
this.verticesBuffer.push(x1, y1, x2, y1, x1, y2, x2, y2)
|
|
58
|
+
|
|
59
|
+
//colors, 3 parts (RGB), one per vertice
|
|
60
|
+
this.colorsBuffer.push(cR, cG, cB, cR, cG, cB, cR, cG, cB, cR, cG, cB)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** */
|
|
64
|
+
draw(transfoMat) {
|
|
65
|
+
const gl = this.gl
|
|
66
|
+
|
|
67
|
+
//vertice data
|
|
68
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer())
|
|
69
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.verticesBuffer), gl.STATIC_DRAW)
|
|
70
|
+
const position = gl.getAttribLocation(this.program, 'pos')
|
|
71
|
+
gl.vertexAttribPointer(
|
|
72
|
+
position,
|
|
73
|
+
2, //numComponents
|
|
74
|
+
gl.FLOAT, //type
|
|
75
|
+
false, //normalise
|
|
76
|
+
0, //stride
|
|
77
|
+
0 //offset
|
|
78
|
+
)
|
|
79
|
+
gl.enableVertexAttribArray(position)
|
|
80
|
+
|
|
81
|
+
//color data
|
|
82
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer())
|
|
83
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.colorsBuffer), gl.STATIC_DRAW)
|
|
84
|
+
var color = gl.getAttribLocation(this.program, 'color')
|
|
85
|
+
gl.vertexAttribPointer(color, 3, gl.FLOAT, false, 0, 0)
|
|
86
|
+
gl.enableVertexAttribArray(color)
|
|
87
|
+
|
|
88
|
+
//transformation
|
|
89
|
+
gl.uniformMatrix3fv(gl.getUniformLocation(this.program, 'mat'), false, new Float32Array(transfoMat))
|
|
90
|
+
|
|
91
|
+
// Enable the depth test
|
|
92
|
+
//gl.enable(gl.DEPTH_TEST);
|
|
93
|
+
// Clear the color buffer bit
|
|
94
|
+
gl.clear(gl.COLOR_BUFFER_BIT)
|
|
95
|
+
// Set the view port
|
|
96
|
+
//gl.viewport(0, 0, cg.w, cg.h);
|
|
97
|
+
|
|
98
|
+
for (let i = 0; i < this.verticesBuffer.length / 8; i++) {
|
|
99
|
+
gl.drawArrays(
|
|
100
|
+
gl.TRIANGLE_STRIP, // mode,see https://miro.medium.com/max/700/0*HQHB5lCGqlOUiysy.jpg
|
|
101
|
+
i * 4, // vertex list start
|
|
102
|
+
4 // vertex count
|
|
103
|
+
)
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
//@ts-check
|
|
2
|
+
'use strict'
|
|
3
|
+
|
|
4
|
+
import { initShaderProgram, createShader } from './webGLUtils'
|
|
5
|
+
import { color } from 'd3-color'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Everything to easily draw colored squares with webGL.
|
|
9
|
+
* All the same size, but different fill color.
|
|
10
|
+
*/
|
|
11
|
+
export class WebGLSquareColoring {
|
|
12
|
+
/**
|
|
13
|
+
*
|
|
14
|
+
* @param {WebGLRenderingContext} gl
|
|
15
|
+
*/
|
|
16
|
+
constructor(gl, sizePix) {
|
|
17
|
+
this.gl = gl
|
|
18
|
+
this.sizePix = sizePix || 10.0
|
|
19
|
+
|
|
20
|
+
this.program = initShaderProgram(
|
|
21
|
+
gl,
|
|
22
|
+
createShader(
|
|
23
|
+
gl,
|
|
24
|
+
gl.VERTEX_SHADER,
|
|
25
|
+
`
|
|
26
|
+
attribute vec2 pos;
|
|
27
|
+
uniform float sizePix;
|
|
28
|
+
uniform mat3 mat;
|
|
29
|
+
attribute vec4 color;
|
|
30
|
+
varying vec4 vColor;
|
|
31
|
+
void main() {
|
|
32
|
+
gl_Position = vec4(mat * vec3(pos, 1.0), 1.0);
|
|
33
|
+
gl_PointSize = sizePix;
|
|
34
|
+
vColor = color;
|
|
35
|
+
}
|
|
36
|
+
`
|
|
37
|
+
),
|
|
38
|
+
createShader(
|
|
39
|
+
gl,
|
|
40
|
+
gl.FRAGMENT_SHADER,
|
|
41
|
+
`
|
|
42
|
+
precision mediump float;
|
|
43
|
+
varying vec4 vColor;
|
|
44
|
+
void main(void) {
|
|
45
|
+
vec4 vColor_ = vColor / 255.0;
|
|
46
|
+
vColor_[3] = 255.0 * vColor_[3];
|
|
47
|
+
gl_FragColor = vColor_;
|
|
48
|
+
}`
|
|
49
|
+
)
|
|
50
|
+
)
|
|
51
|
+
gl.useProgram(this.program)
|
|
52
|
+
|
|
53
|
+
//buffer data
|
|
54
|
+
this.verticesBuffer = []
|
|
55
|
+
this.colorsBuffer = []
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Add data to vertices/size/color buffers for color squares drawing */
|
|
59
|
+
addPointData(xC, yC, col) {
|
|
60
|
+
//convert color
|
|
61
|
+
const cc = color(col)
|
|
62
|
+
//const cc = {r:45,g:87,b:98,opacity:0.9}
|
|
63
|
+
if (!cc) return
|
|
64
|
+
|
|
65
|
+
//vertices
|
|
66
|
+
this.verticesBuffer.push(xC, yC)
|
|
67
|
+
//color
|
|
68
|
+
this.colorsBuffer.push(cc.r, cc.g, cc.b, cc.opacity)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
addPointData2(xC, yC, r, g, b, opacity) {
|
|
72
|
+
//vertices
|
|
73
|
+
this.verticesBuffer.push(xC, yC)
|
|
74
|
+
//color
|
|
75
|
+
this.colorsBuffer.push(r, g, b, opacity)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** */
|
|
79
|
+
draw(transfoMat) {
|
|
80
|
+
const gl = this.gl
|
|
81
|
+
|
|
82
|
+
//vertice data
|
|
83
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer())
|
|
84
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.verticesBuffer), gl.STATIC_DRAW)
|
|
85
|
+
const position = gl.getAttribLocation(this.program, 'pos')
|
|
86
|
+
gl.vertexAttribPointer(
|
|
87
|
+
position,
|
|
88
|
+
2, //numComponents
|
|
89
|
+
gl.FLOAT, //type
|
|
90
|
+
false, //normalise
|
|
91
|
+
0, //stride
|
|
92
|
+
0 //offset
|
|
93
|
+
)
|
|
94
|
+
gl.enableVertexAttribArray(position)
|
|
95
|
+
|
|
96
|
+
//color data
|
|
97
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer())
|
|
98
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.colorsBuffer), gl.STATIC_DRAW)
|
|
99
|
+
var color = gl.getAttribLocation(this.program, 'color')
|
|
100
|
+
gl.vertexAttribPointer(color, 4, gl.FLOAT, false, 0, 0)
|
|
101
|
+
gl.enableVertexAttribArray(color)
|
|
102
|
+
|
|
103
|
+
//sizePix
|
|
104
|
+
gl.uniform1f(gl.getUniformLocation(this.program, 'sizePix'), 1.0 * this.sizePix)
|
|
105
|
+
|
|
106
|
+
//transformation
|
|
107
|
+
gl.uniformMatrix3fv(gl.getUniformLocation(this.program, 'mat'), false, new Float32Array(transfoMat))
|
|
108
|
+
|
|
109
|
+
// Enable the depth test
|
|
110
|
+
//gl.enable(gl.DEPTH_TEST);
|
|
111
|
+
// Clear the color buffer bit
|
|
112
|
+
gl.clear(gl.COLOR_BUFFER_BIT)
|
|
113
|
+
// Set the view port
|
|
114
|
+
//gl.viewport(0, 0, cg.w, cg.h);
|
|
115
|
+
|
|
116
|
+
gl.drawArrays(gl.POINTS, 0, this.verticesBuffer.length / 2)
|
|
117
|
+
}
|
|
118
|
+
}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
//@ts-check
|
|
2
|
+
'use strict'
|
|
3
|
+
|
|
4
|
+
import { initShaderProgram, createShader } from './webGLUtils'
|
|
5
|
+
import { color } from 'd3-color'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Everything to easily draw colored squares with webGL.
|
|
9
|
+
* All the same size, but different fill color.
|
|
10
|
+
* The color interpolation is computed in the fragment shader program, by the GPU, thus it is less flexible but faster.
|
|
11
|
+
*/
|
|
12
|
+
export class WebGLSquareColoringAdvanced {
|
|
13
|
+
//see:
|
|
14
|
+
//https://webglfundamentals.org/webgl/lessons/fr/webgl-shaders-and-glsl.html#les-uniforms-dans-les-shaders-de-vertex
|
|
15
|
+
//https://thebookofshaders.com/glossary/?search=mix
|
|
16
|
+
//https://thebookofshaders.com/06/
|
|
17
|
+
//https://thebookofshaders.com/glossary/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
*
|
|
21
|
+
* @param {*} gl
|
|
22
|
+
* @param {Array.<String>} colors
|
|
23
|
+
* @param {{fun:string,alpha:number}} stretching
|
|
24
|
+
* @param {number} sizePix
|
|
25
|
+
* @param {number|undefined} globalOpacity
|
|
26
|
+
*/
|
|
27
|
+
constructor(gl, colors, stretching, sizePix = 10, globalOpacity = undefined) {
|
|
28
|
+
/** @type {WebGLRenderingContext} */
|
|
29
|
+
this.gl = gl
|
|
30
|
+
//gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
|
|
31
|
+
//gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
|
|
32
|
+
//gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
|
|
33
|
+
|
|
34
|
+
/** @type {WebGLShader} */
|
|
35
|
+
const vShader = createShader(
|
|
36
|
+
gl,
|
|
37
|
+
gl.VERTEX_SHADER,
|
|
38
|
+
`
|
|
39
|
+
attribute vec2 pos;
|
|
40
|
+
uniform float sizePix;
|
|
41
|
+
uniform mat3 mat;
|
|
42
|
+
|
|
43
|
+
attribute float t;
|
|
44
|
+
varying float vt;
|
|
45
|
+
|
|
46
|
+
void main() {
|
|
47
|
+
gl_Position = vec4(mat * vec3(pos, 1.0), 1.0);
|
|
48
|
+
gl_PointSize = sizePix;
|
|
49
|
+
vt = t;
|
|
50
|
+
}
|
|
51
|
+
`
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
//prepare fragment shader code
|
|
55
|
+
//declare the uniform and other variables
|
|
56
|
+
let fshString =
|
|
57
|
+
'' +
|
|
58
|
+
'precision mediump float;\n' +
|
|
59
|
+
'varying float vt;\n' +
|
|
60
|
+
'uniform float alpha;\n' +
|
|
61
|
+
(() => {
|
|
62
|
+
const out = []
|
|
63
|
+
for (let i = 0; i < colors.length; i++) out.push('uniform vec4 c' + i + ';\n')
|
|
64
|
+
return out.join('')
|
|
65
|
+
})() +
|
|
66
|
+
//start the main function, apply the stretching of t
|
|
67
|
+
'void main(void) {\n'
|
|
68
|
+
|
|
69
|
+
if (stretching) {
|
|
70
|
+
if (stretching.fun == 'pow')
|
|
71
|
+
//sPow = (t, alpha = 3) => Math.pow(t, alpha);
|
|
72
|
+
fshString += ' float t = pow(vt, alpha);\n'
|
|
73
|
+
else if (stretching.fun == 'powRev')
|
|
74
|
+
//sPowRev = (t, alpha = 3) => 1 - Math.pow(1 - t, 1 / alpha);
|
|
75
|
+
fshString += ' float t = 1.0-pow(1.0-vt, 1.0/alpha);\n'
|
|
76
|
+
else if (stretching.fun == 'exp')
|
|
77
|
+
//sExp = (t, alpha = 3) => alpha == 0 ? t : (Math.exp(t * alpha) - 1) / (Math.exp(alpha) - 1);
|
|
78
|
+
fshString +=
|
|
79
|
+
stretching.alpha == 0
|
|
80
|
+
? `float t = vt;`
|
|
81
|
+
: ' float t = (exp(vt * alpha) - 1.0) / (exp(alpha) - 1.0);\n'
|
|
82
|
+
else if (stretching.fun == 'expRev')
|
|
83
|
+
//sExpRev = (t, alpha = 3) => alpha == 0 ? t : 1 - (1 / alpha) * Math.log(Math.exp(alpha) * (1 - t) + t);
|
|
84
|
+
fshString +=
|
|
85
|
+
stretching.alpha == 0
|
|
86
|
+
? `float t = vt;`
|
|
87
|
+
: ' float t = 1.0 - (1.0 / alpha) * log(exp(alpha) * (1.0 - vt) + vt);\n'
|
|
88
|
+
else if (stretching.fun == 'circleLow') {
|
|
89
|
+
if (stretching.alpha == 0)
|
|
90
|
+
//if (alpha == 0) return t;
|
|
91
|
+
fshString += ' float t = vt;\n'
|
|
92
|
+
else if (stretching.alpha == 1)
|
|
93
|
+
// if (alpha == 1) return Math.sqrt(2 * t - t * t);
|
|
94
|
+
fshString += ' float t = sqrt(vt * (2.0 - vt));\n'
|
|
95
|
+
else {
|
|
96
|
+
//const a = alpha / (1 - alpha);
|
|
97
|
+
//return Math.sqrt(1 / (a * a) + t * (2 / a + 2 - t)) - 1 / a;
|
|
98
|
+
fshString +=
|
|
99
|
+
' float a = alpha / (1.0 - alpha);\n' +
|
|
100
|
+
' float t = sqrt(1.0 / (a * a) + vt * ( 2.0/a + 2.0 - vt )) - 1.0 / a;\n'
|
|
101
|
+
}
|
|
102
|
+
} else if (stretching.fun == 'circleHigh') {
|
|
103
|
+
// 1 - sCircleLow(1 - t, alpha)
|
|
104
|
+
if (stretching.alpha == 0)
|
|
105
|
+
//if (alpha == 0) return t;
|
|
106
|
+
fshString += ' float t = vt;\n'
|
|
107
|
+
else if (stretching.alpha == 1)
|
|
108
|
+
// if (alpha == 1) return Math.sqrt(2 * t - t * t);
|
|
109
|
+
fshString += ' float t = 1.0 - sqrt((1.0 - vt) * (1.0 + vt));\n'
|
|
110
|
+
else {
|
|
111
|
+
//const a = alpha / (1 - alpha);
|
|
112
|
+
//return Math.sqrt(1 / (a * a) + (2 * t) / a + 2 * t - t * t) - 1 / a;
|
|
113
|
+
fshString +=
|
|
114
|
+
' float a = alpha / (1.0 - alpha);\n' +
|
|
115
|
+
' float t = 1.0 - sqrt(1.0 / (a * a) + (1.0-vt) * ( 2.0/a + 1.0 + vt )) + 1.0 / a;\n'
|
|
116
|
+
}
|
|
117
|
+
} else {
|
|
118
|
+
console.error('Unexpected stretching function code: ' + stretching.fun)
|
|
119
|
+
fshString += ' float t = vt;\n'
|
|
120
|
+
}
|
|
121
|
+
} else {
|
|
122
|
+
fshString += ' float t = vt;\n'
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
//choose initial and final colors, and adjust t value
|
|
126
|
+
if (colors.length == 1) fshString += ' vec4 cI=c0;\n vec4 cF=c0;\n'
|
|
127
|
+
else if (colors.length == 2) fshString += ' vec4 cI=c0;\n vec4 cF=c1;\n'
|
|
128
|
+
else {
|
|
129
|
+
const nb = colors.length - 1
|
|
130
|
+
const nbs = nb + '.0'
|
|
131
|
+
fshString += ' vec4 cI;\n'
|
|
132
|
+
fshString += ' vec4 cF;\n'
|
|
133
|
+
fshString += ' if(t<1.0/' + nbs + ') { cI=c0; cF=c1; t=t*' + nbs + '; }\n'
|
|
134
|
+
for (let i = 2; i < nb; i++)
|
|
135
|
+
fshString +=
|
|
136
|
+
' else if(t<' +
|
|
137
|
+
i +
|
|
138
|
+
'.0/' +
|
|
139
|
+
nbs +
|
|
140
|
+
') { cI=c' +
|
|
141
|
+
(i - 1) +
|
|
142
|
+
'; cF=c' +
|
|
143
|
+
i +
|
|
144
|
+
'; t=' +
|
|
145
|
+
nbs +
|
|
146
|
+
'*t-' +
|
|
147
|
+
(i - 1) +
|
|
148
|
+
'.0; }\n'
|
|
149
|
+
fshString +=
|
|
150
|
+
' else { cI=c' + (nb - 1) + '; cF=c' + nb + '; t=' + nbs + '*t-' + (nb - 1) + '.0; }\n'
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
//one single color
|
|
154
|
+
if (colors.length == 1) fshString += ' gl_FragColor = vec4(c0[0], c0[1], c0[2], c0[3]);}\n'
|
|
155
|
+
//set interpolated color, between initial and final one
|
|
156
|
+
else fshString += ' gl_FragColor = mix(cI, cF, t);}\n'
|
|
157
|
+
|
|
158
|
+
//console.log(fshString)
|
|
159
|
+
|
|
160
|
+
/** @type {WebGLShader} */
|
|
161
|
+
const fShader = createShader(gl, gl.FRAGMENT_SHADER, fshString)
|
|
162
|
+
|
|
163
|
+
/** @type {WebGLProgram} */
|
|
164
|
+
this.program = initShaderProgram(gl, vShader, fShader)
|
|
165
|
+
gl.useProgram(this.program)
|
|
166
|
+
|
|
167
|
+
//set uniforms
|
|
168
|
+
|
|
169
|
+
//sizePix
|
|
170
|
+
//TODO: bug here. Seems to be limited to some threshold value (around 250).
|
|
171
|
+
gl.uniform1f(gl.getUniformLocation(this.program, 'sizePix'), 1.0 * sizePix)
|
|
172
|
+
|
|
173
|
+
//stretching alpha factor
|
|
174
|
+
gl.uniform1f(gl.getUniformLocation(this.program, 'alpha'), stretching ? 1.0 * stretching.alpha : 0.0)
|
|
175
|
+
|
|
176
|
+
//colors
|
|
177
|
+
for (let i = 0; i < colors.length; i++) {
|
|
178
|
+
const c = color(colors[i])
|
|
179
|
+
|
|
180
|
+
let opacity = c.opacity
|
|
181
|
+
if (c.opacity == 1 && globalOpacity != undefined) opacity = globalOpacity
|
|
182
|
+
|
|
183
|
+
gl.uniform4fv(gl.getUniformLocation(this.program, 'c' + i), [
|
|
184
|
+
+c.r / 255.0,
|
|
185
|
+
+c.g / 255.0,
|
|
186
|
+
+c.b / 255.0,
|
|
187
|
+
+opacity,
|
|
188
|
+
])
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** */
|
|
193
|
+
draw(verticesBuffer, tBuffer, transfoMat) {
|
|
194
|
+
const gl = this.gl
|
|
195
|
+
const program = this.program
|
|
196
|
+
|
|
197
|
+
//vertice data
|
|
198
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer())
|
|
199
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(verticesBuffer), gl.STATIC_DRAW)
|
|
200
|
+
const position = gl.getAttribLocation(program, 'pos')
|
|
201
|
+
gl.vertexAttribPointer(
|
|
202
|
+
position,
|
|
203
|
+
2, //numComponents
|
|
204
|
+
gl.FLOAT, //type
|
|
205
|
+
false, //normalise
|
|
206
|
+
0, //stride
|
|
207
|
+
0 //offset
|
|
208
|
+
)
|
|
209
|
+
gl.enableVertexAttribArray(position)
|
|
210
|
+
|
|
211
|
+
//t data
|
|
212
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer())
|
|
213
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(tBuffer), gl.STATIC_DRAW)
|
|
214
|
+
const t = gl.getAttribLocation(program, 't')
|
|
215
|
+
gl.vertexAttribPointer(t, 1, gl.FLOAT, false, 0, 0)
|
|
216
|
+
gl.enableVertexAttribArray(t)
|
|
217
|
+
|
|
218
|
+
//transformation
|
|
219
|
+
gl.uniformMatrix3fv(gl.getUniformLocation(program, 'mat'), false, new Float32Array(transfoMat))
|
|
220
|
+
|
|
221
|
+
// Enable the depth test
|
|
222
|
+
//gl.enable(gl.DEPTH_TEST);
|
|
223
|
+
// Clear the color buffer bit
|
|
224
|
+
gl.clear(gl.COLOR_BUFFER_BIT)
|
|
225
|
+
// Set the view port
|
|
226
|
+
//gl.viewport(0, 0, cg.w, cg.h);
|
|
227
|
+
|
|
228
|
+
gl.drawArrays(gl.POINTS, 0, verticesBuffer.length / 2)
|
|
229
|
+
}
|
|
230
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
//@ts-check
|
|
2
|
+
'use strict'
|
|
3
|
+
|
|
4
|
+
import { initShaderProgram, createShader } from './webGLUtils'
|
|
5
|
+
import { color } from 'd3-color'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Everything to easily draw colored squares with webGL.
|
|
9
|
+
* All the same size, but different fill color.
|
|
10
|
+
* Color based on categories.
|
|
11
|
+
*/
|
|
12
|
+
export class WebGLSquareColoringCatAdvanced {
|
|
13
|
+
/**
|
|
14
|
+
* @param {Array.<string>} colors
|
|
15
|
+
*/
|
|
16
|
+
constructor(colors) {
|
|
17
|
+
/**
|
|
18
|
+
* @type {Array.<string>} */
|
|
19
|
+
this.colors = colors
|
|
20
|
+
|
|
21
|
+
/** Vector shader program
|
|
22
|
+
* @type {string} */
|
|
23
|
+
this.vshString = `
|
|
24
|
+
attribute vec2 pos;
|
|
25
|
+
uniform float sizePix;
|
|
26
|
+
uniform mat3 mat;
|
|
27
|
+
|
|
28
|
+
attribute float i;
|
|
29
|
+
varying float vi;
|
|
30
|
+
|
|
31
|
+
void main() {
|
|
32
|
+
gl_Position = vec4(mat * vec3(pos, 1.0), 1.0);
|
|
33
|
+
gl_PointSize = sizePix;
|
|
34
|
+
vi = i;
|
|
35
|
+
}
|
|
36
|
+
`
|
|
37
|
+
|
|
38
|
+
//prepare fragment shader code
|
|
39
|
+
//declare the uniform and other variables
|
|
40
|
+
const out = []
|
|
41
|
+
out.push('precision mediump float;\nvarying float vi;\n')
|
|
42
|
+
//add color uniforms
|
|
43
|
+
out.push('uniform vec4')
|
|
44
|
+
for (let i = 0; i < colors.length; i++) {
|
|
45
|
+
if (i > 0) out.push(',')
|
|
46
|
+
out.push(' c' + i)
|
|
47
|
+
}
|
|
48
|
+
out.push(';\n')
|
|
49
|
+
//start the main function
|
|
50
|
+
out.push('void main(void) {\n')
|
|
51
|
+
//choose color i
|
|
52
|
+
for (let i = 0; i < colors.length; i++) {
|
|
53
|
+
if (i > 0) out.push('else ')
|
|
54
|
+
out.push('if(vi==')
|
|
55
|
+
out.push(i)
|
|
56
|
+
out.push('.0) gl_FragColor = vec4(c')
|
|
57
|
+
out.push(i)
|
|
58
|
+
out.push('[0], c')
|
|
59
|
+
out.push(i)
|
|
60
|
+
out.push('[1], c')
|
|
61
|
+
out.push(i)
|
|
62
|
+
out.push('[2], c')
|
|
63
|
+
out.push(i)
|
|
64
|
+
out.push('[3]);\n')
|
|
65
|
+
}
|
|
66
|
+
out.push('else gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);\n}')
|
|
67
|
+
/** Fragment shader program
|
|
68
|
+
* @type {string} */
|
|
69
|
+
this.fshString = out.join('')
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** */
|
|
73
|
+
draw(gl, verticesBuffer, iBuffer, transfoMat, sizePix = 10) {
|
|
74
|
+
/** @type {WebGLShader} */
|
|
75
|
+
const vShader = createShader(gl, gl.VERTEX_SHADER, this.vshString)
|
|
76
|
+
|
|
77
|
+
/** @type {WebGLShader} */
|
|
78
|
+
const fShader = createShader(gl, gl.FRAGMENT_SHADER, this.fshString)
|
|
79
|
+
|
|
80
|
+
/** @type {WebGLProgram} */
|
|
81
|
+
const program = initShaderProgram(gl, vShader, fShader)
|
|
82
|
+
gl.useProgram(program)
|
|
83
|
+
|
|
84
|
+
//set uniforms
|
|
85
|
+
|
|
86
|
+
//sizePix
|
|
87
|
+
gl.uniform1f(gl.getUniformLocation(program, 'sizePix'), 1.0 * sizePix)
|
|
88
|
+
|
|
89
|
+
//colors
|
|
90
|
+
for (let i = 0; i < this.colors.length; i++) {
|
|
91
|
+
const c = color(this.colors[i])
|
|
92
|
+
gl.uniform4fv(gl.getUniformLocation(program, 'c' + i), [
|
|
93
|
+
+c.r / 255.0,
|
|
94
|
+
+c.g / 255.0,
|
|
95
|
+
+c.b / 255.0,
|
|
96
|
+
+c.opacity,
|
|
97
|
+
])
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
//vertice data
|
|
101
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer())
|
|
102
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(verticesBuffer), gl.STATIC_DRAW)
|
|
103
|
+
const position = gl.getAttribLocation(program, 'pos')
|
|
104
|
+
gl.vertexAttribPointer(
|
|
105
|
+
position,
|
|
106
|
+
2, //numComponents
|
|
107
|
+
gl.FLOAT, //type
|
|
108
|
+
false, //normalise
|
|
109
|
+
0, //stride
|
|
110
|
+
0 //offset
|
|
111
|
+
)
|
|
112
|
+
gl.enableVertexAttribArray(position)
|
|
113
|
+
|
|
114
|
+
//i data
|
|
115
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer())
|
|
116
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(iBuffer), gl.STATIC_DRAW)
|
|
117
|
+
const i = gl.getAttribLocation(program, 'i')
|
|
118
|
+
gl.vertexAttribPointer(i, 1, gl.FLOAT, false, 0, 0)
|
|
119
|
+
gl.enableVertexAttribArray(i)
|
|
120
|
+
|
|
121
|
+
//transformation
|
|
122
|
+
gl.uniformMatrix3fv(gl.getUniformLocation(program, 'mat'), false, new Float32Array(transfoMat))
|
|
123
|
+
|
|
124
|
+
// Enable the depth test
|
|
125
|
+
//gl.enable(gl.DEPTH_TEST);
|
|
126
|
+
// Clear the color buffer bit
|
|
127
|
+
gl.clear(gl.COLOR_BUFFER_BIT)
|
|
128
|
+
// Set the view port
|
|
129
|
+
//gl.viewport(0, 0, cg.w, cg.h);
|
|
130
|
+
|
|
131
|
+
gl.drawArrays(gl.POINTS, 0, verticesBuffer.length / 2)
|
|
132
|
+
}
|
|
133
|
+
}
|