threejs-cleanup-loaded-files.html 6.3 KB

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