More efficient way to create Circles

Hello,

I have been doing some stress testing on cesium to get a base line for the number of geometries I would be able to render at one time. When I try to draw circles I found that a HUGE amount of memory was being used to create just one circle (around 750KB). I was just curious if there was a more efficient way to create a circle or if this is something I will have to work around. Below is a snippet of my test code...

    var primitives16circ = new Cesium.PrimitiveCollection();
    var size = 1;
    var count=0;
    var appear = new Cesium.PerInstanceColorAppearance({
        closed: true
    })
    for (var lon=-180;lon<=180-size*2;lon=lon+size*5)
    {
      for (var lat=-90;lat<=90-size*2;lat=lat+size*5)
        {
        count++;
        var circleGeometry = new Cesium.CircleGeometry({
            center : Cesium.Cartesian3.fromDegrees(lon, lat),
            radius : 50000.0
        });
        var redCircleInstance = new Cesium.GeometryInstance({
            geometry : circleGeometry,
            attributes : {
                color : Cesium.ColorGeometryInstanceAttribute.fromColor(new Cesium.Color(1.0, 0.0, 0.0, 0.5))
            }
        });
        // Add the geometry instance to primitives.
        primitives16circ.add(new Cesium.Primitive({
            geometryInstances: [redCircleInstance],
            appearance: appear
        }));
        }
    }
primitives.add(primitives16circ);

I am currently using version b30 of Cesiumjs. I also tried changing the granularity and this did reduce the amount of memory required but I am still curious if there are other options I am over looking.

Thanks for your help!
Daniel

Hi Daniel,

Changing the granularity is one of the most effective things you can do, but there are also a few others:

  • Instead of creating a primitive for each geometry instance, build an array of geometry instances and give it to a single primitive. This will also improve rendering performance. Have you seen the tutorial?
  • If you don’t need 2D or Columbus view (2.5D) modes, create the viewer widget with “scene3DOnly: true”, which will save memory for each vertex in each circle.
  • If you don’t need to be able to pick on circles, create the primitive with “allowPicking : false”, which will also save memory per-vertex.

For example, copy and paste this into Sandcastle:

var viewer = new Cesium.Viewer(‘cesiumContainer’, {

scene3DOnly: true

});

var instances = ;

var size = 1;

var count=0;

for (var lon=-180;lon<=180-size2;lon=lon+size5)

{

for (var lat=-90;lat<=90-size2;lat=lat+size5)

{

count++;

var circleGeometry = new Cesium.CircleGeometry({

center : Cesium.Cartesian3.fromDegrees(lon, lat),

radius : 50000.0,

granularity : 5 * Cesium.Math.RADIANS_PER_DEGREE

});

var redCircleInstance = new Cesium.GeometryInstance({

geometry : circleGeometry,

attributes : {

color : Cesium.ColorGeometryInstanceAttribute.fromColor(new Cesium.Color(1.0, 0.0, 0.0, 0.5))

}

});

instances.push(redCircleInstance);

}

}

var appear = new Cesium.PerInstanceColorAppearance({

closed: true

});

viewer.scene.primitives.add(new Cesium.Primitive({

geometryInstances: instances,

appearance: appear,

allowPicking: false

}));

Patrick

I notice you did not use any entities in your example and are only using primitives. Does it make sense for this massive amount of geometries to not use entities and use primitives instead?

Entities use primitives under the hood and we go through a lot of pain to make sure they are used as close to optimally as possible. So the official recommendation would be to use Entities. If you were creating a tons of circles in one giant batch, runtime performance is going to be identical and the Entity API gives you a lot more features and flexibility. Entities use a little more memory, but in most cases it’s negligible and the trade off is worth it. The only reason to do something directly with Primitive that you can also do with Entity is if you have a specific issue or problem that you are trying to work around (at which point you should also tell us about it first so that we can look into it).

Also, b30 didn’t have the Entity API in it’s current form, so Primitives were really the only option.