canvas-textured-labels-one-canvas.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 - Canvas Textured Labels One Canvas</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 {OrbitControls} from '../../examples/jsm/controls/OrbitControls.js';
  36. function main() {
  37. const canvas = document.querySelector('#c');
  38. const renderer = new THREE.WebGLRenderer({canvas});
  39. const fov = 75;
  40. const aspect = 2; // the canvas default
  41. const near = 0.1;
  42. const far = 50;
  43. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  44. camera.position.set(0, 2, 5);
  45. const controls = new OrbitControls(camera, canvas);
  46. controls.target.set(0, 2, 0);
  47. controls.update();
  48. const scene = new THREE.Scene();
  49. scene.background = new THREE.Color('white');
  50. function addLight(position) {
  51. const color = 0xFFFFFF;
  52. const intensity = 1;
  53. const light = new THREE.DirectionalLight(color, intensity);
  54. light.position.set(...position);
  55. scene.add(light);
  56. scene.add(light.target);
  57. }
  58. addLight([-3, 1, 1]);
  59. addLight([ 2, 1, .5]);
  60. const bodyRadiusTop = .4;
  61. const bodyRadiusBottom = .2;
  62. const bodyHeight = 2;
  63. const bodyRadialSegments = 6;
  64. const bodyGeometry = new THREE.CylinderGeometry(
  65. bodyRadiusTop, bodyRadiusBottom, bodyHeight, bodyRadialSegments);
  66. const headRadius = bodyRadiusTop * 0.8;
  67. const headLonSegments = 12;
  68. const headLatSegments = 5;
  69. const headGeometry = new THREE.SphereGeometry(
  70. headRadius, headLonSegments, headLatSegments);
  71. const labelGeometry = new THREE.PlaneGeometry(1, 1);
  72. const ctx = document.createElement('canvas').getContext('2d');
  73. function makeLabelCanvas(baseWidth, size, name) {
  74. const borderSize = 2;
  75. const font = `${size}px bold sans-serif`;
  76. ctx.font = font;
  77. // measure how long the name will be
  78. const textWidth = ctx.measureText(name).width;
  79. const doubleBorderSize = borderSize * 2;
  80. const width = baseWidth + doubleBorderSize;
  81. const height = size + doubleBorderSize;
  82. ctx.canvas.width = width;
  83. ctx.canvas.height = height;
  84. // need to set font again after resizing canvas
  85. ctx.font = font;
  86. ctx.textBaseline = 'middle';
  87. ctx.textAlign = 'center';
  88. ctx.fillStyle = 'blue';
  89. ctx.fillRect(0, 0, width, height);
  90. // scale to fit but don't stretch
  91. const scaleFactor = Math.min(1, baseWidth / textWidth);
  92. ctx.translate(width / 2, height / 2);
  93. ctx.scale(scaleFactor, 1);
  94. ctx.fillStyle = 'white';
  95. ctx.fillText(name, 0, 0);
  96. return ctx.canvas;
  97. }
  98. const forceTextureInitialization = function() {
  99. const material = new THREE.MeshBasicMaterial();
  100. const geometry = new THREE.PlaneGeometry();
  101. const scene = new THREE.Scene();
  102. scene.add(new THREE.Mesh(geometry, material));
  103. const camera = new THREE.Camera();
  104. return function forceTextureInitialization(texture) {
  105. material.map = texture;
  106. renderer.render(scene, camera);
  107. };
  108. }();
  109. function makePerson(x, labelWidth, size, name, color) {
  110. const canvas = makeLabelCanvas(labelWidth, size, name);
  111. const texture = new THREE.CanvasTexture(canvas);
  112. // because our canvas is likely not a power of 2
  113. // in both dimensions set the filtering appropriately.
  114. texture.minFilter = THREE.LinearFilter;
  115. texture.wrapS = THREE.ClampToEdgeWrapping;
  116. texture.wrapT = THREE.ClampToEdgeWrapping;
  117. forceTextureInitialization(texture);
  118. const labelMaterial = new THREE.MeshBasicMaterial({
  119. map: texture,
  120. side: THREE.DoubleSide,
  121. transparent: true,
  122. });
  123. const bodyMaterial = new THREE.MeshPhongMaterial({
  124. color,
  125. flatShading: true,
  126. });
  127. const root = new THREE.Object3D();
  128. root.position.x = x;
  129. const body = new THREE.Mesh(bodyGeometry, bodyMaterial);
  130. root.add(body);
  131. body.position.y = bodyHeight / 2;
  132. const head = new THREE.Mesh(headGeometry, bodyMaterial);
  133. root.add(head);
  134. head.position.y = bodyHeight + headRadius * 1.1;
  135. const label = new THREE.Mesh(labelGeometry, labelMaterial);
  136. root.add(label);
  137. label.position.y = bodyHeight * 4 / 5;
  138. label.position.z = bodyRadiusTop * 1.01;
  139. // if units are meters then 0.01 here makes size
  140. // of the label into centimeters.
  141. const labelBaseScale = 0.01;
  142. label.scale.x = canvas.width * labelBaseScale;
  143. label.scale.y = canvas.height * labelBaseScale;
  144. scene.add(root);
  145. return root;
  146. }
  147. makePerson(-3, 150, 32, 'Purple People Eater', 'purple');
  148. makePerson(-0, 150, 32, 'Green Machine', 'green');
  149. makePerson(+3, 150, 32, 'Red Menace', 'red');
  150. function resizeRendererToDisplaySize(renderer) {
  151. const canvas = renderer.domElement;
  152. const width = canvas.clientWidth;
  153. const height = canvas.clientHeight;
  154. const needResize = canvas.width !== width || canvas.height !== height;
  155. if (needResize) {
  156. renderer.setSize(width, height, false);
  157. }
  158. return needResize;
  159. }
  160. function render() {
  161. if (resizeRendererToDisplaySize(renderer)) {
  162. const canvas = renderer.domElement;
  163. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  164. camera.updateProjectionMatrix();
  165. }
  166. renderer.render(scene, camera);
  167. requestAnimationFrame(render);
  168. }
  169. requestAnimationFrame(render);
  170. }
  171. main();
  172. </script>
  173. </html>