Raising instance to top

Hi!
Is there a way to raise polygon intance to top?
What i mean is a function like primitives.raiseToTop () that gets a geometry instance/instance id and raises it to top.
Also, can i get the geometry instance by its id?

If you break the geometries into separate primitives, you can use primitives.raiseToTop(), but it is not possible to reorder geometries inside a primitive.

If you need to get a geometry instance from an id, keep a separate object that maps the id to the geometry instance (or maybe even make the id the geometry instance). For efficiency Cesium does not store the instance. From the tutorial:

“Using id, instead of the reference to the instance itself, allows the primitive - and our application - to avoid keeping a reference to the full instance, including its reference to the geometry, in memory after the primitive is constructed. Since a geometry can contain several large typed arrays, this allows us to save a significant amount of memory.”

Patrick

Can you give an example for your proposed solution?
Let’s say I do this pseudo code which creates 2 serieses of polygons and adds them to a primitive:

CreatePolygons() and put them in array polygons;

primitves.add(polygons);

CreatePolygons() and put them in array polygons;

primitves.add(polygons);

Now that the polygons are inside the primitive collection, how do I go about ordering them? Using “raise” and “lower” or “raisetotop” or “lowertobottom” seem to have no effect I can see.

Thanks

Here’s a sample code to make it clearer:

var myCesium = new Cesium.Viewer(elm.attr('id'),{
 imageryProvider : new Cesium.ArcGisMapServerImageryProvider({
 url : 'http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer'
 }),
 baseLayerPicker : false
});

var pins = {};

pins.pinBuilder = new Cesium.PinBuilder();
pins.billboardCollection = new Cesium.BillboardCollection();
myCesium.scene.primitives.add(scope.cesium.billboardCollection);

function createPolygon(x, y, color){
 // Create the polygon geometry.
 var index = 0;
 var primitiveIndex = scope.cesium.viewer.scene.primitives._primitives.length;
 
 var positions = Cesium.Cartesian3.fromDegreesArray([
 x[1], y[1], x[1], y[0],x[0], y[0],x[0], y[1]
 ]);

 //Create a blank, solid colored pin.
 billboardCollection.add({
 image : scope.cesium.pinBuilder.fromColor(Cesium.Color.ROYALBLUE, 24),
 verticalOrigin : Cesium.VerticalOrigin.BOTTOM,
 position : Cesium.Cartesian3.fromDegrees((x[1]+x[0])/2, (y[1]+y[0])/2, 0)
 });
 
 if (angular.isUndefined(color))
 color = Cesium.Color.fromRandom({alpha : 1.0});
 // Create a geometry instance using the polygon geometry.
 var polygonInstance = new Cesium.GeometryInstance({
 geometry : Cesium.PolygonGeometry.fromPositions({
 positions : positions,
 vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT
 }),
 attributes : {
 color : Cesium.ColorGeometryInstanceAttribute.fromColor(color)
 },
 id: index
 });

 return polygonInstance;
}

function insertPolygons(polygons){
 // inserts a polygon to the map
 if (polygons.length){
 myCesium.scene.primitives.add(new Cesium.Primitive({
 geometryInstances: polygons,
 appearance: new Cesium.PerInstanceColorAppearance({
 closed : true,
 translucent : false
 })
 }));
 }
}

function simulatePolygons(data){
 // this function constantly adds polygons (or updates existing)
 // some/time => update/ms
 var some = 10; //updates
 var time = 1000; //ms
 var index = 0;

 function addSome(){
 var draw;
 var newPolygons = [];
 
 var color = Cesium.Color.fromRandom({alpha : 1.0});
 for (var i=0; i<some; i++){
 newPolygons.push(createPolygon(data[index++].x,data[index].y,color));
 }
 insertPolygons(newPolygons);
 
 }
 setInterval(addSome, time, 10);
}

simulatePolygons(getDataFromServer());

So now (if I didn’t ruin it during the simplification) I have a primitive collection of primitive collections that inside I have polygons. Can you please direct me as to how to use the raise/lower commands in order to control which primitive collection will be on top?