primitives-text.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - Primitives</title>
  8. <style>
  9. html, body {
  10. height: 100%;
  11. margin: 0;
  12. }
  13. #c {
  14. width: 100%;
  15. height: 100%;
  16. display: block;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <canvas id="c"></canvas>
  22. </body>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../../build/three.module.js",
  27. "three/addons/": "../../examples/jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three';
  33. import { FontLoader } from 'three/addons/loaders/FontLoader.js';
  34. import { TextGeometry } from 'three/addons/geometries/TextGeometry.js';
  35. function main() {
  36. const canvas = document.querySelector( '#c' );
  37. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
  38. const fov = 40;
  39. const aspect = 2; // the canvas default
  40. const near = 0.1;
  41. const far = 1000;
  42. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  43. camera.position.z = 40;
  44. const scene = new THREE.Scene();
  45. scene.background = new THREE.Color( 0xAAAAAA );
  46. {
  47. const color = 0xFFFFFF;
  48. const intensity = 3;
  49. const light = new THREE.DirectionalLight( color, intensity );
  50. light.position.set( - 1, 2, 4 );
  51. scene.add( light );
  52. }
  53. {
  54. const color = 0xFFFFFF;
  55. const intensity = 3;
  56. const light = new THREE.DirectionalLight( color, intensity );
  57. light.position.set( 1, - 2, - 4 );
  58. scene.add( light );
  59. }
  60. const objects = [];
  61. const spread = 15;
  62. function addObject( x, y, obj ) {
  63. obj.position.x = x * spread;
  64. obj.position.y = y * spread;
  65. scene.add( obj );
  66. objects.push( obj );
  67. }
  68. function createMaterial() {
  69. const material = new THREE.MeshPhongMaterial( {
  70. side: THREE.DoubleSide,
  71. } );
  72. const hue = Math.random();
  73. const saturation = 1;
  74. const luminance = .5;
  75. material.color.setHSL( hue, saturation, luminance );
  76. return material;
  77. }
  78. function addSolidGeometry( x, y, geometry ) {
  79. const mesh = new THREE.Mesh( geometry, createMaterial() );
  80. addObject( x, y, mesh );
  81. }
  82. {
  83. const loader = new FontLoader();
  84. // promisify font loading
  85. function loadFont( url ) {
  86. return new Promise( ( resolve, reject ) => {
  87. loader.load( url, resolve, undefined, reject );
  88. } );
  89. }
  90. async function doit() {
  91. const font = await loadFont( '/examples/fonts/helvetiker_regular.typeface.json' ); /* threejs.org: url */
  92. const geometry = new TextGeometry( 'three.js', {
  93. font: font,
  94. size: 3.0,
  95. depth: .2,
  96. curveSegments: 12,
  97. bevelEnabled: true,
  98. bevelThickness: 0.15,
  99. bevelSize: .3,
  100. bevelSegments: 5,
  101. } );
  102. addSolidGeometry( - .5, 0, geometry );
  103. const mesh = new THREE.Mesh( geometry, createMaterial() );
  104. geometry.computeBoundingBox();
  105. geometry.boundingBox.getCenter( mesh.position ).multiplyScalar( - 1 );
  106. const parent = new THREE.Object3D();
  107. parent.add( mesh );
  108. addObject( .5, 0, parent );
  109. }
  110. doit();
  111. }
  112. function resizeRendererToDisplaySize( renderer ) {
  113. const canvas = renderer.domElement;
  114. const width = canvas.clientWidth;
  115. const height = canvas.clientHeight;
  116. const needResize = canvas.width !== width || canvas.height !== height;
  117. if ( needResize ) {
  118. renderer.setSize( width, height, false );
  119. }
  120. return needResize;
  121. }
  122. function render( time ) {
  123. time *= 0.001;
  124. if ( resizeRendererToDisplaySize( renderer ) ) {
  125. const canvas = renderer.domElement;
  126. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  127. camera.updateProjectionMatrix();
  128. }
  129. objects.forEach( ( obj, ndx ) => {
  130. const speed = .5 + ndx * .05;
  131. const rot = time * speed;
  132. obj.rotation.x = rot;
  133. obj.rotation.y = rot;
  134. } );
  135. renderer.render( scene, camera );
  136. requestAnimationFrame( render );
  137. }
  138. requestAnimationFrame( render );
  139. }
  140. main();
  141. </script>
  142. </html>