1
0

cleanup.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <!DOCTYPE html><html lang="zh"><head>
  2. <meta charset="utf-8">
  3. <title>清除</title>
  4. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  5. <meta name="twitter:card" content="summary_large_image">
  6. <meta name="twitter:site" content="@threejs">
  7. <meta name="twitter:title" content="Three.js – Cleanup">
  8. <meta property="og:image" content="https://threejs.org/files/share.png">
  9. <link rel="shortcut icon" href="../../files/favicon_white.ico" media="(prefers-color-scheme: dark)">
  10. <link rel="shortcut icon" href="../../files/favicon.ico" media="(prefers-color-scheme: light)">
  11. <link rel="stylesheet" href="../resources/lesson.css">
  12. <link rel="stylesheet" href="../resources/lang.css">
  13. <!-- Import maps polyfill -->
  14. <!-- Remove this when import maps will be widely supported -->
  15. <script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../../build/three.module.js"
  20. }
  21. }
  22. </script>
  23. </head>
  24. <body>
  25. <div class="container">
  26. <div class="lesson-title">
  27. <h1>清除</h1>
  28. </div>
  29. <div class="lesson">
  30. <div class="lesson-main">
  31. <p>Three.js应用经常使用大量的内存。一个3D模型的所有节点,可能占用1-20M内存。
  32. 一个模型可能会使用很多纹理,即使它们被压缩成了图片文件,也必须被展开成为未压缩的形态来使用。每个 1024 x 1024 大小的纹理会占用4-5M内存。</p>
  33. <p>大多数的three.js应用在初始化的时候加载资源,并且一直使用这些资源直到页面关闭。但是,如果你想随时间的变动加载和改变资源怎么办呢?</p>
  34. <p>不像大多数的JavaScript库,three.js不能自动的清除这些资源。
  35. 如果你切换页面,浏览器会清除这些资源,其它时候如何管理它们取决于你。这是WebGL设计的问题,three.js没有追索权只能将释放资源的责任托付给你。</p>
  36. <p>通过在<a href="textures.html">纹理</a>、
  37. <a href="primitives.html">图元</a>和
  38. <a href="materials.html">材质</a>对象上调用<code class="notranslate" translate="no">dispose</code>方法来释放资源
  39. </p>
  40. <p>你可以手动来处理。起初,你可能创建了一些资源。</p>
  41. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const boxGeometry = new THREE.BoxGeometry(...);
  42. const boxTexture = textureLoader.load(...);
  43. const boxMaterial = new THREE.MeshPhongMaterial({map: texture});
  44. </pre>
  45. <p>然后,当你处理完了它们,可以释放它们</p>
  46. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">boxGeometry.dispose();
  47. boxTexture.dispose();
  48. boxMaterial.dispose();
  49. </pre>
  50. <p>随着你使用越来越多的资源,这将会变得越来越乏味。</p>
  51. <p>为了减少一些乏味的工作,让我们创建一个类来跟踪这些资源。我们会请求这个类来帮我们做清除的工作。
  52. </p>
  53. <p>这个类一开始是这个样子。</p>
  54. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">class ResourceTracker {
  55. constructor() {
  56. this.resources = new Set();
  57. }
  58. track(resource) {
  59. if (resource.dispose) {
  60. this.resources.add(resource);
  61. }
  62. return resource;
  63. }
  64. untrack(resource) {
  65. this.resources.delete(resource);
  66. }
  67. dispose() {
  68. for (const resource of this.resources) {
  69. resource.dispose();
  70. }
  71. this.resources.clear();
  72. }
  73. }
  74. </pre>
  75. <p>让我们在<a href="textures.html">纹理文章</a>中的例子中使用这个类。我们可以创建一个这个类的实例。</p>
  76. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const resTracker = new ResourceTracker();
  77. </pre>
  78. <p>然后为了让这个类的使用更加地简单,让我们来为<code class="notranslate" translate="no">track</code>方法创建一个绑定函数。</p>
  79. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const resTracker = new ResourceTracker();
  80. +const track = resTracker.track.bind(resTracker);
  81. </pre>
  82. <p>现在,我们只需要在我们创建的每个图元、纹理、材质对象上调用<code class="notranslate" translate="no">track</code>方法就可以使用它。</p>
  83. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const boxWidth = 1;
  84. const boxHeight = 1;
  85. const boxDepth = 1;
  86. -const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  87. +const geometry = track(new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth));
  88. const cubes = []; // an array we can use to rotate the cubes
  89. const loader = new THREE.TextureLoader();
  90. -const material = new THREE.MeshBasicMaterial({
  91. - map: loader.load('resources/images/wall.jpg'),
  92. -});
  93. +const material = track(new THREE.MeshBasicMaterial({
  94. + map: track(loader.load('resources/images/wall.jpg')),
  95. +}));
  96. const cube = new THREE.Mesh(geometry, material);
  97. scene.add(cube);
  98. cubes.push(cube); // add to our list of cubes to rotate
  99. </pre>
  100. <p>然后,我们从场景中移除这些立方体,再然后调用<code class="notranslate" translate="no">resTracker.dispose</code>来释放它们。</p>
  101. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">for (const cube of cubes) {
  102. scene.remove(cube);
  103. }
  104. cubes.length = 0; // clears the cubes array
  105. resTracker.dispose();
  106. </pre>
  107. <p>这起作用了,但是我发现必须从场景中移除立方体有些乏味。让我们给<code class="notranslate" translate="no">ResourceTracker</code>增加这个功能。</p>
  108. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">class ResourceTracker {
  109. constructor() {
  110. this.resources = new Set();
  111. }
  112. track(resource) {
  113. - if (resource.dispose) {
  114. + if (resource.dispose || resource instanceof THREE.Object3D) {
  115. this.resources.add(resource);
  116. }
  117. return resource;
  118. }
  119. untrack(resource) {
  120. this.resources.delete(resource);
  121. }
  122. dispose() {
  123. for (const resource of this.resources) {
  124. - resource.dispose();
  125. + if (resource instanceof THREE.Object3D) {
  126. + if (resource.parent) {
  127. + resource.parent.remove(resource);
  128. + }
  129. + }
  130. + if (resource.dispose) {
  131. + resource.dispose();
  132. + }
  133. + }
  134. this.resources.clear();
  135. }
  136. }
  137. </pre>
  138. <p>现在我们可以跟踪这些立方体了</p>
  139. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const material = track(new THREE.MeshBasicMaterial({
  140. map: track(loader.load('resources/images/wall.jpg')),
  141. }));
  142. const cube = track(new THREE.Mesh(geometry, material));
  143. scene.add(cube);
  144. cubes.push(cube); // add to our list of cubes to rotate
  145. </pre>
  146. <p>我们不再需要编码从场景中移除这些立方体了。</p>
  147. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-for (const cube of cubes) {
  148. - scene.remove(cube);
  149. -}
  150. cubes.length = 0; // clears the cube array
  151. resTracker.dispose();
  152. </pre>
  153. <p>让我们来调整一下代码,这样我们可以重新添加立方体、纹理和材质。</p>
  154. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const scene = new THREE.Scene();
  155. *const cubes = []; // just an array we can use to rotate the cubes
  156. +function addStuffToScene() {
  157. const resTracker = new ResourceTracker();
  158. const track = resTracker.track.bind(resTracker);
  159. const boxWidth = 1;
  160. const boxHeight = 1;
  161. const boxDepth = 1;
  162. const geometry = track(new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth));
  163. const loader = new THREE.TextureLoader();
  164. const material = track(new THREE.MeshBasicMaterial({
  165. map: track(loader.load('resources/images/wall.jpg')),
  166. }));
  167. const cube = track(new THREE.Mesh(geometry, material));
  168. scene.add(cube);
  169. cubes.push(cube); // add to our list of cubes to rotate
  170. + return resTracker;
  171. +}
  172. </pre>
  173. <p>让我们来编写一些随着时间变动添加和移除物体的代码。</p>
  174. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function waitSeconds(seconds = 0) {
  175. return new Promise(resolve =&gt; setTimeout(resolve, seconds * 1000));
  176. }
  177. async function process() {
  178. for (;;) {
  179. const resTracker = addStuffToScene();
  180. await wait(2);
  181. cubes.length = 0; // remove the cubes
  182. resTracker.dispose();
  183. await wait(1);
  184. }
  185. }
  186. process();
  187. </pre>
  188. <p>代码将会创建立方体、纹理和材质,等待2秒,然后释放它们,然后等待1秒,重复这个过程。</p>
  189. <p></p><div translate="no" class="threejs_example_container notranslate">
  190. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/cleanup-simple.html"></iframe></div>
  191. <a class="threejs_center" href="/manual/examples/cleanup-simple.html" target="_blank">点击此处打开独立窗口</a>
  192. </div>
  193. <p></p>
  194. <p>这好像能工作了。</p>
  195. <p>对于加载文件来说,还需要一点额外的工作。大多数的加载器仅仅返回一个<a href="/docs/#api/en/core/Object3D"><code class="notranslate" translate="no">Object3D</code></a>
  196. 对象,作为它加载的层次对象的根节点,因此我们需要去发现所有的这些资源是哪些。</p>
  197. <p>让我们更新<code class="notranslate" translate="no">ResourceTracker</code>来试着去实现它。</p>
  198. <p>首先,我们来检测这个物体是否是一个<a href="/docs/#api/en/core/Object3D"><code class="notranslate" translate="no">Object3D</code></a>,然后跟踪它的图元、材质和子对象。</p>
  199. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">class ResourceTracker {
  200. constructor() {
  201. this.resources = new Set();
  202. }
  203. track(resource) {
  204. if (resource.dispose || resource instanceof THREE.Object3D) {
  205. this.resources.add(resource);
  206. }
  207. + if (resource instanceof THREE.Object3D) {
  208. + this.track(resource.geometry);
  209. + this.track(resource.material);
  210. + this.track(resource.children);
  211. + }
  212. return resource;
  213. }
  214. ...
  215. }
  216. </pre>
  217. <p>现在,因为任意的<code class="notranslate" translate="no">resource.geometry</code>、<code class="notranslate" translate="no">resource.material</code>和<code class="notranslate" translate="no">resource.children</code>有可能为null或undefined,
  218. 我们将在<code class="notranslate" translate="no">track</code>的入口执行检查。</p>
  219. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">class ResourceTracker {
  220. constructor() {
  221. this.resources = new Set();
  222. }
  223. track(resource) {
  224. + if (!resource) {
  225. + return resource;
  226. + }
  227. if (resource.dispose || resource instanceof THREE.Object3D) {
  228. this.resources.add(resource);
  229. }
  230. if (resource instanceof THREE.Object3D) {
  231. this.track(resource.geometry);
  232. this.track(resource.material);
  233. this.track(resource.children);
  234. }
  235. return resource;
  236. }
  237. ...
  238. }
  239. </pre>
  240. <p>同时,因为<code class="notranslate" translate="no">resource.children</code>是一个数组,
  241. 同时<code class="notranslate" translate="no">resource.material</code>也可能是数组,让我们对数组做检测。
  242. </p>
  243. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">class ResourceTracker {
  244. constructor() {
  245. this.resources = new Set();
  246. }
  247. track(resource) {
  248. if (!resource) {
  249. return resource;
  250. }
  251. + // handle children and when material is an array of materials.
  252. + if (Array.isArray(resource)) {
  253. + resource.forEach(resource =&gt; this.track(resource));
  254. + return resource;
  255. + }
  256. if (resource.dispose || resource instanceof THREE.Object3D) {
  257. this.resources.add(resource);
  258. }
  259. if (resource instanceof THREE.Object3D) {
  260. this.track(resource.geometry);
  261. this.track(resource.material);
  262. this.track(resource.children);
  263. }
  264. return resource;
  265. }
  266. ...
  267. }
  268. </pre>
  269. <p>最后我们需要遍历这些材质的属性和uniforms来寻找纹理。</p>
  270. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">class ResourceTracker {
  271. constructor() {
  272. this.resources = new Set();
  273. }
  274. track(resource) {
  275. if (!resource) {
  276. return resource;
  277. }
  278. * // handle children and when material is an array of materials or
  279. * // uniform is array of textures
  280. if (Array.isArray(resource)) {
  281. resource.forEach(resource =&gt; this.track(resource));
  282. return resource;
  283. }
  284. if (resource.dispose || resource instanceof THREE.Object3D) {
  285. this.resources.add(resource);
  286. }
  287. if (resource instanceof THREE.Object3D) {
  288. this.track(resource.geometry);
  289. this.track(resource.material);
  290. this.track(resource.children);
  291. - }
  292. + } else if (resource instanceof THREE.Material) {
  293. + // We have to check if there are any textures on the material
  294. + for (const value of Object.values(resource)) {
  295. + if (value instanceof THREE.Texture) {
  296. + this.track(value);
  297. + }
  298. + }
  299. + // We also have to check if any uniforms reference textures or arrays of textures
  300. + if (resource.uniforms) {
  301. + for (const value of Object.values(resource.uniforms)) {
  302. + if (value) {
  303. + const uniformValue = value.value;
  304. + if (uniformValue instanceof THREE.Texture ||
  305. + Array.isArray(uniformValue)) {
  306. + this.track(uniformValue);
  307. + }
  308. + }
  309. + }
  310. + }
  311. + }
  312. return resource;
  313. }
  314. ...
  315. }
  316. </pre>
  317. <p>让我们来使用<a href="load-gltf.html">“加载gltf文件文章“</a>中的例子,让它能够加载和释放文件。</p>
  318. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gltfLoader = new GLTFLoader();
  319. function loadGLTF(url) {
  320. return new Promise((resolve, reject) =&gt; {
  321. gltfLoader.load(url, resolve, undefined, reject);
  322. });
  323. }
  324. function waitSeconds(seconds = 0) {
  325. return new Promise(resolve =&gt; setTimeout(resolve, seconds * 1000));
  326. }
  327. const fileURLs = [
  328. 'resources/models/cartoon_lowpoly_small_city_free_pack/scene.gltf',
  329. 'resources/models/3dbustchallange_submission/scene.gltf',
  330. 'resources/models/mountain_landscape/scene.gltf',
  331. 'resources/models/simple_house_scene/scene.gltf',
  332. ];
  333. async function loadFiles() {
  334. for (;;) {
  335. for (const url of fileURLs) {
  336. const resMgr = new ResourceTracker();
  337. const track = resMgr.track.bind(resMgr);
  338. const gltf = await loadGLTF(url);
  339. const root = track(gltf.scene);
  340. scene.add(root);
  341. // compute the box that contains all the stuff
  342. // from root and below
  343. const box = new THREE.Box3().setFromObject(root);
  344. const boxSize = box.getSize(new THREE.Vector3()).length();
  345. const boxCenter = box.getCenter(new THREE.Vector3());
  346. // set the camera to frame the box
  347. frameArea(boxSize * 1.1, boxSize, boxCenter, camera);
  348. await waitSeconds(2);
  349. renderer.render(scene, camera);
  350. resMgr.dispose();
  351. await waitSeconds(1);
  352. }
  353. }
  354. }
  355. loadFiles();
  356. </pre>
  357. <p>然后我们得到了</p>
  358. <p></p><div translate="no" class="threejs_example_container notranslate">
  359. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/cleanup-loaded-files.html"></iframe></div>
  360. <a class="threejs_center" href="/manual/examples/cleanup-loaded-files.html" target="_blank">点击此处在独立窗口中打开</a>
  361. </div>
  362. <p></p>
  363. <p>关于代码的一些注释。</p>
  364. <p>如果我们想要加载2个或者更多的文件,并且想要随时地释放它们,我们将要每个文件使用一个<code class="notranslate" translate="no">ResourceTracker</code>。</p>
  365. <p>在上面中,我们只是在场景加载之后跟踪了<code class="notranslate" translate="no">gltf.scene</code>。
  366. 根据<code class="notranslate" translate="no">ResourceTracker</code>的当前实现版本,它会跟踪刚刚加载的所有资源。如果我们向场景中添加了更多的资源,我们需要决定是否要跟踪它们。
  367. </p>
  368. <p>举例来说,在我们加载了一个角色之后,我们把一个工具放入它的手中,这是通过把工具成为手的子对象来实现的。因此工具将不会被释放。我猜更多的时候,这不是我们想要的。</p>
  369. <p>这带来了一个问题。当我起初写上面的<code class="notranslate" translate="no">ResourceTracker</code>的时候,
  370. 我是在<code class="notranslate" translate="no">dispose</code>方法中遍历所有对象而不是在
  371. <code class="notranslate" translate="no">track</code>方法中。稍后,我就想到了上面的成为手的子对象的工具的这个例子,
  372. 在<code class="notranslate" translate="no">track</code>方法中确切地跟踪哪些对象需要被释放更加地灵活,按理来说也更加地准确,因为我们可以跟踪从文件中加载了什么,而不是稍后从资源图中释放状态。
  373. </p>
  374. <p>诚实地说,我对ResourceTracker并不是100%满意。在3D引擎中,做这样的事情并不是很常见。我们不应该去猜测什么资源被加载了,我们应该知道。
  375. 如果three.js能做出改变,所有的文件加载器返回能够引用所有加载资源的标准对象,就太好了。至少在这个时候,three.js在场景加载的时候并没有提供给我们其它的信息。因此,这个方法是有效的。</p>
  376. <p>我希望这个例子能给你带来帮助或者至少成为在three.js中如何释放资源的一份好的参考</p>
  377. </div>
  378. </div>
  379. </div>
  380. <script src="../resources/prettify.js"></script>
  381. <script src="../resources/lesson.js"></script>
  382. </body></html>