billboard-trees-static-billboards.html 5.7 KB

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