cleanup-simple.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 - Cleanup</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. #root {
  19. position: absolute;
  20. left: 0;
  21. top: 0;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <canvas id="c"></canvas>
  27. </body>
  28. <!-- Import maps polyfill -->
  29. <!-- Remove this when import maps will be widely supported -->
  30. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  31. <script type="importmap">
  32. {
  33. "imports": {
  34. "three": "../../build/three.module.js"
  35. }
  36. }
  37. </script>
  38. <script type="module">
  39. import * as THREE from 'three';
  40. class ResourceTracker {
  41. constructor() {
  42. this.resources = new Set();
  43. }
  44. track( resource ) {
  45. if ( resource.dispose || resource instanceof THREE.Object3D ) {
  46. this.resources.add( resource );
  47. }
  48. return resource;
  49. }
  50. untrack( resource ) {
  51. this.resources.delete( resource );
  52. }
  53. dispose() {
  54. for ( const resource of this.resources ) {
  55. if ( resource instanceof THREE.Object3D ) {
  56. if ( resource.parent ) {
  57. resource.parent.remove( resource );
  58. }
  59. }
  60. if ( resource.dispose ) {
  61. resource.dispose();
  62. }
  63. }
  64. this.resources.clear();
  65. }
  66. }
  67. function main() {
  68. const canvas = document.querySelector( '#c' );
  69. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
  70. renderer.useLegacyLights = false;
  71. const fov = 75;
  72. const aspect = 2; // the canvas default
  73. const near = 0.1;
  74. const far = 5;
  75. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  76. camera.position.z = 2;
  77. const scene = new THREE.Scene();
  78. const cubes = []; // an array we can use to rotate the cubes
  79. function addStuffToScene() {
  80. const resTracker = new ResourceTracker();
  81. const track = resTracker.track.bind( resTracker );
  82. const boxWidth = 1;
  83. const boxHeight = 1;
  84. const boxDepth = 1;
  85. const geometry = track( new THREE.BoxGeometry( boxWidth, boxHeight, boxDepth ) );
  86. const loader = new THREE.TextureLoader();
  87. const texture = loader.load( 'resources/images/wall.jpg' );
  88. texture.colorSpace = THREE.SRGBColorSpace;
  89. const material = track( new THREE.MeshBasicMaterial( {
  90. map: track( texture ),
  91. } ) );
  92. const cube = track( new THREE.Mesh( geometry, material ) );
  93. scene.add( cube );
  94. cubes.push( cube ); // add to our list of cubes to rotate
  95. return resTracker;
  96. }
  97. function waitSeconds( seconds = 0 ) {
  98. return new Promise( resolve => setTimeout( resolve, seconds * 1000 ) );
  99. }
  100. async function process() {
  101. for ( ;; ) {
  102. const resTracker = addStuffToScene();
  103. await waitSeconds( 2 );
  104. cubes.length = 0; // remove the cubes
  105. resTracker.dispose();
  106. await waitSeconds( 1 );
  107. }
  108. }
  109. process();
  110. function resizeRendererToDisplaySize( renderer ) {
  111. const canvas = renderer.domElement;
  112. const width = canvas.clientWidth;
  113. const height = canvas.clientHeight;
  114. const needResize = canvas.width !== width || canvas.height !== height;
  115. if ( needResize ) {
  116. renderer.setSize( width, height, false );
  117. }
  118. return needResize;
  119. }
  120. function render( time ) {
  121. time *= 0.001;
  122. if ( resizeRendererToDisplaySize( renderer ) ) {
  123. const canvas = renderer.domElement;
  124. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  125. camera.updateProjectionMatrix();
  126. }
  127. cubes.forEach( ( cube, ndx ) => {
  128. const speed = .2 + ndx * .1;
  129. const rot = time * speed;
  130. cube.rotation.x = rot;
  131. cube.rotation.y = rot;
  132. } );
  133. renderer.render( scene, camera );
  134. requestAnimationFrame( render );
  135. }
  136. requestAnimationFrame( render );
  137. }
  138. main();
  139. </script>
  140. </html>