-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtextme06.html
526 lines (364 loc) · 13.6 KB
/
textme06.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - postprocessing - glyphs</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
html, body {
font-family: Monospace;
background-color: #000;
color: #fff;
margin: 0px;
overflow: hidden;
width: 100%;
height: 100%;
}
canvas {
width: 100%;
height: 100%;
}
#info {
position: absolute;
top: 10px;
width: 100%;
text-align: center;
z-index: 100;
display:block;
}
#info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
.dg.ac {
z-index: 1000 !important;
}
</style>
</head>
<body>
<div id="info">
<a href="http://threejs.org" target="_blank">three.js</a> - glyph postprocessing - by <a href="http://games.greggman.com" target="_blank">greggman</a>
</div>
<script src="js/three.min.js"></script>
<script src="js/three/js/utils/GeometryUtils.js"></script>
<script src="js/three/js/libs/kd-tree.js"></script>
<script src="js/three/js/utils/GlyphUtilsV2.js"></script>
<script src="js/three/js/shaders/CopyShader.js"></script>
<script src="js/three/js/shaders/GlyphShaderV2.js"></script>
<script src="js/three/js/postprocessing/EffectComposer.js"></script>
<script src="js/three/js/postprocessing/MaskPass.js"></script>
<script src="js/three/js/postprocessing/RenderPass.js"></script>
<script src="js/three/js/postprocessing/ShaderPass.js"></script>
<script src="js/three/js/postprocessing/GlyphPassV2.js"></script>
<script src="js/three/js/Detector.js"></script>
<script src="js/three/js/libs/stats.min.js"></script>
<script src="js/three/js/libs/dat.gui.min.js"></script>
<script>
"use strict";
if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
var container, stats, permalink, hex, color;
var camera, cameraTarget, scene, renderer;
var composer;
var renderTarget;
var group, textMesh1, textMesh2, textGeo, material;
var scenes = {};
var text = "three.js",
height = 20,
size = 70,
hover = 30;
var targetRotation = 0;
var targetRotationOnMouseDown = 0;
var mouseX = 0;
var mouseXOnMouseDown = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
var postprocessing = { enabled : true };
var glow = 0.9;
var globalOptions = {
};
var fontOptions = {
font: "monospace",
fontSize: 20,
};
var fontFamilies = {
"monospace": "monospace",
"courier": "'Courier New',Courier,monospace",
"narrow-serif": "'Courier New',Courier,monospace",
"wide-serif": "Georgia,Utopia,Charter,serif",
"narrow-sans-serif": "Helvetica,Arial,sans-serif",
"wide-sans-serif": "Verdana,Geneva,sans-serif",
};
var renderModel;
var glyphExtras = {
color: [255, 255, 255 ],
};
var glyphPass;
var glyphSets = {};
var glyphPassOptions = {
glyphOptions: {
numLevels: 8,
checker: false,
weighNeighbors: false,
glyphSets: [ "ascii" ],
fontOptions: {
}
}
};
init();
animate();
function init() {
container = document.body;
// CAMERA
camera = new THREE.PerspectiveCamera( 30, 1, 1, 1500 );
camera.position.set( 0, 400, 700 );
cameraTarget = new THREE.Vector3( 0, 150, 0 );
globalOptions.scene = "spheres";
scenes.spheres = createSphereScene();
scenes.cubes = createCubeScene( false );
scenes[ "wireframe cubes" ] = createCubeScene( true );
scene = scenes.spheres;
// RENDERER
renderer = new THREE.WebGLRenderer( { antialias: true } );
container.appendChild( renderer.domElement );
renderer.setPixelRatio( 2 );
// STATS
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
//container.appendChild( stats.domElement );
// POSTPROCESSING
renderer.autoClear = false;
renderModel = new THREE.RenderPass( scene, camera );
glyphPass = new THREE.GlyphPass( glyphPassOptions );
glyphPass.renderToScreen = true;
var parameters = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat, stencilBuffer: false };
renderTarget = new THREE.WebGLRenderTarget( 1, 1, parameters );
setRenderTargetSize();
composer = new THREE.EffectComposer( renderer, renderTarget );
composer.addPass( renderModel );
composer.addPass( glyphPass );
//
initGUI();
// EVENTS
renderer.domElement.addEventListener( 'mousedown', onDocumentMouseDown, false );
renderer.domElement.addEventListener( 'touchstart', onDocumentTouchStart, false );
renderer.domElement.addEventListener( 'touchmove', onDocumentTouchMove, false );
window.addEventListener( 'resize', onWindowResize, false );
onWindowResize();
}
function createSphereScene() {
// SCENE
var scene = new THREE.Scene();
scene.fog = new THREE.Fog( 0x000000, 250, 1400 );
// LIGHTS
var dirLight = new THREE.DirectionalLight( 0xffffff, 1.725 );
dirLight.position.set( 200, 100, 100 ).normalize();
scene.add( dirLight );
var pointLight = new THREE.PointLight( 0xffffff, 1.5 );
pointLight.position.set( -120, 0, 50 );
scene.add( pointLight );
var group = new THREE.Group();
group.position.y = 120;
var radius = 150;
var geometry = new THREE.IcosahedronGeometry( radius, 1 );
var mat1 = new THREE.MeshLambertMaterial( { color: 0xff4f8f, shading: THREE.FlatShading } );
var modelM = new THREE.Mesh( geometry, mat1 );
group.add( modelM );
var geometry = new THREE.SphereGeometry( radius, 48, 24 );
var mat1 = new THREE.MeshLambertMaterial( { color: 0x4f8fff, shading: THREE.SmoothShading } );
var modelL = new THREE.Mesh( geometry, mat1 );
modelL.position.x -= radius * 2.5;
group.add( modelL );
var geometry = new THREE.SphereGeometry( radius, 4, 2 );
var mat1 = new THREE.MeshLambertMaterial( { color: 0x8fff4f, shading: THREE.FlatShading } );
var modelR = new THREE.Mesh( geometry, mat1 );
modelR.position.x = radius * 2.5;
group.add( modelR );
scene.add( group );
return {
update: function() {
modelR.rotation.z -= 0.003;
modelM.rotation.z += 0.002;
group.rotation.y += ( targetRotation - group.rotation.y ) * 0.05;
},
getScene: function() {
return scene;
}
};
}
function createCubeScene( wireframe ) {
// SCENE
var scene = new THREE.Scene();
scene.fog = new THREE.Fog( 0x000000, 250, 1400 );
// LIGHTS
var dirLight = new THREE.DirectionalLight( 0xffffff, 0.725 );
dirLight.position.set( 200, 100, 100 ).normalize();
scene.add( dirLight );
var pointLight = new THREE.HemisphereLight( 0xddeeff, 0x806040, 0.5 );
pointLight.position.set( -120, 0, 50 );
scene.add( pointLight );
var group = new THREE.Group();
group.position.y = 120;
var size = 50;
var spread = size * 2.5;
var geometry = new THREE.BoxGeometry( size, size, size );
var materials = [];
var num = 4;
for ( var xx = -num; xx <= num; ++xx ) {
for ( var yy = -num; yy <= num; ++yy ) {
for ( var zz = -num; zz <= num; ++zz ) {
var color = ( (( (xx / num * 0.5 + 0.5 ) * 255 ) | 0 ) << 0 ) |
( (( (yy / num * 0.5 + 0.5 ) * 255 ) | 0 ) << 8 ) |
( (( (zz / num * 0.5 + 0.5 ) * 255 ) | 0 ) << 16 ) ;
var mat = new THREE.MeshLambertMaterial( { color: color, shading: THREE.FlatShading, wireframe: wireframe } );
var model = new THREE.Mesh( geometry, mat );
materials.push( mat );
model.position.x = xx * spread;
model.position.y = yy * spread;
model.position.z = zz * spread;
group.add( model );
}
}
}
scene.add( group );
return {
resize: function() {
var lineWidth = Math.max(1, renderer.domElement.clientHeight / 30);
materials.forEach( function( mat ) {
mat.wireframeLinewidth = lineWidth;
});
},
update: function() {
group.rotation.z += 0.002;
group.rotation.y += ( targetRotation - group.rotation.y ) * 0.05;
},
getScene: function() {
return scene;
}
};
}
function isMobile() {
var ua = navigator.userAgent.toLowerCase();
return ua.indexOf( "iphone" ) >= 0 || ua.indexOf( "mobile" ) >= 0;
}
function initGUI() {
var gui = new dat.GUI();
gui.add( globalOptions, 'scene', Object.keys( scenes )).onChange( function() {
scene = scenes[ globalOptions.scene ];
} );
gui.add( postprocessing, 'enabled' );
gui.add( glyphPass, 'textScale', 0.2, 2.0 );
gui.addColor( glyphExtras, 'color' ).onChange( function() {
var c = glyphExtras.color;
glyphPass.color.setRGB( c[ 0 ] / 255, c[ 1 ] / 255, c[ 2 ] / 255 );
} );
var glyphsFolder = gui.addFolder( 'glyphs' );
Object.keys( THREE.GlyphUtils.unicodeRanges ).forEach( function( rangeName ) {
glyphSets[ rangeName ] = glyphPassOptions.glyphOptions.glyphSets.indexOf( rangeName ) >= 0;
glyphsFolder.add( glyphSets, rangeName ).onChange( function() {
updateGlyphSets();
} );
} );
var debugFolder = gui.addFolder( 'debug' );
debugFolder.add( glyphPassOptions.glyphOptions, 'numLevels', [ 2, 4, 8, 16, 32, 64 ] ).onFinishChange( updateGlyphSets );
debugFolder.add( glyphPassOptions.glyphOptions, 'weighNeighbors' ).onChange( updateGlyphSets );
debugFolder.add( glyphPassOptions.glyphOptions, 'checker' ).onChange( updateGlyphSets );
debugFolder.add( glyphPass, 'dosColors' );
debugFolder.add( fontOptions, 'font', Object.keys( fontFamilies )).onFinishChange( updateGlyphSets );
debugFolder.add( fontOptions, 'fontSize', 6, 32 ).step( 1 ).onFinishChange( updateGlyphSets );
if ( isMobile() ) {
gui.close();
}
}
function updateGlyphSets() {
glyphPassOptions.glyphOptions.glyphSets = Object.keys( glyphSets ).filter( function( set ) {
return glyphSets[ set ];
} );
glyphPassOptions.glyphOptions.fontOptions.font =
fontOptions.fontSize.toString() + "px " + fontFamilies[ fontOptions.font ];
console.log( glyphPassOptions.glyphOptions.fontOptions.font );
glyphPass.updateGlyphs( glyphPassOptions );
}
function setRenderTargetSize() {
// Make it twice as many pixels as there are glyphs
var width = ( renderer.context.drawingBufferWidth / ( glyphPass.glyphs.glyphWidth * glyphPass.textScale ) | 0 ) * 2;
var height = ( renderer.context.drawingBufferHeight / ( glyphPass.glyphs.glyphHeight * glyphPass.textScale ) | 0 ) * 2;
if ( renderTarget.width !== width || renderTarget.height !== height ) {
renderTarget.setSize( width, height );
}
}
function onWindowResize() {
windowHalfX = window.innerWidth / 2;
windowHalfY = window.innerHeight / 2;
renderer.setSize( renderer.domElement.clientWidth, renderer.domElement.clientHeight, false );
setRenderTargetSize();
composer.reset();
Object.keys( scenes ).forEach( function( sceneName ) {
var scene = scenes[ sceneName ];
if ( scene.resize ) {
scene.resize();
}
} );
}
//
function onDocumentMouseDown( event ) {
event.preventDefault();
renderer.domElement.addEventListener( 'mousemove', onDocumentMouseMove, false );
renderer.domElement.addEventListener( 'mouseup', onDocumentMouseUp, false );
renderer.domElement.addEventListener( 'mouseout', onDocumentMouseOut, false );
mouseXOnMouseDown = event.clientX - windowHalfX;
targetRotationOnMouseDown = targetRotation;
}
function onDocumentMouseMove( event ) {
event.preventDefault();
mouseX = event.clientX - windowHalfX;
targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
}
function onDocumentMouseUp( event ) {
event.preventDefault();
renderer.domElement.removeEventListener( 'mousemove', onDocumentMouseMove, false );
renderer.domElement.removeEventListener( 'mouseup', onDocumentMouseUp, false );
renderer.domElement.removeEventListener( 'mouseout', onDocumentMouseOut, false );
}
function onDocumentMouseOut( event ) {
event.preventDefault();
renderer.domElement.removeEventListener( 'mousemove', onDocumentMouseMove, false );
renderer.domElement.removeEventListener( 'mouseup', onDocumentMouseUp, false );
renderer.domElement.removeEventListener( 'mouseout', onDocumentMouseOut, false );
}
function onDocumentTouchStart( event ) {
if ( event.touches.length == 1 ) {
event.preventDefault();
mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
targetRotationOnMouseDown = targetRotation;
}
}
function onDocumentTouchMove( event ) {
if ( event.touches.length == 1 ) {
event.preventDefault();
mouseX = event.touches[ 0 ].pageX - windowHalfX;
targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
}
}
//
function animate() {
requestAnimationFrame( animate );
render();
stats.update();
}
function render() {
setRenderTargetSize();
scene.update();
var renderScene = scene.getScene();
renderModel.scene = renderScene;
renderer.setClearColor( renderScene.fog.color );
camera.aspect = renderer.domElement.clientWidth / renderer.domElement.clientHeight;
camera.updateProjectionMatrix();
camera.lookAt( cameraTarget );
renderer.clear();
if ( postprocessing.enabled ) {
composer.render( 0.05 );
} else {
renderer.render( renderScene, camera );
}
}
</script>
</body>
</html>