billboard-trees-no-billboards.html 3.6 KB

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