voxel-geometry.html 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. Title: Three.js Voxel(Minecraft Like) Geometry
  2. Description: How to make voxel geometry like Minecraft
  3. TOC: Making Voxel Geometry (Minecraft)
  4. I've seen this topic come up more than once in various places.
  5. That is basically, "How do I make a voxel display like Minecraft".
  6. Most people first attempt this by making a cube geometry and then
  7. making a mesh at each voxel position. Just for fun I tried
  8. this. I made a 16777216 element `Uint8Array` to represent
  9. a 256x256x256 cube of voxels.
  10. ```js
  11. const cellSize = 256;
  12. const cell = new Uint8Array(cellSize * cellSize * cellSize);
  13. ```
  14. I then made a single layer with a kind of hills of
  15. sine waves like this
  16. ```js
  17. for (let y = 0; y < cellSize; ++y) {
  18. for (let z = 0; z < cellSize; ++z) {
  19. for (let x = 0; x < cellSize; ++x) {
  20. const height = (Math.sin(x / cellSize * Math.PI * 4) + Math.sin(z / cellSize * Math.PI * 6)) * 20 + cellSize / 2;
  21. if (height > y && height < y + 1) {
  22. const offset = y * cellSize * cellSize +
  23. z * cellSize +
  24. x;
  25. cell[offset] = 1;
  26. }
  27. }
  28. }
  29. }
  30. ```
  31. I then walked through all the cells and if they were not
  32. 0 I created a mesh with a cube.
  33. ```js
  34. const geometry = new THREE.BoxGeometry(1, 1, 1);
  35. const material = new THREE.MeshPhongMaterial({color: 'green'});
  36. for (let y = 0; y < cellSize; ++y) {
  37. for (let z = 0; z < cellSize; ++z) {
  38. for (let x = 0; x < cellSize; ++x) {
  39. const offset = y * cellSize * cellSize +
  40. z * cellSize +
  41. x;
  42. const block = cell[offset];
  43. const mesh = new THREE.Mesh(geometry, material);
  44. mesh.position.set(x, y, z);
  45. scene.add(mesh);
  46. }
  47. }
  48. }
  49. ```
  50. The rest of the code is based on the example from
  51. [the article on rendering on demand](threejs-rendering-on-demand.html).
  52. {{{example url="../threejs-voxel-geometry-separate-cubes.html" }}}
  53. It takes a while to start and if you try to move the camera
  54. it's likely too slow. Like [the article on how to optimize lots of objects](threejs-optimize-lots-of-objects.html)
  55. the problem is there are just way too many objects. 256x256
  56. is 65536 boxes!
  57. Using [the technique of merging the geometry](threejs-rendering-on-demand.html)
  58. will fix the issue for this example but what if instead of just making
  59. a single layer we filled in everything below the ground with voxel.
  60. In other words change the loop filling in the voxels to this
  61. ```js
  62. for (let y = 0; y < cellSize; ++y) {
  63. for (let z = 0; z < cellSize; ++z) {
  64. for (let x = 0; x < cellSize; ++x) {
  65. const height = (Math.sin(x / cellSize * Math.PI * 4) + Math.sin(z / cellSize * Math.PI * 6)) * 20 + cellSize / 2;
  66. - if (height > y && height < y + 1) {
  67. + if (height < y + 1) {
  68. const offset = y * cellSize * cellSize +
  69. z * cellSize +
  70. x;
  71. cell[offset] = 1;
  72. }
  73. }
  74. }
  75. }
  76. ```
  77. I tried it once just to see the results. It churned for
  78. about a minute and then crashed with *out of memory* 😅
  79. There are several issues but the biggest issue is
  80. we're making all these faces inside the cubes that
  81. we can actually never see.
  82. In other words lets say we have a box of voxels
  83. 3x2x2. By merging cubes we're getting this
  84. <div class="spread">
  85. <div data-diagram="mergedCubes" style="height: 300px;"></div>
  86. </div>
  87. but we really want this
  88. <div class="spread">
  89. <div data-diagram="culledCubes" style="height: 300px;"></div>
  90. </div>
  91. In the top box there are faces between the voxels. Faces
  92. that are a waste since they can't be seen. It's not just
  93. one face between each voxel, there are 2 faces, one for
  94. each voxel facing its neighbor that are a waste. All these extra faces,
  95. especially for a large volume of voxels will kill performance.
  96. It should be clear that we can't just merge geometry.
  97. We need to build it ourselves, taking into account that
  98. if a voxel has an adjacent neighbor it doesn't need the
  99. face facing that neighbor.
  100. The next issue is that 256x256x256 is just too big. 16meg is a lot of memory and
  101. if nothing else in much of the space nothing is there so that's a lot of wasted
  102. memory. It's also a huge number of voxels, 16 million! That's too much to
  103. consider at once.
  104. A solution is to divide the area into smaller areas.
  105. Any area that has nothing in it needs no storage. Let's use
  106. 32x32x32 areas (that's 32k) and only create an area if something is in it.
  107. We'll call one of these larger 32x32x32 areas a "cell".
  108. Let's break this into pieces. First let's make a class to manage the voxel data.
  109. ```js
  110. class VoxelWorld {
  111. constructor(cellSize) {
  112. this.cellSize = cellSize;
  113. }
  114. }
  115. ```
  116. Let's make the function that makes geometry for a cell.
  117. Let's assume you pass in a cell position.
  118. In other words if you want the geometry for the cell that covers voxels (0-31x, 0-31y, 0-31z)
  119. then you'd pass in 0,0,0. For the cell that covers voxels (32-63x, 0-31y, 0-31z) you'd
  120. pass in 1,0,0.
  121. We need to be able to check the neighboring voxels so let's assume our class
  122. has a function `getVoxel` that given a voxel position returns the value of
  123. the voxel there. In other words if you pass it 35,0,0 and the cellSize is 32
  124. it's going to look at cell 1,0,0 and in that cell it will look at voxel 3,0,0.
  125. Using this function we can look at a voxel's neighboring voxels even if they
  126. happen to be in neighboring cells.
  127. ```js
  128. class VoxelWorld {
  129. constructor(cellSize) {
  130. this.cellSize = cellSize;
  131. }
  132. + generateGeometryDataForCell(cellX, cellY, cellZ) {
  133. + const {cellSize} = this;
  134. + const startX = cellX * cellSize;
  135. + const startY = cellY * cellSize;
  136. + const startZ = cellZ * cellSize;
  137. +
  138. + for (let y = 0; y < cellSize; ++y) {
  139. + const voxelY = startY + y;
  140. + for (let z = 0; z < cellSize; ++z) {
  141. + const voxelZ = startZ + z;
  142. + for (let x = 0; x < cellSize; ++x) {
  143. + const voxelX = startX + x;
  144. + const voxel = this.getVoxel(voxelX, voxelY, voxelZ);
  145. + if (voxel) {
  146. + for (const {dir} of VoxelWorld.faces) {
  147. + const neighbor = this.getVoxel(
  148. + voxelX + dir[0],
  149. + voxelY + dir[1],
  150. + voxelZ + dir[2]);
  151. + if (!neighbor) {
  152. + // this voxel has no neighbor in this direction so we need a face
  153. + // here.
  154. + }
  155. + }
  156. + }
  157. + }
  158. + }
  159. + }
  160. + }
  161. }
  162. +VoxelWorld.faces = [
  163. + { // left
  164. + dir: [ -1, 0, 0, ],
  165. + },
  166. + { // right
  167. + dir: [ 1, 0, 0, ],
  168. + },
  169. + { // bottom
  170. + dir: [ 0, -1, 0, ],
  171. + },
  172. + { // top
  173. + dir: [ 0, 1, 0, ],
  174. + },
  175. + { // back
  176. + dir: [ 0, 0, -1, ],
  177. + },
  178. + { // front
  179. + dir: [ 0, 0, 1, ],
  180. + },
  181. +];
  182. ```
  183. So using the code above we know when we need a face. Let's generate the faces.
  184. ```js
  185. class VoxelWorld {
  186. constructor(cellSize) {
  187. this.cellSize = cellSize;
  188. }
  189. generateGeometryDataForCell(cellX, cellY, cellZ) {
  190. const {cellSize} = this;
  191. + const positions = [];
  192. + const normals = [];
  193. + const indices = [];
  194. const startX = cellX * cellSize;
  195. const startY = cellY * cellSize;
  196. const startZ = cellZ * cellSize;
  197. for (let y = 0; y < cellSize; ++y) {
  198. const voxelY = startY + y;
  199. for (let z = 0; z < cellSize; ++z) {
  200. const voxelZ = startZ + z;
  201. for (let x = 0; x < cellSize; ++x) {
  202. const voxelX = startX + x;
  203. const voxel = this.getVoxel(voxelX, voxelY, voxelZ);
  204. if (voxel) {
  205. - for (const {dir} of VoxelWorld.faces) {
  206. + for (const {dir, corners} of VoxelWorld.faces) {
  207. const neighbor = this.getVoxel(
  208. voxelX + dir[0],
  209. voxelY + dir[1],
  210. voxelZ + dir[2]);
  211. if (!neighbor) {
  212. // this voxel has no neighbor in this direction so we need a face.
  213. + const ndx = positions.length / 3;
  214. + for (const pos of corners) {
  215. + positions.push(pos[0] + x, pos[1] + y, pos[2] + z);
  216. + normals.push(...dir);
  217. + }
  218. + indices.push(
  219. + ndx, ndx + 1, ndx + 2,
  220. + ndx + 2, ndx + 1, ndx + 3,
  221. + );
  222. }
  223. }
  224. }
  225. }
  226. }
  227. }
  228. + return {
  229. + positions,
  230. + normals,
  231. + indices,
  232. };
  233. }
  234. }
  235. VoxelWorld.faces = [
  236. { // left
  237. dir: [ -1, 0, 0, ],
  238. + corners: [
  239. + [ 0, 1, 0 ],
  240. + [ 0, 0, 0 ],
  241. + [ 0, 1, 1 ],
  242. + [ 0, 0, 1 ],
  243. + ],
  244. },
  245. { // right
  246. dir: [ 1, 0, 0, ],
  247. + corners: [
  248. + [ 1, 1, 1 ],
  249. + [ 1, 0, 1 ],
  250. + [ 1, 1, 0 ],
  251. + [ 1, 0, 0 ],
  252. + ],
  253. },
  254. { // bottom
  255. dir: [ 0, -1, 0, ],
  256. + corners: [
  257. + [ 1, 0, 1 ],
  258. + [ 0, 0, 1 ],
  259. + [ 1, 0, 0 ],
  260. + [ 0, 0, 0 ],
  261. + ],
  262. },
  263. { // top
  264. dir: [ 0, 1, 0, ],
  265. + corners: [
  266. + [ 0, 1, 1 ],
  267. + [ 1, 1, 1 ],
  268. + [ 0, 1, 0 ],
  269. + [ 1, 1, 0 ],
  270. + ],
  271. },
  272. { // back
  273. dir: [ 0, 0, -1, ],
  274. + corners: [
  275. + [ 1, 0, 0 ],
  276. + [ 0, 0, 0 ],
  277. + [ 1, 1, 0 ],
  278. + [ 0, 1, 0 ],
  279. + ],
  280. },
  281. { // front
  282. dir: [ 0, 0, 1, ],
  283. + corners: [
  284. + [ 0, 0, 1 ],
  285. + [ 1, 0, 1 ],
  286. + [ 0, 1, 1 ],
  287. + [ 1, 1, 1 ],
  288. + ],
  289. },
  290. ];
  291. ```
  292. The code above would make basic geometry data for us. We just need to supply
  293. the `getVoxel` function. Let's start with just one hard coded cell.
  294. ```js
  295. class VoxelWorld {
  296. constructor(cellSize) {
  297. this.cellSize = cellSize;
  298. + this.cell = new Uint8Array(cellSize * cellSize * cellSize);
  299. }
  300. + getCellForVoxel(x, y, z) {
  301. + const {cellSize} = this;
  302. + const cellX = Math.floor(x / cellSize);
  303. + const cellY = Math.floor(y / cellSize);
  304. + const cellZ = Math.floor(z / cellSize);
  305. + if (cellX !== 0 || cellY !== 0 || cellZ !== 0) {
  306. + return null
  307. + }
  308. + return this.cell;
  309. + }
  310. + getVoxel(x, y, z) {
  311. + const cell = this.getCellForVoxel(x, y, z);
  312. + if (!cell) {
  313. + return 0;
  314. + }
  315. + const {cellSize} = this;
  316. + const voxelX = THREE.MathUtils.euclideanModulo(x, cellSize) | 0;
  317. + const voxelY = THREE.MathUtils.euclideanModulo(y, cellSize) | 0;
  318. + const voxelZ = THREE.MathUtils.euclideanModulo(z, cellSize) | 0;
  319. + const voxelOffset = voxelY * cellSize * cellSize +
  320. + voxelZ * cellSize +
  321. + voxelX;
  322. + return cell[voxelOffset];
  323. + }
  324. generateGeometryDataForCell(cellX, cellY, cellZ) {
  325. ...
  326. }
  327. ```
  328. This seems like it would work. Let's make a `setVoxel` function
  329. so we can set some data.
  330. ```js
  331. class VoxelWorld {
  332. constructor(cellSize) {
  333. this.cellSize = cellSize;
  334. this.cell = new Uint8Array(cellSize * cellSize * cellSize);
  335. }
  336. getCellForVoxel(x, y, z) {
  337. const {cellSize} = this;
  338. const cellX = Math.floor(x / cellSize);
  339. const cellY = Math.floor(y / cellSize);
  340. const cellZ = Math.floor(z / cellSize);
  341. if (cellX !== 0 || cellY !== 0 || cellZ !== 0) {
  342. return null
  343. }
  344. return this.cell;
  345. }
  346. + setVoxel(x, y, z, v) {
  347. + let cell = this.getCellForVoxel(x, y, z);
  348. + if (!cell) {
  349. + return; // TODO: add a new cell?
  350. + }
  351. + const {cellSize} = this;
  352. + const voxelX = THREE.MathUtils.euclideanModulo(x, cellSize) | 0;
  353. + const voxelY = THREE.MathUtils.euclideanModulo(y, cellSize) | 0;
  354. + const voxelZ = THREE.MathUtils.euclideanModulo(z, cellSize) | 0;
  355. + const voxelOffset = voxelY * cellSize * cellSize +
  356. + voxelZ * cellSize +
  357. + voxelX;
  358. + cell[voxelOffset] = v;
  359. + }
  360. getVoxel(x, y, z) {
  361. const cell = this.getCellForVoxel(x, y, z);
  362. if (!cell) {
  363. return 0;
  364. }
  365. const {cellSize} = this;
  366. const voxelX = THREE.MathUtils.euclideanModulo(x, cellSize) | 0;
  367. const voxelY = THREE.MathUtils.euclideanModulo(y, cellSize) | 0;
  368. const voxelZ = THREE.MathUtils.euclideanModulo(z, cellSize) | 0;
  369. const voxelOffset = voxelY * cellSize * cellSize +
  370. voxelZ * cellSize +
  371. voxelX;
  372. return cell[voxelOffset];
  373. }
  374. generateGeometryDataForCell(cellX, cellY, cellZ) {
  375. ...
  376. }
  377. ```
  378. Hmmm, I see a lot of repeated code. Let's fix that up
  379. ```js
  380. class VoxelWorld {
  381. constructor(cellSize) {
  382. this.cellSize = cellSize;
  383. + this.cellSliceSize = cellSize * cellSize;
  384. this.cell = new Uint8Array(cellSize * cellSize * cellSize);
  385. }
  386. getCellForVoxel(x, y, z) {
  387. const {cellSize} = this;
  388. const cellX = Math.floor(x / cellSize);
  389. const cellY = Math.floor(y / cellSize);
  390. const cellZ = Math.floor(z / cellSize);
  391. if (cellX !== 0 || cellY !== 0 || cellZ !== 0) {
  392. return null;
  393. }
  394. return this.cell;
  395. }
  396. + computeVoxelOffset(x, y, z) {
  397. + const {cellSize, cellSliceSize} = this;
  398. + const voxelX = THREE.MathUtils.euclideanModulo(x, cellSize) | 0;
  399. + const voxelY = THREE.MathUtils.euclideanModulo(y, cellSize) | 0;
  400. + const voxelZ = THREE.MathUtils.euclideanModulo(z, cellSize) | 0;
  401. + return voxelY * cellSliceSize +
  402. + voxelZ * cellSize +
  403. + voxelX;
  404. + }
  405. setVoxel(x, y, z, v) {
  406. const cell = this.getCellForVoxel(x, y, z);
  407. if (!cell) {
  408. return; // TODO: add a new cell?
  409. }
  410. - const {cellSize} = this;
  411. - const voxelX = THREE.MathUtils.euclideanModulo(x, cellSize) | 0;
  412. - const voxelY = THREE.MathUtils.euclideanModulo(y, cellSize) | 0;
  413. - const voxelZ = THREE.MathUtils.euclideanModulo(z, cellSize) | 0;
  414. - const voxelOffset = voxelY * cellSize * cellSize +
  415. - voxelZ * cellSize +
  416. - voxelX;
  417. + const voxelOffset = this.computeVoxelOffset(x, y, z);
  418. cell[voxelOffset] = v;
  419. }
  420. getVoxel(x, y, z) {
  421. const cell = this.getCellForVoxel(x, y, z);
  422. if (!cell) {
  423. return 0;
  424. }
  425. - const {cellSize} = this;
  426. - const voxelX = THREE.MathUtils.euclideanModulo(x, cellSize) | 0;
  427. - const voxelY = THREE.MathUtils.euclideanModulo(y, cellSize) | 0;
  428. - const voxelZ = THREE.MathUtils.euclideanModulo(z, cellSize) | 0;
  429. - const voxelOffset = voxelY * cellSize * cellSize +
  430. - voxelZ * cellSize +
  431. - voxelX;
  432. + const voxelOffset = this.computeVoxelOffset(x, y, z);
  433. return cell[voxelOffset];
  434. }
  435. generateGeometryDataForCell(cellX, cellY, cellZ) {
  436. ...
  437. }
  438. ```
  439. Now let's make some code to fill out the first cell with voxels.
  440. ```js
  441. const cellSize = 32;
  442. const world = new VoxelWorld(cellSize);
  443. for (let y = 0; y < cellSize; ++y) {
  444. for (let z = 0; z < cellSize; ++z) {
  445. for (let x = 0; x < cellSize; ++x) {
  446. const height = (Math.sin(x / cellSize * Math.PI * 2) + Math.sin(z / cellSize * Math.PI * 3)) * (cellSize / 6) + (cellSize / 2);
  447. if (y < height) {
  448. world.setVoxel(x, y, z, 1);
  449. }
  450. }
  451. }
  452. }
  453. ```
  454. and some code to actually generate geometry like we covered in
  455. [the article on custom BufferGeometry](threejs-custom-buffergeometry.html).
  456. ```js
  457. const {positions, normals, indices} = world.generateGeometryDataForCell(0, 0, 0);
  458. const geometry = new THREE.BufferGeometry();
  459. const material = new THREE.MeshLambertMaterial({color: 'green'});
  460. const positionNumComponents = 3;
  461. const normalNumComponents = 3;
  462. geometry.setAttribute(
  463. 'position',
  464. new THREE.BufferAttribute(new Float32Array(positions), positionNumComponents));
  465. geometry.setAttribute(
  466. 'normal',
  467. new THREE.BufferAttribute(new Float32Array(normals), normalNumComponents));
  468. geometry.setIndex(indices);
  469. const mesh = new THREE.Mesh(geometry, material);
  470. scene.add(mesh);
  471. ```
  472. let's try it
  473. {{{example url="../threejs-voxel-geometry-culled-faces.html" }}}
  474. That seems to be working! Okay, let's add in textures.
  475. Searching on the net I found [this set](https://www.minecraftforum.net/forums/mapping-and-modding-java-edition/resource-packs/1245961-16x-1-7-4-wip-flourish)
  476. of [CC-BY-NC-SA](https://creativecommons.org/licenses/by-nc-sa/4.0/) licensed minecraft textures
  477. by [Joshtimus](https://www.minecraftforum.net/members/Joshtimus).
  478. I picked a few at random and built this [texture atlas](https://www.google.com/search?q=texture+atlas).
  479. <div class="threejs_center"><img class="checkerboard" src="../resources/images/minecraft/flourish-cc-by-nc-sa.png" style="width: 512px; image-rendering: pixelated;"></div>
  480. To make things simple they are arranged a voxel type per column
  481. where the top row is the side of a voxel. The 2nd row is
  482. the top of voxel, and the 3rd row is the bottom of the voxel.
  483. Knowing that we can add info to our `VoxelWorld.faces` data
  484. to specify for each face which row to use and the UVs to use
  485. for that face.
  486. ```js
  487. VoxelWorld.faces = [
  488. { // left
  489. + uvRow: 0,
  490. dir: [ -1, 0, 0, ],
  491. corners: [
  492. - [ 0, 1, 0 ],
  493. - [ 0, 0, 0 ],
  494. - [ 0, 1, 1 ],
  495. - [ 0, 0, 1 ],
  496. + { pos: [ 0, 1, 0 ], uv: [ 0, 1 ], },
  497. + { pos: [ 0, 0, 0 ], uv: [ 0, 0 ], },
  498. + { pos: [ 0, 1, 1 ], uv: [ 1, 1 ], },
  499. + { pos: [ 0, 0, 1 ], uv: [ 1, 0 ], },
  500. ],
  501. },
  502. { // right
  503. + uvRow: 0,
  504. dir: [ 1, 0, 0, ],
  505. corners: [
  506. - [ 1, 1, 1 ],
  507. - [ 1, 0, 1 ],
  508. - [ 1, 1, 0 ],
  509. - [ 1, 0, 0 ],
  510. + { pos: [ 1, 1, 1 ], uv: [ 0, 1 ], },
  511. + { pos: [ 1, 0, 1 ], uv: [ 0, 0 ], },
  512. + { pos: [ 1, 1, 0 ], uv: [ 1, 1 ], },
  513. + { pos: [ 1, 0, 0 ], uv: [ 1, 0 ], },
  514. ],
  515. },
  516. { // bottom
  517. + uvRow: 1,
  518. dir: [ 0, -1, 0, ],
  519. corners: [
  520. - [ 1, 0, 1 ],
  521. - [ 0, 0, 1 ],
  522. - [ 1, 0, 0 ],
  523. - [ 0, 0, 0 ],
  524. + { pos: [ 1, 0, 1 ], uv: [ 1, 0 ], },
  525. + { pos: [ 0, 0, 1 ], uv: [ 0, 0 ], },
  526. + { pos: [ 1, 0, 0 ], uv: [ 1, 1 ], },
  527. + { pos: [ 0, 0, 0 ], uv: [ 0, 1 ], },
  528. ],
  529. },
  530. { // top
  531. + uvRow: 2,
  532. dir: [ 0, 1, 0, ],
  533. corners: [
  534. - [ 0, 1, 1 ],
  535. - [ 1, 1, 1 ],
  536. - [ 0, 1, 0 ],
  537. - [ 1, 1, 0 ],
  538. + { pos: [ 0, 1, 1 ], uv: [ 1, 1 ], },
  539. + { pos: [ 1, 1, 1 ], uv: [ 0, 1 ], },
  540. + { pos: [ 0, 1, 0 ], uv: [ 1, 0 ], },
  541. + { pos: [ 1, 1, 0 ], uv: [ 0, 0 ], },
  542. ],
  543. },
  544. { // back
  545. + uvRow: 0,
  546. dir: [ 0, 0, -1, ],
  547. corners: [
  548. - [ 1, 0, 0 ],
  549. - [ 0, 0, 0 ],
  550. - [ 1, 1, 0 ],
  551. - [ 0, 1, 0 ],
  552. + { pos: [ 1, 0, 0 ], uv: [ 0, 0 ], },
  553. + { pos: [ 0, 0, 0 ], uv: [ 1, 0 ], },
  554. + { pos: [ 1, 1, 0 ], uv: [ 0, 1 ], },
  555. + { pos: [ 0, 1, 0 ], uv: [ 1, 1 ], },
  556. ],
  557. },
  558. { // front
  559. + uvRow: 0,
  560. dir: [ 0, 0, 1, ],
  561. corners: [
  562. - [ 0, 0, 1 ],
  563. - [ 1, 0, 1 ],
  564. - [ 0, 1, 1 ],
  565. - [ 1, 1, 1 ],
  566. + { pos: [ 0, 0, 1 ], uv: [ 0, 0 ], },
  567. + { pos: [ 1, 0, 1 ], uv: [ 1, 0 ], },
  568. + { pos: [ 0, 1, 1 ], uv: [ 0, 1 ], },
  569. + { pos: [ 1, 1, 1 ], uv: [ 1, 1 ], },
  570. ],
  571. },
  572. ];
  573. ```
  574. And we can update the code to use that data. We need to
  575. know the size of a tile in the texture atlas and the dimensions
  576. of the texture.
  577. ```js
  578. class VoxelWorld {
  579. - constructor(cellSize) {
  580. - this.cellSize = cellSize;
  581. + constructor(options) {
  582. + this.cellSize = options.cellSize;
  583. + this.tileSize = options.tileSize;
  584. + this.tileTextureWidth = options.tileTextureWidth;
  585. + this.tileTextureHeight = options.tileTextureHeight;
  586. + const {cellSize} = this;
  587. + this.cellSliceSize = cellSize * cellSize;
  588. + this.cell = new Uint8Array(cellSize * cellSize * cellSize);
  589. }
  590. ...
  591. generateGeometryDataForCell(cellX, cellY, cellZ) {
  592. - const {cellSize} = this;
  593. + const {cellSize, tileSize, tileTextureWidth, tileTextureHeight} = this;
  594. const positions = [];
  595. const normals = [];
  596. + const uvs = [];
  597. const indices = [];
  598. const startX = cellX * cellSize;
  599. const startY = cellY * cellSize;
  600. const startZ = cellZ * cellSize;
  601. for (let y = 0; y < cellSize; ++y) {
  602. const voxelY = startY + y;
  603. for (let z = 0; z < cellSize; ++z) {
  604. const voxelZ = startZ + z;
  605. for (let x = 0; x < cellSize; ++x) {
  606. const voxelX = startX + x;
  607. const voxel = this.getVoxel(voxelX, voxelY, voxelZ);
  608. if (voxel) {
  609. const uvVoxel = voxel - 1; // voxel 0 is sky so for UVs we start at 0
  610. // There is a voxel here but do we need faces for it?
  611. - for (const {dir, corners} of VoxelWorld.faces) {
  612. + for (const {dir, corners, uvRow} of VoxelWorld.faces) {
  613. const neighbor = this.getVoxel(
  614. voxelX + dir[0],
  615. voxelY + dir[1],
  616. voxelZ + dir[2]);
  617. if (!neighbor) {
  618. // this voxel has no neighbor in this direction so we need a face.
  619. const ndx = positions.length / 3;
  620. - for (const pos of corners) {
  621. + for (const {pos, uv} of corners) {
  622. positions.push(pos[0] + x, pos[1] + y, pos[2] + z);
  623. normals.push(...dir);
  624. + uvs.push(
  625. + (uvVoxel + uv[0]) * tileSize / tileTextureWidth,
  626. + 1 - (uvRow + 1 - uv[1]) * tileSize / tileTextureHeight);
  627. }
  628. indices.push(
  629. ndx, ndx + 1, ndx + 2,
  630. ndx + 2, ndx + 1, ndx + 3,
  631. );
  632. }
  633. }
  634. }
  635. }
  636. }
  637. }
  638. return {
  639. positions,
  640. normals,
  641. uvs,
  642. indices,
  643. };
  644. }
  645. }
  646. ```
  647. We then need to [load the texture](threejs-textures.html)
  648. ```js
  649. const loader = new THREE.TextureLoader();
  650. const texture = loader.load('resources/images/minecraft/flourish-cc-by-nc-sa.png', render);
  651. texture.magFilter = THREE.NearestFilter;
  652. texture.minFilter = THREE.NearestFilter;
  653. ```
  654. and pass the settings to the `VoxelWorld` class
  655. ```js
  656. +const tileSize = 16;
  657. +const tileTextureWidth = 256;
  658. +const tileTextureHeight = 64;
  659. -const world = new VoxelWorld(cellSize);
  660. +const world = new VoxelWorld({
  661. + cellSize,
  662. + tileSize,
  663. + tileTextureWidth,
  664. + tileTextureHeight,
  665. +});
  666. ```
  667. Let's actually use the UVs when we create the geometry
  668. and the texture when we make the material
  669. ```js
  670. -const {positions, normals, indices} = world.generateGeometryDataForCell(0, 0, 0);
  671. +const {positions, normals, uvs, indices} = world.generateGeometryDataForCell(0, 0, 0);
  672. const geometry = new THREE.BufferGeometry();
  673. -const material = new THREE.MeshLambertMaterial({color: 'green'});
  674. +const material = new THREE.MeshLambertMaterial({
  675. + map: texture,
  676. + side: THREE.DoubleSide,
  677. + alphaTest: 0.1,
  678. + transparent: true,
  679. +});
  680. const positionNumComponents = 3;
  681. const normalNumComponents = 3;
  682. +const uvNumComponents = 2;
  683. geometry.setAttribute(
  684. 'position',
  685. new THREE.BufferAttribute(new Float32Array(positions), positionNumComponents));
  686. geometry.setAttribute(
  687. 'normal',
  688. new THREE.BufferAttribute(new Float32Array(normals), normalNumComponents));
  689. +geometry.setAttribute(
  690. + 'uv',
  691. + new THREE.BufferAttribute(new Float32Array(uvs), uvNumComponents));
  692. geometry.setIndex(indices);
  693. const mesh = new THREE.Mesh(geometry, material);
  694. scene.add(mesh);
  695. ```
  696. One last thing, we actually need to set some voxels
  697. to use different textures.
  698. ```js
  699. for (let y = 0; y < cellSize; ++y) {
  700. for (let z = 0; z < cellSize; ++z) {
  701. for (let x = 0; x < cellSize; ++x) {
  702. const height = (Math.sin(x / cellSize * Math.PI * 2) + Math.sin(z / cellSize * Math.PI * 3)) * (cellSize / 6) + (cellSize / 2);
  703. if (y < height) {
  704. - world.setVoxel(x, y, z, 1);
  705. + world.setVoxel(x, y, z, randInt(1, 17));
  706. }
  707. }
  708. }
  709. }
  710. +function randInt(min, max) {
  711. + return Math.floor(Math.random() * (max - min) + min);
  712. +}
  713. ```
  714. and with that we get textures!
  715. {{{example url="../threejs-voxel-geometry-culled-faces-with-textures.html"}}}
  716. Let's make it support more than one cell.
  717. To do this lets store cells in an object using cell ids.
  718. A cell id will just be a cell's coordinates separated by
  719. a comma. In other words if we ask for voxel 35,0,0
  720. that is in cell 1,0,0 so its id is `"1,0,0"`.
  721. ```js
  722. class VoxelWorld {
  723. constructor(options) {
  724. this.cellSize = options.cellSize;
  725. this.tileSize = options.tileSize;
  726. this.tileTextureWidth = options.tileTextureWidth;
  727. this.tileTextureHeight = options.tileTextureHeight;
  728. const {cellSize} = this;
  729. this.cellSliceSize = cellSize * cellSize;
  730. - this.cell = new Uint8Array(cellSize * cellSize * cellSize);
  731. + this.cells = {};
  732. }
  733. + computeCellId(x, y, z) {
  734. + const {cellSize} = this;
  735. + const cellX = Math.floor(x / cellSize);
  736. + const cellY = Math.floor(y / cellSize);
  737. + const cellZ = Math.floor(z / cellSize);
  738. + return `${cellX},${cellY},${cellZ}`;
  739. + }
  740. + getCellForVoxel(x, y, z) {
  741. - const cellX = Math.floor(x / cellSize);
  742. - const cellY = Math.floor(y / cellSize);
  743. - const cellZ = Math.floor(z / cellSize);
  744. - if (cellX !== 0 || cellY !== 0 || cellZ !== 0) {
  745. - return null;
  746. - }
  747. - return this.cell;
  748. + return this.cells[this.computeCellId(x, y, z)];
  749. }
  750. ...
  751. }
  752. ```
  753. and now we can make `setVoxel` add new cells if
  754. we try to set a voxel in a cell that does not yet exist
  755. ```js
  756. setVoxel(x, y, z, v) {
  757. - const cell = this.getCellForVoxel(x, y, z);
  758. + let cell = this.getCellForVoxel(x, y, z);
  759. if (!cell) {
  760. - return 0;
  761. + cell = this.addCellForVoxel(x, y, z);
  762. }
  763. const voxelOffset = this.computeVoxelOffset(x, y, z);
  764. cell[voxelOffset] = v;
  765. }
  766. + addCellForVoxel(x, y, z) {
  767. + const cellId = this.computeCellId(x, y, z);
  768. + let cell = this.cells[cellId];
  769. + if (!cell) {
  770. + const {cellSize} = this;
  771. + cell = new Uint8Array(cellSize * cellSize * cellSize);
  772. + this.cells[cellId] = cell;
  773. + }
  774. + return cell;
  775. + }
  776. ```
  777. Let's make this editable.
  778. First we`ll add a UI. Using radio buttons we can make an 8x2
  779. array of tiles
  780. ```html
  781. <body>
  782. <canvas id="c"></canvas>
  783. + <div id="ui">
  784. + <div class="tiles">
  785. + <input type="radio" name="voxel" id="voxel1" value="1"><label for="voxel1" style="background-position: -0% -0%"></label>
  786. + <input type="radio" name="voxel" id="voxel2" value="2"><label for="voxel2" style="background-position: -100% -0%"></label>
  787. + <input type="radio" name="voxel" id="voxel3" value="3"><label for="voxel3" style="background-position: -200% -0%"></label>
  788. + <input type="radio" name="voxel" id="voxel4" value="4"><label for="voxel4" style="background-position: -300% -0%"></label>
  789. + <input type="radio" name="voxel" id="voxel5" value="5"><label for="voxel5" style="background-position: -400% -0%"></label>
  790. + <input type="radio" name="voxel" id="voxel6" value="6"><label for="voxel6" style="background-position: -500% -0%"></label>
  791. + <input type="radio" name="voxel" id="voxel7" value="7"><label for="voxel7" style="background-position: -600% -0%"></label>
  792. + <input type="radio" name="voxel" id="voxel8" value="8"><label for="voxel8" style="background-position: -700% -0%"></label>
  793. + </div>
  794. + <div class="tiles">
  795. + <input type="radio" name="voxel" id="voxel9" value="9" ><label for="voxel9" style="background-position: -800% -0%"></label>
  796. + <input type="radio" name="voxel" id="voxel10" value="10"><label for="voxel10" style="background-position: -900% -0%"></label>
  797. + <input type="radio" name="voxel" id="voxel11" value="11"><label for="voxel11" style="background-position: -1000% -0%"></label>
  798. + <input type="radio" name="voxel" id="voxel12" value="12"><label for="voxel12" style="background-position: -1100% -0%"></label>
  799. + <input type="radio" name="voxel" id="voxel13" value="13"><label for="voxel13" style="background-position: -1200% -0%"></label>
  800. + <input type="radio" name="voxel" id="voxel14" value="14"><label for="voxel14" style="background-position: -1300% -0%"></label>
  801. + <input type="radio" name="voxel" id="voxel15" value="15"><label for="voxel15" style="background-position: -1400% -0%"></label>
  802. + <input type="radio" name="voxel" id="voxel16" value="16"><label for="voxel16" style="background-position: -1500% -0%"></label>
  803. + </div>
  804. + </div>
  805. </body>
  806. ```
  807. And add some CSS to style it, display the tiles and highlight
  808. the current selection
  809. ```css
  810. body {
  811. margin: 0;
  812. }
  813. #c {
  814. width: 100%;
  815. height: 100%;
  816. display: block;
  817. }
  818. +#ui {
  819. + position: absolute;
  820. + left: 10px;
  821. + top: 10px;
  822. + background: rgba(0, 0, 0, 0.8);
  823. + padding: 5px;
  824. +}
  825. +#ui input[type=radio] {
  826. + width: 0;
  827. + height: 0;
  828. + display: none;
  829. +}
  830. +#ui input[type=radio] + label {
  831. + background-image: url('resources/images/minecraft/flourish-cc-by-nc-sa.png');
  832. + background-size: 1600% 400%;
  833. + image-rendering: pixelated;
  834. + width: 64px;
  835. + height: 64px;
  836. + display: inline-block;
  837. +}
  838. +#ui input[type=radio]:checked + label {
  839. + outline: 3px solid red;
  840. +}
  841. +@media (max-width: 600px), (max-height: 600px) {
  842. + #ui input[type=radio] + label {
  843. + width: 32px;
  844. + height: 32px;
  845. + }
  846. +}
  847. ```
  848. The UX will be as follows. If no tile is selected and you click a voxel that
  849. voxel will be erased or if you click a voxel and are holding the shift key it
  850. will be erased. Otherwise if a tiles is selected it will be added. You can
  851. deselect the selected tile type by clicking it again.
  852. This code will let the user unselect the highlighted
  853. radio button.
  854. ```js
  855. let currentVoxel = 0;
  856. let currentId;
  857. document.querySelectorAll('#ui .tiles input[type=radio][name=voxel]').forEach((elem) => {
  858. elem.addEventListener('click', allowUncheck);
  859. });
  860. function allowUncheck() {
  861. if (this.id === currentId) {
  862. this.checked = false;
  863. currentId = undefined;
  864. currentVoxel = 0;
  865. } else {
  866. currentId = this.id;
  867. currentVoxel = parseInt(this.value);
  868. }
  869. }
  870. ```
  871. And this below code will let us set a voxel based on where
  872. the user clicks. It uses code similar to the code we
  873. made in [the article on picking](threejs-picking.html)
  874. but it's not using the built in `RayCaster`. Instead
  875. it's using `VoxelWorld.intersectRay` which returns
  876. the position of intersection and the normal of the face
  877. hit.
  878. ```js
  879. function getCanvasRelativePosition(event) {
  880. const rect = canvas.getBoundingClientRect();
  881. return {
  882. x: (event.clientX - rect.left) * canvas.width / rect.width,
  883. y: (event.clientY - rect.top ) * canvas.height / rect.height,
  884. };
  885. }
  886. function placeVoxel(event) {
  887. const pos = getCanvasRelativePosition(event);
  888. const x = (pos.x / canvas.width ) * 2 - 1;
  889. const y = (pos.y / canvas.height) * -2 + 1; // note we flip Y
  890. const start = new THREE.Vector3();
  891. const end = new THREE.Vector3();
  892. start.setFromMatrixPosition(camera.matrixWorld);
  893. end.set(x, y, 1).unproject(camera);
  894. const intersection = world.intersectRay(start, end);
  895. if (intersection) {
  896. const voxelId = event.shiftKey ? 0 : currentVoxel;
  897. // the intersection point is on the face. That means
  898. // the math imprecision could put us on either side of the face.
  899. // so go half a normal into the voxel if removing (currentVoxel = 0)
  900. // our out of the voxel if adding (currentVoxel > 0)
  901. const pos = intersection.position.map((v, ndx) => {
  902. return v + intersection.normal[ndx] * (voxelId > 0 ? 0.5 : -0.5);
  903. });
  904. world.setVoxel(...pos, voxelId);
  905. updateVoxelGeometry(...pos);
  906. requestRenderIfNotRequested();
  907. }
  908. }
  909. const mouse = {
  910. x: 0,
  911. y: 0,
  912. };
  913. function recordStartPosition(event) {
  914. mouse.x = event.clientX;
  915. mouse.y = event.clientY;
  916. mouse.moveX = 0;
  917. mouse.moveY = 0;
  918. }
  919. function recordMovement(event) {
  920. mouse.moveX += Math.abs(mouse.x - event.clientX);
  921. mouse.moveY += Math.abs(mouse.y - event.clientY);
  922. }
  923. function placeVoxelIfNoMovement(event) {
  924. if (mouse.moveX < 5 && mouse.moveY < 5) {
  925. placeVoxel(event);
  926. }
  927. window.removeEventListener('pointermove', recordMovement);
  928. window.removeEventListener('pointerup', placeVoxelIfNoMovement);
  929. }
  930. canvas.addEventListener('pointerdown', (event) => {
  931. event.preventDefault();
  932. recordStartPosition(event);
  933. window.addEventListener('pointermove', recordMovement);
  934. window.addEventListener('pointerup', placeVoxelIfNoMovement);
  935. }, {passive: false});
  936. canvas.addEventListener('touchstart', (event) => {
  937. // stop scrolling
  938. event.preventDefault();
  939. }, {passive: false});
  940. ```
  941. There's a lot going on in the code above. Basically the mouse
  942. has a dual purpose. One is to move the camera. The other is to
  943. edit the world. Placing/Erasing a voxel happen when you let off the mouse
  944. but only if you have not moved the mouse since you first pressed down.
  945. This is just a guess that if you did move the mouse you were trying
  946. to move the camera, not place a block. `moveX` and `moveY` are
  947. in absolute movement so if you move to the left 10 and then back to
  948. the right 10 you'll have moved 20 units. In that case the user likely
  949. was just rotating the model back and forth and does not want to
  950. place a block. I didn't do any testing to see if `5` is a good range or not.
  951. In the code we call `world.setVoxel` to set a voxel and
  952. then `updateVoxelGeometry` to update the three.js geometry
  953. based on what's changed.
  954. Let's make that now. If the user clicks a
  955. voxel on the edge of a cell then the geometry for the voxel
  956. in the adjacent cell might need new geometry. This means
  957. we need to check the cell for the voxel we just edited
  958. as well as in all 6 directions from that cell.
  959. ```js
  960. const neighborOffsets = [
  961. [ 0, 0, 0], // self
  962. [-1, 0, 0], // left
  963. [ 1, 0, 0], // right
  964. [ 0, -1, 0], // down
  965. [ 0, 1, 0], // up
  966. [ 0, 0, -1], // back
  967. [ 0, 0, 1], // front
  968. ];
  969. function updateVoxelGeometry(x, y, z) {
  970. const updatedCellIds = {};
  971. for (const offset of neighborOffsets) {
  972. const ox = x + offset[0];
  973. const oy = y + offset[1];
  974. const oz = z + offset[2];
  975. const cellId = world.computeCellId(ox, oy, oz);
  976. if (!updatedCellIds[cellId]) {
  977. updatedCellIds[cellId] = true;
  978. updateCellGeometry(ox, oy, oz);
  979. }
  980. }
  981. }
  982. ```
  983. I thought about checking for adjacent cells like
  984. ```js
  985. const voxelX = THREE.MathUtils.euclideanModulo(x, cellSize) | 0;
  986. if (voxelX === 0) {
  987. // update cell to the left
  988. } else if (voxelX === cellSize - 1) {
  989. // update cell to the right
  990. }
  991. ```
  992. and there would be 4 more checks for the other 4 directions
  993. but it occurred to me the code would be much simpler with
  994. just an array of offsets and saving off the cell ids of
  995. the cells we already updated. If the updated voxel is not
  996. on the edge of a cell then the test will quickly reject updating
  997. the same cell.
  998. For `updateCellGeometry` we're just going to take the code we
  999. had before that was generating the geometry for one cell
  1000. and make it handle multiple cells.
  1001. ```js
  1002. const cellIdToMesh = {};
  1003. function updateCellGeometry(x, y, z) {
  1004. const cellX = Math.floor(x / cellSize);
  1005. const cellY = Math.floor(y / cellSize);
  1006. const cellZ = Math.floor(z / cellSize);
  1007. const cellId = world.computeCellId(x, y, z);
  1008. let mesh = cellIdToMesh[cellId];
  1009. const geometry = mesh ? mesh.geometry : new THREE.BufferGeometry();
  1010. const {positions, normals, uvs, indices} = world.generateGeometryDataForCell(cellX, cellY, cellZ);
  1011. const positionNumComponents = 3;
  1012. geometry.setAttribute('position', new THREE.BufferAttribute(new Float32Array(positions), positionNumComponents));
  1013. const normalNumComponents = 3;
  1014. geometry.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(normals), normalNumComponents));
  1015. const uvNumComponents = 2;
  1016. geometry.setAttribute('uv', new THREE.BufferAttribute(new Float32Array(uvs), uvNumComponents));
  1017. geometry.setIndex(indices);
  1018. geometry.computeBoundingSphere();
  1019. if (!mesh) {
  1020. mesh = new THREE.Mesh(geometry, material);
  1021. mesh.name = cellId;
  1022. cellIdToMesh[cellId] = mesh;
  1023. scene.add(mesh);
  1024. mesh.position.set(cellX * cellSize, cellY * cellSize, cellZ * cellSize);
  1025. }
  1026. }
  1027. ```
  1028. The code above checks a map of cell ids to meshes. If
  1029. we ask for a cell that doesn't exist a new `Mesh` is made
  1030. and added to the correct place in world space.
  1031. At the end we update the attributes and indices with the new data.
  1032. {{{example url="../threejs-voxel-geometry-culled-faces-ui.html"}}}
  1033. Some notes:
  1034. `RayCaster` might have worked just fine. I didn't try it.
  1035. Instead I found [a voxel specific raycaster](http://www.cse.chalmers.se/edu/year/2010/course/TDA361/grid.pdf).
  1036. that is optimized for voxels.
  1037. I made `intersectRay` part of VoxelWorld because it seemed
  1038. like if it gets too slow we could raycast against cells
  1039. before raycasting on voxels as a simple speed up if it becomes
  1040. too slow.
  1041. You might want to change the length of the raycast
  1042. as currently it's all the way to Z-far. I expect if the
  1043. user clicks something too far way they don't really want
  1044. to be placing blocks on the other side of the world that
  1045. are 1 or 2 pixel large.
  1046. Calling `geometry.computeBoundingSphere` might be slow.
  1047. We could just manually set the bounding sphere to the fit
  1048. the entire cell.
  1049. Do we want remove cells if all voxels in that cell are 0?
  1050. That would probably be reasonable change if we wanted to ship this.
  1051. Thinking about how this works it's clear the absolute
  1052. worst case is a checkerboard of on and off voxels. I don't
  1053. know off the top of my head what other strategies to use
  1054. if things get too slow. Maybe getting too slow would just
  1055. encourage the user not to make giant checkerboard areas.
  1056. To keep it simple the texture atlas is just 1 column
  1057. per voxel type. It would be better to make something more
  1058. flexible where we have a table of voxel types and each
  1059. type can specify where its face textures are in the atlas.
  1060. As it is lots of space is wasted.
  1061. Looking at real minecraft there are tiles that are not
  1062. voxels, not cubes. Like a fence tile or flowers. To do that
  1063. we'd again need some table of voxel types and for each
  1064. voxel whether it's a cube or some other geometry. If it's
  1065. not a cube the neighbor check when generating the geometry
  1066. would also need to change. A flower voxel next to another
  1067. voxel should not remove the faces between them.
  1068. If you want to make some minecraft like thing using three.js
  1069. I hope this has given you some ideas where to start and how
  1070. to generate some what efficient geometry.
  1071. <canvas id="c"></canvas>
  1072. <script type="module" src="resources/threejs-voxel-geometry.js"></script>
粤ICP备19079148号