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

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