threejs-voxel-geometry-culled-faces-ui.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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 - UI</title>
  8. <style>
  9. body {
  10. margin: 0;
  11. }
  12. #c {
  13. width: 100vw;
  14. height: 100vh;
  15. display: block;
  16. }
  17. #ui {
  18. position: absolute;
  19. left: 10px;
  20. top: 10px;
  21. background: rgba(0, 0, 0, 0.8);
  22. padding: 5px;
  23. }
  24. #ui input[type=radio] {
  25. width: 0;
  26. height: 0;
  27. display: none;
  28. }
  29. #ui input[type=radio] + label {
  30. background-image: url('resources/images/minecraft/flourish-cc-by-nc-sa.png');
  31. background-size: 1600% 400%;
  32. image-rendering: pixelated;
  33. width: 64px;
  34. height: 64px;
  35. display: inline-block;
  36. }
  37. #ui input[type=radio]:checked + label {
  38. outline: 3px solid red;
  39. }
  40. @media (max-width: 600px), (max-height: 600px) {
  41. #ui input[type=radio] + label {
  42. width: 32px;
  43. height: 32px;
  44. }
  45. }
  46. </style>
  47. </head>
  48. <body>
  49. <canvas id="c"></canvas>
  50. <div id="ui">
  51. <div class="tiles">
  52. <input type="radio" name="voxel" id="voxel1" value="1"><label for="voxel1" style="background-position: -0% -0%"></label>
  53. <input type="radio" name="voxel" id="voxel2" value="2"><label for="voxel2" style="background-position: -100% -0%"></label>
  54. <input type="radio" name="voxel" id="voxel3" value="3"><label for="voxel3" style="background-position: -200% -0%"></label>
  55. <input type="radio" name="voxel" id="voxel4" value="4"><label for="voxel4" style="background-position: -300% -0%"></label>
  56. <input type="radio" name="voxel" id="voxel5" value="5"><label for="voxel5" style="background-position: -400% -0%"></label>
  57. <input type="radio" name="voxel" id="voxel6" value="6"><label for="voxel6" style="background-position: -500% -0%"></label>
  58. <input type="radio" name="voxel" id="voxel7" value="7"><label for="voxel7" style="background-position: -600% -0%"></label>
  59. <input type="radio" name="voxel" id="voxel8" value="8"><label for="voxel8" style="background-position: -700% -0%"></label>
  60. </div>
  61. <div class="tiles">
  62. <input type="radio" name="voxel" id="voxel9" value="9" ><label for="voxel9" style="background-position: -800% -0%"></label>
  63. <input type="radio" name="voxel" id="voxel10" value="10"><label for="voxel10" style="background-position: -900% -0%"></label>
  64. <input type="radio" name="voxel" id="voxel11" value="11"><label for="voxel11" style="background-position: -1000% -0%"></label>
  65. <input type="radio" name="voxel" id="voxel12" value="12"><label for="voxel12" style="background-position: -1100% -0%"></label>
  66. <input type="radio" name="voxel" id="voxel13" value="13"><label for="voxel13" style="background-position: -1200% -0%"></label>
  67. <input type="radio" name="voxel" id="voxel14" value="14"><label for="voxel14" style="background-position: -1300% -0%"></label>
  68. <input type="radio" name="voxel" id="voxel15" value="15"><label for="voxel15" style="background-position: -1400% -0%"></label>
  69. <input type="radio" name="voxel" id="voxel16" value="16"><label for="voxel16" style="background-position: -1500% -0%"></label>
  70. </div>
  71. </div>
  72. </body>
  73. <script type="module">
  74. import * as THREE from './resources/threejs/r108/build/three.module.js';
  75. import {OrbitControls} from './resources/threejs/r108/examples/jsm/controls/OrbitControls.js';
  76. class VoxelWorld {
  77. constructor(options) {
  78. this.cellSize = options.cellSize;
  79. this.tileSize = options.tileSize;
  80. this.tileTextureWidth = options.tileTextureWidth;
  81. this.tileTextureHeight = options.tileTextureHeight;
  82. const {cellSize} = this;
  83. this.cellSliceSize = cellSize * cellSize;
  84. this.cells = {};
  85. }
  86. computeVoxelOffset(x, y, z) {
  87. const {cellSize, cellSliceSize} = this;
  88. const voxelX = THREE.Math.euclideanModulo(x, cellSize) | 0;
  89. const voxelY = THREE.Math.euclideanModulo(y, cellSize) | 0;
  90. const voxelZ = THREE.Math.euclideanModulo(z, cellSize) | 0;
  91. return voxelY * cellSliceSize +
  92. voxelZ * cellSize +
  93. voxelX;
  94. }
  95. computeCellId(x, y, z) {
  96. const {cellSize} = this;
  97. const cellX = Math.floor(x / cellSize);
  98. const cellY = Math.floor(y / cellSize);
  99. const cellZ = Math.floor(z / cellSize);
  100. return `${cellX},${cellY},${cellZ}`;
  101. }
  102. addCellForVoxel(x, y, z) {
  103. const cellId = this.computeCellId(x, y, z);
  104. let cell = this.cells[cellId];
  105. if (!cell) {
  106. const {cellSize} = this;
  107. cell = new Uint8Array(cellSize * cellSize * cellSize);
  108. this.cells[cellId] = cell;
  109. }
  110. return cell;
  111. }
  112. getCellForVoxel(x, y, z) {
  113. return this.cells[this.computeCellId(x, y, z)];
  114. }
  115. setVoxel(x, y, z, v, addCell = true) {
  116. let cell = this.getCellForVoxel(x, y, z);
  117. if (!cell) {
  118. if (!addCell) {
  119. return;
  120. }
  121. cell = this.addCellForVoxel(x, y, z);
  122. }
  123. const voxelOffset = this.computeVoxelOffset(x, y, z);
  124. cell[voxelOffset] = v;
  125. }
  126. getVoxel(x, y, z) {
  127. const cell = this.getCellForVoxel(x, y, z);
  128. if (!cell) {
  129. return 0;
  130. }
  131. const voxelOffset = this.computeVoxelOffset(x, y, z);
  132. return cell[voxelOffset];
  133. }
  134. generateGeometryDataForCell(cellX, cellY, cellZ) {
  135. const {cellSize, tileSize, tileTextureWidth, tileTextureHeight} = this;
  136. const positions = [];
  137. const normals = [];
  138. const uvs = [];
  139. const indices = [];
  140. const startX = cellX * cellSize;
  141. const startY = cellY * cellSize;
  142. const startZ = cellZ * cellSize;
  143. for (let y = 0; y < cellSize; ++y) {
  144. const voxelY = startY + y;
  145. for (let z = 0; z < cellSize; ++z) {
  146. const voxelZ = startZ + z;
  147. for (let x = 0; x < cellSize; ++x) {
  148. const voxelX = startX + x;
  149. const voxel = this.getVoxel(voxelX, voxelY, voxelZ);
  150. if (voxel) {
  151. // voxel 0 is sky (empty) so for UVs we start at 0
  152. const uvVoxel = voxel - 1;
  153. // There is a voxel here but do we need faces for it?
  154. for (const {dir, corners, uvRow} of VoxelWorld.faces) {
  155. const neighbor = this.getVoxel(
  156. voxelX + dir[0],
  157. voxelY + dir[1],
  158. voxelZ + dir[2]);
  159. if (!neighbor) {
  160. // this voxel has no neighbor in this direction so we need a face.
  161. const ndx = positions.length / 3;
  162. for (const {pos, uv} of corners) {
  163. positions.push(pos[0] + x, pos[1] + y, pos[2] + z);
  164. normals.push(...dir);
  165. uvs.push(
  166. (uvVoxel + uv[0]) * tileSize / tileTextureWidth,
  167. 1 - (uvRow + 1 - uv[1]) * tileSize / tileTextureHeight);
  168. }
  169. indices.push(
  170. ndx, ndx + 1, ndx + 2,
  171. ndx + 2, ndx + 1, ndx + 3,
  172. );
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }
  179. return {
  180. positions,
  181. normals,
  182. uvs,
  183. indices,
  184. };
  185. }
  186. // from
  187. // http://www.cse.chalmers.se/edu/year/2010/course/TDA361/grid.pdf
  188. intersectRay(start, end) {
  189. let dx = end.x - start.x;
  190. let dy = end.y - start.y;
  191. let dz = end.z - start.z;
  192. const lenSq = dx * dx + dy * dy + dz * dz;
  193. const len = Math.sqrt(lenSq);
  194. dx /= len;
  195. dy /= len;
  196. dz /= len;
  197. let t = 0.0;
  198. let ix = Math.floor(start.x);
  199. let iy = Math.floor(start.y);
  200. let iz = Math.floor(start.z);
  201. const stepX = (dx > 0) ? 1 : -1;
  202. const stepY = (dy > 0) ? 1 : -1;
  203. const stepZ = (dz > 0) ? 1 : -1;
  204. const txDelta = Math.abs(1 / dx);
  205. const tyDelta = Math.abs(1 / dy);
  206. const tzDelta = Math.abs(1 / dz);
  207. const xDist = (stepX > 0) ? (ix + 1 - start.x) : (start.x - ix);
  208. const yDist = (stepY > 0) ? (iy + 1 - start.y) : (start.y - iy);
  209. const zDist = (stepZ > 0) ? (iz + 1 - start.z) : (start.z - iz);
  210. // location of nearest voxel boundary, in units of t
  211. let txMax = (txDelta < Infinity) ? txDelta * xDist : Infinity;
  212. let tyMax = (tyDelta < Infinity) ? tyDelta * yDist : Infinity;
  213. let tzMax = (tzDelta < Infinity) ? tzDelta * zDist : Infinity;
  214. let steppedIndex = -1;
  215. // main loop along raycast vector
  216. while (t <= len) {
  217. const voxel = this.getVoxel(ix, iy, iz);
  218. if (voxel) {
  219. return {
  220. position: [
  221. start.x + t * dx,
  222. start.y + t * dy,
  223. start.z + t * dz,
  224. ],
  225. normal: [
  226. steppedIndex === 0 ? -stepX : 0,
  227. steppedIndex === 1 ? -stepY : 0,
  228. steppedIndex === 2 ? -stepZ : 0,
  229. ],
  230. voxel,
  231. };
  232. }
  233. // advance t to next nearest voxel boundary
  234. if (txMax < tyMax) {
  235. if (txMax < tzMax) {
  236. ix += stepX;
  237. t = txMax;
  238. txMax += txDelta;
  239. steppedIndex = 0;
  240. } else {
  241. iz += stepZ;
  242. t = tzMax;
  243. tzMax += tzDelta;
  244. steppedIndex = 2;
  245. }
  246. } else {
  247. if (tyMax < tzMax) {
  248. iy += stepY;
  249. t = tyMax;
  250. tyMax += tyDelta;
  251. steppedIndex = 1;
  252. } else {
  253. iz += stepZ;
  254. t = tzMax;
  255. tzMax += tzDelta;
  256. steppedIndex = 2;
  257. }
  258. }
  259. }
  260. return null;
  261. }
  262. }
  263. VoxelWorld.faces = [
  264. { // left
  265. uvRow: 0,
  266. dir: [ -1, 0, 0, ],
  267. corners: [
  268. { pos: [ 0, 1, 0 ], uv: [ 0, 1 ], },
  269. { pos: [ 0, 0, 0 ], uv: [ 0, 0 ], },
  270. { pos: [ 0, 1, 1 ], uv: [ 1, 1 ], },
  271. { pos: [ 0, 0, 1 ], uv: [ 1, 0 ], },
  272. ],
  273. },
  274. { // right
  275. uvRow: 0,
  276. dir: [ 1, 0, 0, ],
  277. corners: [
  278. { pos: [ 1, 1, 1 ], uv: [ 0, 1 ], },
  279. { pos: [ 1, 0, 1 ], uv: [ 0, 0 ], },
  280. { pos: [ 1, 1, 0 ], uv: [ 1, 1 ], },
  281. { pos: [ 1, 0, 0 ], uv: [ 1, 0 ], },
  282. ],
  283. },
  284. { // bottom
  285. uvRow: 1,
  286. dir: [ 0, -1, 0, ],
  287. corners: [
  288. { pos: [ 1, 0, 1 ], uv: [ 1, 0 ], },
  289. { pos: [ 0, 0, 1 ], uv: [ 0, 0 ], },
  290. { pos: [ 1, 0, 0 ], uv: [ 1, 1 ], },
  291. { pos: [ 0, 0, 0 ], uv: [ 0, 1 ], },
  292. ],
  293. },
  294. { // top
  295. uvRow: 2,
  296. dir: [ 0, 1, 0, ],
  297. corners: [
  298. { pos: [ 0, 1, 1 ], uv: [ 1, 1 ], },
  299. { pos: [ 1, 1, 1 ], uv: [ 0, 1 ], },
  300. { pos: [ 0, 1, 0 ], uv: [ 1, 0 ], },
  301. { pos: [ 1, 1, 0 ], uv: [ 0, 0 ], },
  302. ],
  303. },
  304. { // back
  305. uvRow: 0,
  306. dir: [ 0, 0, -1, ],
  307. corners: [
  308. { pos: [ 1, 0, 0 ], uv: [ 0, 0 ], },
  309. { pos: [ 0, 0, 0 ], uv: [ 1, 0 ], },
  310. { pos: [ 1, 1, 0 ], uv: [ 0, 1 ], },
  311. { pos: [ 0, 1, 0 ], uv: [ 1, 1 ], },
  312. ],
  313. },
  314. { // front
  315. uvRow: 0,
  316. dir: [ 0, 0, 1, ],
  317. corners: [
  318. { pos: [ 0, 0, 1 ], uv: [ 0, 0 ], },
  319. { pos: [ 1, 0, 1 ], uv: [ 1, 0 ], },
  320. { pos: [ 0, 1, 1 ], uv: [ 0, 1 ], },
  321. { pos: [ 1, 1, 1 ], uv: [ 1, 1 ], },
  322. ],
  323. },
  324. ];
  325. function main() {
  326. const canvas = document.querySelector('#c');
  327. const renderer = new THREE.WebGLRenderer({canvas});
  328. const cellSize = 32;
  329. const fov = 75;
  330. const aspect = 2; // the canvas default
  331. const near = 0.1;
  332. const far = 1000;
  333. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  334. camera.position.set(-cellSize * .3, cellSize * .8, -cellSize * .3);
  335. const controls = new OrbitControls(camera, canvas);
  336. controls.target.set(cellSize / 2, cellSize / 3, cellSize / 2);
  337. controls.update();
  338. const scene = new THREE.Scene();
  339. scene.background = new THREE.Color('lightblue');
  340. const tileSize = 16;
  341. const tileTextureWidth = 256;
  342. const tileTextureHeight = 64;
  343. const loader = new THREE.TextureLoader();
  344. const texture = loader.load('resources/images/minecraft/flourish-cc-by-nc-sa.png', render);
  345. texture.magFilter = THREE.NearestFilter;
  346. texture.minFilter = THREE.NearestFilter;
  347. function addLight(x, y, z) {
  348. const color = 0xFFFFFF;
  349. const intensity = 1;
  350. const light = new THREE.DirectionalLight(color, intensity);
  351. light.position.set(x, y, z);
  352. scene.add(light);
  353. }
  354. addLight(-1, 2, 4);
  355. addLight( 1, -1, -2);
  356. const world = new VoxelWorld({
  357. cellSize,
  358. tileSize,
  359. tileTextureWidth,
  360. tileTextureHeight,
  361. });
  362. const material = new THREE.MeshLambertMaterial({
  363. map: texture,
  364. side: THREE.DoubleSide,
  365. alphaTest: 0.1,
  366. transparent: true,
  367. });
  368. const cellIdToMesh = {};
  369. function updateCellGeometry(x, y, z) {
  370. const cellX = Math.floor(x / cellSize);
  371. const cellY = Math.floor(y / cellSize);
  372. const cellZ = Math.floor(z / cellSize);
  373. const cellId = world.computeCellId(x, y, z);
  374. let mesh = cellIdToMesh[cellId];
  375. if (!mesh) {
  376. const geometry = new THREE.BufferGeometry();
  377. const positionNumComponents = 3;
  378. const normalNumComponents = 3;
  379. const uvNumComponents = 2;
  380. geometry.addAttribute(
  381. 'position',
  382. new THREE.BufferAttribute(new Float32Array(0), positionNumComponents));
  383. geometry.addAttribute(
  384. 'normal',
  385. new THREE.BufferAttribute(new Float32Array(0), normalNumComponents));
  386. geometry.addAttribute(
  387. 'uv',
  388. new THREE.BufferAttribute(new Float32Array(0), uvNumComponents));
  389. mesh = new THREE.Mesh(geometry, material);
  390. mesh.name = cellId;
  391. cellIdToMesh[cellId] = mesh;
  392. scene.add(mesh);
  393. mesh.position.set(cellX * cellSize, cellY * cellSize, cellZ * cellSize);
  394. }
  395. const {positions, normals, uvs, indices} = world.generateGeometryDataForCell(cellX, cellY, cellZ);
  396. const geometry = mesh.geometry;
  397. geometry.getAttribute('position').setArray(new Float32Array(positions)).needsUpdate = true;
  398. geometry.getAttribute('normal').setArray(new Float32Array(normals)).needsUpdate = true;
  399. geometry.getAttribute('uv').setArray(new Float32Array(uvs)).needsUpdate = true;
  400. geometry.setIndex(indices);
  401. geometry.computeBoundingSphere();
  402. }
  403. const neighborOffsets = [
  404. [ 0, 0, 0], // self
  405. [-1, 0, 0], // left
  406. [ 1, 0, 0], // right
  407. [ 0, -1, 0], // down
  408. [ 0, 1, 0], // up
  409. [ 0, 0, -1], // back
  410. [ 0, 0, 1], // front
  411. ];
  412. function updateVoxelGeometry(x, y, z) {
  413. const updatedCellIds = {};
  414. for (const offset of neighborOffsets) {
  415. const ox = x + offset[0];
  416. const oy = y + offset[1];
  417. const oz = z + offset[2];
  418. const cellId = world.computeCellId(ox, oy, oz);
  419. if (!updatedCellIds[cellId]) {
  420. updatedCellIds[cellId] = true;
  421. updateCellGeometry(ox, oy, oz);
  422. }
  423. }
  424. }
  425. for (let y = 0; y < cellSize; ++y) {
  426. for (let z = 0; z < cellSize; ++z) {
  427. for (let x = 0; x < cellSize; ++x) {
  428. const height = (Math.sin(x / cellSize * Math.PI * 2) + Math.sin(z / cellSize * Math.PI * 3)) * (cellSize / 6) + (cellSize / 2);
  429. if (y < height) {
  430. world.setVoxel(x, y, z, randInt(1, 17));
  431. }
  432. }
  433. }
  434. }
  435. function randInt(min, max) {
  436. return Math.floor(Math.random() * (max - min) + min);
  437. }
  438. updateVoxelGeometry(1, 1, 1); // 0,0,0 will generate
  439. function resizeRendererToDisplaySize(renderer) {
  440. const canvas = renderer.domElement;
  441. const width = canvas.clientWidth;
  442. const height = canvas.clientHeight;
  443. const needResize = canvas.width !== width || canvas.height !== height;
  444. if (needResize) {
  445. renderer.setSize(width, height, false);
  446. }
  447. return needResize;
  448. }
  449. let renderRequested = false;
  450. function render() {
  451. renderRequested = undefined;
  452. if (resizeRendererToDisplaySize(renderer)) {
  453. const canvas = renderer.domElement;
  454. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  455. camera.updateProjectionMatrix();
  456. }
  457. controls.update();
  458. renderer.render(scene, camera);
  459. }
  460. render();
  461. function requestRenderIfNotRequested() {
  462. if (!renderRequested) {
  463. renderRequested = true;
  464. requestAnimationFrame(render);
  465. }
  466. }
  467. let currentVoxel = 0;
  468. let currentId;
  469. document.querySelectorAll('#ui .tiles input[type=radio][name=voxel]').forEach((elem) => {
  470. elem.addEventListener('click', allowUncheck);
  471. });
  472. function allowUncheck() {
  473. if (this.id === currentId) {
  474. this.checked = false;
  475. currentId = undefined;
  476. currentVoxel = 0;
  477. } else {
  478. currentId = this.id;
  479. currentVoxel = parseInt(this.value);
  480. }
  481. }
  482. function getCanvasRelativePosition(event) {
  483. const rect = canvas.getBoundingClientRect();
  484. return {
  485. x: event.clientX - rect.left,
  486. y: event.clientY - rect.top,
  487. };
  488. }
  489. function placeVoxel(event) {
  490. const pos = getCanvasRelativePosition(event);
  491. const x = (pos.x / canvas.clientWidth ) * 2 - 1;
  492. const y = (pos.y / canvas.clientHeight) * -2 + 1; // note we flip Y
  493. const start = new THREE.Vector3();
  494. const end = new THREE.Vector3();
  495. start.setFromMatrixPosition(camera.matrixWorld);
  496. end.set(x, y, 1).unproject(camera);
  497. const intersection = world.intersectRay(start, end);
  498. if (intersection) {
  499. const voxelId = event.shiftKey ? 0 : currentVoxel;
  500. // the intersection point is on the face. That means
  501. // the math imprecision could put us on either side of the face.
  502. // so go half a normal into the voxel if removing (currentVoxel = 0)
  503. // our out of the voxel if adding (currentVoxel > 0)
  504. const pos = intersection.position.map((v, ndx) => {
  505. return v + intersection.normal[ndx] * (voxelId > 0 ? 0.5 : -0.5);
  506. });
  507. world.setVoxel(...pos, voxelId);
  508. updateVoxelGeometry(...pos);
  509. requestRenderIfNotRequested();
  510. }
  511. }
  512. const mouse = {
  513. x: 0,
  514. y: 0,
  515. };
  516. function recordStartPosition(event) {
  517. mouse.x = event.clientX;
  518. mouse.y = event.clientY;
  519. mouse.moveX = 0;
  520. mouse.moveY = 0;
  521. }
  522. function recordMovement(event) {
  523. mouse.moveX += Math.abs(mouse.x - event.clientX);
  524. mouse.moveY += Math.abs(mouse.y - event.clientY);
  525. }
  526. function placeVoxelIfNoMovement(event) {
  527. if (mouse.moveX < 5 && mouse.moveY < 5) {
  528. placeVoxel(event);
  529. }
  530. window.removeEventListener('mousemove', recordMovement);
  531. window.removeEventListener('mouseup', placeVoxelIfNoMovement);
  532. }
  533. canvas.addEventListener('mousedown', (event) => {
  534. event.preventDefault();
  535. recordStartPosition(event);
  536. window.addEventListener('mousemove', recordMovement);
  537. window.addEventListener('mouseup', placeVoxelIfNoMovement);
  538. }, {passive: false});
  539. canvas.addEventListener('touchstart', (event) => {
  540. event.preventDefault();
  541. recordStartPosition(event.touches[0]);
  542. }, {passive: false});
  543. canvas.addEventListener('touchmove', (event) => {
  544. event.preventDefault();
  545. recordMovement(event.touches[0]);
  546. }, {passive: false});
  547. canvas.addEventListener('touchend', () => {
  548. placeVoxelIfNoMovement({
  549. clientX: mouse.x,
  550. clientY: mouse.y,
  551. });
  552. });
  553. controls.addEventListener('change', requestRenderIfNotRequested);
  554. window.addEventListener('resize', requestRenderIfNotRequested);
  555. }
  556. main();
  557. </script>
  558. </html>