threejs-voxel-geometry-culled-faces.html 7.1 KB

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