-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
255 lines (202 loc) · 9.26 KB
/
index.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/png" href="/dino.png" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>DaiF's Little Shaders</title>
</head>
<body>
<div id="screen">
<div id="topbar">
<select id="shader-select">
<option value="default" selected>Default</option>
<option value="gooch">Gooch</option>
<option value="comics">Comics</option>
<option value="drawing">Hand-Drawn</option>
</select>
<div>
<input type="checkbox" id="debug-box" name="debug-box">
<label for="debug-box">Show Debug Panel</label>
</div>
</div>
<div id="output">
<canvas id="c"></canvas>
<div id="debug">
<input type="range" min="-100" max="100" value="0" class="slider" id="camera-x">
<label for="camera-x">Camera X</label>
<input type="range" min="-100" max="100" value="10" class="slider" id="camera-y">
<label for="camera-y">Camera Y</label>
<input type="range" min="-100" max="100" value="0" class="slider" id="camera-z">
<label for="camera-z">Camera Z</label>
</div>
</div>
</div>
<script type="module" src="/main.js"></script>
<script id="default-vertex" type="shader/vertex">
attribute vec4 a_position;
attribute vec2 a_texcoord;
attribute vec3 a_normal;
uniform mat4 u_worldViewMatrix;
uniform mat4 u_world;
uniform mat4 u_shadowMatrix;
varying vec4 v_position;
varying vec2 v_texcoord;
varying vec4 v_shadowTexcoord;
varying vec3 v_normal;
void main() {
gl_Position = u_worldViewMatrix * a_position;
v_position = u_world * a_position;
v_texcoord = a_texcoord;
v_shadowTexcoord = u_shadowMatrix * u_world * a_position;
v_normal = mat3(u_world) * a_normal;
}
</script>
<script id="default-fragment" type="shader/fragment">
precision mediump float;
varying vec2 v_texcoord;
varying vec4 v_shadowTexcoord;
varying vec3 v_normal;
uniform sampler2D u_texture;
uniform sampler2D u_shadowTexture;
uniform vec3 u_reverseLightDir;
void main() {
vec3 normal = normalize(v_normal);
float light = (1.0 + dot(normal, u_reverseLightDir)) / 2.0;
vec3 shadowTexcoord = v_shadowTexcoord.xyz / v_shadowTexcoord.w;
float currDepth = shadowTexcoord.z;
bool inRange =
shadowTexcoord.x >= 0.0 &&
shadowTexcoord.x <= 1.0 &&
shadowTexcoord.y >= 0.0 &&
shadowTexcoord.y <= 1.0;
vec4 texColor = texture2D(u_texture, v_texcoord) * light;
float projDepth = texture2D(u_shadowTexture, shadowTexcoord.xy).r;
float shadowLight = (inRange && projDepth <= currDepth) ? 0.0 : 1.0;
gl_FragColor = vec4(texColor.rgb * shadowLight, texColor.a);
}
</script>
<script id="gooch-fragment" type="shader/fragment">
precision mediump float;
varying vec2 v_texcoord;
varying vec3 v_normal;
uniform sampler2D u_texture;
uniform vec3 u_reverseLightDir;
void main() {
vec3 normal = normalize(v_normal);
float light = (1.0 + dot(normal, u_reverseLightDir)) / 2.0;
vec4 color = texture2D(u_texture, v_texcoord);
float alpha = 0.2;
float beta = 0.4;
vec3 coolColor = vec3(129.0 / 255.0, 188.0 / 255.0, 230.0 / 255.0);
vec3 warmColor = vec3(230.0 / 255.0, 189.0 / 255.0, 129.0 / 255.0);
vec3 kCool = coolColor + vec3(alpha) * color.xyz;
vec3 kWarm = warmColor + vec3(beta) * color.xyz;
vec3 gooch = (light * kWarm) + ((1.0 - light) * kCool);
gl_FragColor = vec4(gooch, 1);
}
</script>
<script id="comics-fragment" type="shader/fragment">
precision mediump float;
varying vec2 v_texcoord;
varying vec3 v_normal;
uniform sampler2D u_texture;
uniform vec3 u_reverseLightDir;
void main() {
// color
vec3 normal = normalize(v_normal);
float light = (1.0 + dot(normal, u_reverseLightDir)) / 2.0;
vec4 color = texture2D(u_texture, v_texcoord);
vec3 shadow = vec3(0.5, 0.5, 0.8);
float f = step(0.2, light) * (0.5 * step(0.4, light) + 0.5);
vec3 col = vec3(f) * color.xyz + vec3(1.0 - f) * color.xyz * shadow.xyz;
gl_FragColor = vec4(col, 1);
}
</script>
<script id="drawing-fragment" type="shader/fragment">
precision mediump float;
varying vec4 v_position;
varying vec2 v_texcoord;
varying vec3 v_normal;
uniform sampler2D u_texture;
uniform sampler2D u_textureHash;
uniform vec3 u_reverseLightDir;
uniform mat4 u_cameraView;
uniform float u_time;
float rand(vec2 st) {
return fract(sin(dot(st.xy, vec2(12.9898,78.233))) * 43758.5453123);
}
float noise(vec2 st) {
vec2 i = floor(st);
vec2 f = fract(st);
float a = rand(i);
float b = rand(i + vec2(1.0, 0.0));
float c = rand(i + vec2(0.0, 1.0));
float d = rand(i + vec2(1.0, 1.0));
vec2 u = smoothstep(0.,1.,f);
return mix(a, b, u.x) +
(c - a)* u.y * (1.0 - u.x) +
(d - b) * u.x * u.y;
}
void main() {
// color
vec3 normal = normalize(v_normal);
float light = (1.0 + dot(normal, u_reverseLightDir)) / 2.0;
// generated coords
float scale = 10.0;
float time = cos(u_time * 10.0);
time = (floor(time) + ceil(time)) * 0.25;
vec4 pos = v_position * u_cameraView;
vec2 st = vec2(pos.x * scale, pos.y * scale * 0.5 + time);
vec2 noiseSt = vec2(v_position.x * scale, v_position.y * scale);
vec3 filter = vec3(step(0.3, noise(noiseSt * 100.0)) * (1.0 - step(0.4, light)) -
step(0.8, noise(noiseSt * 10.0) * 0.5 + noise(noiseSt * 2.0)) * (step(0.388, light)));
vec3 colorHash = texture2D(u_textureHash, st).rgb * filter;
vec3 shadow = vec3(0.5, 0.5, 0.5);
float f = step(0.2, light);
vec3 col = 1.0 - vec3((f - step(0.205, light))) * step(0.3, noise(noiseSt * 10.0)) * shadow.rgb -
vec3(1.0 - f) * shadow.rgb;
gl_FragColor = vec4(col - colorHash, 1.0);
}
</script>
<script id="default-vertex-postp" type="shader/vertex">
attribute vec4 a_position;
attribute vec2 a_texcoord;
varying vec2 v_texcoord;
void main() {
gl_Position = a_position;
v_texcoord = a_texcoord;
}
</script>
<script id="default-fragment-postp" type="shader/fragment">
precision mediump float;
varying vec2 v_texcoord;
uniform sampler2D u_screenTexture;
void main() {
gl_FragColor = texture2D(u_screenTexture, v_texcoord);
}
</script>
<script id="outline-fragment-postp" type="shader/fragment">
precision mediump float;
varying vec2 v_texcoord;
uniform sampler2D u_depthTexture;
uniform sampler2D u_screenTexture;
uniform float u_width;
uniform float u_height;
void main() {
const float outlineWidth = 5.0;
vec3 n[9];
float w = (1.0 / u_width) * outlineWidth;
float h = (1.0 / u_height) * outlineWidth;
vec3 leftColor = texture2D(u_depthTexture, v_texcoord + vec2(-w, 0)).rgb;
vec3 rightColor = texture2D(u_depthTexture, v_texcoord + vec2(w, 0)).rgb;
vec3 topColor = texture2D(u_depthTexture, v_texcoord + vec2(0, h)).rgb;
vec3 bottomColor = texture2D(u_depthTexture, v_texcoord + vec2(0, -h)).rgb;
vec2 gradient = vec2(length(rightColor - leftColor), length(topColor - bottomColor));
float outline = 1.0 - step(0.01, length(gradient));
vec3 color = vec3(outline) * texture2D(u_screenTexture, v_texcoord).rgb;
gl_FragColor = vec4(color, 1.0);
}
</script>
</body>
</html>