-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathlightball02-vertex-shader-discard.html
441 lines (400 loc) · 12.2 KB
/
lightball02-vertex-shader-discard.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebGL vertexId example</title>
<style>
html, body {
width: 100%;
height: 100%;
border: 0px;
padding: 0px;
margin: 0px;
background-color: red;
font-family: monospace;
overflow: hidden;
color: #fff;
}
a {
color: #fff;
}
#info {
font-size: small;
position: absolute;
top: 0px; width: 100%;
padding: 5px;
text-align: center;
z-index: 2;
}
CANVAS {
background-color: gray;
}
.fpsContainer {
position: absolute;
top: 10px;
left: 10px;
z-index: 2;
color: white;
background-color: rgba(0,0,0,0.5);
border-radius: 10px;
padding: 10px;
}
#viewContainer {
width: 100%;
height: 100%;
}
</style>
<script type="text/javascript" src="js/tdl.min.js"></script>
<script type="text/javascript">
//tdl.require('tdl.buffers');
//tdl.require('tdl.fast');
//tdl.require('tdl.fps');
//tdl.require('tdl.log');
//tdl.require('tdl.math');
//tdl.require('tdl.models');
//tdl.require('tdl.primitives');
//tdl.require('tdl.programs');
//tdl.require('tdl.textures');
//tdl.require('tdl.webgl');
window.onload = initialize;
// globals
var gl; // the gl context.
var canvas; // the canvas
var math; // the math lib.
var fast; // the fast math lib.
var g_eyeSpeed = 0.1;
var g_eyeHeight = 2;
var g_eyeRadius = 9;
var expandIndexedVertices = function(arrays) {
var indices = arrays.indices;
var numVerts = indices.numElements * 3;
var newArrays = {};
Object.keys(arrays).forEach(function(key) {
if (key !== 'indices') {
var srcArray = arrays[key]
var dstArray = new tdl.primitives.AttribBuffer(srcArray.numComponents, numVerts, srcArray.type);
newArrays[key] = dstArray;
for (var ndx = 0; ndx < numVerts; ++ndx) {
var verts = indices.getElement(ndx);
dstArray.push(srcArray.getElement(verts[0]));
dstArray.push(srcArray.getElement(verts[1]));
dstArray.push(srcArray.getElement(verts[2]));
}
}
});
return newArrays;
}
function createProgramFromTags(vertexTagId, fragmentTagId) {
return tdl.programs.loadProgram(
document.getElementById(vertexTagId).text,
document.getElementById(fragmentTagId).text);
}
/**
* Sets up Planet.
*/
function setupSphere() {
var textures = {};
var program = createProgramFromTags(
'sphereVertexShader',
'sphereFragmentShader');
var arrays = tdl.primitives.createSphere(4, 48, 24);
arrays = expandIndexedVertices(arrays);
var numVerts = arrays.position.numElements;
arrays.vertexId = new tdl.primitives.AttribBuffer(1, numVerts);
for (var ii = 0; ii < numVerts; ++ii) {
arrays.vertexId.push([ii]);
}
return new tdl.models.Model(program, arrays, textures);
}
function setupCube() {
var textures = {};
var program = createProgramFromTags(
'cubeVertexShader',
'cubeFragmentShader');
var arrays = tdl.primitives.createCube(4);
return new tdl.models.Model(program, arrays, textures);
}
var numberSuffix = (function() {
var suffixes = [ "th", "st", "nd", "rd" ];
return function(v) {
return suffixes[v] || "th";
};
}());
function initialize() {
math = tdl.math;
fast = tdl.fast;
canvas = document.getElementById("canvas");
gl = tdl.webgl.setupWebGL(canvas, { alpha: false });
if (!gl) {
return false;
}
function getTextNode(id) {
var elem = document.getElementById(id);
var node = document.createTextNode("");
elem.appendChild(node);
return node;
}
var sphere = setupSphere();
var cube = setupCube();
var then = 0.0;
var clock = 0.0;
var triModNode = getTextNode("triMod");
var triToHideNode = getTextNode("triToHide")
// 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 = {
viewInverse: viewInverse,
lightWorldPos: lightWorldPos,
specular: one4,
shininess: 50,
specularFactor: 0.2};
var spherePer = {
lightColor: new Float32Array([0,0,0,1]),
world: world,
worldViewProjection: worldViewProjection,
worldInverse: worldInverse,
worldInverseTranspose: worldInverseTranspose,
triMod: 10,
triToHide: 5,
};
var frameCount = 0;
function render() {
if (frameCount % (60 * 3) == 0) {
spherePer.triMod = spherePer.triMod + 1;
if (spherePer.triMod > 20) {
spherePer.triMod = 2;
}
triModNode.nodeValue = spherePer.triMod;
}
if (frameCount % 30 == 0) {
spherePer.triToHide = (spherePer.triToHide + 1) % spherePer.triMod;
var byOne = spherePer.triToHide + 1;
triToHideNode.nodeValue = byOne.toString() + numberSuffix(byOne);
}
++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;
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(0,0,0,0);
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);
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,
var lightColor = spherePer.lightColor;
var scale = 1;
fast.matrix4.rotationY(m4t0, clock * 0.63);
fast.matrix4.rotationZ(m4t1, clock * 0.77);
fast.matrix4.mul(world, m4t0, m4t1);
fast.matrix4.mul(worldViewProjection, world, viewProjection);
fast.matrix4.inverse(worldInverse, world);
fast.matrix4.transpose(worldInverseTranspose, worldInverse);
lightColor[0] = 0.7;
lightColor[1] = 0.5;
lightColor[2] = 1;
cube.drawPrep(sphereConst);
cube.draw(spherePer);
fast.matrix4.scaling(m4t0, [scale, scale, scale]);
fast.matrix4.translation(m4t1, [0, 0, 0]);
fast.matrix4.mul(world, m4t0, m4t1);
fast.matrix4.mul(worldViewProjection, world, viewProjection);
fast.matrix4.inverse(worldInverse, world);
fast.matrix4.transpose(worldInverseTranspose, worldInverse);
lightColor[0] = 0.5;
lightColor[1] = 1;
lightColor[2] = 0.7;
sphere.drawPrep(sphereConst);
sphere.draw(spherePer);
tdl.webgl.requestAnimationFrame(render, canvas);
}
render();
return true;
}
</script>
</head>
<body>
<div id="info"><a href="http://threedlibrary.googlecode.com" target="_blank">tdl.js</a> - vertexId example</div>
<div class="fpsContainer">
<div class="fps">for every <span id="triMod"></span> triangles hide the <span id="triToHide"></span> one</div>
</div>
<div id="viewContainer">
<canvas id="canvas" width="1024" height="1024" style="width: 100%; height: 100%;"></canvas>
</div>
</body>
<script id="sphereVertexShader" type="text/something-not-javascript">
uniform mat4 worldViewProjection;
uniform vec3 lightWorldPos;
uniform mat4 world;
uniform mat4 viewInverse;
uniform mat4 worldInverseTranspose;
uniform float triMod;
uniform float triToHide;
attribute vec4 position;
attribute vec3 normal;
attribute vec2 texCoord;
attribute float vertexId;
varying vec4 v_position;
varying vec2 v_texCoord;
varying vec3 v_normal;
varying vec3 v_surfaceToLight;
varying vec3 v_surfaceToView;
void main() {
v_position = (worldViewProjection * position);
float triangleId = floor(vertexId / 3.0) + 0.05; // apparently we need to add a tiny bit so it doesn't underflow
float triId = mod(triangleId, triMod);
float diff = abs(triId - triToHide);
if (diff < 0.1) {
v_position = vec4(0, 0, 2, 1); // put the vertex behind the camera
}
v_texCoord = texCoord;
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="sphereFragmentShader" 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 bumpSampler;
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() {
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((
lightColor * (litR.y
+ specular * litR.z * specularFactor)).rgb,
1);
}
</script>
<script id="cubeVertexShader" 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="cubeFragmentShader" 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 bumpSampler;
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() {
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((
lightColor * (litR.y
+ specular * litR.z * specularFactor)).rgb,
1);
}
</script>
</html>