0

I have a a question that's been bothering me for some time.

I am using the three.js webgl library to render a large scene with many textures and meshes.

This question is not necessarily bound to webgl, but more javascript arrays and memory management.

I am basically doing this:

var modelArray = [];

var model = function(geometry,db_data){
  var tex   = THREE.ImageUtils.loadTexture('texture.jpg');
  var mat   = new THREE.MeshPhongMaterial({map:tex})
  this.mesh = new THREE.Mesh(geometry,mat);
  this.db   = db_data;
  scene.add(this.mesh);
};

function loadModels(model_array){
  for(i=0;i<geometry.length;i++){
    modelArray.push(new model(model_array[i]['geometry'],model_array[i]['db_info']));
  }
}

loadModels();

Am I being inefficient here? Am I essentially doubling up the amount of memory being used since I have the mesh loaded to the scene and an array. Or does the model (specifically the model.mesh) object in the array simply point to a singular memory block?

Should I just create an array of mesh ids and reference the scene objects, or is it ok to add the mesh to the scene and an array?

Thanks in advance and I hope I was clear enough.

4

1 回答 1

1

The main thing that jumps out at me is this:

var tex   = THREE.ImageUtils.loadTexture('texture.jpg');
var mat   = new THREE.MeshPhongMaterial({map:tex})

If you are loading the same texture every time you create a new model, that could create a lot of overhead (and it can also be pretty slow). I would load the texture(s) and corresponding material(s) you need outside of your loop once.

Your modelArray is a list of plain model objects, each of which has a pointer to the corresponding mesh object (and db object). The scene has a pointer to the same mesh object so you are not exploding your memory use by cloning meshes.

It's possible that your memory use is just because your mesh geometries take up a lot of memory. Try loading your models one by one while watching memory usage; perhaps you have one that is unexpectedly detailed.

于 2013-07-26T11:21:01.600 回答