threejs-billboard-trees-no-billboards.html 3.5 KB

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