123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - simple text from json</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 - simple text from json fonts
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- var camera, scene, renderer;
- init();
- animate();
- function init( ) {
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
- camera.position.set( 0, - 400, 600 );
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0xf0f0f0 );
- var loader = new THREE.FontLoader();
- loader.load( 'fonts/helvetiker_regular.typeface.json', function ( font ) {
- var xMid, text;
- var color = 0x006699;
- var matDark = new THREE.LineBasicMaterial( {
- color: color,
- side: THREE.DoubleSide
- } );
- var matLite = new THREE.MeshBasicMaterial( {
- color: color,
- transparent: true,
- opacity: 0.4,
- side: THREE.DoubleSide
- } );
- var message = " Three.js\nSimple text.";
- var shapes = font.generateShapes( message, 100 );
- var geometry = new THREE.ShapeBufferGeometry( shapes );
- geometry.computeBoundingBox();
- xMid = - 0.5 * ( geometry.boundingBox.max.x - geometry.boundingBox.min.x );
- geometry.translate( xMid, 0, 0 );
- // make shape ( N.B. edge view not visible )
- text = new THREE.Mesh( geometry, matLite );
- text.position.z = - 150;
- scene.add( text );
- // make line shape ( N.B. edge view remains visible )
- var holeShapes = [];
- for ( var i = 0; i < shapes.length; i ++ ) {
- var shape = shapes[ i ];
- if ( shape.holes && shape.holes.length > 0 ) {
- for ( var j = 0; j < shape.holes.length; j ++ ) {
- var hole = shape.holes[ j ];
- holeShapes.push( hole );
- }
- }
- }
- shapes.push.apply( shapes, holeShapes );
- var lineText = new THREE.Object3D();
- for ( var i = 0; i < shapes.length; i ++ ) {
- var shape = shapes[ i ];
- var points = shape.getPoints();
- var geometry = new THREE.BufferGeometry().setFromPoints( points );
- geometry.translate( xMid, 0, 0 );
- var lineMesh = new THREE.Line( geometry, matDark );
- lineText.add( lineMesh );
- }
- scene.add( lineText );
- } ); //end load function
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- document.body.appendChild( renderer.domElement );
- var controls = new OrbitControls( camera, renderer.domElement );
- controls.target.set( 0, 0, 0 );
- controls.update();
- window.addEventListener( 'resize', onWindowResize, false );
- } // end init
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- requestAnimationFrame( animate );
- render();
- }
- function render() {
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|