Where scene object update viewer.dataSourceDisplay's primitiveCollection?

I’ve got how Entity transform to Primitive, which can be found in private Batch class:

Batch.prototype.update = function () {
  // ...
  if (this.createPrimitive) {
    const geometries = this.geometry.values;
    const geometriesLength = geometries.length;
    if (geometriesLength > 0) {
      primitive = new Primitive({
        /* ... */
      });
      primitives.add(primitive);
      isUpdated = false;
    }
  }
  // ...
}

And I know the rendering process of scene object render a frame. Scene obj need update primitiveCollection that hold by itself to create DrawCommand.

My question is, since that Batch create and push new Primitive for Entity into PrimitiveCollection that passed from DataSourceDisplay:

function DataSourceDisplay(/* ... */) {
  // ...
  const defaultDataSource = new CustomDataSource();
  this._onDataSourceAdded(undefined, defaultDataSource);

  // ...
}

DataSourceDisplay.prototype._onDataSourceAdded = function (
  dataSourceCollection,
  dataSource
) {
  // ...
  const displayPrimitives = this._primitives;
  // ...
  const primitives = displayPrimitives.add(new PrimitiveCollection());
  // ...
  dataSource._primitives = primitives; // here will set DataSourceDisplay's PrimitiveCollection to CustomDataSource who hold the Viewer's entityCollection
  // ...
}

where scene object will update this PrimitiveCollection (not scene’s default PrimitiveCollection)?

I search all source code but only found one case that Scene object update primitiveCollection:

// Scene.js
function updateAndRenderPrimitives(scene) {
  const frameState = scene._frameState;

  scene._groundPrimitives.update(frameState);
  scene._primitives.update(frameState); // here

  updateDebugFrustumPlanes(scene);
  updateShadowMaps(scene);

  if (scene._globe) {
    scene._globe.render(frameState);
  }
}

OK, I found it. Thanks!

// DataSourceDisplay.js -- constructor

function DataSourceDisplay(options) {
  // ...

  let primitivesAdded = false;
  const primitives = new PrimitiveCollection();
  const groundPrimitives = new PrimitiveCollection();

  if (dataSourceCollection.length > 0) {
    scene.primitives.add(primitives);
    scene.groundPrimitives.add(groundPrimitives);
    primitivesAdded = true;
  }

  // ...
}

When Viewer object create dataSourceDisplay, will also create a PrimitiveCollection for that display object, then immediately add the PrimitiveCollection to scene.primitives, so the default EntityCollection of Viewer can be render by render loop of CesiumWidget.

Hi @onsummer,

Glad that you were able to resolve this :grinning:

Thanks for sharing your solution with the community!

-Sam

1 Like