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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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/r102/three.min.js"></script>
  23. <script src="resources/threejs/r102/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. function makePerson(x, labelWidth, size, name, color) {
  90. const canvas = makeLabelCanvas(labelWidth, size, name);
  91. const texture = new THREE.CanvasTexture(canvas);
  92. // because our canvas is likely not a power of 2
  93. // in both dimensions set the filtering appropriately.
  94. texture.minFilter = THREE.LinearFilter;
  95. texture.wrapS = THREE.ClampToEdgeWrapping;
  96. texture.wrapT = THREE.ClampToEdgeWrapping;
  97. renderer.setTexture2D(texture, 0);
  98. const labelMaterial = new THREE.MeshBasicMaterial({
  99. map: texture,
  100. side: THREE.DoubleSide,
  101. transparent: true,
  102. });
  103. const bodyMaterial = new THREE.MeshPhongMaterial({
  104. color,
  105. flatShading: true,
  106. });
  107. const root = new THREE.Object3D();
  108. root.position.x = x;
  109. const body = new THREE.Mesh(bodyGeometry, bodyMaterial);
  110. root.add(body);
  111. body.position.y = bodyHeight / 2;
  112. const head = new THREE.Mesh(headGeometry, bodyMaterial);
  113. root.add(head);
  114. head.position.y = bodyHeight + headRadius * 1.1;
  115. const label = new THREE.Mesh(labelGeometry, labelMaterial);
  116. root.add(label);
  117. label.position.y = bodyHeight * 4 / 5;
  118. label.position.z = bodyRadiusTop * 1.01;
  119. // if units are meters then 0.01 here makes size
  120. // of the label into centimeters.
  121. const labelBaseScale = 0.01;
  122. label.scale.x = canvas.width * labelBaseScale;
  123. label.scale.y = canvas.height * labelBaseScale;
  124. scene.add(root);
  125. return root;
  126. }
  127. makePerson(-3, 150, 32, 'Purple People Eater', 'purple');
  128. makePerson(-0, 150, 32, 'Green Machine', 'green');
  129. makePerson(+3, 150, 32, 'Red Menace', 'red');
  130. function resizeRendererToDisplaySize(renderer) {
  131. const canvas = renderer.domElement;
  132. const width = canvas.clientWidth;
  133. const height = canvas.clientHeight;
  134. const needResize = canvas.width !== width || canvas.height !== height;
  135. if (needResize) {
  136. renderer.setSize(width, height, false);
  137. }
  138. return needResize;
  139. }
  140. function render() {
  141. if (resizeRendererToDisplaySize(renderer)) {
  142. const canvas = renderer.domElement;
  143. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  144. camera.updateProjectionMatrix();
  145. }
  146. renderer.render(scene, camera);
  147. requestAnimationFrame(render);
  148. }
  149. requestAnimationFrame(render);
  150. }
  151. main();
  152. </script>
  153. </html>