How to determine when a model has been loaded when using the Entity API?


I can figure out how to determine a model has been loaded when using the Primitive API (using Model.readyPromise) but I can't figure out how to do the same thing when I load a model as part of an entity like such:

    var entity = viewer.entities.add({
        position : Cesium.Cartesian3.fromDegrees(-75, 39),
        model : {
            uri : 'SampleData/models/CesiumAir/Cesium_Air.gltf'

All I am trying to do is grab the bounding sphere after the model loads. I am using the function getModelForEntity(entity) here:

It doesn't work when the page loads, but if I wait a few seconds and execute the function call in the console I am fine.

What am I doing wrong? Also, if there is some private API so that I can access an entities underlying primitives, I would be fine with that too but I haven't been able to figure out what it is. Perhaps they are only correlated by having the same ID?


Hello Nick,

I think you can use viewer.dataSourceDisplay.getBoundingSphere to get the bounding sphere for the model. This function will return either Cesium.BoundingSphereState.DONE, Cesium.BoundingSphereState.PENDING or Cesium.BoundingSphereState.FAILED based on whether the entity is ready. Poll this function until Cesium.BoundingSphereState.DONE is returned. The result variable will be the bounding sphere for the entity. Here is the function:

Here is an example of how to use call the function:

var boundingSphere = new Cesium.BoundingSphere();
var state = viewer.dataSourceDisplay.getBoundingSphere(entity, false, boundingSphere);
if (state === Cesium.BoundingSphereState.DONE) {
//boundingSphere has been updated to match the bounding sphere of the entity