gridviz 2.0.45 → 2.0.46
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/README.md +119 -119
- package/dist/gridviz.js +6937 -6937
- package/package.json +76 -76
- package/src/App.js +668 -668
- package/src/BackgroundLayer.js +182 -182
- package/src/BackgroundLayerWMS.js +131 -131
- package/src/Dataset.js +76 -76
- package/src/DatasetComponent.js +101 -101
- package/src/GeoCanvas.js +420 -420
- package/src/LabelLayer.js +190 -190
- package/src/Layer.js +92 -92
- package/src/Legend.js +86 -86
- package/src/LineLayer.js +150 -150
- package/src/Style.js +114 -114
- package/src/Tooltip.js +172 -172
- package/src/dataset/COGeo.js +24 -24
- package/src/dataset/CSVGrid.js +104 -104
- package/src/dataset/GeoTIFF.js +127 -127
- package/src/dataset/GridTile.js +41 -41
- package/src/dataset/TiledGrid.js +264 -264
- package/src/index.js +177 -176
- package/src/legend/ColorCategoryLegend.js +124 -124
- package/src/legend/ColorDiscreteLegend.js +103 -103
- package/src/legend/ColorLegend.js +124 -124
- package/src/legend/SegmentOrientationLegend.js +87 -87
- package/src/legend/SegmentWidthLegend.js +97 -97
- package/src/legend/SizeLegend.js +141 -141
- package/src/style/CompositionStyle.js +300 -300
- package/src/style/ContourStyle.js +54 -54
- package/src/style/DotDensityStyle.js +156 -156
- package/src/style/JoyPlotStyle.js +135 -135
- package/src/style/LegoStyle.js +112 -112
- package/src/style/MosaicStyle.js +107 -107
- package/src/style/NinjaStarStyle.js +124 -124
- package/src/style/PillarStyle.js +230 -230
- package/src/style/SegmentStyle.js +142 -142
- package/src/style/ShapeColorSizeStyle.js +126 -126
- package/src/style/SideCatStyle.js +172 -172
- package/src/style/SideStyle.js +218 -218
- package/src/style/SquareColorCatWGLStyle.js +120 -120
- package/src/style/SquareColorWGLStyle.js +146 -146
- package/src/style/StrokeStyle.js +131 -131
- package/src/style/TanakaStyle.js +160 -160
- package/src/style/TextStyle.js +132 -132
- package/src/utils/Utils.js +35 -35
- package/src/utils/WebGLRectangleColoring.js +106 -106
- package/src/utils/WebGLSquareColoring.js +118 -118
- package/src/utils/WebGLSquareColoringAdvanced.js +230 -230
- package/src/utils/WebGLSquareColoringCatAdvanced.js +133 -133
- package/src/utils/WebGLSquareColoringKS.js +153 -153
- package/src/utils/stretching.js +131 -131
- package/src/utils/webGLUtils.js +71 -71
- package/src/utils/zoomUtils.js +142 -142
|
@@ -1,133 +1,133 @@
|
|
|
1
|
-
//@ts-check
|
|
2
|
-
'use strict'
|
|
3
|
-
|
|
4
|
-
import { initShaderProgram, createShader } from './webGLUtils.js'
|
|
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
|
-
}
|
|
1
|
+
//@ts-check
|
|
2
|
+
'use strict'
|
|
3
|
+
|
|
4
|
+
import { initShaderProgram, createShader } from './webGLUtils.js'
|
|
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
|
+
}
|
|
@@ -1,153 +1,153 @@
|
|
|
1
|
-
//@ts-check
|
|
2
|
-
'use strict'
|
|
3
|
-
|
|
4
|
-
import { initShaderProgram, createShader } from './webGLUtils.js'
|
|
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 WebGLSquareColoringKS {
|
|
13
|
-
//see:
|
|
14
|
-
//https://webglfundamentals.org/
|
|
15
|
-
//https://webglfundamentals.org/webgl/lessons/fr/webgl-shaders-and-glsl.html#les-uniforms-dans-les-shaders-de-vertex
|
|
16
|
-
//https://thebookofshaders.com/glossary/?search=mix
|
|
17
|
-
//https://thebookofshaders.com/06/
|
|
18
|
-
//https://thebookofshaders.com/glossary/
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
*
|
|
22
|
-
* @param {*} gl
|
|
23
|
-
* @param {Array.<String>} colors
|
|
24
|
-
* @param {{fun:string,alpha:number}} stretching
|
|
25
|
-
* @param {number} sizePix
|
|
26
|
-
*/
|
|
27
|
-
constructor(gl, colors, stretching, sizePix = 10) {
|
|
28
|
-
/** @type {WebGLRenderingContext} */
|
|
29
|
-
this.gl = gl
|
|
30
|
-
|
|
31
|
-
/** @type {WebGLShader} */
|
|
32
|
-
const vShader = createShader(
|
|
33
|
-
gl,
|
|
34
|
-
gl.VERTEX_SHADER,
|
|
35
|
-
`
|
|
36
|
-
attribute vec2 pos;
|
|
37
|
-
uniform float sizePix;
|
|
38
|
-
uniform vec3 data[2];
|
|
39
|
-
uniform mat3 mat;
|
|
40
|
-
|
|
41
|
-
varying float vt;
|
|
42
|
-
|
|
43
|
-
void main() {
|
|
44
|
-
|
|
45
|
-
//one vertex for each screen pixels
|
|
46
|
-
//list of data as uniform
|
|
47
|
-
//compute smoothed t here from uniforms
|
|
48
|
-
|
|
49
|
-
//compute cell screen position
|
|
50
|
-
gl_Position = vec4(mat * vec3(pos, 1.0), 1.0);
|
|
51
|
-
|
|
52
|
-
//set pixel size - same for all
|
|
53
|
-
gl_PointSize = sizePix;
|
|
54
|
-
|
|
55
|
-
//pass the pixel value to the fragment as varying
|
|
56
|
-
vt = data[2];
|
|
57
|
-
}
|
|
58
|
-
`
|
|
59
|
-
)
|
|
60
|
-
|
|
61
|
-
//prepare fragment shader code
|
|
62
|
-
//declare the uniform and other variables
|
|
63
|
-
let fshString = `
|
|
64
|
-
precision mediump float;
|
|
65
|
-
varying float vt;
|
|
66
|
-
uniform float alpha;
|
|
67
|
-
uniform vec4 c0;
|
|
68
|
-
uniform vec4 c1;
|
|
69
|
-
uniform vec4 c2;
|
|
70
|
-
uniform vec4 c3;
|
|
71
|
-
uniform vec4 c4;
|
|
72
|
-
void main(void) {
|
|
73
|
-
if(vt == 0.0) return;
|
|
74
|
-
float t = 1.0 - (1.0 / alpha) * log(exp(alpha) * (1.0 - vt) + vt);
|
|
75
|
-
vec4 cI;
|
|
76
|
-
vec4 cF;
|
|
77
|
-
if(t<1.0/4.0) { cI=c0; cF=c1; t=t*4.0; }
|
|
78
|
-
else if(t<2.0/4.0) { cI=c1; cF=c2; t=4.0*t-1.0; }
|
|
79
|
-
else if(t<3.0/4.0) { cI=c2; cF=c3; t=4.0*t-2.0; }
|
|
80
|
-
else { cI=c3; cF=c4; t=4.0*t-3.0; }
|
|
81
|
-
gl_FragColor = mix(cI, cF, t);
|
|
82
|
-
}`
|
|
83
|
-
|
|
84
|
-
/** @type {WebGLShader} */
|
|
85
|
-
const fShader = createShader(gl, gl.FRAGMENT_SHADER, fshString)
|
|
86
|
-
|
|
87
|
-
/** @type {WebGLProgram} */
|
|
88
|
-
this.program = initShaderProgram(gl, vShader, fShader)
|
|
89
|
-
gl.useProgram(this.program)
|
|
90
|
-
|
|
91
|
-
//set uniforms
|
|
92
|
-
|
|
93
|
-
//sizePix
|
|
94
|
-
gl.uniform1f(gl.getUniformLocation(this.program, 'sizePix'), 1.0 * sizePix)
|
|
95
|
-
|
|
96
|
-
//stretching alpha factor
|
|
97
|
-
gl.uniform1f(gl.getUniformLocation(this.program, 'alpha'), stretching ? 1.0 * stretching.alpha : 0.0)
|
|
98
|
-
|
|
99
|
-
//colors
|
|
100
|
-
for (let i = 0; i < colors.length; i++) {
|
|
101
|
-
const c = color(colors[i])
|
|
102
|
-
gl.uniform4fv(gl.getUniformLocation(this.program, 'c' + i), [
|
|
103
|
-
+c.r / 255.0,
|
|
104
|
-
+c.g / 255.0,
|
|
105
|
-
+c.b / 255.0,
|
|
106
|
-
+c.opacity,
|
|
107
|
-
])
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/** */
|
|
112
|
-
draw(verticesBuffer, data, transfoMat) {
|
|
113
|
-
const gl = this.gl
|
|
114
|
-
const program = this.program
|
|
115
|
-
|
|
116
|
-
//vertice data
|
|
117
|
-
gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer())
|
|
118
|
-
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(verticesBuffer), gl.STATIC_DRAW)
|
|
119
|
-
const position = gl.getAttribLocation(program, 'pos')
|
|
120
|
-
gl.vertexAttribPointer(
|
|
121
|
-
position,
|
|
122
|
-
2, //numComponents
|
|
123
|
-
gl.FLOAT, //type
|
|
124
|
-
false, //normalise
|
|
125
|
-
0, //stride
|
|
126
|
-
0 //offset
|
|
127
|
-
)
|
|
128
|
-
gl.enableVertexAttribArray(position)
|
|
129
|
-
|
|
130
|
-
//t data
|
|
131
|
-
/*gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
|
|
132
|
-
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(tBuffer), gl.STATIC_DRAW);
|
|
133
|
-
const t = gl.getAttribLocation(program, "t");
|
|
134
|
-
gl.vertexAttribPointer(t, 1, gl.FLOAT, false, 0, 0);
|
|
135
|
-
gl.enableVertexAttribArray(t);*/
|
|
136
|
-
|
|
137
|
-
//data
|
|
138
|
-
//gl.uniformMatrix3fv(gl.getUniformLocation(program, "mat"), false, new Float32Array(transfoMat));
|
|
139
|
-
gl.uniform3fv(gl.getUniformLocation(program, 'data'), 2, new Float32Array(data))
|
|
140
|
-
|
|
141
|
-
//transformation
|
|
142
|
-
gl.uniformMatrix3fv(gl.getUniformLocation(program, 'mat'), false, new Float32Array(transfoMat))
|
|
143
|
-
|
|
144
|
-
// Enable the depth test
|
|
145
|
-
//gl.enable(gl.DEPTH_TEST);
|
|
146
|
-
// Clear the color buffer bit
|
|
147
|
-
gl.clear(gl.COLOR_BUFFER_BIT)
|
|
148
|
-
// Set the view port
|
|
149
|
-
//gl.viewport(0, 0, cg.w, cg.h);
|
|
150
|
-
|
|
151
|
-
gl.drawArrays(gl.POINTS, 0, verticesBuffer.length / 2)
|
|
152
|
-
}
|
|
153
|
-
}
|
|
1
|
+
//@ts-check
|
|
2
|
+
'use strict'
|
|
3
|
+
|
|
4
|
+
import { initShaderProgram, createShader } from './webGLUtils.js'
|
|
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 WebGLSquareColoringKS {
|
|
13
|
+
//see:
|
|
14
|
+
//https://webglfundamentals.org/
|
|
15
|
+
//https://webglfundamentals.org/webgl/lessons/fr/webgl-shaders-and-glsl.html#les-uniforms-dans-les-shaders-de-vertex
|
|
16
|
+
//https://thebookofshaders.com/glossary/?search=mix
|
|
17
|
+
//https://thebookofshaders.com/06/
|
|
18
|
+
//https://thebookofshaders.com/glossary/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
*
|
|
22
|
+
* @param {*} gl
|
|
23
|
+
* @param {Array.<String>} colors
|
|
24
|
+
* @param {{fun:string,alpha:number}} stretching
|
|
25
|
+
* @param {number} sizePix
|
|
26
|
+
*/
|
|
27
|
+
constructor(gl, colors, stretching, sizePix = 10) {
|
|
28
|
+
/** @type {WebGLRenderingContext} */
|
|
29
|
+
this.gl = gl
|
|
30
|
+
|
|
31
|
+
/** @type {WebGLShader} */
|
|
32
|
+
const vShader = createShader(
|
|
33
|
+
gl,
|
|
34
|
+
gl.VERTEX_SHADER,
|
|
35
|
+
`
|
|
36
|
+
attribute vec2 pos;
|
|
37
|
+
uniform float sizePix;
|
|
38
|
+
uniform vec3 data[2];
|
|
39
|
+
uniform mat3 mat;
|
|
40
|
+
|
|
41
|
+
varying float vt;
|
|
42
|
+
|
|
43
|
+
void main() {
|
|
44
|
+
|
|
45
|
+
//one vertex for each screen pixels
|
|
46
|
+
//list of data as uniform
|
|
47
|
+
//compute smoothed t here from uniforms
|
|
48
|
+
|
|
49
|
+
//compute cell screen position
|
|
50
|
+
gl_Position = vec4(mat * vec3(pos, 1.0), 1.0);
|
|
51
|
+
|
|
52
|
+
//set pixel size - same for all
|
|
53
|
+
gl_PointSize = sizePix;
|
|
54
|
+
|
|
55
|
+
//pass the pixel value to the fragment as varying
|
|
56
|
+
vt = data[2];
|
|
57
|
+
}
|
|
58
|
+
`
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
//prepare fragment shader code
|
|
62
|
+
//declare the uniform and other variables
|
|
63
|
+
let fshString = `
|
|
64
|
+
precision mediump float;
|
|
65
|
+
varying float vt;
|
|
66
|
+
uniform float alpha;
|
|
67
|
+
uniform vec4 c0;
|
|
68
|
+
uniform vec4 c1;
|
|
69
|
+
uniform vec4 c2;
|
|
70
|
+
uniform vec4 c3;
|
|
71
|
+
uniform vec4 c4;
|
|
72
|
+
void main(void) {
|
|
73
|
+
if(vt == 0.0) return;
|
|
74
|
+
float t = 1.0 - (1.0 / alpha) * log(exp(alpha) * (1.0 - vt) + vt);
|
|
75
|
+
vec4 cI;
|
|
76
|
+
vec4 cF;
|
|
77
|
+
if(t<1.0/4.0) { cI=c0; cF=c1; t=t*4.0; }
|
|
78
|
+
else if(t<2.0/4.0) { cI=c1; cF=c2; t=4.0*t-1.0; }
|
|
79
|
+
else if(t<3.0/4.0) { cI=c2; cF=c3; t=4.0*t-2.0; }
|
|
80
|
+
else { cI=c3; cF=c4; t=4.0*t-3.0; }
|
|
81
|
+
gl_FragColor = mix(cI, cF, t);
|
|
82
|
+
}`
|
|
83
|
+
|
|
84
|
+
/** @type {WebGLShader} */
|
|
85
|
+
const fShader = createShader(gl, gl.FRAGMENT_SHADER, fshString)
|
|
86
|
+
|
|
87
|
+
/** @type {WebGLProgram} */
|
|
88
|
+
this.program = initShaderProgram(gl, vShader, fShader)
|
|
89
|
+
gl.useProgram(this.program)
|
|
90
|
+
|
|
91
|
+
//set uniforms
|
|
92
|
+
|
|
93
|
+
//sizePix
|
|
94
|
+
gl.uniform1f(gl.getUniformLocation(this.program, 'sizePix'), 1.0 * sizePix)
|
|
95
|
+
|
|
96
|
+
//stretching alpha factor
|
|
97
|
+
gl.uniform1f(gl.getUniformLocation(this.program, 'alpha'), stretching ? 1.0 * stretching.alpha : 0.0)
|
|
98
|
+
|
|
99
|
+
//colors
|
|
100
|
+
for (let i = 0; i < colors.length; i++) {
|
|
101
|
+
const c = color(colors[i])
|
|
102
|
+
gl.uniform4fv(gl.getUniformLocation(this.program, 'c' + i), [
|
|
103
|
+
+c.r / 255.0,
|
|
104
|
+
+c.g / 255.0,
|
|
105
|
+
+c.b / 255.0,
|
|
106
|
+
+c.opacity,
|
|
107
|
+
])
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** */
|
|
112
|
+
draw(verticesBuffer, data, transfoMat) {
|
|
113
|
+
const gl = this.gl
|
|
114
|
+
const program = this.program
|
|
115
|
+
|
|
116
|
+
//vertice data
|
|
117
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer())
|
|
118
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(verticesBuffer), gl.STATIC_DRAW)
|
|
119
|
+
const position = gl.getAttribLocation(program, 'pos')
|
|
120
|
+
gl.vertexAttribPointer(
|
|
121
|
+
position,
|
|
122
|
+
2, //numComponents
|
|
123
|
+
gl.FLOAT, //type
|
|
124
|
+
false, //normalise
|
|
125
|
+
0, //stride
|
|
126
|
+
0 //offset
|
|
127
|
+
)
|
|
128
|
+
gl.enableVertexAttribArray(position)
|
|
129
|
+
|
|
130
|
+
//t data
|
|
131
|
+
/*gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
|
|
132
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(tBuffer), gl.STATIC_DRAW);
|
|
133
|
+
const t = gl.getAttribLocation(program, "t");
|
|
134
|
+
gl.vertexAttribPointer(t, 1, gl.FLOAT, false, 0, 0);
|
|
135
|
+
gl.enableVertexAttribArray(t);*/
|
|
136
|
+
|
|
137
|
+
//data
|
|
138
|
+
//gl.uniformMatrix3fv(gl.getUniformLocation(program, "mat"), false, new Float32Array(transfoMat));
|
|
139
|
+
gl.uniform3fv(gl.getUniformLocation(program, 'data'), 2, new Float32Array(data))
|
|
140
|
+
|
|
141
|
+
//transformation
|
|
142
|
+
gl.uniformMatrix3fv(gl.getUniformLocation(program, 'mat'), false, new Float32Array(transfoMat))
|
|
143
|
+
|
|
144
|
+
// Enable the depth test
|
|
145
|
+
//gl.enable(gl.DEPTH_TEST);
|
|
146
|
+
// Clear the color buffer bit
|
|
147
|
+
gl.clear(gl.COLOR_BUFFER_BIT)
|
|
148
|
+
// Set the view port
|
|
149
|
+
//gl.viewport(0, 0, cg.w, cg.h);
|
|
150
|
+
|
|
151
|
+
gl.drawArrays(gl.POINTS, 0, verticesBuffer.length / 2)
|
|
152
|
+
}
|
|
153
|
+
}
|