-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgl-matrix.html
179 lines (166 loc) · 6.03 KB
/
webgl-matrix.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>矩阵,圆角</title>
<script src="./js/initShader.js"></script>
<script src="./js/matrix.js"></script>
<style>
html,
body {
width: 100vw;
height: 100vh;
padding: 0;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
}
body::after {
content: '';
display: block;
width: 1px;
height: 500px;
background-color: aqua;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
body::before {
content: '';
display: block;
width: 500px;
height: 1px;
background-color: darkviolet;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<canvas id="draw1" width="400" height="400" style="background: pink;">浏览器不支持canvas</canvas>
</body>
</html>
<script type="module">
const draw1 = document.getElementById('draw1');
const gl = draw1.getContext('webgl');
//attribute只传递顶点数量
const VERTEX_SHADER_SOURCE = `
attribute vec4 aPosition;
attribute float aPointSize;
uniform mat4 mat;
varying vec4 vPosition;
void main(){
// gl_Position=mat*aPosition;
gl_Position=aPosition;
vPosition=aPosition;
// gl_PointSize=aPointSize;
gl_PointSize=10.0;
}
`; //顶点
const FRAGMENT_SHADER_SOURCE = `
// precision mediump float;
precision lowp float;
uniform vec2 uColor;
varying vec4 vPosition;
uniform float radius; //自定义圆角半径
uniform float w;
uniform float h;
void setColor(float x, float y, vec2 target) {
float d = distance(vec2(target), vec2(x, y));
if (d > radius) {
discard;
} else {
gl_FragColor = vec4(1.0,0.0,0.0,1.0);
}
}
void main() {
float x = vPosition.x;
float y = vPosition.y;
// 左上边界,判断是否设置圆角
vec2 topLeft = vec2(-w + radius, h - radius);
// 右上
vec2 topRight = vec2(w - radius, h - radius);
// 左下
vec2 bottomLeft = vec2(-w + radius, -h + radius);
// 右下
vec2 bottomRight = vec2(w - radius, -h + radius);
if (x < topLeft.x && y > topLeft.y) {
setColor(x, y, topLeft);
} else if (x > topRight.x && y > topRight.y) {
setColor(x, y, topRight);
} else if (x < bottomLeft.x && y < bottomLeft.y) {
setColor(x, y, bottomLeft);
} else if (x > bottomRight.x && y < bottomRight.y) {
setColor(x, y, bottomRight);
} else {
gl_FragColor = vec4(1.0,0.0,0.0,1.0);
}
}
`;//片元,uniform统一声明,所有点颜色一样
const program = initShader(gl, VERTEX_SHADER_SOURCE, FRAGMENT_SHADER_SOURCE);
const aPosition = gl.getAttribLocation(program, 'aPosition');//第二个参数是源码里attribute声明的变量名
const aPointSize = gl.getAttribLocation(program, 'aPointSize');
const mat = gl.getUniformLocation(program, 'mat');
const uColor = gl.getUniformLocation(program, 'uColor');
const radius = gl.getUniformLocation(program, 'radius');
gl.uniform2f(uColor, 1.0, 1.0);
const w = gl.getUniformLocation(program, 'w');
const h = gl.getUniformLocation(program, 'h');
const size = 0.5;
gl.uniform1f(w, size);
gl.uniform1f(h, size);
//用类型化数组创建顶点数据,合并顶点location和size
const points = new Float32Array([
-size, -size, 0.0, 1.0, 10.0,
size, -size, 0.0, 1.0, 20.0,
// 0.0, 0.5, 30.0,
-size, size, 0.0, 1.0, 30.0,
size, size, 0.0, 1.0, 20.0,
// 0.5, 0.8, 10.0,
// 0.8, 0.5, 10.0
]);
const buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, points, gl.STATIC_DRAW);
const BYTES = points.BYTES_PER_ELEMENT//获取属性字节数
gl.vertexAttribPointer(aPosition, 4, gl.FLOAT, false, BYTES * 5, 0);
void gl.enableVertexAttribArray(aPosition);
gl.vertexAttribPointer(aPointSize, 1, gl.FLOAT, false, BYTES * 5, BYTES * 4);
void gl.enableVertexAttribArray(aPointSize);
gl.uniform1f(radius, 0.1);//注意顺序,保证在绘制之前
// gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
// let x = 0;
// function animation() {
// //平移
// // x += 0.01;
// // if(x>1){
// // x=-1;
// // }
// // const matrix=getTranslateMatrix(x,x);
// //缩放
// // x += 0.01;
// // if (x > 1.5) {
// // x = 0.1;
// // }
// // const matrix = getScaleMatrix(x,x);
// //旋转
// x += 0.01;
// const matrix = getRotateMatrix(x);
// gl.uniformMatrix4fv(mat, false, matrix);//(location,transpose,array)location对应uniform存储位置,transpose在webgl中恒为false,array矩阵数组
// gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
// requestAnimationFrame(animation);
// }
// animation();
const start = () => {
//这种不需要考虑执行顺序
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
requestAnimationFrame(start)
}
start();
</script>