webgl_geometry_text_shapes.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - simple text from json</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. body {
  10. background-color: #f0f0f0;
  11. color: #444;
  12. }
  13. a {
  14. color: #08f;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info">
  20. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - simple text from json fonts
  21. </div>
  22. <script type="module">
  23. import * as THREE from '../build/three.module.js';
  24. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  25. var camera, scene, renderer;
  26. init();
  27. animate();
  28. function init( ) {
  29. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
  30. camera.position.set( 0, - 400, 600 );
  31. scene = new THREE.Scene();
  32. scene.background = new THREE.Color( 0xf0f0f0 );
  33. var loader = new THREE.FontLoader();
  34. loader.load( 'fonts/helvetiker_regular.typeface.json', function ( font ) {
  35. var xMid, text;
  36. var color = 0x006699;
  37. var matDark = new THREE.LineBasicMaterial( {
  38. color: color,
  39. side: THREE.DoubleSide
  40. } );
  41. var matLite = new THREE.MeshBasicMaterial( {
  42. color: color,
  43. transparent: true,
  44. opacity: 0.4,
  45. side: THREE.DoubleSide
  46. } );
  47. var message = " Three.js\nSimple text.";
  48. var shapes = font.generateShapes( message, 100 );
  49. var geometry = new THREE.ShapeBufferGeometry( shapes );
  50. geometry.computeBoundingBox();
  51. xMid = - 0.5 * ( geometry.boundingBox.max.x - geometry.boundingBox.min.x );
  52. geometry.translate( xMid, 0, 0 );
  53. // make shape ( N.B. edge view not visible )
  54. text = new THREE.Mesh( geometry, matLite );
  55. text.position.z = - 150;
  56. scene.add( text );
  57. // make line shape ( N.B. edge view remains visible )
  58. var holeShapes = [];
  59. for ( var i = 0; i < shapes.length; i ++ ) {
  60. var shape = shapes[ i ];
  61. if ( shape.holes && shape.holes.length > 0 ) {
  62. for ( var j = 0; j < shape.holes.length; j ++ ) {
  63. var hole = shape.holes[ j ];
  64. holeShapes.push( hole );
  65. }
  66. }
  67. }
  68. shapes.push.apply( shapes, holeShapes );
  69. var lineText = new THREE.Object3D();
  70. for ( var i = 0; i < shapes.length; i ++ ) {
  71. var shape = shapes[ i ];
  72. var points = shape.getPoints();
  73. var geometry = new THREE.BufferGeometry().setFromPoints( points );
  74. geometry.translate( xMid, 0, 0 );
  75. var lineMesh = new THREE.Line( geometry, matDark );
  76. lineText.add( lineMesh );
  77. }
  78. scene.add( lineText );
  79. } ); //end load function
  80. renderer = new THREE.WebGLRenderer( { antialias: true } );
  81. renderer.setPixelRatio( window.devicePixelRatio );
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. document.body.appendChild( renderer.domElement );
  84. var controls = new OrbitControls( camera, renderer.domElement );
  85. controls.target.set( 0, 0, 0 );
  86. controls.update();
  87. window.addEventListener( 'resize', onWindowResize, false );
  88. } // end init
  89. function onWindowResize() {
  90. camera.aspect = window.innerWidth / window.innerHeight;
  91. camera.updateProjectionMatrix();
  92. renderer.setSize( window.innerWidth, window.innerHeight );
  93. }
  94. function animate() {
  95. requestAnimationFrame( animate );
  96. render();
  97. }
  98. function render() {
  99. renderer.render( scene, camera );
  100. }
  101. </script>
  102. </body>
  103. </html>