-
Notifications
You must be signed in to change notification settings - Fork 0
/
glUtil.js
310 lines (287 loc) · 8.71 KB
/
glUtil.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
/**
*
* @param {WebGLRenderingContext} gl
* @param {String} type
* @param {String} source
*/
function createShader(gl, type, source) {
let shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
let success = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
if (success) {
return shader;
}
console.log(gl.getShaderInfoLog(shader));
gl.deleteShader(shader);
}
/**
*
* @param {WebGLRenderingContext} gl
* @param {WebGLShader} vertexShader
* @param {WebGLShader} fragmentShader
*/
function createProgram(gl, vertexShader, fragmentShader) {
let program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
let success = gl.getProgramParameter(program, gl.LINK_STATUS);
if (success) {
return gl.program = program;
}
console.log(gl.getProgramInfoLog(program));
gl.deleteProgram(program);
}
/**
*
* @param {WebGLRenderingContext} gl
* @param {String} VERTEX_SHADER
* @param {String} FRAGMENT_SHADER
*/
function createProgramBySource(gl, VERTEX_SHADER, FRAGMENT_SHADER) {
let vertexShader = createShader(gl, gl.VERTEX_SHADER, VERTEX_SHADER);
let fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, FRAGMENT_SHADER);
let program = createProgram(gl, vertexShader, fragmentShader);
gl.useProgram(program);
gl.program = program;
return program;
}
/**
*
* @param {Object} center
* @param {Number} rotate
*/
function createRotateMatrix(center, rotate) {
let cos = Math.cos(rotate * Math.PI / 180);
let sin = Math.sin(rotate * Math.PI / 180);
return new Float32Array([
cos, sin, 0.0, 0.0,
-sin, cos, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
(1 - cos) * center.x + sin * center.y, (1 - cos) * center.y - sin * center.x, 0.0, 1.0,
]);
}
/**
*
* @param {Number} tx
* @param {Number} ty
*/
function createTranslateMatrix(tx, ty) {
return new Float32Array([
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
tx, ty, 0.0, 1.0,
]);
}
/**
*
* @param {Number} scaleX
* @param {Number} scaleY
*/
function createScaleMatrix(scaleX, scaleY, center = {
x: 0,
y: 0
}) {
return new Float32Array([
scaleX, 0.0, 0.0, 0.0,
0.0, scaleY, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
-scaleX * center.x + center.x, -scaleY * center.y + center.y, 0.0, 1.0,
]);
}
/**
* @desc 对比度矩阵
* @param {Number} value
*/
function createContrastMatrix(value) {
return new Float32Array([
value, 0.0, 0.0, 0.0,
0.0, value, 0.0, 0.0,
0.0, 0.0, value, 0.0,
0.5 * (1 - value), 0.5 * (1 - value), 0.5 * (1 - value), 1.0,
]);
}
/**
* @desc 色相旋转矩阵
* @param {Number} value
*/
function createHueRotateMatrix(value) {
let sin = Math.sin(value * Math.PI / 180);
let cos = Math.cos(value * Math.PI / 180);
return new Float32Array([
0.213 + cos * 0.787 - sin * 0.213, 0.213 - cos * 0.213 + sin * 0.143, 0.213 - cos * 0.213 - sin * 0.787, 0.0,
0.715 - cos * 0.715 - sin * 0.715, 0.715 + cos * 0.285 + sin * 0.140, 0.715 - cos * 0.715 + sin * 0.715, 0.0,
0.072 - cos * 0.072 + sin * 0.928, 0.072 - cos * 0.072 - sin * 0.283, 0.072 + cos * 0.928 + sin * 0.072, 0.0,
0.0, 0.0, 0.0, 1.0,
]);
}
/**
* @desc 饱和度矩阵
* @param {Number} value
*/
function createSaturateMatrix(value) {
return new Float32Array([
0.3086 * (1 - value) + value, 0.3086 * (1 - value), 0.3086 * (1 - value), 0.0,
0.6094*(1 - value), 0.6094*(1 - value) + value, 0.6094*(1 - value), 0.0,
0.0820 * (1 - value), 0.0820 * (1 - value), 0.0820 * (1 - value) + value, 0.0,
0.0, 0.0, 0.0, 1.0
]);
}
/**
* @param {WebGLRenderingContext} gl
* @param {Number} x 中心x坐标
* @param {Number} y 中心y坐标
* @param {radius} radius 圆弧半径
* @param {Number} startArc 起始圆弧半径
* @param {Number} endArc 终止圆弧半径
* @param {Boolean} clockwise 方向,默认顺时针
*/
function createArcVertex(gl, x, y, radius, startArc, endArc, isInverse = false) {
let precision = 1;
let oneArc = Math.PI / 180
let points = [x, y, x / gl.canvas.width, y / gl.canvas.height];
for (let i = startArc; i <= endArc; i += precision) {
if (!isInverse) {
points.push(
x + radius * Math.sin(i * oneArc),
(y - radius * Math.cos(i * oneArc)),
(x + radius * Math.sin(i * oneArc)) / gl.canvas.width,
(y - radius * Math.cos(i * oneArc)) / gl.canvas.height);
} else {
points.push(
x - radius * Math.sin(i * oneArc),
(y - radius * Math.cos(i * oneArc)),
(x - radius * Math.sin(i * oneArc)) / gl.canvas.width,
(y - radius * Math.cos(i * oneArc)) / gl.canvas.height);
}
}
return new Float32Array(points);
}
/**
*
* @param {WebGLRenderingContext} gl
*/
function createTexture(gl) {
let texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
return texture;
}
function computeKernalWeight(kernal) {
let weight = kernal.reduce((prev, cur) => {
return prev + cur;
}, 0);
return weight <= 0 ? 1: weight;
}
/**
*
* @param {Number} x
* @param {Number} y
* @param {Array} vertX
* @param {Array} vertY
*/
function checkPointIn(x, y, vertX, vertY) {
let minX = Math.min(...vertX);
let minY = Math.min(...vertY);
let maxX = Math.max(...vertX);
let maxY = Math.max(...vertY);
if (x < minX || x > maxX || y < minY || y > maxY) {
return false;
} else {
let i, j, r;
r = false;
for (let i = 0, j = vertX.length - 1; i < vertX.length; j = i++) {
if ((vertY[i] > y) !== (vertY[j] > y) &&
(x < (y - vertY[i]) * (vertX[j] - vertX[i]) / (vertY[j] - vertY[i]) + vertX[i])) {
r = !r;
}
}
return r;
}
}
/**
*
* @param {Number} x
* @param {Number} y
* @param {Array} vertX
* @param {Array} vertY
*/
function checkPointIn2 (x, y, vertX, vertY) {
let minX = Math.min(...vertX);
let minY = Math.min(...vertY);
let maxX = Math.max(...vertX);
let maxY = Math.max(...vertY);
if (x < minX || x > maxX || y < minY || y > maxY) {
return false;
} else {
let i, j, r;
r = false;
for (let i = 0, j = vertX.length - 1; i < vertX.length; j = i++) {
if ((vertY[i] > y) !== (vertY[j] > y) &&
(x < (y - vertY[i]) * (vertX[j] - vertX[i]) / (vertY[j] - vertY[i]) + vertX[i])) {
r = !r;
}
}
return r;
}
}
/**
*
* @param {String} hex
*/
function hexToRGB(hex) {
if (/#[a-f0-9]{6}/i.test(hex)) {
let r = +('0x' + hex.substr(1, 2));
let g = +('0x' + hex.substr(3, 2));
let b = +('0x' + hex.substr(5, 2));
return [r / 255, g / 255, b / 255];
}
}
function generateImageByDiv(width, height, html) {
let svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
svg.setAttribute('viewBox', `0 0 ${width} ${height}`);
svg.setAttribute('width', width);
svg.setAttribute('height', height);
let style = document.createElementNS('http://www.w3.org/2000/svg', 'style');
style.textContent = `
@font-face {
font-family: 'ShouJinTi';
src: url('http://localhost:8081/WebGLTraining/assets/shoujin.ttf');
}
`;
let foreignObject = document.createElementNS('http://www.w3.org/2000/svg', 'foreignObject');
foreignObject.setAttribute('x', 0);
foreignObject.setAttribute('y', 0);
foreignObject.setAttribute('width', width);
foreignObject.setAttribute('height', height);
let div = document.createElementNS('http://www.w3.org/1999/xhtml', 'div');
div.setAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
div.innerHTML = html;
foreignObject.appendChild(div);
svg.appendChild(style);
svg.appendChild(foreignObject);
return svg;
}
export default {
createShader,
createProgram,
createProgramBySource,
createRotateMatrix,
createTranslateMatrix,
createScaleMatrix,
createContrastMatrix,
createHueRotateMatrix,
createArcVertex,
createTexture,
computeKernalWeight,
checkPointIn,
createSaturateMatrix,
hexToRGB,
generateImageByDiv
}