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
| <!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>Fang Idle Sprite Animation. WebGL 2.0, JavaScript</title>
<script src="https://cdn.jsdelivr.net/npm/gl-matrix@3.4.3/gl-matrix-min.js"></script>
</head>
<body>
<canvas id="renderCanvas" width="300" height="300"></canvas>
<script>
const canvas = document.getElementById("renderCanvas");
const gl = canvas.getContext("webgl2");
const vertShaderSrc =
`#version 300 es
in vec2 aPosition;
in vec2 aTexCoord;
uniform mat4 uMvpMatrix;
out vec2 vTexCoord;
void main()
{
gl_Position = uMvpMatrix * vec4(aPosition, 0.0, 1.0);
vTexCoord = aTexCoord;
}`;
const fragShaderSrc =
`#version 300 es
precision mediump float;
uniform sampler2D uSampler;
in vec2 vTexCoord;
out vec4 fragColor;
void main()
{
fragColor = texture(uSampler, vTexCoord);
}`;
const vShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vShader, vertShaderSrc);
gl.compileShader(vShader);
let ok = gl.getShaderParameter(vShader, gl.COMPILE_STATUS);
if (!ok) { console.log(gl.getShaderInfoLog(vShader));; }
const fShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fShader, fragShaderSrc);
gl.compileShader(fShader);
ok = gl.getShaderParameter(fShader, gl.COMPILE_STATUS);
if (!ok) { console.log(gl.getShaderInfoLog(fShader));; }
const program = gl.createProgram();
gl.attachShader(program, vShader);
gl.attachShader(program, fShader);
gl.linkProgram(program);
ok = gl.getProgramParameter(program, gl.LINK_STATUS);
if (!ok) { console.log(gl.getProgramInfoLog(program)); }
gl.useProgram(program);
const aPositionLocation = gl.getAttribLocation(program, "aPosition");
const aTexCoordLocation = gl.getAttribLocation(program, "aTexCoord");
const vertPositions = [
-0.5, -0.5, // frame 00
0.5, -0.5,
-0.5, 0.5,
0.5, 0.5,
-0.5, -0.5, // frame 01
0.5, -0.5,
-0.5, 0.5,
0.5, 0.5,
-0.5, -0.5, // frame 02
0.5, -0.5,
-0.5, 0.5,
0.5, 0.5,
-0.5, -0.5, // frame 03
0.5, -0.5,
-0.5, 0.5,
0.5, 0.5
];
const vertPosVBO = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertPosVBO);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertPositions), gl.STATIC_DRAW);
gl.vertexAttribPointer(aPositionLocation, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(aPositionLocation);
const texCoords = [
0, 1, // frame 00
1 / 4, 1,
0, 0,
1 / 4, 0,
1 / 4, 1, // frame 01
2 / 4, 1,
1 / 4, 0,
2 / 4, 0,
2 / 4, 1, // frame 02
3 / 4, 1,
2 / 4, 0,
3 / 4, 0,
3 / 4, 1, // frame 03
1, 1,
3 / 4, 0,
1, 0
];
const texCoordVBO = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, texCoordVBO);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(texCoords), gl.STATIC_DRAW);
gl.vertexAttribPointer(aTexCoordLocation, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(aTexCoordLocation);
const projMatrix = glMatrix.mat4.create();
glMatrix.mat4.ortho(projMatrix, 0, 100, 0, 100, 100, -100);
const viewMatrix = glMatrix.mat4.create();
glMatrix.mat4.lookAt(viewMatrix, [0, 0, 90], [0, 0, 0], [0, 1, 0]);
const projViewMatrix = glMatrix.mat4.create();
const mvpMatrix = glMatrix.mat4.create();
const modelMatrix = glMatrix.mat4.create();
glMatrix.mat4.mul(projViewMatrix, projMatrix, viewMatrix);
const uMvpMatrixLocation = gl.getUniformLocation(program, "uMvpMatrix");
const uSamplerLocation = gl.getUniformLocation(program, "uSampler");
gl.uniform1i(uSamplerLocation, 0);
gl.enable(gl.BLEND);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
gl.clearColor(0.93, 0.95, 0.98, 1);
let currentTime, lastTime, deltaTime;
let animationTime = 0;
const timePeriod = 250;
let drawingIndex = 0;
const image = new Image();
image.onload =
() =>
{
gl.activeTexture(gl.TEXTURE0);
const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
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);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
glMatrix.mat4.identity(modelMatrix);
glMatrix.mat4.translate(modelMatrix, modelMatrix, [50, 50, 0]);
// glMatrix.mat4.rotateZ(modelMatrix, modelMatrix, 30 * Math. PI / 180);
glMatrix.mat4.scale(modelMatrix, modelMatrix, [40, 50, 1]);
glMatrix.mat4.mul(mvpMatrix, projViewMatrix, modelMatrix);
gl.uniformMatrix4fv(uMvpMatrixLocation, false, mvpMatrix);
lastTime = Date.now();
animationLoop();
};
image.src = "assets/fang-idle.png";
function animationLoop()
{
requestAnimationFrame(() => animationLoop());
currentTime = Date.now();
deltaTime = currentTime - lastTime;
lastTime = currentTime;
animationTime += deltaTime;
if (animationTime >= timePeriod)
{
animationTime = 0;
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLE_STRIP, drawingIndex, 4);
drawingIndex += 4;
if (drawingIndex >= 16) { drawingIndex = 0; }
}
}
</script>
</body>
</html> |