threejs-cleanup-loaded-files.html 6.3 KB

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