voxel-geometry-culled-faces.html 7.3 KB

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