voxel-geometry-culled-faces.html 7.3 KB

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