primitives-text.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. <!-- Import maps polyfill -->
  24. <!-- Remove this when import maps will be widely supported -->
  25. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  26. <script type="importmap">
  27. {
  28. "imports": {
  29. "three": "../../build/three.module.js"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three';
  35. import {FontLoader} from '../../examples/jsm/loaders/FontLoader.js';
  36. import {TextGeometry} from '../../examples/jsm/geometries/TextGeometry.js';
  37. function main() {
  38. const canvas = document.querySelector('#c');
  39. const renderer = new THREE.WebGLRenderer({canvas});
  40. const fov = 40;
  41. const aspect = 2; // the canvas default
  42. const near = 0.1;
  43. const far = 1000;
  44. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  45. camera.position.z = 40;
  46. const scene = new THREE.Scene();
  47. scene.background = new THREE.Color(0xAAAAAA);
  48. {
  49. const color = 0xFFFFFF;
  50. const intensity = 1;
  51. const light = new THREE.DirectionalLight(color, intensity);
  52. light.position.set(-1, 2, 4);
  53. scene.add(light);
  54. }
  55. {
  56. const color = 0xFFFFFF;
  57. const intensity = 1;
  58. const light = new THREE.DirectionalLight(color, intensity);
  59. light.position.set(1, -2, -4);
  60. scene.add(light);
  61. }
  62. const objects = [];
  63. const spread = 15;
  64. function addObject(x, y, obj) {
  65. obj.position.x = x * spread;
  66. obj.position.y = y * spread;
  67. scene.add(obj);
  68. objects.push(obj);
  69. }
  70. function createMaterial() {
  71. const material = new THREE.MeshPhongMaterial({
  72. side: THREE.DoubleSide,
  73. });
  74. const hue = Math.random();
  75. const saturation = 1;
  76. const luminance = .5;
  77. material.color.setHSL(hue, saturation, luminance);
  78. return material;
  79. }
  80. function addSolidGeometry(x, y, geometry) {
  81. const mesh = new THREE.Mesh(geometry, createMaterial());
  82. addObject(x, y, mesh);
  83. }
  84. {
  85. const loader = new FontLoader();
  86. // promisify font loading
  87. function loadFont(url) {
  88. return new Promise((resolve, reject) => {
  89. loader.load(url, resolve, undefined, reject);
  90. });
  91. }
  92. async function doit() {
  93. const font = await loadFont('/examples/fonts/helvetiker_regular.typeface.json'); /* threejs.org: url */
  94. const geometry = new TextGeometry('three.js', {
  95. font: font,
  96. size: 3.0,
  97. height: .2,
  98. curveSegments: 12,
  99. bevelEnabled: true,
  100. bevelThickness: 0.15,
  101. bevelSize: .3,
  102. bevelSegments: 5,
  103. });
  104. addSolidGeometry(-.5, 0, geometry);
  105. const mesh = new THREE.Mesh(geometry, createMaterial());
  106. geometry.computeBoundingBox();
  107. geometry.boundingBox.getCenter(mesh.position).multiplyScalar(-1);
  108. const parent = new THREE.Object3D();
  109. parent.add(mesh);
  110. addObject(.5, 0, parent);
  111. }
  112. doit();
  113. }
  114. function resizeRendererToDisplaySize(renderer) {
  115. const canvas = renderer.domElement;
  116. const width = canvas.clientWidth;
  117. const height = canvas.clientHeight;
  118. const needResize = canvas.width !== width || canvas.height !== height;
  119. if (needResize) {
  120. renderer.setSize(width, height, false);
  121. }
  122. return needResize;
  123. }
  124. function render(time) {
  125. time *= 0.001;
  126. if (resizeRendererToDisplaySize(renderer)) {
  127. const canvas = renderer.domElement;
  128. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  129. camera.updateProjectionMatrix();
  130. }
  131. objects.forEach((obj, ndx) => {
  132. const speed = .5 + ndx * .05;
  133. const rot = time * speed;
  134. obj.rotation.x = rot;
  135. obj.rotation.y = rot;
  136. });
  137. renderer.render(scene, camera);
  138. requestAnimationFrame(render);
  139. }
  140. requestAnimationFrame(render);
  141. }
  142. main();
  143. </script>
  144. </html>