cleanup-loaded-files.html 6.6 KB

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