billboard-trees-static-billboards.html 5.7 KB

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