debug-js-params.html 4.9 KB

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