billboard-trees-static-billboards.html 5.8 KB

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