billboard-trees-static-billboards.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 Trees Static Billboards</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 = 1000;
  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.minPolarAngle = 0;
  46. controls.maxPolarAngle = Math.PI / 2;
  47. controls.update();
  48. const scene = new THREE.Scene();
  49. function addLight( position ) {
  50. const color = 0xFFFFFF;
  51. const intensity = 3;
  52. const light = new THREE.DirectionalLight( color, intensity );
  53. light.position.set( ...position );
  54. scene.add( light );
  55. scene.add( light.target );
  56. }
  57. addLight( [ - 3, 1, 1 ] );
  58. addLight( [ 2, 1, .5 ] );
  59. const trunkRadius = .2;
  60. const trunkHeight = 1;
  61. const trunkRadialSegments = 12;
  62. const trunkGeometry = new THREE.CylinderGeometry(
  63. trunkRadius, trunkRadius, trunkHeight, trunkRadialSegments );
  64. const topRadius = trunkRadius * 4;
  65. const topHeight = trunkHeight * 2;
  66. const topSegments = 12;
  67. const topGeometry = new THREE.ConeGeometry(
  68. topRadius, topHeight, topSegments );
  69. const trunkMaterial = new THREE.MeshPhongMaterial( { color: 'brown' } );
  70. const topMaterial = new THREE.MeshPhongMaterial( { color: 'green' } );
  71. function makeTree( x, z ) {
  72. const root = new THREE.Object3D();
  73. const trunk = new THREE.Mesh( trunkGeometry, trunkMaterial );
  74. trunk.position.y = trunkHeight / 2;
  75. root.add( trunk );
  76. const top = new THREE.Mesh( topGeometry, topMaterial );
  77. top.position.y = trunkHeight + topHeight / 2;
  78. root.add( top );
  79. root.position.set( x, 0, z );
  80. scene.add( root );
  81. return root;
  82. }
  83. function frameArea( sizeToFitOnScreen, boxSize, boxCenter, camera ) {
  84. const halfSizeToFitOnScreen = sizeToFitOnScreen * 0.5;
  85. const halfFovY = THREE.MathUtils.degToRad( camera.fov * .5 );
  86. const distance = halfSizeToFitOnScreen / Math.tan( halfFovY );
  87. camera.position.copy( boxCenter );
  88. camera.position.z += distance;
  89. // pick some near and far values for the frustum that
  90. // will contain the box.
  91. camera.near = boxSize / 100;
  92. camera.far = boxSize * 100;
  93. camera.updateProjectionMatrix();
  94. }
  95. function makeSpriteTexture( textureSize, obj ) {
  96. const rt = new THREE.WebGLRenderTarget( textureSize, textureSize );
  97. const aspect = 1; // because the render target is square
  98. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  99. scene.add( obj );
  100. // compute the box that contains obj
  101. const box = new THREE.Box3().setFromObject( obj );
  102. const boxSize = box.getSize( new THREE.Vector3() );
  103. const boxCenter = box.getCenter( new THREE.Vector3() );
  104. // set the camera to frame the box
  105. const fudge = 1.1;
  106. const size = Math.max( ...boxSize.toArray() ) * fudge;
  107. frameArea( size, size, boxCenter, camera );
  108. renderer.autoClear = false;
  109. renderer.setRenderTarget( rt );
  110. renderer.render( scene, camera );
  111. renderer.setRenderTarget( null );
  112. renderer.autoClear = true;
  113. scene.remove( obj );
  114. return {
  115. offset: boxCenter.multiplyScalar( fudge ),
  116. scale: size,
  117. texture: rt.texture,
  118. };
  119. }
  120. // make billboard texture
  121. const tree = makeTree( 0, 0 );
  122. const facadeSize = 64;
  123. const treeSpriteInfo = makeSpriteTexture( facadeSize, tree );
  124. function makeSprite( spriteInfo, x, z ) {
  125. const { texture, offset, scale } = spriteInfo;
  126. const mat = new THREE.SpriteMaterial( {
  127. map: texture,
  128. transparent: true,
  129. } );
  130. const sprite = new THREE.Sprite( mat );
  131. scene.add( sprite );
  132. sprite.position.set(
  133. offset.x + x,
  134. offset.y,
  135. offset.z + z );
  136. sprite.scale.set( scale, scale, scale );
  137. }
  138. for ( let z = - 50; z <= 50; z += 10 ) {
  139. for ( let x = - 50; x <= 50; x += 10 ) {
  140. makeSprite( treeSpriteInfo, x, z );
  141. }
  142. }
  143. scene.background = new THREE.Color( 'lightblue' );
  144. {
  145. const size = 400;
  146. const geometry = new THREE.PlaneGeometry( size, size );
  147. const material = new THREE.MeshPhongMaterial( { color: 'gray' } );
  148. const mesh = new THREE.Mesh( geometry, material );
  149. mesh.rotation.x = Math.PI * - 0.5;
  150. scene.add( mesh );
  151. }
  152. function resizeRendererToDisplaySize( renderer ) {
  153. const canvas = renderer.domElement;
  154. const width = canvas.clientWidth;
  155. const height = canvas.clientHeight;
  156. const needResize = canvas.width !== width || canvas.height !== height;
  157. if ( needResize ) {
  158. renderer.setSize( width, height, false );
  159. }
  160. return needResize;
  161. }
  162. function render() {
  163. if ( resizeRendererToDisplaySize( renderer ) ) {
  164. const canvas = renderer.domElement;
  165. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  166. camera.updateProjectionMatrix();
  167. }
  168. renderer.render( scene, camera );
  169. requestAnimationFrame( render );
  170. }
  171. requestAnimationFrame( render );
  172. }
  173. main();
  174. </script>
  175. </html>