cleanup-loaded-files.html 6.3 KB

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