voxel-geometry-culled-faces.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 - Culled Faces</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. class VoxelWorld {
  27. constructor(cellSize) {
  28. this.cellSize = cellSize;
  29. this.cellSliceSize = cellSize * cellSize;
  30. this.cell = new Uint8Array(cellSize * cellSize * cellSize);
  31. }
  32. computeVoxelOffset(x, y, z) {
  33. const {cellSize, cellSliceSize} = this;
  34. const voxelX = THREE.MathUtils.euclideanModulo(x, cellSize) | 0;
  35. const voxelY = THREE.MathUtils.euclideanModulo(y, cellSize) | 0;
  36. const voxelZ = THREE.MathUtils.euclideanModulo(z, cellSize) | 0;
  37. return voxelY * cellSliceSize +
  38. voxelZ * cellSize +
  39. voxelX;
  40. }
  41. getCellForVoxel(x, y, z) {
  42. const {cellSize} = this;
  43. const cellX = Math.floor(x / cellSize);
  44. const cellY = Math.floor(y / cellSize);
  45. const cellZ = Math.floor(z / cellSize);
  46. if (cellX !== 0 || cellY !== 0 || cellZ !== 0) {
  47. return null;
  48. }
  49. return this.cell;
  50. }
  51. setVoxel(x, y, z, v) {
  52. const cell = this.getCellForVoxel(x, y, z);
  53. if (!cell) {
  54. return; // TODO: add a new cell?
  55. }
  56. const voxelOffset = this.computeVoxelOffset(x, y, z);
  57. cell[voxelOffset] = v;
  58. }
  59. getVoxel(x, y, z) {
  60. const cell = this.getCellForVoxel(x, y, z);
  61. if (!cell) {
  62. return 0;
  63. }
  64. const voxelOffset = this.computeVoxelOffset(x, y, z);
  65. return cell[voxelOffset];
  66. }
  67. generateGeometryDataForCell(cellX, cellY, cellZ) {
  68. const {cellSize} = this;
  69. const positions = [];
  70. const normals = [];
  71. const indices = [];
  72. const startX = cellX * cellSize;
  73. const startY = cellY * cellSize;
  74. const startZ = cellZ * cellSize;
  75. for (let y = 0; y < cellSize; ++y) {
  76. const voxelY = startY + y;
  77. for (let z = 0; z < cellSize; ++z) {
  78. const voxelZ = startZ + z;
  79. for (let x = 0; x < cellSize; ++x) {
  80. const voxelX = startX + x;
  81. const voxel = this.getVoxel(voxelX, voxelY, voxelZ);
  82. if (voxel) {
  83. // There is a voxel here but do we need faces for it?
  84. for (const {dir, corners} of VoxelWorld.faces) {
  85. const neighbor = this.getVoxel(
  86. voxelX + dir[0],
  87. voxelY + dir[1],
  88. voxelZ + dir[2]);
  89. if (!neighbor) {
  90. // this voxel has no neighbor in this direction so we need a face.
  91. const ndx = positions.length / 3;
  92. for (const pos of corners) {
  93. positions.push(pos[0] + x, pos[1] + y, pos[2] + z);
  94. normals.push(...dir);
  95. }
  96. indices.push(
  97. ndx, ndx + 1, ndx + 2,
  98. ndx + 2, ndx + 1, ndx + 3,
  99. );
  100. }
  101. }
  102. }
  103. }
  104. }
  105. }
  106. return {
  107. positions,
  108. normals,
  109. indices,
  110. };
  111. }
  112. }
  113. VoxelWorld.faces = [
  114. { // left
  115. dir: [ -1, 0, 0, ],
  116. corners: [
  117. [ 0, 1, 0 ],
  118. [ 0, 0, 0 ],
  119. [ 0, 1, 1 ],
  120. [ 0, 0, 1 ],
  121. ],
  122. },
  123. { // right
  124. dir: [ 1, 0, 0, ],
  125. corners: [
  126. [ 1, 1, 1 ],
  127. [ 1, 0, 1 ],
  128. [ 1, 1, 0 ],
  129. [ 1, 0, 0 ],
  130. ],
  131. },
  132. { // bottom
  133. dir: [ 0, -1, 0, ],
  134. corners: [
  135. [ 1, 0, 1 ],
  136. [ 0, 0, 1 ],
  137. [ 1, 0, 0 ],
  138. [ 0, 0, 0 ],
  139. ],
  140. },
  141. { // top
  142. dir: [ 0, 1, 0, ],
  143. corners: [
  144. [ 0, 1, 1 ],
  145. [ 1, 1, 1 ],
  146. [ 0, 1, 0 ],
  147. [ 1, 1, 0 ],
  148. ],
  149. },
  150. { // back
  151. dir: [ 0, 0, -1, ],
  152. corners: [
  153. [ 1, 0, 0 ],
  154. [ 0, 0, 0 ],
  155. [ 1, 1, 0 ],
  156. [ 0, 1, 0 ],
  157. ],
  158. },
  159. { // front
  160. dir: [ 0, 0, 1, ],
  161. corners: [
  162. [ 0, 0, 1 ],
  163. [ 1, 0, 1 ],
  164. [ 0, 1, 1 ],
  165. [ 1, 1, 1 ],
  166. ],
  167. },
  168. ];
  169. function main() {
  170. const canvas = document.querySelector('#c');
  171. const renderer = new THREE.WebGLRenderer({canvas});
  172. const cellSize = 32;
  173. const fov = 75;
  174. const aspect = 2; // the canvas default
  175. const near = 0.1;
  176. const far = 1000;
  177. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  178. camera.position.set(-cellSize * .3, cellSize * .8, -cellSize * .3);
  179. const controls = new OrbitControls(camera, canvas);
  180. controls.target.set(cellSize / 2, cellSize / 3, cellSize / 2);
  181. controls.update();
  182. const scene = new THREE.Scene();
  183. scene.background = new THREE.Color('lightblue');
  184. function addLight(x, y, z) {
  185. const color = 0xFFFFFF;
  186. const intensity = 1;
  187. const light = new THREE.DirectionalLight(color, intensity);
  188. light.position.set(x, y, z);
  189. scene.add(light);
  190. }
  191. addLight(-1, 2, 4);
  192. addLight( 1, -1, -2);
  193. const world = new VoxelWorld(cellSize);
  194. for (let y = 0; y < cellSize; ++y) {
  195. for (let z = 0; z < cellSize; ++z) {
  196. for (let x = 0; x < cellSize; ++x) {
  197. const height = (Math.sin(x / cellSize * Math.PI * 2) + Math.sin(z / cellSize * Math.PI * 3)) * (cellSize / 6) + (cellSize / 2);
  198. if (y < height) {
  199. world.setVoxel(x, y, z, 1);
  200. }
  201. }
  202. }
  203. }
  204. const {positions, normals, indices} = world.generateGeometryDataForCell(0, 0, 0);
  205. const geometry = new THREE.BufferGeometry();
  206. const material = new THREE.MeshLambertMaterial({color: 'green'});
  207. const positionNumComponents = 3;
  208. const normalNumComponents = 3;
  209. geometry.setAttribute(
  210. 'position',
  211. new THREE.BufferAttribute(new Float32Array(positions), positionNumComponents));
  212. geometry.setAttribute(
  213. 'normal',
  214. new THREE.BufferAttribute(new Float32Array(normals), normalNumComponents));
  215. geometry.setIndex(indices);
  216. const mesh = new THREE.Mesh(geometry, material);
  217. scene.add(mesh);
  218. function resizeRendererToDisplaySize(renderer) {
  219. const canvas = renderer.domElement;
  220. const width = canvas.clientWidth;
  221. const height = canvas.clientHeight;
  222. const needResize = canvas.width !== width || canvas.height !== height;
  223. if (needResize) {
  224. renderer.setSize(width, height, false);
  225. }
  226. return needResize;
  227. }
  228. let renderRequested = false;
  229. function render() {
  230. renderRequested = undefined;
  231. if (resizeRendererToDisplaySize(renderer)) {
  232. const canvas = renderer.domElement;
  233. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  234. camera.updateProjectionMatrix();
  235. }
  236. controls.update();
  237. renderer.render(scene, camera);
  238. }
  239. render();
  240. function requestRenderIfNotRequested() {
  241. if (!renderRequested) {
  242. renderRequested = true;
  243. requestAnimationFrame(render);
  244. }
  245. }
  246. controls.addEventListener('change', requestRenderIfNotRequested);
  247. window.addEventListener('resize', requestRenderIfNotRequested);
  248. }
  249. main();
  250. </script>
  251. </html>