cleanup-loaded-files.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. renderer.outputEncoding = THREE.sRGBEncoding;
  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 = 0.8;
  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>