cleanup-loaded-files.html 6.4 KB

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