threejs-voxel-geometry-merged.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 - Voxel Geometry - Merged</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/r105/three.min.js"></script>
  23. <script src="resources/threejs/r105/js/utils/BufferGeometryUtils.js"></script>
  24. <script src="resources/threejs/r105/js/controls/OrbitControls.js"></script>
  25. <script>
  26. 'use strict';
  27. /* global THREE */
  28. function main() {
  29. const canvas = document.querySelector('#c');
  30. const renderer = new THREE.WebGLRenderer({canvas});
  31. const cellSize = 256;
  32. const fov = 75;
  33. const aspect = 2; // the canvas default
  34. const near = 0.1;
  35. const far = 1000;
  36. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  37. camera.position.set(cellSize / 2, 60, cellSize);
  38. const controls = new THREE.OrbitControls(camera, canvas);
  39. controls.target.set(cellSize / 2, 60, cellSize / 2);
  40. controls.update();
  41. const scene = new THREE.Scene();
  42. scene.background = new THREE.Color('lightblue');
  43. {
  44. const color = 0xFFFFFF;
  45. const intensity = 1;
  46. const light = new THREE.DirectionalLight(color, intensity);
  47. light.position.set(-1, 2, 4);
  48. scene.add(light);
  49. }
  50. const geometries = [];
  51. for (let y = 0; y < cellSize; ++y) {
  52. for (let z = 0; z < cellSize; ++z) {
  53. for (let x = 0; x < cellSize; ++x) {
  54. const height = (Math.sin(x / cellSize * Math.PI * 4) + Math.sin(z / cellSize * Math.PI * 6)) * 20 + 60;
  55. //if (y < height) {
  56. if (height > y && height < y + 1) {
  57. const geometry = new THREE.BoxBufferGeometry(1, 1, 1);
  58. geometry.applyMatrix((new THREE.Matrix4()).makeTranslation(x, y, z));
  59. geometries.push(geometry);
  60. }
  61. }
  62. }
  63. }
  64. const mergedGeometry = THREE.BufferGeometryUtils.mergeBufferGeometries(
  65. geometries, false);
  66. const material = new THREE.MeshPhongMaterial({color: 'green'});
  67. const mesh = new THREE.Mesh(mergedGeometry, material);
  68. scene.add(mesh);
  69. function resizeRendererToDisplaySize(renderer) {
  70. const canvas = renderer.domElement;
  71. const width = canvas.clientWidth;
  72. const height = canvas.clientHeight;
  73. const needResize = canvas.width !== width || canvas.height !== height;
  74. if (needResize) {
  75. renderer.setSize(width, height, false);
  76. }
  77. return needResize;
  78. }
  79. let renderRequested = false;
  80. function render() {
  81. renderRequested = undefined;
  82. if (resizeRendererToDisplaySize(renderer)) {
  83. const canvas = renderer.domElement;
  84. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  85. camera.updateProjectionMatrix();
  86. }
  87. controls.update();
  88. renderer.render(scene, camera);
  89. }
  90. render();
  91. function requestRenderIfNotRequested() {
  92. if (!renderRequested) {
  93. renderRequested = true;
  94. requestAnimationFrame(render);
  95. }
  96. }
  97. controls.addEventListener('change', requestRenderIfNotRequested);
  98. window.addEventListener('resize', requestRenderIfNotRequested);
  99. }
  100. main();
  101. </script>
  102. </html>