canvas-textured-labels.html 5.1 KB

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