-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathindex.html
More file actions
executable file
·366 lines (257 loc) · 9.85 KB
/
index.html
File metadata and controls
executable file
·366 lines (257 loc) · 9.85 KB
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
<!doctype html>
<html lang="en">
<head>
<title>Numerical renderer</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link rel="stylesheet" type="text/css" href="../css/styles.css" media="screen" />
<link href="https://fonts.googleapis.com/css?family=Lato:100,700|Playfair+Display:700" rel="stylesheet">
</head>
<body>
<div id="container"></div>
<div id="info" >
<p><a href="../20" >Previous</a> | <a href="../22" >Next</a></p>
<h1>21. Numerical renderer</h1>
<p>Like those ASCII renderers, but with less characters: just 10.</p>
<p id="moreDetails"><b>More details...</b></p>
<div id="details" class="hidden">
<p>A simple scene rendered with lights, shadows and MeshStandardMaterial is rendered to a framebuffer.</p>
<p>That framebuffer is then processed to add some glow, and then a scaled down version is used to feed a shader that will replace every block of 32x32 pixels with a part of a texture that has the numbers 0 to 9, according to the brightness of the block. The number is aditionally shaded by that intensity, so there's more gradients. The texture with numbers has a slight glow preapplied on it.</p>
<p><b>Credits</b></p>
<p>Inspired by <a href="http://archillect.com/40844" >this animation</a>.</p>
<p>Coded using WebGL with <a href="http://threejs.org/" >three.js</a>, <a href="https://github.com/kaimallea/isMobile" >isMobile.js</a> and <a href="https://github.com/spite/Maf.js/tree/master" >Maf.js</a></p>
</div>
<p><a href="https://clicktorelease.com/code/codevember-2016/" >See other experiments for Codevember 2016</a></p>
</div>
<script src="../js/three.js"></script>
<script src="../js/OrbitControls.js"></script>
<script src="../js/Maf.js"></script>
<script src="../js/THREE.FBOHelper.js"></script>
<script src="../js/Common.js"></script>
<script src="../js/isMobile.min.js"></script>
<script type="x-shader/x-vertex" id="ortho-vs" >
precision highp float;
attribute vec3 position;
attribute vec2 uv;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1. );
}
</script>
<script type="x-shader/x-fragment" id="copy-fs" >
precision highp float;
uniform sampler2D inputTexture;
varying vec2 vUv;
void main() {
gl_FragColor = texture2D( inputTexture, vUv );
}
</script>
<script type="x-shader/x-fragment" id="glow-fs" >
precision highp float;
uniform sampler2D inputTexture;
uniform sampler2D glowTexture;
uniform vec2 resolution;
varying vec2 vUv;
void main() {
vec4 color = texture2D( inputTexture, vUv );
vec4 glow1 = texture2D( glowTexture, vUv, 1. );
vec4 glow2 = texture2D( glowTexture, vUv, 2. );
vec4 glow4 = texture2D( glowTexture, vUv, 4. );
vec4 glow8 = texture2D( glowTexture, vUv, 8. );
vec4 glow16 = texture2D( glowTexture, vUv, 16. );
vec4 glow = glow1;
glow += 2. * glow2;
glow += 4. * glow4;
glow += 8. * glow8;
glow += 16. * glow16;
glow /= 10.;
glow = clamp( glow, vec4( 0. ), vec4( 1. ) );
color += glow;
gl_FragColor = color;
}
</script>
<script type="x-shader/x-vertex" id="numerical-fs" >
precision highp float;
uniform sampler2D inputTexture;
uniform sampler2D gradientMap;
uniform vec2 resolution;
varying vec2 vUv;
void main() {
vec2 uv = vUv;
uv *= resolution;
uv = mod( uv, vec2( 1. ) );
if( uv.x < .05 || uv.y < .05) {
gl_FragColor = vec4(0.);
return;
}
float intensity = texture2D( inputTexture, vUv ).r;
float level = floor( intensity * 9. ) / 10.;
uv.x = uv.x / 10. + level;
gl_FragColor = 2. * intensity * texture2D( gradientMap, uv );
gl_FragColor.rgb *= vec3( 0., 169., 226. ) / 255.;
}
</script>
<script>
// http://archillect.com/40844
'use strict';
var container, renderer, camera, controls, scene;
var world = new THREE.Object3D(), mesh;
var helper;
var container = document.getElementById( 'container' );
var numericalShader, glowShader, copyShader, resolution = new THREE.Vector2();
var orthoQuad, orthoCamera, orthoScene;
var baseFBO, glowFBO, combinedFBO;
function createRenderTarget() {
return new THREE.WebGLRenderTarget( 1, 1, {
wrapS: THREE.ClampToEdgeWrapping,
wrapT: THREE.ClampToEdgeWrapping,
format: THREE.RGBAFormat,
stencilBuffer: false,
depthBuffer: true
});
}
function addRandomLight( intensity ) {
var x = Maf.randomInRange( -20, 20 );
var z = Maf.randomInRange( -20, 20 );
var light = new THREE.SpotLight( 0xffffff, intensity, 200, .4, .5, .5 );
light.position.set( x, 15, z );
light.position.normalize().multiplyScalar( 40 );
light.target.position.set( 0, 0, 0 );
light.castShadow = true;
scene.add( light );
renderer.render( scene, camera );
helper.attach( light.shadow.map, 'light' );
}
function initScene() {
var light = new THREE.AmbientLight( 0x101010 );
//scene.add( light );
var light = new THREE.HemisphereLight( 0, 0xffffff, .5 );
scene.add( light );
if( isMobile.any ) {
addRandomLight( 1 );
} else {
for( var j = 0; j < 2; j++ ) {
addRandomLight( .5 );
}
}
var geometry = new THREE.TorusBufferGeometry( 10, 3, 36, 6 );
var material = new THREE.MeshStandardMaterial( {
roughness: .5,
metalness: .1
} )
scene.add( world );
mesh = new THREE.Mesh( geometry, material );
world.add( mesh );
mesh.receiveShadow = true;
mesh.castShadow = true;
baseFBO = createRenderTarget();
glowFBO = createRenderTarget();
combinedFBO = createRenderTarget();
combinedFBO.texture.minFilter = THREE.NearestFilter;
combinedFBO.texture.magFilter = THREE.NearestFilter;
combinedFBO.texture.generateMipmaps = false;
glowFBO.texture.minFilter = THREE.LinearMipMapLinearFilter;
glowFBO.texture.wrapS = glowFBO.texture.wrapT = THREE.ClampToEdgeWrapping;
helper.attach( baseFBO, 'base' );
helper.attach( glowFBO, 'glow' );
helper.attach( combinedFBO, 'combined' );
numericalShader = new THREE.RawShaderMaterial( {
uniforms: {
gradientMap: { type: 't', value: new THREE.TextureLoader().load( '../assets/numbers.jpg' ) },
inputTexture: { type: 't', value: combinedFBO.texture },
resolution: { type: 'v2', value: new THREE.Vector2( 0, 0 ) },
boost: { type: 'f', value: 1 },
reduction: { type: 'f', value: 1.2 },
amount: { type: 'f', value: .05 },
time: { type: 'f', value: 0 }
},
vertexShader: document.getElementById('ortho-vs').textContent,
fragmentShader: document.getElementById('numerical-fs').textContent,
} );
copyShader = new THREE.RawShaderMaterial( {
uniforms: {
inputTexture: { type: 't', value: baseFBO.texture }
},
vertexShader: document.getElementById('ortho-vs').textContent,
fragmentShader: document.getElementById('copy-fs').textContent,
} );
glowShader = new THREE.RawShaderMaterial( {
uniforms: {
inputTexture: { type: 't', value: baseFBO.texture },
glowTexture: { type: 't', value: glowFBO.texture },
resolution: { type: 'v2', value: resolution }
},
vertexShader: document.getElementById('ortho-vs').textContent,
fragmentShader: document.getElementById('glow-fs').textContent,
} );
orthoScene = new THREE.Scene();
orthoCamera = new THREE.OrthographicCamera( 1 / - 2, 1 / 2, 1 / 2, 1 / - 2, .00001, 1000 );
orthoQuad = new THREE.Mesh( new THREE.PlaneBufferGeometry( 1, 1 ), glowShader );
orthoScene.add( orthoQuad );
}
function init() {
container = document.getElementById( 'container' );
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, .1, 1000 );
camera.target = new THREE.Vector3( 0, 0, 0 );
camera.position.set( 0, 0, 30 );
camera.lookAt( camera.target );
scene.add( camera );
renderer = new THREE.WebGLRenderer( { antialias: true, preserveDrawingBuffer: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setClearColor( 0, 1 );
container.appendChild( renderer.domElement );
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFShadowMap;
helper = new FBOHelper( renderer );
helper.show( false );
controls = new THREE.OrbitControls( camera, renderer.domElement );
addFullscreenShortcut( renderer.domElement, onWindowResized );
initScene();
onWindowResized();
window.addEventListener( 'resize', onWindowResized );
animate();
}
function onWindowResized( event ) {
var w = container.clientWidth;
var h = container.clientHeight;
renderer.setSize( w, h );
camera.aspect = w / h;
camera.updateProjectionMatrix();
var sW = Maf.nextPowerOfTwo( w ) / 2;
var sH = Maf.nextPowerOfTwo( h ) / 2;
var dPR = renderer.getPixelRatio();
resolution.set( w * dPR, h * dPR );
baseFBO.setSize( w * dPR, h * dPR );
combinedFBO.setSize( Math.floor( w * dPR / 32 ), Math.floor( h * dPR / 32 ) );
numericalShader.uniforms.resolution.value.set( combinedFBO.width, combinedFBO.height );
glowFBO.setSize( sW, sH );
orthoQuad.scale.set( w, h, 1 );
orthoCamera.left = - w / 2;
orthoCamera.right = w / 2;
orthoCamera.top = h / 2;
orthoCamera.bottom = - h / 2;
orthoCamera.updateProjectionMatrix();
helper.setSize( w, h );
}
function animate() {
requestAnimationFrame( animate );
controls.update();
var t = .001 * performance.now();
world.rotation.set( t, 1.1 * t, .9 * t );
renderer.render( scene, camera, baseFBO );
orthoQuad.material = copyShader;
renderer.render( orthoScene, orthoCamera, glowFBO );
orthoQuad.material = glowShader;
renderer.render( orthoScene, orthoCamera, combinedFBO );
orthoQuad.material = numericalShader;
renderer.render( orthoScene, orthoCamera );
helper.update();
}
window.addEventListener( 'load', init );
</script>
</body>
</html>