canvas-textured-labels.html 5.1 KB

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