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

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