Hello there,
I am wondering if it is possible to get the type of a primitive from the scene? For example, I am able to get the collection of all primitives in the scene, and I know that a billboardCollection is a type of primitive, as the following code works fine.
scene.getPrimitives().add(billboards);
(where billboards is a billboardCollection).
How can I know if a primitive is a billboardCollection, or any other type of primitive?
Here is a code snippet that shows how I'd like to do the check.
//Get all of the primitives in the scene (this includes billboardCollection(s)).
var primitives = scene.getPrimitives();
//Loop for each primitive in the scene.
for(var i = 0; i < primitives.getLength(); i++)
{
//Check if the primitive is a billboardCollection.
//How can I get the type of the primitive? I'm looking for something like
//Type = billboardCollection.
if(primitives.get(i).type == "billboardCollection")
{
//Do stuff if it is a billboardCollection.
}
}
Determining the type of an object in JavaScript is sometimes difficult, due to how loose the type system can be. In this case, though, you could try comparing the constructor property of the primitive against BillboardCollection:
if (primitives.get(i).constructor === Cesium.BillboardCollection) {
Scott, is the constructor notation preferred over
if (primitives.get(i) instanceof Cesium.BillboardCollection)
{
If so, why?
Either will work. instanceof will walk up the prototype chain, whereas an object has only one constructor. So, for example:
var billboardCollection = new BillboardCollection();
billboardCollection instanceof BillboardCollection;
// ==> true
billboardCollection instanceof Object;
// ==> true
billboardCollection.constructor === BillboardCollection;
// ==> true
billboardCollection.constructor === Object;
// ==> false
In Cesium, we don’t use the prototype chain for inheritance, so for our types, they’re mostly equivalent. When I wrote my earlier email, I forgot about the instanceof operator.
Ahh I see. Fantastic, this is exactly what I was looking for.
Thanks a lot for the response! and for the discussion comparing the constructor property vs the instanceof operator. Things are much more clear now.