threejs-debug-js-params.html 4.6 KB

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