cleanup-loaded-files.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 Loaded Files</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. <script type="importmap">
  29. {
  30. "imports": {
  31. "three": "../../build/three.module.js",
  32. "three/addons/": "../../examples/jsm/"
  33. }
  34. }
  35. </script>
  36. <script type="module">
  37. import * as THREE from 'three';
  38. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  39. class ResourceTracker {
  40. constructor() {
  41. this.resources = new Set();
  42. }
  43. track( resource ) {
  44. if ( ! resource ) {
  45. return resource;
  46. }
  47. // handle children and when material is an array of materials or
  48. // uniform is array of textures
  49. if ( Array.isArray( resource ) ) {
  50. resource.forEach( resource => this.track( resource ) );
  51. return resource;
  52. }
  53. if ( resource.dispose || resource instanceof THREE.Object3D ) {
  54. this.resources.add( resource );
  55. }
  56. if ( resource instanceof THREE.Object3D ) {
  57. this.track( resource.geometry );
  58. this.track( resource.material );
  59. this.track( resource.children );
  60. } else if ( resource instanceof THREE.Material ) {
  61. // We have to check if there are any textures on the material
  62. for ( const value of Object.values( resource ) ) {
  63. if ( value instanceof THREE.Texture ) {
  64. this.track( value );
  65. }
  66. }
  67. // We also have to check if any uniforms reference textures or arrays of textures
  68. if ( resource.uniforms ) {
  69. for ( const value of Object.values( resource.uniforms ) ) {
  70. if ( value ) {
  71. const uniformValue = value.value;
  72. if ( uniformValue instanceof THREE.Texture ||
  73. Array.isArray( uniformValue ) ) {
  74. this.track( uniformValue );
  75. }
  76. }
  77. }
  78. }
  79. }
  80. return resource;
  81. }
  82. untrack( resource ) {
  83. this.resources.delete( resource );
  84. }
  85. dispose() {
  86. for ( const resource of this.resources ) {
  87. if ( resource instanceof THREE.Object3D ) {
  88. if ( resource.parent ) {
  89. resource.parent.remove( resource );
  90. }
  91. }
  92. if ( resource.dispose ) {
  93. resource.dispose();
  94. }
  95. }
  96. this.resources.clear();
  97. }
  98. }
  99. function main() {
  100. const canvas = document.querySelector( '#c' );
  101. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
  102. const fov = 75;
  103. const aspect = 2; // the canvas default
  104. const near = 0.1;
  105. const far = 5;
  106. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  107. camera.position.z = 2;
  108. const scene = new THREE.Scene();
  109. scene.background = new THREE.Color( 'lightblue' );
  110. function addLight( ...pos ) {
  111. const color = 0xFFFFFF;
  112. const intensity = 2.5;
  113. const light = new THREE.DirectionalLight( color, intensity );
  114. light.position.set( ...pos );
  115. scene.add( light );
  116. }
  117. addLight( - 1, 2, 4 );
  118. addLight( 2, - 2, 3 );
  119. function frameArea( sizeToFitOnScreen, boxSize, boxCenter, camera ) {
  120. const halfSizeToFitOnScreen = sizeToFitOnScreen * 0.5;
  121. const halfFovY = THREE.MathUtils.degToRad( camera.fov * .5 );
  122. const distance = halfSizeToFitOnScreen / Math.tan( halfFovY );
  123. // compute a unit vector that points in the direction the camera is now
  124. // in the xz plane from the center of the box
  125. const direction = ( new THREE.Vector3() )
  126. .subVectors( camera.position, boxCenter )
  127. .multiply( new THREE.Vector3( 1, 0, 1 ) )
  128. .normalize();
  129. // move the camera to a position distance units way from the center
  130. // in whatever direction the camera was from the center already
  131. camera.position.copy( direction.multiplyScalar( distance ).add( boxCenter ) );
  132. // pick some near and far values for the frustum that
  133. // will contain the box.
  134. camera.near = boxSize / 100;
  135. camera.far = boxSize * 100;
  136. camera.updateProjectionMatrix();
  137. // point the camera to look at the center of the box
  138. camera.lookAt( boxCenter.x, boxCenter.y, boxCenter.z );
  139. }
  140. const gltfLoader = new GLTFLoader();
  141. function loadGLTF( url ) {
  142. return new Promise( ( resolve, reject ) => {
  143. gltfLoader.load( url, resolve, undefined, reject );
  144. } );
  145. }
  146. function waitSeconds( seconds = 0 ) {
  147. return new Promise( resolve => setTimeout( resolve, seconds * 1000 ) );
  148. }
  149. const fileURLs = [
  150. 'resources/models/cartoon_lowpoly_small_city_free_pack/scene.gltf', /* threejs.org: url */
  151. 'resources/models/3dbustchallange_submission/scene.gltf', /* threejs.org: url */
  152. 'resources/models/mountain_landscape/scene.gltf', /* threejs.org: url */
  153. 'resources/models/simple_house_scene/scene.gltf', /* threejs.org: url */
  154. ];
  155. async function loadFiles() {
  156. for ( ;; ) {
  157. for ( const url of fileURLs ) {
  158. const resMgr = new ResourceTracker();
  159. const track = resMgr.track.bind( resMgr );
  160. const gltf = await loadGLTF( url );
  161. const root = track( gltf.scene );
  162. scene.add( root );
  163. // compute the box that contains all the stuff
  164. // from root and below
  165. const box = new THREE.Box3().setFromObject( root );
  166. const boxSize = box.getSize( new THREE.Vector3() ).length();
  167. const boxCenter = box.getCenter( new THREE.Vector3() );
  168. // set the camera to frame the box
  169. frameArea( boxSize * 1.1, boxSize, boxCenter, camera );
  170. await waitSeconds( 2 );
  171. renderer.render( scene, camera );
  172. resMgr.dispose();
  173. await waitSeconds( 1 );
  174. }
  175. }
  176. }
  177. loadFiles();
  178. function resizeRendererToDisplaySize( renderer ) {
  179. const canvas = renderer.domElement;
  180. const width = canvas.clientWidth;
  181. const height = canvas.clientHeight;
  182. const needResize = canvas.width !== width || canvas.height !== height;
  183. if ( needResize ) {
  184. renderer.setSize( width, height, false );
  185. }
  186. return needResize;
  187. }
  188. function render() {
  189. if ( resizeRendererToDisplaySize( renderer ) ) {
  190. const canvas = renderer.domElement;
  191. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  192. camera.updateProjectionMatrix();
  193. }
  194. renderer.render( scene, camera );
  195. requestAnimationFrame( render );
  196. }
  197. requestAnimationFrame( render );
  198. }
  199. main();
  200. </script>
  201. </html>