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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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. const cellSize = 32;
  341. const fov = 75;
  342. const aspect = 2; // the canvas default
  343. const near = 0.1;
  344. const far = 1000;
  345. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  346. camera.position.set( - cellSize * .3, cellSize * .8, - cellSize * .3 );
  347. const controls = new OrbitControls( camera, canvas );
  348. controls.target.set( cellSize / 2, cellSize / 3, cellSize / 2 );
  349. controls.update();
  350. const scene = new THREE.Scene();
  351. scene.background = new THREE.Color( 'lightblue' );
  352. const tileSize = 16;
  353. const tileTextureWidth = 256;
  354. const tileTextureHeight = 64;
  355. const loader = new THREE.TextureLoader();
  356. const texture = loader.load( 'resources/images/minecraft/flourish-cc-by-nc-sa.png', render );
  357. texture.magFilter = THREE.NearestFilter;
  358. texture.minFilter = THREE.NearestFilter;
  359. texture.colorSpace = THREE.SRGBColorSpace;
  360. function addLight( x, y, z ) {
  361. const color = 0xFFFFFF;
  362. const intensity = 3;
  363. const light = new THREE.DirectionalLight( color, intensity );
  364. light.position.set( x, y, z );
  365. scene.add( light );
  366. }
  367. addLight( - 1, 2, 4 );
  368. addLight( 1, - 1, - 2 );
  369. const world = new VoxelWorld( {
  370. cellSize,
  371. tileSize,
  372. tileTextureWidth,
  373. tileTextureHeight,
  374. } );
  375. const material = new THREE.MeshLambertMaterial( {
  376. map: texture,
  377. side: THREE.DoubleSide,
  378. alphaTest: 0.1,
  379. transparent: true,
  380. } );
  381. const cellIdToMesh = {};
  382. function updateCellGeometry( x, y, z ) {
  383. const cellX = Math.floor( x / cellSize );
  384. const cellY = Math.floor( y / cellSize );
  385. const cellZ = Math.floor( z / cellSize );
  386. const cellId = world.computeCellId( x, y, z );
  387. let mesh = cellIdToMesh[ cellId ];
  388. const geometry = mesh ? mesh.geometry : new THREE.BufferGeometry();
  389. const { positions, normals, uvs, indices } = world.generateGeometryDataForCell( cellX, cellY, cellZ );
  390. const positionNumComponents = 3;
  391. geometry.setAttribute( 'position', new THREE.BufferAttribute( new Float32Array( positions ), positionNumComponents ) );
  392. const normalNumComponents = 3;
  393. geometry.setAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( normals ), normalNumComponents ) );
  394. const uvNumComponents = 2;
  395. geometry.setAttribute( 'uv', new THREE.BufferAttribute( new Float32Array( uvs ), uvNumComponents ) );
  396. geometry.setIndex( indices );
  397. geometry.computeBoundingSphere();
  398. if ( ! mesh ) {
  399. mesh = new THREE.Mesh( geometry, material );
  400. mesh.name = cellId;
  401. cellIdToMesh[ cellId ] = mesh;
  402. scene.add( mesh );
  403. mesh.position.set( cellX * cellSize, cellY * cellSize, cellZ * cellSize );
  404. }
  405. }
  406. const neighborOffsets = [
  407. [ 0, 0, 0 ], // self
  408. [ - 1, 0, 0 ], // left
  409. [ 1, 0, 0 ], // right
  410. [ 0, - 1, 0 ], // down
  411. [ 0, 1, 0 ], // up
  412. [ 0, 0, - 1 ], // back
  413. [ 0, 0, 1 ], // front
  414. ];
  415. function updateVoxelGeometry( x, y, z ) {
  416. const updatedCellIds = {};
  417. for ( const offset of neighborOffsets ) {
  418. const ox = x + offset[ 0 ];
  419. const oy = y + offset[ 1 ];
  420. const oz = z + offset[ 2 ];
  421. const cellId = world.computeCellId( ox, oy, oz );
  422. if ( ! updatedCellIds[ cellId ] ) {
  423. updatedCellIds[ cellId ] = true;
  424. updateCellGeometry( ox, oy, oz );
  425. }
  426. }
  427. }
  428. for ( let y = 0; y < cellSize; ++ y ) {
  429. for ( let z = 0; z < cellSize; ++ z ) {
  430. for ( let x = 0; x < cellSize; ++ x ) {
  431. const height = ( Math.sin( x / cellSize * Math.PI * 2 ) + Math.sin( z / cellSize * Math.PI * 3 ) ) * ( cellSize / 6 ) + ( cellSize / 2 );
  432. if ( y < height ) {
  433. world.setVoxel( x, y, z, randInt( 1, 17 ) );
  434. }
  435. }
  436. }
  437. }
  438. function randInt( min, max ) {
  439. return Math.floor( Math.random() * ( max - min ) + min );
  440. }
  441. updateVoxelGeometry( 1, 1, 1 ); // 0,0,0 will generate
  442. function resizeRendererToDisplaySize( renderer ) {
  443. const canvas = renderer.domElement;
  444. const width = canvas.clientWidth;
  445. const height = canvas.clientHeight;
  446. const needResize = canvas.width !== width || canvas.height !== height;
  447. if ( needResize ) {
  448. renderer.setSize( width, height, false );
  449. }
  450. return needResize;
  451. }
  452. let renderRequested = false;
  453. function render() {
  454. renderRequested = undefined;
  455. if ( resizeRendererToDisplaySize( renderer ) ) {
  456. const canvas = renderer.domElement;
  457. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  458. camera.updateProjectionMatrix();
  459. }
  460. controls.update();
  461. renderer.render( scene, camera );
  462. }
  463. render();
  464. function requestRenderIfNotRequested() {
  465. if ( ! renderRequested ) {
  466. renderRequested = true;
  467. requestAnimationFrame( render );
  468. }
  469. }
  470. let currentVoxel = 0;
  471. let currentId;
  472. document.querySelectorAll( '#ui .tiles input[type=radio][name=voxel]' ).forEach( ( elem ) => {
  473. elem.addEventListener( 'click', allowUncheck );
  474. } );
  475. function allowUncheck() {
  476. if ( this.id === currentId ) {
  477. this.checked = false;
  478. currentId = undefined;
  479. currentVoxel = 0;
  480. } else {
  481. currentId = this.id;
  482. currentVoxel = parseInt( this.value );
  483. }
  484. }
  485. function getCanvasRelativePosition( event ) {
  486. const rect = canvas.getBoundingClientRect();
  487. return {
  488. x: ( event.clientX - rect.left ) * canvas.width / rect.width,
  489. y: ( event.clientY - rect.top ) * canvas.height / rect.height,
  490. };
  491. }
  492. function placeVoxel( event ) {
  493. const pos = getCanvasRelativePosition( event );
  494. const x = ( pos.x / canvas.width ) * 2 - 1;
  495. const y = ( pos.y / canvas.height ) * - 2 + 1; // note we flip Y
  496. const start = new THREE.Vector3();
  497. const end = new THREE.Vector3();
  498. start.setFromMatrixPosition( camera.matrixWorld );
  499. end.set( x, y, 1 ).unproject( camera );
  500. const intersection = world.intersectRay( start, end );
  501. if ( intersection ) {
  502. const voxelId = event.shiftKey ? 0 : currentVoxel;
  503. // the intersection point is on the face. That means
  504. // the math imprecision could put us on either side of the face.
  505. // so go half a normal into the voxel if removing (currentVoxel = 0)
  506. // our out of the voxel if adding (currentVoxel > 0)
  507. const pos = intersection.position.map( ( v, ndx ) => {
  508. return v + intersection.normal[ ndx ] * ( voxelId > 0 ? 0.5 : - 0.5 );
  509. } );
  510. world.setVoxel( ...pos, voxelId );
  511. updateVoxelGeometry( ...pos );
  512. requestRenderIfNotRequested();
  513. }
  514. }
  515. const mouse = {
  516. x: 0,
  517. y: 0,
  518. };
  519. function recordStartPosition( event ) {
  520. mouse.x = event.clientX;
  521. mouse.y = event.clientY;
  522. mouse.moveX = 0;
  523. mouse.moveY = 0;
  524. }
  525. function recordMovement( event ) {
  526. mouse.moveX += Math.abs( mouse.x - event.clientX );
  527. mouse.moveY += Math.abs( mouse.y - event.clientY );
  528. }
  529. function placeVoxelIfNoMovement( event ) {
  530. if ( mouse.moveX < 5 && mouse.moveY < 5 ) {
  531. placeVoxel( event );
  532. }
  533. window.removeEventListener( 'pointermove', recordMovement );
  534. window.removeEventListener( 'pointerup', placeVoxelIfNoMovement );
  535. }
  536. canvas.addEventListener( 'pointerdown', ( event ) => {
  537. event.preventDefault();
  538. recordStartPosition( event );
  539. window.addEventListener( 'pointermove', recordMovement );
  540. window.addEventListener( 'pointerup', placeVoxelIfNoMovement );
  541. }, { passive: false } );
  542. canvas.addEventListener( 'touchstart', ( event ) => {
  543. // prevent scrolling
  544. event.preventDefault();
  545. }, { passive: false } );
  546. controls.addEventListener( 'change', requestRenderIfNotRequested );
  547. window.addEventListener( 'resize', requestRenderIfNotRequested );
  548. }
  549. main();
  550. </script>
  551. </html>