threejs-billboard-labels-w-sprites-adjust-height.html 5.0 KB

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