voxel-geometry-culled-faces-with-textures.html 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 - Textures</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( options ) {
  36. this.cellSize = options.cellSize;
  37. this.tileSize = options.tileSize;
  38. this.tileTextureWidth = options.tileTextureWidth;
  39. this.tileTextureHeight = options.tileTextureHeight;
  40. const { cellSize } = this;
  41. this.cellSliceSize = cellSize * cellSize;
  42. this.cell = new Uint8Array( cellSize * cellSize * cellSize );
  43. }
  44. computeVoxelOffset( x, y, z ) {
  45. const { cellSize, cellSliceSize } = this;
  46. const voxelX = THREE.MathUtils.euclideanModulo( x, cellSize ) | 0;
  47. const voxelY = THREE.MathUtils.euclideanModulo( y, cellSize ) | 0;
  48. const voxelZ = THREE.MathUtils.euclideanModulo( z, cellSize ) | 0;
  49. return voxelY * cellSliceSize +
  50. voxelZ * cellSize +
  51. voxelX;
  52. }
  53. getCellForVoxel( x, y, z ) {
  54. const { cellSize } = this;
  55. const cellX = Math.floor( x / cellSize );
  56. const cellY = Math.floor( y / cellSize );
  57. const cellZ = Math.floor( z / cellSize );
  58. if ( cellX !== 0 || cellY !== 0 || cellZ !== 0 ) {
  59. return null;
  60. }
  61. return this.cell;
  62. }
  63. setVoxel( x, y, z, v ) {
  64. const cell = this.getCellForVoxel( x, y, z );
  65. if ( ! cell ) {
  66. return; // TODO: add a new cell?
  67. }
  68. const voxelOffset = this.computeVoxelOffset( x, y, z );
  69. cell[ voxelOffset ] = v;
  70. }
  71. getVoxel( x, y, z ) {
  72. const cell = this.getCellForVoxel( x, y, z );
  73. if ( ! cell ) {
  74. return 0;
  75. }
  76. const voxelOffset = this.computeVoxelOffset( x, y, z );
  77. return cell[ voxelOffset ];
  78. }
  79. generateGeometryDataForCell( cellX, cellY, cellZ ) {
  80. const { cellSize, tileSize, tileTextureWidth, tileTextureHeight } = this;
  81. const positions = [];
  82. const normals = [];
  83. const uvs = [];
  84. const indices = [];
  85. const startX = cellX * cellSize;
  86. const startY = cellY * cellSize;
  87. const startZ = cellZ * cellSize;
  88. for ( let y = 0; y < cellSize; ++ y ) {
  89. const voxelY = startY + y;
  90. for ( let z = 0; z < cellSize; ++ z ) {
  91. const voxelZ = startZ + z;
  92. for ( let x = 0; x < cellSize; ++ x ) {
  93. const voxelX = startX + x;
  94. const voxel = this.getVoxel( voxelX, voxelY, voxelZ );
  95. if ( voxel ) {
  96. // voxel 0 is sky (empty) so for UVs we start at 0
  97. const uvVoxel = voxel - 1;
  98. // There is a voxel here but do we need faces for it?
  99. for ( const { dir, corners, uvRow } of VoxelWorld.faces ) {
  100. const neighbor = this.getVoxel(
  101. voxelX + dir[ 0 ],
  102. voxelY + dir[ 1 ],
  103. voxelZ + dir[ 2 ] );
  104. if ( ! neighbor ) {
  105. // this voxel has no neighbor in this direction so we need a face.
  106. const ndx = positions.length / 3;
  107. for ( const { pos, uv } of corners ) {
  108. positions.push( pos[ 0 ] + x, pos[ 1 ] + y, pos[ 2 ] + z );
  109. normals.push( ...dir );
  110. uvs.push(
  111. ( uvVoxel + uv[ 0 ] ) * tileSize / tileTextureWidth,
  112. 1 - ( uvRow + 1 - uv[ 1 ] ) * tileSize / tileTextureHeight );
  113. }
  114. indices.push(
  115. ndx, ndx + 1, ndx + 2,
  116. ndx + 2, ndx + 1, ndx + 3,
  117. );
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }
  124. return {
  125. positions,
  126. normals,
  127. uvs,
  128. indices,
  129. };
  130. }
  131. }
  132. VoxelWorld.faces = [
  133. { // left
  134. uvRow: 0,
  135. dir: [ - 1, 0, 0, ],
  136. corners: [
  137. { pos: [ 0, 1, 0 ], uv: [ 0, 1 ], },
  138. { pos: [ 0, 0, 0 ], uv: [ 0, 0 ], },
  139. { pos: [ 0, 1, 1 ], uv: [ 1, 1 ], },
  140. { pos: [ 0, 0, 1 ], uv: [ 1, 0 ], },
  141. ],
  142. },
  143. { // right
  144. uvRow: 0,
  145. dir: [ 1, 0, 0, ],
  146. corners: [
  147. { pos: [ 1, 1, 1 ], uv: [ 0, 1 ], },
  148. { pos: [ 1, 0, 1 ], uv: [ 0, 0 ], },
  149. { pos: [ 1, 1, 0 ], uv: [ 1, 1 ], },
  150. { pos: [ 1, 0, 0 ], uv: [ 1, 0 ], },
  151. ],
  152. },
  153. { // bottom
  154. uvRow: 1,
  155. dir: [ 0, - 1, 0, ],
  156. corners: [
  157. { pos: [ 1, 0, 1 ], uv: [ 1, 0 ], },
  158. { pos: [ 0, 0, 1 ], uv: [ 0, 0 ], },
  159. { pos: [ 1, 0, 0 ], uv: [ 1, 1 ], },
  160. { pos: [ 0, 0, 0 ], uv: [ 0, 1 ], },
  161. ],
  162. },
  163. { // top
  164. uvRow: 2,
  165. dir: [ 0, 1, 0, ],
  166. corners: [
  167. { pos: [ 0, 1, 1 ], uv: [ 1, 1 ], },
  168. { pos: [ 1, 1, 1 ], uv: [ 0, 1 ], },
  169. { pos: [ 0, 1, 0 ], uv: [ 1, 0 ], },
  170. { pos: [ 1, 1, 0 ], uv: [ 0, 0 ], },
  171. ],
  172. },
  173. { // back
  174. uvRow: 0,
  175. dir: [ 0, 0, - 1, ],
  176. corners: [
  177. { pos: [ 1, 0, 0 ], uv: [ 0, 0 ], },
  178. { pos: [ 0, 0, 0 ], uv: [ 1, 0 ], },
  179. { pos: [ 1, 1, 0 ], uv: [ 0, 1 ], },
  180. { pos: [ 0, 1, 0 ], uv: [ 1, 1 ], },
  181. ],
  182. },
  183. { // front
  184. uvRow: 0,
  185. dir: [ 0, 0, 1, ],
  186. corners: [
  187. { pos: [ 0, 0, 1 ], uv: [ 0, 0 ], },
  188. { pos: [ 1, 0, 1 ], uv: [ 1, 0 ], },
  189. { pos: [ 0, 1, 1 ], uv: [ 0, 1 ], },
  190. { pos: [ 1, 1, 1 ], uv: [ 1, 1 ], },
  191. ],
  192. },
  193. ];
  194. function main() {
  195. const canvas = document.querySelector( '#c' );
  196. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
  197. const cellSize = 32;
  198. const fov = 75;
  199. const aspect = 2; // the canvas default
  200. const near = 0.1;
  201. const far = 1000;
  202. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  203. camera.position.set( - cellSize * .3, cellSize * .8, - cellSize * .3 );
  204. const controls = new OrbitControls( camera, canvas );
  205. controls.target.set( cellSize / 2, cellSize / 3, cellSize / 2 );
  206. controls.update();
  207. const scene = new THREE.Scene();
  208. scene.background = new THREE.Color( 'lightblue' );
  209. function addLight( x, y, z ) {
  210. const color = 0xFFFFFF;
  211. const intensity = 3;
  212. const light = new THREE.DirectionalLight( color, intensity );
  213. light.position.set( x, y, z );
  214. scene.add( light );
  215. }
  216. addLight( - 1, 2, 4 );
  217. addLight( 1, - 1, - 2 );
  218. const loader = new THREE.TextureLoader();
  219. const texture = loader.load( 'resources/images/minecraft/flourish-cc-by-nc-sa.png', render );
  220. texture.magFilter = THREE.NearestFilter;
  221. texture.minFilter = THREE.NearestFilter;
  222. texture.colorSpace = THREE.SRGBColorSpace;
  223. const tileSize = 16;
  224. const tileTextureWidth = 256;
  225. const tileTextureHeight = 64;
  226. const world = new VoxelWorld( {
  227. cellSize,
  228. tileSize,
  229. tileTextureWidth,
  230. tileTextureHeight,
  231. } );
  232. for ( let y = 0; y < cellSize; ++ y ) {
  233. for ( let z = 0; z < cellSize; ++ z ) {
  234. for ( let x = 0; x < cellSize; ++ x ) {
  235. const height = ( Math.sin( x / cellSize * Math.PI * 2 ) + Math.sin( z / cellSize * Math.PI * 3 ) ) * ( cellSize / 6 ) + ( cellSize / 2 );
  236. if ( y < height ) {
  237. world.setVoxel( x, y, z, randInt( 1, 17 ) );
  238. }
  239. }
  240. }
  241. }
  242. function randInt( min, max ) {
  243. return Math.floor( Math.random() * ( max - min ) + min );
  244. }
  245. const { positions, normals, uvs, indices } = world.generateGeometryDataForCell( 0, 0, 0 );
  246. const geometry = new THREE.BufferGeometry();
  247. const material = new THREE.MeshLambertMaterial( {
  248. map: texture,
  249. side: THREE.DoubleSide,
  250. alphaTest: 0.1,
  251. transparent: true,
  252. } );
  253. const positionNumComponents = 3;
  254. const normalNumComponents = 3;
  255. const uvNumComponents = 2;
  256. geometry.setAttribute(
  257. 'position',
  258. new THREE.BufferAttribute( new Float32Array( positions ), positionNumComponents ) );
  259. geometry.setAttribute(
  260. 'normal',
  261. new THREE.BufferAttribute( new Float32Array( normals ), normalNumComponents ) );
  262. geometry.setAttribute(
  263. 'uv',
  264. new THREE.BufferAttribute( new Float32Array( uvs ), uvNumComponents ) );
  265. geometry.setIndex( indices );
  266. const mesh = new THREE.Mesh( geometry, material );
  267. scene.add( mesh );
  268. function resizeRendererToDisplaySize( renderer ) {
  269. const canvas = renderer.domElement;
  270. const width = canvas.clientWidth;
  271. const height = canvas.clientHeight;
  272. const needResize = canvas.width !== width || canvas.height !== height;
  273. if ( needResize ) {
  274. renderer.setSize( width, height, false );
  275. }
  276. return needResize;
  277. }
  278. let renderRequested = false;
  279. function render() {
  280. renderRequested = undefined;
  281. if ( resizeRendererToDisplaySize( renderer ) ) {
  282. const canvas = renderer.domElement;
  283. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  284. camera.updateProjectionMatrix();
  285. }
  286. controls.update();
  287. renderer.render( scene, camera );
  288. }
  289. render();
  290. function requestRenderIfNotRequested() {
  291. if ( ! renderRequested ) {
  292. renderRequested = true;
  293. requestAnimationFrame( render );
  294. }
  295. }
  296. controls.addEventListener( 'change', requestRenderIfNotRequested );
  297. window.addEventListener( 'resize', requestRenderIfNotRequested );
  298. }
  299. main();
  300. </script>
  301. </html>