cleanup-loaded-files.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. renderer.outputColorSpace = THREE.SRGBColorSpace;
  106. const fov = 75;
  107. const aspect = 2; // the canvas default
  108. const near = 0.1;
  109. const far = 5;
  110. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  111. camera.position.z = 2;
  112. const scene = new THREE.Scene();
  113. scene.background = new THREE.Color('lightblue');
  114. function addLight(...pos) {
  115. const color = 0xFFFFFF;
  116. const intensity = 0.8;
  117. const light = new THREE.DirectionalLight(color, intensity);
  118. light.position.set(...pos);
  119. scene.add(light);
  120. }
  121. addLight(-1, 2, 4);
  122. addLight( 2, -2, 3);
  123. function frameArea(sizeToFitOnScreen, boxSize, boxCenter, camera) {
  124. const halfSizeToFitOnScreen = sizeToFitOnScreen * 0.5;
  125. const halfFovY = THREE.MathUtils.degToRad(camera.fov * .5);
  126. const distance = halfSizeToFitOnScreen / Math.tan(halfFovY);
  127. // compute a unit vector that points in the direction the camera is now
  128. // in the xz plane from the center of the box
  129. const direction = (new THREE.Vector3())
  130. .subVectors(camera.position, boxCenter)
  131. .multiply(new THREE.Vector3(1, 0, 1))
  132. .normalize();
  133. // move the camera to a position distance units way from the center
  134. // in whatever direction the camera was from the center already
  135. camera.position.copy(direction.multiplyScalar(distance).add(boxCenter));
  136. // pick some near and far values for the frustum that
  137. // will contain the box.
  138. camera.near = boxSize / 100;
  139. camera.far = boxSize * 100;
  140. camera.updateProjectionMatrix();
  141. // point the camera to look at the center of the box
  142. camera.lookAt(boxCenter.x, boxCenter.y, boxCenter.z);
  143. }
  144. const gltfLoader = new GLTFLoader();
  145. function loadGLTF(url) {
  146. return new Promise((resolve, reject) => {
  147. gltfLoader.load(url, resolve, undefined, reject);
  148. });
  149. }
  150. function waitSeconds(seconds = 0) {
  151. return new Promise(resolve => setTimeout(resolve, seconds * 1000));
  152. }
  153. const fileURLs = [
  154. 'resources/models/cartoon_lowpoly_small_city_free_pack/scene.gltf', /* threejs.org: url */
  155. 'resources/models/3dbustchallange_submission/scene.gltf', /* threejs.org: url */
  156. 'resources/models/mountain_landscape/scene.gltf', /* threejs.org: url */
  157. 'resources/models/simple_house_scene/scene.gltf', /* threejs.org: url */
  158. ];
  159. async function loadFiles() {
  160. for (;;) {
  161. for (const url of fileURLs) {
  162. const resMgr = new ResourceTracker();
  163. const track = resMgr.track.bind(resMgr);
  164. const gltf = await loadGLTF(url);
  165. const root = track(gltf.scene);
  166. scene.add(root);
  167. // compute the box that contains all the stuff
  168. // from root and below
  169. const box = new THREE.Box3().setFromObject(root);
  170. const boxSize = box.getSize(new THREE.Vector3()).length();
  171. const boxCenter = box.getCenter(new THREE.Vector3());
  172. // set the camera to frame the box
  173. frameArea(boxSize * 1.1, boxSize, boxCenter, camera);
  174. await waitSeconds(2);
  175. renderer.render(scene, camera);
  176. resMgr.dispose();
  177. await waitSeconds(1);
  178. }
  179. }
  180. }
  181. loadFiles();
  182. function resizeRendererToDisplaySize(renderer) {
  183. const canvas = renderer.domElement;
  184. const width = canvas.clientWidth;
  185. const height = canvas.clientHeight;
  186. const needResize = canvas.width !== width || canvas.height !== height;
  187. if (needResize) {
  188. renderer.setSize(width, height, false);
  189. }
  190. return needResize;
  191. }
  192. function render() {
  193. if (resizeRendererToDisplaySize(renderer)) {
  194. const canvas = renderer.domElement;
  195. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  196. camera.updateProjectionMatrix();
  197. }
  198. renderer.render(scene, camera);
  199. requestAnimationFrame(render);
  200. }
  201. requestAnimationFrame(render);
  202. }
  203. main();
  204. </script>
  205. </html>