-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
122 lines (96 loc) · 3.72 KB
/
test.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hi</title>
<style>
body {
background: #0088ff;
}
canvas {
border: 5px solid black;
}
</style>
</head>
<body>
<canvas width="800" height="800"></canvas>
<script id="vertex-shader" type="x-shader/x-vertex">
#version 300 es
in vec2 position;
void main() {
gl_Position = vec4(position, 0, 1);
}
</script>
<script id="fragment-shader" type="x-shader/x-fragment">
#version 300 es
precision highp float;
out vec4 outColor;
uniform vec2 xRange;
uniform vec2 yRange;
uniform float time;
void main() {
//gl_FragColor = vec4(gl_FragCoord.x / 800.0, gl_FragCoord.y / 800.0, 0, 1.);
float x = (gl_FragCoord.x - xRange.x) / (xRange.y - xRange.x);
float y = (gl_FragCoord.y - yRange.x) / (yRange.y - yRange.x);
float z = sin(x * y * time * time);
outColor = vec4(x, y, 0., z);
}
</script>
<script type="module">
(()=> {
const canvas = document.querySelector('canvas'),
gl = canvas.getContext('webgl2', {premultipliedAlpha: false});
let shaderProgram;
function supplyUniforms(program) {
const xRange = gl.getUniformLocation(program, 'xRange');
gl.uniform2fv(xRange, [0, 800]);
const yRange = gl.getUniformLocation(program, 'yRange');
gl.uniform2fv(yRange, [0, 800]);
const time = gl.getUniformLocation(program, 'time');
gl.uniform1f(time, performance.now() / 1000);
}
function configureShader() {
const vertexShaderText = document.getElementById('vertex-shader').text.trim(),
fragmentShaderText = document.getElementById('fragment-shader').text.trim(),
vertexShader = gl.createShader(gl.VERTEX_SHADER),
fragmentShader = gl.createShader(gl.FRAGMENT_SHADER),
program = gl.createProgram();
gl.shaderSource(vertexShader, vertexShaderText);
gl.compileShader(vertexShader);
gl.shaderSource(fragmentShader, fragmentShaderText);
gl.compileShader(fragmentShader);
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
gl.useProgram(program);
return program;
}
function init() {
gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
const buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER,
new Float32Array([
-1.0, -1.0,
1.0, -1.0,
-1.0, 1.0,
-1.0, 1.0,
1.0, -1.0,
1.0, 1.0
]), gl.STATIC_DRAW);
shaderProgram = configureShader();
}
function draw() {
requestAnimationFrame(draw);
supplyUniforms(shaderProgram);
const positionLocation = gl.getAttribLocation(shaderProgram, 'position');
gl.enableVertexAttribArray(positionLocation);
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
gl.drawArrays(gl.TRIANGLES, 0, 6);
}
init();
draw();
})();
</script>
</body>
</html>