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

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