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

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