threejs-billboard-trees-static-billboards.html 5.5 KB

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