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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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. <script type="importmap">
  75. {
  76. "imports": {
  77. "three": "../../build/three.module.js",
  78. "three/addons/": "../../examples/jsm/"
  79. }
  80. }
  81. </script>
  82. <script type="module">
  83. import * as THREE from 'three';
  84. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  85. class VoxelWorld {
  86. constructor( options ) {
  87. this.cellSize = options.cellSize;
  88. this.tileSize = options.tileSize;
  89. this.tileTextureWidth = options.tileTextureWidth;
  90. this.tileTextureHeight = options.tileTextureHeight;
  91. const { cellSize } = this;
  92. this.cellSliceSize = cellSize * cellSize;
  93. this.cells = {};
  94. }
  95. computeVoxelOffset( x, y, z ) {
  96. const { cellSize, cellSliceSize } = this;
  97. const voxelX = THREE.MathUtils.euclideanModulo( x, cellSize ) | 0;
  98. const voxelY = THREE.MathUtils.euclideanModulo( y, cellSize ) | 0;
  99. const voxelZ = THREE.MathUtils.euclideanModulo( z, cellSize ) | 0;
  100. return voxelY * cellSliceSize +
  101. voxelZ * cellSize +
  102. voxelX;
  103. }
  104. computeCellId( x, y, z ) {
  105. const { cellSize } = this;
  106. const cellX = Math.floor( x / cellSize );
  107. const cellY = Math.floor( y / cellSize );
  108. const cellZ = Math.floor( z / cellSize );
  109. return `${cellX},${cellY},${cellZ}`;
  110. }
  111. addCellForVoxel( x, y, z ) {
  112. const cellId = this.computeCellId( x, y, z );
  113. let cell = this.cells[ cellId ];
  114. if ( ! cell ) {
  115. const { cellSize } = this;
  116. cell = new Uint8Array( cellSize * cellSize * cellSize );
  117. this.cells[ cellId ] = cell;
  118. }
  119. return cell;
  120. }
  121. getCellForVoxel( x, y, z ) {
  122. return this.cells[ this.computeCellId( x, y, z ) ];
  123. }
  124. setVoxel( x, y, z, v, addCell = true ) {
  125. let cell = this.getCellForVoxel( x, y, z );
  126. if ( ! cell ) {
  127. if ( ! addCell ) {
  128. return;
  129. }
  130. cell = this.addCellForVoxel( x, y, z );
  131. }
  132. const voxelOffset = this.computeVoxelOffset( x, y, z );
  133. cell[ voxelOffset ] = v;
  134. }
  135. getVoxel( x, y, z ) {
  136. const cell = this.getCellForVoxel( x, y, z );
  137. if ( ! cell ) {
  138. return 0;
  139. }
  140. const voxelOffset = this.computeVoxelOffset( x, y, z );
  141. return cell[ voxelOffset ];
  142. }
  143. generateGeometryDataForCell( cellX, cellY, cellZ ) {
  144. const { cellSize, tileSize, tileTextureWidth, tileTextureHeight } = this;
  145. const positions = [];
  146. const normals = [];
  147. const uvs = [];
  148. const indices = [];
  149. const startX = cellX * cellSize;
  150. const startY = cellY * cellSize;
  151. const startZ = cellZ * cellSize;
  152. for ( let y = 0; y < cellSize; ++ y ) {
  153. const voxelY = startY + y;
  154. for ( let z = 0; z < cellSize; ++ z ) {
  155. const voxelZ = startZ + z;
  156. for ( let x = 0; x < cellSize; ++ x ) {
  157. const voxelX = startX + x;
  158. const voxel = this.getVoxel( voxelX, voxelY, voxelZ );
  159. if ( voxel ) {
  160. // voxel 0 is sky (empty) so for UVs we start at 0
  161. const uvVoxel = voxel - 1;
  162. // There is a voxel here but do we need faces for it?
  163. for ( const { dir, corners, uvRow } of VoxelWorld.faces ) {
  164. const neighbor = this.getVoxel(
  165. voxelX + dir[ 0 ],
  166. voxelY + dir[ 1 ],
  167. voxelZ + dir[ 2 ] );
  168. if ( ! neighbor ) {
  169. // this voxel has no neighbor in this direction so we need a face.
  170. const ndx = positions.length / 3;
  171. for ( const { pos, uv } of corners ) {
  172. positions.push( pos[ 0 ] + x, pos[ 1 ] + y, pos[ 2 ] + z );
  173. normals.push( ...dir );
  174. uvs.push(
  175. ( uvVoxel + uv[ 0 ] ) * tileSize / tileTextureWidth,
  176. 1 - ( uvRow + 1 - uv[ 1 ] ) * tileSize / tileTextureHeight );
  177. }
  178. indices.push(
  179. ndx, ndx + 1, ndx + 2,
  180. ndx + 2, ndx + 1, ndx + 3,
  181. );
  182. }
  183. }
  184. }
  185. }
  186. }
  187. }
  188. return {
  189. positions,
  190. normals,
  191. uvs,
  192. indices,
  193. };
  194. }
  195. // from
  196. // https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.42.3443&rep=rep1&type=pdf
  197. intersectRay( start, end ) {
  198. let dx = end.x - start.x;
  199. let dy = end.y - start.y;
  200. let dz = end.z - start.z;
  201. const lenSq = dx * dx + dy * dy + dz * dz;
  202. const len = Math.sqrt( lenSq );
  203. dx /= len;
  204. dy /= len;
  205. dz /= len;
  206. let t = 0.0;
  207. let ix = Math.floor( start.x );
  208. let iy = Math.floor( start.y );
  209. let iz = Math.floor( start.z );
  210. const stepX = ( dx > 0 ) ? 1 : - 1;
  211. const stepY = ( dy > 0 ) ? 1 : - 1;
  212. const stepZ = ( dz > 0 ) ? 1 : - 1;
  213. const txDelta = Math.abs( 1 / dx );
  214. const tyDelta = Math.abs( 1 / dy );
  215. const tzDelta = Math.abs( 1 / dz );
  216. const xDist = ( stepX > 0 ) ? ( ix + 1 - start.x ) : ( start.x - ix );
  217. const yDist = ( stepY > 0 ) ? ( iy + 1 - start.y ) : ( start.y - iy );
  218. const zDist = ( stepZ > 0 ) ? ( iz + 1 - start.z ) : ( start.z - iz );
  219. // location of nearest voxel boundary, in units of t
  220. let txMax = ( txDelta < Infinity ) ? txDelta * xDist : Infinity;
  221. let tyMax = ( tyDelta < Infinity ) ? tyDelta * yDist : Infinity;
  222. let tzMax = ( tzDelta < Infinity ) ? tzDelta * zDist : Infinity;
  223. let steppedIndex = - 1;
  224. // main loop along raycast vector
  225. while ( t <= len ) {
  226. const voxel = this.getVoxel( ix, iy, iz );
  227. if ( voxel ) {
  228. return {
  229. position: [
  230. start.x + t * dx,
  231. start.y + t * dy,
  232. start.z + t * dz,
  233. ],
  234. normal: [
  235. steppedIndex === 0 ? - stepX : 0,
  236. steppedIndex === 1 ? - stepY : 0,
  237. steppedIndex === 2 ? - stepZ : 0,
  238. ],
  239. voxel,
  240. };
  241. }
  242. // advance t to next nearest voxel boundary
  243. if ( txMax < tyMax ) {
  244. if ( txMax < tzMax ) {
  245. ix += stepX;
  246. t = txMax;
  247. txMax += txDelta;
  248. steppedIndex = 0;
  249. } else {
  250. iz += stepZ;
  251. t = tzMax;
  252. tzMax += tzDelta;
  253. steppedIndex = 2;
  254. }
  255. } else {
  256. if ( tyMax < tzMax ) {
  257. iy += stepY;
  258. t = tyMax;
  259. tyMax += tyDelta;
  260. steppedIndex = 1;
  261. } else {
  262. iz += stepZ;
  263. t = tzMax;
  264. tzMax += tzDelta;
  265. steppedIndex = 2;
  266. }
  267. }
  268. }
  269. return null;
  270. }
  271. }
  272. VoxelWorld.faces = [
  273. { // left
  274. uvRow: 0,
  275. dir: [ - 1, 0, 0, ],
  276. corners: [
  277. { pos: [ 0, 1, 0 ], uv: [ 0, 1 ], },
  278. { pos: [ 0, 0, 0 ], uv: [ 0, 0 ], },
  279. { pos: [ 0, 1, 1 ], uv: [ 1, 1 ], },
  280. { pos: [ 0, 0, 1 ], uv: [ 1, 0 ], },
  281. ],
  282. },
  283. { // right
  284. uvRow: 0,
  285. dir: [ 1, 0, 0, ],
  286. corners: [
  287. { pos: [ 1, 1, 1 ], uv: [ 0, 1 ], },
  288. { pos: [ 1, 0, 1 ], uv: [ 0, 0 ], },
  289. { pos: [ 1, 1, 0 ], uv: [ 1, 1 ], },
  290. { pos: [ 1, 0, 0 ], uv: [ 1, 0 ], },
  291. ],
  292. },
  293. { // bottom
  294. uvRow: 1,
  295. dir: [ 0, - 1, 0, ],
  296. corners: [
  297. { pos: [ 1, 0, 1 ], uv: [ 1, 0 ], },
  298. { pos: [ 0, 0, 1 ], uv: [ 0, 0 ], },
  299. { pos: [ 1, 0, 0 ], uv: [ 1, 1 ], },
  300. { pos: [ 0, 0, 0 ], uv: [ 0, 1 ], },
  301. ],
  302. },
  303. { // top
  304. uvRow: 2,
  305. dir: [ 0, 1, 0, ],
  306. corners: [
  307. { pos: [ 0, 1, 1 ], uv: [ 1, 1 ], },
  308. { pos: [ 1, 1, 1 ], uv: [ 0, 1 ], },
  309. { pos: [ 0, 1, 0 ], uv: [ 1, 0 ], },
  310. { pos: [ 1, 1, 0 ], uv: [ 0, 0 ], },
  311. ],
  312. },
  313. { // back
  314. uvRow: 0,
  315. dir: [ 0, 0, - 1, ],
  316. corners: [
  317. { pos: [ 1, 0, 0 ], uv: [ 0, 0 ], },
  318. { pos: [ 0, 0, 0 ], uv: [ 1, 0 ], },
  319. { pos: [ 1, 1, 0 ], uv: [ 0, 1 ], },
  320. { pos: [ 0, 1, 0 ], uv: [ 1, 1 ], },
  321. ],
  322. },
  323. { // front
  324. uvRow: 0,
  325. dir: [ 0, 0, 1, ],
  326. corners: [
  327. { pos: [ 0, 0, 1 ], uv: [ 0, 0 ], },
  328. { pos: [ 1, 0, 1 ], uv: [ 1, 0 ], },
  329. { pos: [ 0, 1, 1 ], uv: [ 0, 1 ], },
  330. { pos: [ 1, 1, 1 ], uv: [ 1, 1 ], },
  331. ],
  332. },
  333. ];
  334. function main() {
  335. const canvas = document.querySelector( '#c' );
  336. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
  337. const cellSize = 32;
  338. const fov = 75;
  339. const aspect = 2; // the canvas default
  340. const near = 0.1;
  341. const far = 1000;
  342. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  343. camera.position.set( - cellSize * .3, cellSize * .8, - cellSize * .3 );
  344. const controls = new OrbitControls( camera, canvas );
  345. controls.target.set( cellSize / 2, cellSize / 3, cellSize / 2 );
  346. controls.update();
  347. const scene = new THREE.Scene();
  348. scene.background = new THREE.Color( 'lightblue' );
  349. const tileSize = 16;
  350. const tileTextureWidth = 256;
  351. const tileTextureHeight = 64;
  352. const loader = new THREE.TextureLoader();
  353. const texture = loader.load( 'resources/images/minecraft/flourish-cc-by-nc-sa.png', render );
  354. texture.magFilter = THREE.NearestFilter;
  355. texture.minFilter = THREE.NearestFilter;
  356. texture.colorSpace = THREE.SRGBColorSpace;
  357. function addLight( x, y, z ) {
  358. const color = 0xFFFFFF;
  359. const intensity = 3;
  360. const light = new THREE.DirectionalLight( color, intensity );
  361. light.position.set( x, y, z );
  362. scene.add( light );
  363. }
  364. addLight( - 1, 2, 4 );
  365. addLight( 1, - 1, - 2 );
  366. const world = new VoxelWorld( {
  367. cellSize,
  368. tileSize,
  369. tileTextureWidth,
  370. tileTextureHeight,
  371. } );
  372. const material = new THREE.MeshLambertMaterial( {
  373. map: texture,
  374. side: THREE.DoubleSide,
  375. alphaTest: 0.1,
  376. transparent: true,
  377. } );
  378. const cellIdToMesh = {};
  379. function updateCellGeometry( x, y, z ) {
  380. const cellX = Math.floor( x / cellSize );
  381. const cellY = Math.floor( y / cellSize );
  382. const cellZ = Math.floor( z / cellSize );
  383. const cellId = world.computeCellId( x, y, z );
  384. let mesh = cellIdToMesh[ cellId ];
  385. const geometry = mesh ? mesh.geometry : new THREE.BufferGeometry();
  386. const { positions, normals, uvs, indices } = world.generateGeometryDataForCell( cellX, cellY, cellZ );
  387. const positionNumComponents = 3;
  388. geometry.setAttribute( 'position', new THREE.BufferAttribute( new Float32Array( positions ), positionNumComponents ) );
  389. const normalNumComponents = 3;
  390. geometry.setAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( normals ), normalNumComponents ) );
  391. const uvNumComponents = 2;
  392. geometry.setAttribute( 'uv', new THREE.BufferAttribute( new Float32Array( uvs ), uvNumComponents ) );
  393. geometry.setIndex( indices );
  394. geometry.computeBoundingSphere();
  395. if ( ! mesh ) {
  396. mesh = new THREE.Mesh( geometry, material );
  397. mesh.name = cellId;
  398. cellIdToMesh[ cellId ] = mesh;
  399. scene.add( mesh );
  400. mesh.position.set( cellX * cellSize, cellY * cellSize, cellZ * cellSize );
  401. }
  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 ) * canvas.width / rect.width,
  486. y: ( event.clientY - rect.top ) * canvas.height / rect.height,
  487. };
  488. }
  489. function placeVoxel( event ) {
  490. const pos = getCanvasRelativePosition( event );
  491. const x = ( pos.x / canvas.width ) * 2 - 1;
  492. const y = ( pos.y / canvas.height ) * - 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( 'pointermove', recordMovement );
  531. window.removeEventListener( 'pointerup', placeVoxelIfNoMovement );
  532. }
  533. canvas.addEventListener( 'pointerdown', ( event ) => {
  534. event.preventDefault();
  535. recordStartPosition( event );
  536. window.addEventListener( 'pointermove', recordMovement );
  537. window.addEventListener( 'pointerup', placeVoxelIfNoMovement );
  538. }, { passive: false } );
  539. canvas.addEventListener( 'touchstart', ( event ) => {
  540. // prevent scrolling
  541. event.preventDefault();
  542. }, { passive: false } );
  543. controls.addEventListener( 'change', requestRenderIfNotRequested );
  544. window.addEventListener( 'resize', requestRenderIfNotRequested );
  545. }
  546. main();
  547. </script>
  548. </html>