billboard-trees-no-billboards.html 3.8 KB

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