如何处理3。js场景正常吗?(r55)

时间:2021-02-05 19:39:41

It seems like Three.js does not have a good way to dispose a THREE.Scene and all of the objects within that scene.

好像三个。js没有处理3的好方法。场景和场景中的所有对象。

Currently I am doing the following:

目前我正在做以下工作:

  $.each(scene.__objects, function(idx, obj) {                               
    scene.remove(obj);                                                                                     
    if (obj.geometry) {                                                                                    
      obj.geometry.dispose();                                                                              
    }                                                                                                      
    if (obj.material) {                                                                                    
      if (obj.material instanceof THREE.MeshFaceMaterial) {                 
        $.each(obj.material.materials, function(idx, obj) {                 
          obj.dispose();                                                                                   
        });                                                                                                
      } else {                                                                                             
        obj.material.dispose();                                                                            
      }                                                                                                    
    }                                                                                                      
    if (obj.dispose) {                                                                                     
      obj.dispose();                                                                                       
    }                                                                                                      
  });             

Looking at the Chrome Heap profiler, there are still many objects that do not get cleaned up (Textures, Shader Materials, Vectors, etc...).

看看Chrome堆分析器,仍然有许多对象没有被清理(纹理、材质、矢量等等)。

1 个解决方案

#1


4  

I agree with arriu that there should be a cleaner and generic way to dispose of memory in three.js, probably starting from the scene node and traversing all the way down. I also think that his generic function above should be extended in order to handle more types of memory allocation. Looking at the example webgl_test_memory.html it does something very specific to the example and frees up memory right after its allocation. Looking at webgl_test_memory2.html this example also does something very specific by adding meshes to an array and then going through and disposing the elements of the array. This method can not handle many memory allocations that have been made within the function calls. I am not saying that the two examples do not free up memory. I think that the scene node should have a method to free all the memory below it.

我同意arriu的观点,应该有一种更干净、更通用的方法来在三种方法中处理内存。js,可能从场景节点开始,一路向下。我还认为,为了处理更多类型的内存分配,应该扩展上面的通用函数。查看示例webgl_test_memory。它执行一些特定于示例的操作,并在分配之后释放内存。看着webgl_test_memory2。这个例子还做了一些非常具体的事情,向数组添加网格,然后遍历并处理数组的元素。这个方法不能处理函数调用中所做的许多内存分配。我并不是说这两个例子不能释放记忆。我认为场景节点应该有一个释放它下面所有内存的方法。

#1


4  

I agree with arriu that there should be a cleaner and generic way to dispose of memory in three.js, probably starting from the scene node and traversing all the way down. I also think that his generic function above should be extended in order to handle more types of memory allocation. Looking at the example webgl_test_memory.html it does something very specific to the example and frees up memory right after its allocation. Looking at webgl_test_memory2.html this example also does something very specific by adding meshes to an array and then going through and disposing the elements of the array. This method can not handle many memory allocations that have been made within the function calls. I am not saying that the two examples do not free up memory. I think that the scene node should have a method to free all the memory below it.

我同意arriu的观点,应该有一种更干净、更通用的方法来在三种方法中处理内存。js,可能从场景节点开始,一路向下。我还认为,为了处理更多类型的内存分配,应该扩展上面的通用函数。查看示例webgl_test_memory。它执行一些特定于示例的操作,并在分配之后释放内存。看着webgl_test_memory2。这个例子还做了一些非常具体的事情,向数组添加网格,然后遍历并处理数组的元素。这个方法不能处理函数调用中所做的许多内存分配。我并不是说这两个例子不能释放记忆。我认为场景节点应该有一个释放它下面所有内存的方法。