threejs-debug-js-params.html 4.6 KB

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