1
0

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

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