canvas-textured-labels.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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</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. function makeLabelCanvas(size, name) {
  63. const borderSize = 2;
  64. const ctx = document.createElement('canvas').getContext('2d');
  65. const font = `${size}px bold sans-serif`;
  66. ctx.font = font;
  67. // measure how long the name will be
  68. const doubleBorderSize = borderSize * 2;
  69. const width = ctx.measureText(name).width + 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 = 'top';
  76. ctx.fillStyle = 'blue';
  77. ctx.fillRect(0, 0, width, height);
  78. ctx.fillStyle = 'white';
  79. ctx.fillText(name, borderSize, borderSize);
  80. return ctx.canvas;
  81. }
  82. function makePerson(x, size, name, color) {
  83. const canvas = makeLabelCanvas(size, name);
  84. const texture = new THREE.CanvasTexture(canvas);
  85. // because our canvas is likely not a power of 2
  86. // in both dimensions set the filtering appropriately.
  87. texture.minFilter = THREE.LinearFilter;
  88. texture.wrapS = THREE.ClampToEdgeWrapping;
  89. texture.wrapT = THREE.ClampToEdgeWrapping;
  90. const labelMaterial = new THREE.MeshBasicMaterial({
  91. map: texture,
  92. side: THREE.DoubleSide,
  93. transparent: true,
  94. });
  95. const bodyMaterial = new THREE.MeshPhongMaterial({
  96. color,
  97. flatShading: true,
  98. });
  99. const root = new THREE.Object3D();
  100. root.position.x = x;
  101. const body = new THREE.Mesh(bodyGeometry, bodyMaterial);
  102. root.add(body);
  103. body.position.y = bodyHeight / 2;
  104. const head = new THREE.Mesh(headGeometry, bodyMaterial);
  105. root.add(head);
  106. head.position.y = bodyHeight + headRadius * 1.1;
  107. const label = new THREE.Mesh(labelGeometry, labelMaterial);
  108. root.add(label);
  109. label.position.y = bodyHeight * 4 / 5;
  110. label.position.z = bodyRadiusTop * 1.01;
  111. // if units are meters then 0.01 here makes size
  112. // of the label into centimeters.
  113. const labelBaseScale = 0.01;
  114. label.scale.x = canvas.width * labelBaseScale;
  115. label.scale.y = canvas.height * labelBaseScale;
  116. scene.add(root);
  117. return root;
  118. }
  119. makePerson(-3, 32, 'Purple People Eater', 'purple');
  120. makePerson(-0, 32, 'Green Machine', 'green');
  121. makePerson(+3, 32, 'Red Menace', 'red');
  122. function resizeRendererToDisplaySize(renderer) {
  123. const canvas = renderer.domElement;
  124. const width = canvas.clientWidth;
  125. const height = canvas.clientHeight;
  126. const needResize = canvas.width !== width || canvas.height !== height;
  127. if (needResize) {
  128. renderer.setSize(width, height, false);
  129. }
  130. return needResize;
  131. }
  132. function render() {
  133. if (resizeRendererToDisplaySize(renderer)) {
  134. const canvas = renderer.domElement;
  135. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  136. camera.updateProjectionMatrix();
  137. }
  138. renderer.render(scene, camera);
  139. requestAnimationFrame(render);
  140. }
  141. requestAnimationFrame(render);
  142. }
  143. main();
  144. </script>
  145. </html>