webgl_geometry_text_stroke.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. import { SVGLoader } from './jsm/loaders/SVGLoader.js';
  26. var camera, scene, renderer;
  27. init();
  28. animate();
  29. function init( ) {
  30. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
  31. camera.position.set( 0, - 400, 600 );
  32. scene = new THREE.Scene();
  33. scene.background = new THREE.Color( 0xf0f0f0 );
  34. var loader = new THREE.FontLoader();
  35. loader.load( 'fonts/helvetiker_regular.typeface.json', function ( font ) {
  36. var xMid, text;
  37. var color = new THREE.Color( 0x006699 );
  38. var matDark = new THREE.MeshBasicMaterial( {
  39. color: color,
  40. side: THREE.DoubleSide
  41. } );
  42. var matLite = new THREE.MeshBasicMaterial( {
  43. color: color,
  44. transparent: true,
  45. opacity: 0.4,
  46. side: THREE.DoubleSide
  47. } );
  48. var message = " Three.js\nStroke text.";
  49. var shapes = font.generateShapes( message, 100 );
  50. var geometry = new THREE.ShapeBufferGeometry( shapes );
  51. geometry.computeBoundingBox();
  52. xMid = - 0.5 * ( geometry.boundingBox.max.x - geometry.boundingBox.min.x );
  53. geometry.translate( xMid, 0, 0 );
  54. // make shape ( N.B. edge view not visible )
  55. text = new THREE.Mesh( geometry, matLite );
  56. text.position.z = - 150;
  57. scene.add( text );
  58. // make line shape ( N.B. edge view remains visible )
  59. var holeShapes = [];
  60. for ( var i = 0; i < shapes.length; i ++ ) {
  61. var shape = shapes[ i ];
  62. if ( shape.holes && shape.holes.length > 0 ) {
  63. for ( var j = 0; j < shape.holes.length; j ++ ) {
  64. var hole = shape.holes[ j ];
  65. holeShapes.push( hole );
  66. }
  67. }
  68. }
  69. shapes.push.apply( shapes, holeShapes );
  70. var style = SVGLoader.getStrokeStyle( 5, color.getStyle() );
  71. var strokeText = new THREE.Group();
  72. for ( var i = 0; i < shapes.length; i ++ ) {
  73. var shape = shapes[ i ];
  74. var points = shape.getPoints();
  75. var geometry = SVGLoader.pointsToStroke( points, style );
  76. geometry.translate( xMid, 0, 0 );
  77. var strokeMesh = new THREE.Mesh( geometry, matDark );
  78. strokeText.add( strokeMesh );
  79. }
  80. scene.add( strokeText );
  81. } ); //end load function
  82. renderer = new THREE.WebGLRenderer( { antialias: true } );
  83. renderer.setPixelRatio( window.devicePixelRatio );
  84. renderer.setSize( window.innerWidth, window.innerHeight );
  85. document.body.appendChild( renderer.domElement );
  86. var controls = new OrbitControls( camera, renderer.domElement );
  87. controls.target.set( 0, 0, 0 );
  88. controls.update();
  89. window.addEventListener( 'resize', onWindowResize, false );
  90. } // end init
  91. function onWindowResize() {
  92. camera.aspect = window.innerWidth / window.innerHeight;
  93. camera.updateProjectionMatrix();
  94. renderer.setSize( window.innerWidth, window.innerHeight );
  95. }
  96. function animate() {
  97. requestAnimationFrame( animate );
  98. render();
  99. }
  100. function render() {
  101. renderer.render( scene, camera );
  102. }
  103. </script>
  104. </body>
  105. </html>