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

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