billboard-trees-no-billboards.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 No 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. renderer.useLegacyLights = false;
  41. const fov = 75;
  42. const aspect = 2; // the canvas default
  43. const near = 0.1;
  44. const far = 1000;
  45. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  46. camera.position.set( 0, 2, 5 );
  47. const controls = new OrbitControls( camera, canvas );
  48. controls.target.set( 0, 2, 0 );
  49. controls.minPolarAngle = 0;
  50. controls.maxPolarAngle = Math.PI / 2;
  51. controls.update();
  52. const scene = new THREE.Scene();
  53. scene.background = new THREE.Color( 'lightblue' );
  54. function addLight( position ) {
  55. const color = 0xFFFFFF;
  56. const intensity = 3;
  57. const light = new THREE.DirectionalLight( color, intensity );
  58. light.position.set( ...position );
  59. scene.add( light );
  60. scene.add( light.target );
  61. }
  62. addLight( [ - 3, 1, 1 ] );
  63. addLight( [ 2, 1, .5 ] );
  64. const trunkRadius = .2;
  65. const trunkHeight = 1;
  66. const trunkRadialSegments = 12;
  67. const trunkGeometry = new THREE.CylinderGeometry(
  68. trunkRadius, trunkRadius, trunkHeight, trunkRadialSegments );
  69. const topRadius = trunkRadius * 4;
  70. const topHeight = trunkHeight * 2;
  71. const topSegments = 12;
  72. const topGeometry = new THREE.ConeGeometry(
  73. topRadius, topHeight, topSegments );
  74. const trunkMaterial = new THREE.MeshPhongMaterial( { color: 'brown' } );
  75. const topMaterial = new THREE.MeshPhongMaterial( { color: 'green' } );
  76. function makeTree( x, z ) {
  77. const root = new THREE.Object3D();
  78. const trunk = new THREE.Mesh( trunkGeometry, trunkMaterial );
  79. trunk.position.y = trunkHeight / 2;
  80. root.add( trunk );
  81. const top = new THREE.Mesh( topGeometry, topMaterial );
  82. top.position.y = trunkHeight + topHeight / 2;
  83. root.add( top );
  84. root.position.set( x, 0, z );
  85. scene.add( root );
  86. return root;
  87. }
  88. for ( let z = - 50; z <= 50; z += 10 ) {
  89. for ( let x = - 50; x <= 50; x += 10 ) {
  90. makeTree( x, z );
  91. }
  92. }
  93. // add ground
  94. {
  95. const size = 400;
  96. const geometry = new THREE.PlaneGeometry( size, size );
  97. const material = new THREE.MeshPhongMaterial( { color: 'gray' } );
  98. const mesh = new THREE.Mesh( geometry, material );
  99. mesh.rotation.x = Math.PI * - 0.5;
  100. scene.add( mesh );
  101. }
  102. function resizeRendererToDisplaySize( renderer ) {
  103. const canvas = renderer.domElement;
  104. const width = canvas.clientWidth;
  105. const height = canvas.clientHeight;
  106. const needResize = canvas.width !== width || canvas.height !== height;
  107. if ( needResize ) {
  108. renderer.setSize( width, height, false );
  109. }
  110. return needResize;
  111. }
  112. function render() {
  113. if ( resizeRendererToDisplaySize( renderer ) ) {
  114. const canvas = renderer.domElement;
  115. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  116. camera.updateProjectionMatrix();
  117. }
  118. renderer.render( scene, camera );
  119. requestAnimationFrame( render );
  120. }
  121. requestAnimationFrame( render );
  122. }
  123. main();
  124. </script>
  125. </html>