billboard-trees-static-billboards.html 5.5 KB

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