123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- <!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">
- <link type="text/css" rel="stylesheet" href="main.css">
- <style>
- body {
- background-color: #f0f0f0;
- color: #444;
- }
- a {
- color: #08f;
- }
- </style>
- </head>
- <body>
- <div id="info">
- <a href="https://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 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 type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- 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 );
- 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 );
- 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.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
- bezierGeometry.setAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
- bezierGeometry.setAttribute( '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 );
- controls = new OrbitControls( camera, renderer.domElement );
- controls.target.set( 50, 100, 0 );
- controls.update();
- 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>
|