debug-js-params.html 4.7 KB

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