Search code examples
performanceactionscript-3flashstage3daway3d

Away3D 4.1 performance issues - how to optimise a scene with many identical meshes?


I have created a simple test using Away3D 4.1 (2500 cubes) but performance is a lot lower than i expected - only 10 FPS.

I assume i am making a noob mistake (being a noob and all) so here are relevant pieces of code:

Lighting:

var light1:DirectionalLight = new DirectionalLight();
light1.position = new Vector3D(400, 300, -200);
light1.lookAt(new Vector3D());
light1.color = 0xFFFFFF;
light1.ambient = 0.25;
lightPicker = new StaticLightPicker([light1]);

Creating cubes:

var material:ColorMaterial = new ColorMaterial(0x999999);
material.lightPicker = lightPicker;
material.specular = 0;
var mesh:Mesh = new Mesh(new CubeGeometry(50, 50, 50), material);

for (var i:uint = 0; i < 50; i++)
{
    for (var j:uint = 0; j < 50; j++)
    {
        var cube:Mesh = Mesh(mesh.clone());
        cube.x = 100*(i-25);
        cube.y = 25;
        cube.z = 100*(j-25);
        scene.addChild(cube);
    }
}

And the camera:

camera = new Camera3D();
camera.position = new Vector3D(0, 1000, -5000);
camera.lookAt(new Vector3D(0, 0, 0));
camera.lens.far = 10000;

Stage3D output in Scout shows that there are many calls between each drawTriangles call and my basic understanding tells me that drawTriangle calls should be 'batched'.

I know that some other frameworks have batch methods but i havent been able to find anything related to Away3D.

Thanks in advance for any help.


Solution

  • Yeah, you should batch your draw calls. I don't have much experience with Away3D but after a quick look through their API reference it seems that away3d.tools.commands.Merge should help you to merge all those cubes into one large batched mesh.