canvas-textured-labels-one-canvas.html 5.8 KB

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