Performance issues with a lot of cubes

Software: Away3D 4.x

Brendon, Newbie
Posted: 18 June 2012 09:22 PM   Total Posts: 4

I started playing around with Away3D because I like to dabble with different programming languages and SDK’s.  I ran into some performance issues when loading a LOT of cubes.  The cubes only take a few seconds to actually load.  The problem is when I pan the camera around to show all of the cubes at once, my frame rate drops to about 5.  My script is very basic, so I was wondering if there is a better way to implement something of this nature to keep my frame rates up.

Thanks in advance.

 

File Attachments
physicstest.zip  (File Size: 1601KB - Downloads: 178)
   

Avatar
Austen, Newbie
Posted: 19 June 2012 03:15 AM   Total Posts: 21   [ # 1 ]

did you create new geometries for each cube?
if you did, you should use the clone method to duplicate your meshes

i’m currently working on a small game displaying about 11k cubes and the framerate is really fine

   

Brendon, Newbie
Posted: 19 June 2012 03:52 AM   Total Posts: 4   [ # 2 ]

The portion of my code that generates the cubes is below.  I have a text file with all of the coordinates for each block.  Originally I loaded each as a new mesh.  Then I ran into vertex buffer issues and used clone instead.  But, that didn’t improve my FPS when panning over the bulk of the cubes.

var colors:Array = [];
    
colors[0] 0x252525;
    
colors[1] 0xff5555;
    
    var 
mesh:Mesh = new Mesh();
    
mesh.geometry = new CubeGeometry11);
    
    var 
_fileArray:Array = e.target.data.split(/\n/) ;
    for 
each ( var _fileObj:String in _fileArray )
    
{
     
var _fileLine:Array = _fileObj.split"," );
     var 
newMesh:Mesh mesh.clone() as Mesh;
     var 
material:ColorMaterial = new ColorMaterialcolors[ _fileLine[ 1 ] ] );
     
newMesh.material material;
     
newMesh._fileLine[2];
     
newMesh._fileLine[3];
     
newMesh._fileLine[4];
     
newMesh.rotationX _fileLine[5];
     
newMesh.rotationY _fileLine[6];
     
     
_scene.addChildnewMesh );
    
   

Avatar
Matse, Sr. Member
Posted: 19 June 2012 10:31 AM   Total Posts: 149   [ # 3 ]

Looks like you have a different material for each cube, depending on how many cubes you have that’s probably a *lot* of materials (and draw calls, which will kill performance).

It’s not a problem with Away3D here, you’re just asking Stage3D (and your graphic card) too much smile

If you don’t need every cube to have a different color, then you should just have one ColorMaterial per color, and re-use that material for every cube with that color.
Then to further improve performance, if the cubes are static (ie you don’t move or rotate them after initialisation) you should merge them into a single mesh per material. Have a look at this post, I provided some explanations and an helper class to do that http://away3d[dot]com/forum/viewthread/2414/ (replace “[dot]” with a dot)

If you really need each cube to have a different color then you should rethink your approach. A possible solution could be to have all your colors as single pixels on a bitmap : use that as a texture in a single TextureMaterial, use that TextureMaterial for all cubes, and just set the UV coords for each cube so that it displays only the pixel of the right color. Then again if thos cubes don’t move/rotate at runtime just merge them all : since they all share a single material you will most probably get a *huge* performance boost.

   

Brendon, Newbie
Posted: 19 June 2012 02:24 PM   Total Posts: 4   [ # 4 ]

Thanks for the info!!  I plan on the cubes being moved around during run-time, so I don’t think that the merging them is the solution.  I did however implement your single material per color method.  Unfortunately, this only helped about 1FPS.  Here’s the updated portion of code.  You had mentioned something about using a texture with a single pixel.  Would this have better performance gains than using ColorMaterial?  I had just assumed that ColorMaterial would be about as fast as it gets since it doesn’t have to parse any bitmap data.

var colors:Array = [];
    var 
grey:ColorMaterial = new ColorMaterial0x252525 );
    var 
pink:ColorMaterial = new ColorMaterial0xff5555 );
    
colors[0] grey;
    
colors[1] pink;
    
    var 
mesh:Mesh = new Mesh();
    
mesh.geometry = new CubeGeometry11);
    
    var 
_fileArray:Array = e.target.data.split(/\n/) ;
    for 
each ( var _fileObj:String in _fileArray )
    
{
     
var _fileLine:Array = _fileObj.split"," );
     var 
newMesh:Mesh mesh.clone() as Mesh;
     
newMesh.material colors[_fileLine[1]];
     
newMesh._fileLine[2];
     
newMesh._fileLine[3];
     
newMesh._fileLine[4];
     
newMesh.rotationX _fileLine[5];
     
newMesh.rotationY _fileLine[6];
     
     
_scene.addChildnewMesh );
    
}
    _file
.close(); 
   

Avatar
Matse, Sr. Member
Posted: 19 June 2012 02:58 PM   Total Posts: 149   [ # 5 ]

Yeah merging will merge all cubes sharing a material into a single mesh : you can still move/rotate that mesh of course, but can’t access the cubes individually anymore.

Ok so you’re sharing the geometry and sharing materials too, and you still get very poor performance. This suggest you must have quite a lot of cubes in there, I guess hundreds or more ?

My suggestion about using a texture with one color per pixel won’t help much here : it would reduce the amount of materials to 1, but you only have 2 (I didn’t know how many you had, I was expecting more) so not worth the trouble.

It’s not really rendering the cubes which eats your performance, it’s iterating through all of them : if it was a single mesh (or rather 2, one per color) performance would most probably fly at 60 FPS without any hit.

Maybe you could describe what you’re trying to achieve, as there might be other/better way(s) ?

From what I see here, unless you’re running on software mode, you’re pretty much “doomed” with this way of doing things smile

The only path I can think of right now would be to make it (again) a single mesh and manipulate the mesh’s geometry directly but I haven’t explored that path yet so can’t help much with how to do it. Maybe you could get some inspiration by looking a Liaocheng’s particle engine, as I think that’s how he does it (using a single mesh, each particle being 2 faces in that mesh) https://github.com/away3d/away3d-core-fp11/tree/particlesfeature

Make sure that your app is not running on software fo some reason first though wink

   

Brendon, Newbie
Posted: 19 June 2012 04:13 PM   Total Posts: 4   [ # 6 ]

Thanks for all of the advice, Matse.  I’ll keep poking around at it and if I find a good alternative, I’ll definitely post the result in case there is anyone else looking for the same answer.

   

Richard Olsson, Administrator
Posted: 20 June 2012 06:14 PM   Total Posts: 1192   [ # 7 ]

How many cubes are we talking about here? Are they all moving every frame?

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X