-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshadertest.html
136 lines (115 loc) · 3.94 KB
/
shadertest.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
<!DOCTYPE html>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, user-scalable=no" />
<meta name="viewport" content="initial-scale=1, maximum-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<title>Shader Test</title>
<style>
html, body, canvas, div { margin: 0; padding: 0 }
html, body { height: 100%; }
body { overflow-x: visible; overflow-y: hidden; }
</style>
<canvas id="shadertest"></canvas>
<script id="vertex-shader" type="x-shader/x-vertex">// <![CDATA[
attribute vec2 a_position;
void main() {
gl_Position = vec4(a_position, 0, 1);
} // ]]></script>
<script id="fragment-shader" type="x-shader/x-fragment">// <![CDATA[
#ifdef GL_ES
precision mediump float;
#endif
#define TWO_PI 6.28318530718
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
// Function from Iñigo Quiles
// https://www.shadertoy.com/view/MsS3Wc
vec3 hsb2rgb( in vec3 c ){
vec3 rgb = clamp(abs(mod(c.x*6.0+vec3(0.0,4.0,2.0),
6.0)-3.0)-1.0, 0.0, 1.0);
rgb = rgb*rgb*(3.0-2.0*rgb);
return c.z * mix( vec3(1.0), rgb, c.y);
}
void main() {
vec2 st = gl_FragCoord.xy/u_resolution;
vec3 color = vec3(0.0);
// Use polar coordinates instead of cartesian
vec2 toCenter = vec2(0.5)-st;
float angle = atan(toCenter.y,toCenter.x) + u_time;
float radius = length(toCenter)*2.0;
// Map the angle (-PI to PI) to the Hue (from 0 to 1)
// and the Saturation to the radius
color = hsb2rgb(vec3((angle/TWO_PI)+0.5,radius,1.0));
gl_FragColor = vec4(color,1.0);
} // ]]></script>
<script type="module">//<![CDATA[
let gl;
let width;
let height;
let program;
let initialized = 0;
let u_resolution;
let u_mouse;
let u_time;
function init() {
let 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
);
program = gl.createProgram();
var shader;
shader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(shader, document.getElementById(
"vertex-shader").text);
gl.compileShader(shader);
gl.attachShader(program, shader);
shader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(shader, document.getElementById(
"fragment-shader").text);
gl.compileShader(shader);
gl.attachShader(program, shader);
gl.linkProgram(program);
u_resolution = gl.getUniformLocation(program, "u_resolution");
u_time = gl.getUniformLocation(program, "u_time");
u_mouse = gl.getUniformLocation(program, "u_mouse");
gl.useProgram(program);
return new Date().getTime() / 1000;
}
function draw() {
const now = new Date().getTime() / 1000;
if (!initialized)
initialized = init();
gl.viewport(0, 0, gl.drawingBufferWidth,
gl.drawingBufferHeight);
gl.uniform2fv(u_resolution, [width, height]);
gl.uniform1f(u_time, now - initialized);
gl.uniform2f(u_mouse, 0.0, 0.0);
gl.clearColor(1.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
var positionLocation = gl.getAttribLocation(program, "a_position");
gl.enableVertexAttribArray(positionLocation);
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
gl.drawArrays(gl.TRIANGLES, 0, 6);
requestAnimationFrame(draw);
}
document.addEventListener('DOMContentLoaded', () => {
const canvas = document.querySelector("#shadertest");
gl = canvas.getContext("webgl");
window.addEventListener("resize", function() {
width = canvas.width = document.body.clientWidth;
height = canvas.height = document.body.clientHeight;
draw();
});
window.dispatchEvent(new Event("resize"));
});
//]]></script>