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

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