voxel-geometry-culled-faces-ui.html 18 KB

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