voxel-geometry-separate-cubes.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 - Separate Cubes</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. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three';
  35. import {OrbitControls} from '../../examples/jsm/controls/OrbitControls.js';
  36. function main() {
  37. const canvas = document.querySelector('#c');
  38. const renderer = new THREE.WebGLRenderer({canvas});
  39. const cellSize = 256;
  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(-cellSize * .3, cellSize * .8, -cellSize * .3);
  46. const controls = new OrbitControls(camera, canvas);
  47. controls.target.set(cellSize / 2, cellSize / 3, cellSize / 2);
  48. controls.update();
  49. const scene = new THREE.Scene();
  50. scene.background = new THREE.Color('lightblue');
  51. {
  52. const color = 0xFFFFFF;
  53. const intensity = 1;
  54. const light = new THREE.DirectionalLight(color, intensity);
  55. light.position.set(-1, 2, 4);
  56. scene.add(light);
  57. }
  58. const cell = new Uint8Array(cellSize * cellSize * cellSize);
  59. for (let y = 0; y < cellSize; ++y) {
  60. for (let z = 0; z < cellSize; ++z) {
  61. for (let x = 0; x < cellSize; ++x) {
  62. const height = (Math.sin(x / cellSize * Math.PI * 4) + Math.sin(z / cellSize * Math.PI * 6)) * 20 + cellSize / 2;
  63. if (height > y && height < y + 1) {
  64. const offset = y * cellSize * cellSize +
  65. z * cellSize +
  66. x;
  67. cell[offset] = 1;
  68. }
  69. }
  70. }
  71. }
  72. const geometry = new THREE.BoxGeometry(1, 1, 1);
  73. const material = new THREE.MeshPhongMaterial({color: 'green'});
  74. for (let y = 0; y < cellSize; ++y) {
  75. for (let z = 0; z < cellSize; ++z) {
  76. for (let x = 0; x < cellSize; ++x) {
  77. const offset = y * cellSize * cellSize +
  78. z * cellSize +
  79. x;
  80. const block = cell[offset];
  81. if (block) {
  82. const mesh = new THREE.Mesh(geometry, material);
  83. mesh.position.set(x, y, z);
  84. scene.add(mesh);
  85. }
  86. }
  87. }
  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. let renderRequested = false;
  100. function render() {
  101. renderRequested = undefined;
  102. if (resizeRendererToDisplaySize(renderer)) {
  103. const canvas = renderer.domElement;
  104. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  105. camera.updateProjectionMatrix();
  106. }
  107. controls.update();
  108. renderer.render(scene, camera);
  109. }
  110. render();
  111. function requestRenderIfNotRequested() {
  112. if (!renderRequested) {
  113. renderRequested = true;
  114. requestAnimationFrame(render);
  115. }
  116. }
  117. controls.addEventListener('change', requestRenderIfNotRequested);
  118. window.addEventListener('resize', requestRenderIfNotRequested);
  119. }
  120. main();
  121. </script>
  122. </html>