voxel-geometry-culled-faces.html 6.9 KB

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