billboard-trees-no-billboards.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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="module">
  24. import * as THREE from '../../build/three.module.js';
  25. import {OrbitControls} from '../../examples/jsm/controls/OrbitControls.js';
  26. function main() {
  27. const canvas = document.querySelector('#c');
  28. const renderer = new THREE.WebGLRenderer({canvas});
  29. const fov = 75;
  30. const aspect = 2; // the canvas default
  31. const near = 0.1;
  32. const far = 1000;
  33. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  34. camera.position.set(0, 2, 5);
  35. const controls = new OrbitControls(camera, canvas);
  36. controls.target.set(0, 2, 0);
  37. controls.minPolarAngle = 0;
  38. controls.maxPolarAngle = Math.PI / 2;
  39. controls.update();
  40. const scene = new THREE.Scene();
  41. scene.background = new THREE.Color('lightblue');
  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.CylinderGeometry(
  56. trunkRadius, trunkRadius, trunkHeight, trunkRadialSegments);
  57. const topRadius = trunkRadius * 4;
  58. const topHeight = trunkHeight * 2;
  59. const topSegments = 12;
  60. const topGeometry = new THREE.ConeGeometry(
  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. for (let z = -50; z <= 50; z += 10) {
  77. for (let x = -50; x <= 50; x += 10) {
  78. makeTree(x, z);
  79. }
  80. }
  81. // add ground
  82. {
  83. const size = 400;
  84. const geometry = new THREE.PlaneGeometry(size, size);
  85. const material = new THREE.MeshPhongMaterial({color: 'gray'});
  86. const mesh = new THREE.Mesh(geometry, material);
  87. mesh.rotation.x = Math.PI * -0.5;
  88. scene.add(mesh);
  89. }
  90. function resizeRendererToDisplaySize(renderer) {
  91. const canvas = renderer.domElement;
  92. const width = canvas.clientWidth;
  93. const height = canvas.clientHeight;
  94. const needResize = canvas.width !== width || canvas.height !== height;
  95. if (needResize) {
  96. renderer.setSize(width, height, false);
  97. }
  98. return needResize;
  99. }
  100. function render() {
  101. if (resizeRendererToDisplaySize(renderer)) {
  102. const canvas = renderer.domElement;
  103. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  104. camera.updateProjectionMatrix();
  105. }
  106. renderer.render(scene, camera);
  107. requestAnimationFrame(render);
  108. }
  109. requestAnimationFrame(render);
  110. }
  111. main();
  112. </script>
  113. </html>