forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgl_shaders_vector.html
331 lines (222 loc) · 7.43 KB
/
webgl_shaders_vector.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - vector - text</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
font-family: Monospace;
background-color: #f0f0f0;
margin: 0px;
overflow: hidden;
}
#info {
position: absolute;
top: 10px;
width: 100%;
text-align: center;
}
</style>
</head>
<body>
<div id="info">
<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - Resolution-Independent Vector Fonts. <a href="https://github.com/mrdoob/three.js/issues/4746">info</a>.
</div>
<script src="../build/three.js"></script>
<script src="./js/controls/OrbitControls.js"></script>
<script src="js/libs/stats.min.js"></script>
<script type="x-shader/x-fragment" id="fs">
varying vec2 vUv;
varying float flip;
uniform vec3 color;
float inCurve(vec2 uv) {
return uv.x * uv.x - uv.y;
}
float delta = 0.1;
void main() {
float x = inCurve(vUv);
if (x * flip > 0.) discard;
gl_FragColor = vec4(color, 1.);
}
</script>
<script type="x-shader/x-vertex" id="vs">
varying vec2 vUv;
attribute float invert;
varying float flip;
void main() {
vUv = uv;
flip = invert;
vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
gl_Position = projectionMatrix * mvPosition;
}
</script>
<script>
var stats;
var camera, scene, renderer, controls;
var group;
var loader = new THREE.FontLoader();
loader.load( 'fonts/helvetiker_regular.typeface.json', function ( font ) {
init( font );
animate();
} );
function init( font ) {
camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.set( 50, 100, 500 );
controls = new THREE.OrbitControls( camera );
controls.target.set( 50, 100, 0 );
controls.update();
scene = new THREE.Scene();
scene.background = new THREE.Color( 0xf0f0f0 );
var theText = '&'; // i % & j b 8
group = new THREE.Group();
scene.add( group );
var textMaterial = new THREE.MeshBasicMaterial( {
color: new THREE.Color( 0, 0, 1 ),
side: THREE.DoubleSide,
wireframe: true
} );
var textShapes = font.generateShapes( theText, 180 );
var geometry = new THREE.ShapeBufferGeometry( textShapes );
var text = new THREE.Mesh( geometry, textMaterial );
text.position.x = - 200;
group.add( text );
//
var vA = new THREE.Vector2();
var vB = new THREE.Vector2();
function processShape( path ) {
var pts = []; // bigger area (convex hull)
var pts2 = []; // smaller area (full solid shapes)
var beziers = []; // quad bezier points
var invert = [];
var z;
var wind;
pts.push( path[ 0 ].getPoint( 0 ) );
pts2.push( path[ 0 ].getPoint( 0 ) );
for ( var i = 0; i < path.length; i ++ ) {
var curve = path[ i ];
if ( curve instanceof THREE.LineCurve ) {
pts.push( curve.v2 );
pts2.push( curve.v2 );
} else if ( curve instanceof THREE.QuadraticBezierCurve ) {
vA = vA.subVectors( curve.v1, curve.v0 );
vB = vB.subVectors( curve.v2, curve.v1 );
z = vA.x * vB.y - vA.y * vB.x; // z component of cross Production
wind = z < 0; // clockwise/anticlock wind
if ( wind ) {
pts.push( curve.v1 );
pts.push( curve.v2 );
pts2.push( curve.v2 );
} else {
pts.push( curve.v2 );
pts2.push( curve.v1 );
pts2.push( curve.v2 );
}
var flip = wind ? 1 : - 1;
// if (reverse) flip *= -1;
invert.push( flip, flip, flip );
beziers.push( curve.v0, curve.v1, curve.v2 );
}
}
return {
pts: pts,
pts2: pts2,
beziers: beziers,
invert: invert
};
}
var pts, pts2;
var subshape;
var convexhullShape;
var solidShape;
var convexhullShapeGroup = [];
var solidShapeGroup = [];
var beziers = [], invert = [];
var process;
var hole;
for ( var s = 0; s < textShapes.length; s ++ ) {
subshape = textShapes[ s ];
process = processShape( subshape.curves );
pts = process.pts;
pts2 = process.pts2;
beziers = beziers.concat( process.beziers );
invert = invert.concat( process.invert );
convexhullShape = new THREE.Shape( pts );
solidShape = new THREE.Shape( pts2 );
convexhullShapeGroup.push( convexhullShape );
solidShapeGroup.push( solidShape );
for ( var i = 0; i < subshape.holes.length; i ++ ) {
hole = subshape.holes[ i ];
process = processShape( hole.curves, true );
pts = process.pts;
pts2 = process.pts2;
beziers = beziers.concat( process.beziers );
invert = invert.concat( process.invert );
convexhullShape.holes.push( new THREE.Shape( pts ) );
solidShape.holes.push( new THREE.Shape( pts2 ) );
}
} // end of subshape
var bezierGeometry = new THREE.BufferGeometry();
var vertices = [];
var uvs = [];
for ( var i = 0; i < beziers.length; i ++ ) {
var p = beziers[ i ];
vertices.push( p.x, p.y, 0 );
}
for ( var i = 0; i < beziers.length; i += 3 ) {
uvs.push( 0, 0, 0.5, 0, 1, 1 );
}
bezierGeometry.addAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
bezierGeometry.addAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
bezierGeometry.addAttribute( 'invert', new THREE.Float32BufferAttribute( invert, 1 ) );
geometry = new THREE.ShapeBufferGeometry( convexhullShapeGroup );
text = new THREE.Mesh( geometry, textMaterial );
text.position.x = 200;
group.add( text );
geometry = new THREE.ShapeBufferGeometry( solidShapeGroup );
text = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: new THREE.Color( 1, 0, 0 ), side: THREE.DoubleSide, wireframe: true } ) );
text.position.x = 200;
group.add( text );
//
var newMaterial = new THREE.ShaderMaterial( {
uniforms: {
color: { value: new THREE.Color( 0.45 * 0xffffff ) }
},
vertexShader: document.getElementById( 'vs' ).textContent,
fragmentShader: document.getElementById( 'fs' ).textContent,
side: THREE.DoubleSide
} );
text = new THREE.Mesh( bezierGeometry, newMaterial );
text.rotation.y = Math.PI * 2;
group.add( text );
//
geometry = new THREE.ShapeBufferGeometry( solidShapeGroup );
text = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0.45 * 0xffffff, side: THREE.DoubleSide } ) );
text.rotation.y = Math.PI * 2;
group.add( text );
//
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
stats = new Stats();
document.body.appendChild( stats.dom );
window.addEventListener( 'resize', onWindowResize, false );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
//
function animate() {
requestAnimationFrame( animate );
render();
stats.update();
}
function render() {
renderer.render( scene, camera );
}
</script>
</body>
</html>