-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblob.html
298 lines (260 loc) · 7.8 KB
/
blob.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebGL Blobs</title>
<script src="webgl-debug.js"></script>
<script src="tdl/base.js"></script>
<script src="immediate_sim.js"></script>
<script src="marching_cubes_tables.js"></script>
<script src="marching_cubes.js"></script>
<script>
tdl.require('tdl.fast');
tdl.require('tdl.fps');
tdl.require('tdl.math');
tdl.require('tdl.primitives');
tdl.require('tdl.shader');
tdl.require('tdl.programs');
tdl.require('tdl.log');
tdl.require('tdl.models');
tdl.require('tdl.buffers');
tdl.require('tdl.framebuffers');
tdl.require('tdl.textures');
tdl.require('tdl.webgl');
var gl;
var canvas;
var aspect;
// Use this to refer to the backbuffer as if it were another framebuffer
var backbuffer;
var quad;
var imm;
var g_numBlobs;
var g_resolution;
var g_requestId;
if (!window.Float32Array) {
// This just makes some errors go away when there is no WebGL.
window.Float32Array = function() { };
}
// Useful global math constants
var up = new Float32Array([0, 1, 0])
var output = alert;
var curBeat = 0;
var then = 0.0;
var singleEffect = null;
var g_fpsTimer; // object to measure frames per second;
window.onload = function() {
g_fpsTimer = new tdl.fps.FPSTimer();
canvas = document.getElementById('render_area');
//tdl.webgl.registerContextLostHandler(canvas, handleContextLost);
//tdl.webgl.registerContextRestoredHandler(canvas, handleContextRestored);
setup();
}
function setup() {
if (initializeGraphics()) {
singleEffect = new MarchingCubesEffect(24);
g_numBlobs = 30;
mainloop()
}
}
function mainloop() {
var BPM = 60.0;
var timeStart = 0.0; // at what time does the beat start?
var timeScale = BPM / 60.0;
var frameCount = 0;
var totalFrameCount = 0;
var fpsElem = document.getElementById("fps");
function render() {
var now = (new Date()).getTime() * 0.001;
var elapsedTime;
if(then == 0.0) {
elapsedTime = 0.0;
} else {
elapsedTime = now - then;
}
then = now;
frameCount++;
g_fpsTimer.update(elapsedTime);
aspect = canvas.clientWidth / canvas.clientHeight
singleEffect.render(null, now, g_numBlobs)
frameCount++;
totalFrameCount++;
g_requestId = tdl.webgl.requestAnimationFrame(render, canvas);
}
// Repeatedly run render(), attempt to hold 60 but the demo is
// framerate independent so we will still keep sync even if we
// lose frames.
render();
}
function initializeGraphics() {
canvas = document.getElementById('render_area');
gl = tdl.webgl.setupWebGL(canvas);
if (!gl) {
return false;
}
aspect = canvas.clientWidth / canvas.clientHeight
backbuffer = tdl.framebuffers.getBackBuffer(canvas)
imm = new ImmSim()
// Set some sane defaults.
gl.disable(gl.BLEND);
gl.depthFunc(gl.LEQUAL);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
return true;
}
/*
function handleContextLost() {
tdl.log("context lost");
tdl.webgl.cancelRequestAnimationFrame(g_requestId);
}
function handleContextRestored() {
tdl.log("context restored");
setup();
}
*/
</script>
</head>
<body>
<div id="viewContainer">
<canvas id="render_area" width="1024" height="512" style="width: 100%; height: 100%;"></canvas>
</div>
</body>
<script id="spinning_cube_fs" type="x-shader/x-fragment">
#ifdef GL_ES
precision mediump float;
#endif
varying vec4 v_color;
void main(void) {
gl_FragColor = v_color;
}
</script>
<script id="spinning_cube_vs" type="x-shader/x-vertex">
attribute vec3 position;
attribute vec3 normal;
attribute vec2 texCoord;
uniform mat4 u_worldviewproj;
varying vec4 v_color;
void main(void) {
v_color = vec4(position.x + 0.5, position.y + 0.5, position.z + 0.5, 1.0);
gl_Position = u_worldviewproj * vec4(position, 1.0);
}
</script>
<script id="marching_cube_fs" type="x-shader/x-fragment">
#ifdef GL_ES
precision mediump float;
#endif
varying vec4 v_color;
varying vec3 v_normal;
uniform vec4 u_ambientUp;
uniform vec4 u_ambientDown;
uniform vec3 u_lightDir;
uniform vec4 u_lightColor;
varying vec3 v_eyeDir;
void main(void) {
float light = max(0.0, dot(normalize(u_lightDir), v_normal));
float glare = max(0.0, dot(v_normal, normalize(v_eyeDir + u_lightDir)));
glare = glare*glare;
glare = glare*glare;
glare = glare*glare;
glare = glare*glare;
glare = glare*glare;
glare = glare*glare;
vec4 ambient = mix(u_ambientDown, u_ambientUp, (v_normal.y + 1.0)/2.0);
// Gamma correction approximation (sqrt)
vec4 finalColor = sqrt(ambient + light * u_lightColor + glare);
finalColor.w = 1.0;
gl_FragColor = finalColor;
}
</script>
<script id="marching_cube_vs" type="x-shader/x-vertex">
attribute vec3 position;
attribute vec3 normal;
attribute vec2 texCoord;
uniform mat4 u_worldviewproj;
uniform mat4 u_worldview;
uniform mat4 u_world;
varying vec4 v_color;
varying vec3 v_normal;
varying vec3 v_eyeDir;
void main(void) {
v_color = vec4(position.x + 0.5, position.y + 0.5, position.z + 0.5, 1.0);
v_normal = (u_world * vec4(normalize(normal), 0.0)).xyz;
v_eyeDir = -normalize((u_worldview * vec4(position, 1.0)).xyz);
gl_Position = u_worldviewproj * vec4(position, 1.0);
}
</script>
<script id="blur_fs" type="text/something-not-javascript">
#ifdef GL_ES
precision mediump float;
#endif
varying vec2 v_texCoord;
uniform vec2 blurSize;
uniform vec4 subtract;
uniform sampler2D mainSampler;
void main() {
vec4 sum = vec4(0.0);
sum += texture2D(mainSampler, v_texCoord - 4.0 * blurSize) * 0.05;
sum += texture2D(mainSampler, v_texCoord - 3.0 * blurSize) * 0.09;
sum += texture2D(mainSampler, v_texCoord - 2.0 * blurSize) * 0.12;
sum += texture2D(mainSampler, v_texCoord - 1.0 * blurSize) * 0.15;
sum += texture2D(mainSampler, v_texCoord ) * 0.16;
sum += texture2D(mainSampler, v_texCoord + 1.0 * blurSize) * 0.15;
sum += texture2D(mainSampler, v_texCoord + 2.0 * blurSize) * 0.12;
sum += texture2D(mainSampler, v_texCoord + 3.0 * blurSize) * 0.09;
sum += texture2D(mainSampler, v_texCoord + 4.0 * blurSize) * 0.05;
gl_FragColor = sum - subtract;
}
</script>
<script id="radial_vs" type="text/something-not-javascript">
attribute vec4 position;
attribute vec2 texCoord;
varying vec4 v_position;
varying vec2 v_texCoord0, v_texCoord1, v_texCoord2, v_texCoord3;
uniform float amount;
void main() {
vec2 tc = (position.xy + vec2(1.0, 1.0)) / 2.0;
// tc.y = 1.0 - tc.y;
v_texCoord0 = tc;
v_texCoord1 = 0.5 + (tc-0.5) * (1.0 / (1.0 + amount));
v_texCoord2 = 0.5 + (tc-0.5) * (1.0 / (1.0 + amount * 0.5));
v_texCoord3 = 0.5 + (tc-0.5) * (1.0 / (1.0 + amount * 1.5));
gl_Position = position;
}
</script>
<script id="radial_fs" type="text/something-not-javascript">
#ifdef GL_ES
precision mediump float;
#endif
varying vec2 v_texCoord0, v_texCoord1, v_texCoord2, v_texCoord3;
uniform sampler2D mainSampler;
uniform float amount;
uniform float glow;
void main() {
vec4 c1 = texture2D(mainSampler, v_texCoord0);
vec4 c2 = texture2D(mainSampler, v_texCoord1);
vec4 c3 = texture2D(mainSampler, v_texCoord2);
vec4 c4 = texture2D(mainSampler, v_texCoord3);
gl_FragColor = (c1 + c2 + c3 + c4) * glow / 4.0;
}
</script>
<script id="copy_fs" type="text/something-not-javascript">
#ifdef GL_ES
precision mediump float;
#endif
varying vec2 v_texCoord;
uniform sampler2D mainSampler;
void main() {
gl_FragColor = texture2D(mainSampler, v_texCoord);
}
</script>
<script id="add_fs" type="text/something-not-javascript">
#ifdef GL_ES
precision mediump float;
#endif
varying vec2 v_texCoord;
uniform sampler2D mainSampler;
uniform sampler2D secondSampler;
void main() {
// TODO: Gamma correct add?
gl_FragColor = texture2D(mainSampler, v_texCoord) + texture2D(secondSampler, v_texCoord);
}
</script>
</html>