debug-js-params.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 - Debug - Params</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. #debug {
  19. position: absolute;
  20. left: 1em;
  21. top: 1em;
  22. padding: 1em;
  23. background: rgba(0, 0, 0, 0.9);
  24. color: white;
  25. font-family: monospace;
  26. pointer-events: none;
  27. }
  28. #info {
  29. position: absolute;
  30. right: 1em;
  31. bottom: 1em;
  32. padding: 1em;
  33. background: rgba(0, 0, 0, 0.9);
  34. color: white;
  35. font-family: monospace;
  36. pointer-events: none;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <canvas id="c"></canvas>
  42. <div id="debug" style="display: none;">
  43. <pre></pre>
  44. </div>
  45. <div id="info">click to launch</div>
  46. </body>
  47. <script type="module">
  48. import * as THREE from '../../build/three.module.js';
  49. /**
  50. * Returns the query parameters as a key/value object.
  51. * Example: If the query parameters are
  52. *
  53. * abc=123&def=456&name=gman
  54. *
  55. * Then `getQuery()` will return an object like
  56. *
  57. * {
  58. * abc: '123',
  59. * def: '456',
  60. * name: 'gman',
  61. * }
  62. */
  63. function getQuery() {
  64. const params = {};
  65. const q = (window.location.search || '').substring(1);
  66. q.split('&').forEach((pair) => {
  67. const keyValue = pair.split('=').map(decodeURIComponent);
  68. params[keyValue[0]] = keyValue[1];
  69. });
  70. return params;
  71. }
  72. class DummyLogger {
  73. log() {}
  74. render() {}
  75. }
  76. class ClearingLogger {
  77. constructor(elem) {
  78. this.elem = elem;
  79. this.lines = [];
  80. }
  81. log(...args) {
  82. this.lines.push([...args].join(' '));
  83. }
  84. render() {
  85. this.elem.textContent = this.lines.join('\n');
  86. this.lines = [];
  87. }
  88. }
  89. const query = getQuery();
  90. const debug = query.debug === 'true';
  91. const logger = debug
  92. ? new ClearingLogger(document.querySelector('#debug pre'))
  93. : new DummyLogger();
  94. if (debug) {
  95. document.querySelector('#debug').style.display = '';
  96. }
  97. function main() {
  98. const canvas = document.querySelector('#c');
  99. const renderer = new THREE.WebGLRenderer({canvas});
  100. const fov = 75;
  101. const aspect = 2; // the canvas default
  102. const near = 0.1;
  103. const far = 50;
  104. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  105. camera.position.z = 20;
  106. const scene = new THREE.Scene();
  107. scene.background = new THREE.Color('cyan');
  108. const geometry = new THREE.SphereGeometry();
  109. const material = new THREE.MeshBasicMaterial({color: 'red'});
  110. const things = [];
  111. function rand(min, max) {
  112. if (max === undefined) {
  113. max = min;
  114. min = 0;
  115. }
  116. return Math.random() * (max - min) + min;
  117. }
  118. function createThing() {
  119. const mesh = new THREE.Mesh(geometry, material);
  120. scene.add(mesh);
  121. things.push({
  122. mesh,
  123. timer: 2,
  124. velocity: new THREE.Vector3(rand(-5, 5), rand(-5, 5), rand(-5, 5)),
  125. });
  126. }
  127. canvas.addEventListener('click', createThing);
  128. function resizeRendererToDisplaySize(renderer) {
  129. const canvas = renderer.domElement;
  130. const width = canvas.clientWidth;
  131. const height = canvas.clientHeight;
  132. const needResize = canvas.width !== width || canvas.height !== height;
  133. if (needResize) {
  134. renderer.setSize(width, height, false);
  135. }
  136. return needResize;
  137. }
  138. let then = 0;
  139. function render(now) {
  140. now *= 0.001; // convert to seconds
  141. const deltaTime = now - then;
  142. then = now;
  143. if (resizeRendererToDisplaySize(renderer)) {
  144. const canvas = renderer.domElement;
  145. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  146. camera.updateProjectionMatrix();
  147. }
  148. if (things.length === 0) {
  149. createThing();
  150. }
  151. logger.log('fps:', (1 / deltaTime).toFixed(1));
  152. logger.log('num things:', things.length);
  153. for (let i = 0; i < things.length;) {
  154. const thing = things[i];
  155. const mesh = thing.mesh;
  156. const pos = mesh.position;
  157. logger.log(
  158. 'timer:', thing.timer.toFixed(3),
  159. 'pos:', pos.x.toFixed(3), pos.y.toFixed(3), pos.z.toFixed(3));
  160. thing.timer -= deltaTime;
  161. if (thing.timer <= 0) {
  162. // remove this thing. Note we don't advance `i`
  163. things.splice(i, 1);
  164. scene.remove(mesh);
  165. } else {
  166. mesh.position.addScaledVector(thing.velocity, deltaTime);
  167. ++i;
  168. }
  169. }
  170. renderer.render(scene, camera);
  171. logger.render();
  172. requestAnimationFrame(render);
  173. }
  174. requestAnimationFrame(render);
  175. }
  176. main();
  177. </script>
  178. </html>