-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathscribble02.html
463 lines (411 loc) · 13.6 KB
/
scribble02.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
<!--
* Copyright 2015, Greggman.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Greggman. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
<html>
<head>
<meta charset="utf-8">
<title>Scribble</title>
<style>
html, body {
width: 100%;
height: 100%;
padding: 0px;
margin: 0px;
overflow: hidden;
}
canvas {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<canvas id="c"></canvas>
</body>
<script id="sphereVertexShader" type="text/something-not-javascript">
uniform mat4 worldViewProjection;
uniform sampler2D u_random;
uniform vec4 u_scale;
uniform vec2 u_offset;
attribute vec4 position;
varying vec4 v_position;
void main() {
vec4 random = texture2D(u_random, position.xy + u_offset) - 0.5;
gl_Position = (worldViewProjection * (position + random * u_scale));
v_position = gl_Position;
}
</script>
<script id="sphereFragmentShader" type="text/something-not-javascript">
precision mediump float;
uniform vec4 u_color;
uniform sampler2D u_random;
varying vec4 v_position;
void main() {
vec4 rand = texture2D(u_random, (v_position.xy * 0.1) + (gl_FragCoord.xy) * 0.001);
float m = rand.x * rand.y;
//m = step(0.3, m);
m = 0.65 - m;
m = m * (2.0 - (v_position.z * 0.5 + 0.5));
vec4 color = u_color;
color = vec4(0,0,0,1);
gl_FragColor = mix(vec4(1,1,1,1), color, m);
}
</script>
<!-- ################ -->
<script id="phongVertexShader" type="text/something-not-javascript">
uniform mat4 worldViewProjection;
uniform vec3 lightWorldPos;
uniform mat4 world;
uniform mat4 viewInverse;
uniform mat4 worldInverseTranspose;
attribute vec4 position;
attribute vec3 normal;
attribute vec2 texCoord;
varying vec4 v_position;
varying vec2 v_texCoord;
varying vec3 v_normal;
varying vec3 v_surfaceToLight;
varying vec3 v_surfaceToView;
void main() {
v_texCoord = texCoord;
v_position = (worldViewProjection * position);
v_normal = (worldInverseTranspose * vec4(normal, 0)).xyz;
v_surfaceToLight = lightWorldPos - (world * position).xyz;
v_surfaceToView = (viewInverse[3] - (world * position)).xyz;
gl_Position = v_position;
}
</script>
<script id="phongFragmentShader" type="text/something-not-javascript">
precision mediump float;
uniform vec4 lightColor;
varying vec4 v_position;
varying vec2 v_texCoord;
varying vec3 v_normal;
varying vec3 v_surfaceToLight;
varying vec3 v_surfaceToView;
uniform vec4 specular;
uniform sampler2D u_random;
uniform float shininess;
uniform float specularFactor;
vec4 lit(float l ,float h, float m) {
return vec4(1.0,
max(l, 0.0),
(l > 0.0) ? pow(max(0.0, h), m) : 0.0,
1.0);
}
void main() {
vec4 diffuse = texture2D(u_random, v_position.xy * 0.125 + gl_FragCoord.xy * 0.0001);
float bw = diffuse.x * diffuse.y * diffuse.z;
float bw2 = 1.0 - pow(bw, 2.0);
diffuse = vec4(bw2, bw2, bw2, 1);
vec3 normal = normalize(v_normal);
vec3 surfaceToLight = normalize(v_surfaceToLight);
vec3 surfaceToView = normalize(v_surfaceToView);
vec3 halfVector = normalize(surfaceToLight + surfaceToView);
vec4 litR = lit(dot(normal, surfaceToLight),
dot(normal, halfVector), shininess);
gl_FragColor = vec4((
(diffuse * litR.y
+ specular * litR.z * specularFactor)).rgb,
diffuse.a);
gl_FragColor.a = 1.0 - bw2;//diffuse.a;
}
</script>
<script type="text/javascript" src="js/typeface-0.15.js"></script>
<script type="text/javascript" src="js/tdl.min.js"></script>
<script type="text/javascript">
var g = {
across: 4,
hdiv: 5,
vdiv: 4,
};
// globals
var gl; // the gl context.
var canvas; // the canvas
var math; // the math lib.
var fast; // the fast math lib.
var g_logGLCalls = true; // whether or not to log webgl calls
var g_debug = false; // whether or not to debug.
var g_drawOnce = false; // draw just one frame.
//g_drawOnce = true;
//g_debug = true;
var g_eyeSpeed = 0.05;
var g_eyeHeight = 2;
var g_eyeRadius = 4;
var g_randomTexture;
function ValidateNoneOfTheArgsAreUndefined(functionName, args) {
for (var ii = 0; ii < args.length; ++ii) {
if (args[ii] === undefined) {
tdl.error("undefined passed to gl." + functionName + "(" +
tdl.webgl.glFunctionArgsToString(functionName, args) + ")");
}
}
}
function Log(msg) {
if (g_logGLCalls) {
tdl.log(msg);
}
}
function LogGLCall(functionName, args) {
if (g_logGLCalls) {
ValidateNoneOfTheArgsAreUndefined(functionName, args)
tdl.log("gl." + functionName + "(" +
tdl.webgl.glFunctionArgsToString(functionName, args) + ")");
}
}
function createProgramFromTags(vertexTagId, fragmentTagId) {
return tdl.programs.loadProgram(
document.getElementById(vertexTagId).text,
document.getElementById(fragmentTagId).text);
}
/**
* Sets up Planet.
*/
function setupSphere() {
var randomPixels = {
width: 256,
height: 256,
};
var pixels = new Uint8Array(randomPixels.width * randomPixels.height * 4);
for (var ii = 0; ii < pixels.length; ++ii) {
pixels[ii] = Math.random() * 256;
}
randomPixels.pixels = pixels;
var textures = {
u_random: new tdl.textures.ColorTexture(randomPixels),
};
g_randomTexture = textures.u_random;
var program = createProgramFromTags(
'sphereVertexShader',
'sphereFragmentShader');
var arrays = tdl.primitives.createSphere(0.4, g.hdiv, g.vdiv);
return new tdl.models.Model(program, arrays, textures, gl.LINES);
}
function setupSphere2() {
var textures = {
u_random: g_randomTexture,
};
var program = createProgramFromTags(
'phongVertexShader',
'phongFragmentShader');
var arrays = tdl.primitives.createSphere(0.30, g.hdiv * 3, g.vdiv * 3);
return new tdl.models.Model(program, arrays, textures);
}
function initialize() {
math = tdl.math;
fast = tdl.fast;
canvas = document.getElementById("c");
gl = tdl.webgl.setupWebGL(canvas, {alpha: false});
if (!gl) {
return false;
}
if (g_debug) {
gl = tdl.webgl.makeDebugContext(gl, undefined, LogGLCall);
}
tdl.misc.applyUrlSettings(g);
Log("--Setup Sphere---------------------------------------");
var sphere = setupSphere();
var sphere2 = setupSphere2();
var then = 0.0;
var clock = 0.0;
// pre-allocate a bunch of arrays
var projection = new Float32Array(16);
var view = new Float32Array(16);
var world = new Float32Array(16);
var worldInverse = new Float32Array(16);
var worldInverseTranspose = new Float32Array(16);
var viewProjection = new Float32Array(16);
var worldViewProjection = new Float32Array(16);
var viewInverse = new Float32Array(16);
var viewProjectionInverse = new Float32Array(16);
var eyePosition = new Float32Array(3);
var target = new Float32Array(3);
var up = new Float32Array([0,1,0]);
var lightWorldPos = new Float32Array(3);
var v3t0 = new Float32Array(3);
var v3t1 = new Float32Array(3);
var v3t2 = new Float32Array(3);
var v3t3 = new Float32Array(3);
var m4t0 = new Float32Array(16);
var m4t1 = new Float32Array(16);
var m4t2 = new Float32Array(16);
var m4t3 = new Float32Array(16);
var zero4 = new Float32Array(4);
var one4 = new Float32Array([1,1,1,1]);
// Sphere uniforms.
var sphereConst = {
};
var spherePer = {
worldViewProjection: worldViewProjection,
u_color: new Float32Array([1, 1, 1, 1]),
u_scale: new Float32Array([0.1, 0.1, 0.1, 0]),
u_offset: new Float32Array([1, 1]),
};
var sphere2Const = {
viewInverse: viewInverse,
lightWorldPos: lightWorldPos,
specular: one4,
shininess: 50,
specularFactor: 0.2,
};
var sphere2Per = {
lightColor: new Float32Array([0,0,0,1]),
world: world,
worldViewProjection: worldViewProjection,
worldInverse: worldInverse,
worldInverseTranspose: worldInverseTranspose,
};
var resize = function(gl) {
var width = gl.canvas.clientWidth;
var height = gl.canvas.clientHeight;
if (width != gl.canvas.width || height != gl.canvas.height) {
gl.canvas.width = width;
gl.canvas.height = height;
}
};
var frameCount = 0;
var wiggleClock = 0;
function render() {
++frameCount;
var now = (new Date()).getTime() * 0.001;
var elapsedTime;
if(then == 0.0) {
elapsedTime = 0.0;
} else {
elapsedTime = now - then;
}
then = now;
clock += elapsedTime;
if (frameCount % 5 == 0)
{
wiggleClock = clock;
}
resize(gl);
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
eyePosition[0] = Math.sin(clock * g_eyeSpeed) * g_eyeRadius;
eyePosition[1] = g_eyeHeight;
eyePosition[2] = Math.cos(clock * g_eyeSpeed) * g_eyeRadius;
gl.colorMask(true, true, true, true);
gl.depthMask(true);
gl.clearColor(1, 1, 1, 1);
gl.clearDepth(1);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
gl.enable(gl.CULL_FACE);
gl.enable(gl.DEPTH_TEST);
gl.lineWidth(1);
gl.enable(gl.BLEND);
fast.matrix4.perspective(
projection,
math.degToRad(60),
canvas.clientWidth / canvas.clientHeight,
1,
5000);
fast.matrix4.lookAt(
view,
eyePosition,
target,
up);
fast.matrix4.mul(viewProjection, view, projection);
fast.matrix4.inverse(viewInverse, view);
fast.matrix4.inverse(viewProjectionInverse, viewProjection);
fast.matrix4.getAxis(v3t0, viewInverse, 0); // x
fast.matrix4.getAxis(v3t1, viewInverse, 1); // y;
fast.matrix4.getAxis(v3t2, viewInverse, 2); // z;
fast.mulScalarVector(v3t0, 10, v3t0);
fast.mulScalarVector(v3t1, 10, v3t1);
fast.mulScalarVector(v3t2, 10, v3t2);
fast.addVector(lightWorldPos, eyePosition, v3t0);
fast.addVector(lightWorldPos, lightWorldPos, v3t1);
fast.addVector(lightWorldPos, lightWorldPos, v3t2);
// view: view,
// projection: projection,
// viewProjection: viewProjection,
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
Log("--Draw sphere---------------------------------------");
sphere2.drawPrep(sphere2Const);
var across = g.across;
var lightColor = sphere2Per.lightColor;
var half = (across - 1) * 0.5;
for (var xx = 0; xx < across; ++xx) {
for (var yy = 0; yy < across; ++yy) {
for (var zz = 0; zz < across; ++zz) {
lightColor[0] = xx / across * 0.5 + 0.5;
lightColor[1] = yy / across * 0.5 + 0.5;
lightColor[2] = zz / across * 0.5 + 0.5;
var scale = (xx + yy + zz) % 4 / 4 + 0.5;
fast.matrix4.scaling(m4t0, [scale, scale, scale]);
fast.matrix4.translation(m4t1, [xx - half, yy - half, zz - half]);
fast.matrix4.mul(world, m4t0, m4t1);
fast.matrix4.translation(world, [xx - half, yy - half, zz - half]);
fast.matrix4.mul(worldViewProjection, world, viewProjection);
fast.matrix4.inverse(worldInverse, world);
fast.matrix4.transpose(worldInverseTranspose, worldInverse);
sphere2.draw(sphere2Per);
}
}
}
gl.enable(gl.BLEND);
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
sphere.drawPrep(sphereConst);
var across = g.across;
var color = spherePer.u_color;
var half = (across - 1) * 0.5;
for (var xx = 0; xx < across; ++xx) {
for (var yy = 0; yy < across; ++yy) {
for (var zz = 0; zz < across; ++zz) {
color[0] = xx / across;
color[1] = yy / across;
color[2] = zz / across;
spherePer.u_offset[0] = xx / across + wiggleClock;
spherePer.u_offset[1] = yy / across + wiggleClock;
var scale = (xx + yy + zz) % 4 / 4 + 0.5;
fast.matrix4.scaling(m4t0, [scale, scale, scale]);
fast.matrix4.translation(m4t1, [xx - half, yy - half, zz - half]);
fast.matrix4.mul(world, m4t0, m4t1);
fast.matrix4.translation(world, [xx - half, yy - half, zz - half]);
fast.matrix4.mul(worldViewProjection, world, viewProjection);
fast.matrix4.inverse(worldInverse, world);
fast.matrix4.transpose(worldInverseTranspose, worldInverse);
sphere.draw(spherePer);
}
}
}
// turn off logging after 1 frame.
g_logGLCalls = false;
if (!g_drawOnce) {
tdl.webgl.requestAnimationFrame(render, canvas);
}
}
render();
return true;
}
initialize();
</script>
</html>