Best way to add new entities every second

Hi,

I’ve been trying to add some geometries in real time to a Cesium map. The geometries are either circles in the ground or a ring/donut shape. Since Cesium doesn’t provide a ring primitive, I have tried it using the following approaches:

  1. Calculating the GeoJSON features using turfjs and adding the dataSource to the map.
    Problem: the loading of the initial features is quick, but every time I add a new feature to the source the map freezes for a split second.

  2. Adding the shape as entities using PolygonHierarchy with circle positions and an inner circle hole.
    Problem: Since drawing the polygon requires a lot of points, (~60 for the outer circle + ~60 for the inner one), this also results in the map freezing every time I add a new entity. Also the performance of the map using this approach is worse than with GeoJSON.

  3. Adding the same polygonHierarchy, but this time as primitives.
    This approach is very performant but I lose the ability to select each element as with the entity approach and show a description.

Is there a middle ground between getting the benefits of using the Entity API without having to sacrifice that much performance? Or maybe I am doing something wrong? I don’t get why adding the same Polygon as an Entity is that slow comparing to using a primitive. When working with simple entity geometries such as points it doesn’t cause a noticeable performance issue.

I will leave a snippet of how I create the entity and the primitive if that helps:

// As an entity
return new cesium.Entity({
  position: position,
  polygon: {
    hierarchy: {
      positions: computeCircle(position, outerRadius),
      holes:
        level !== 0
          ? [
              {
                positions: computeCircle(position, radius),
                holes: [],
              },
            ]
          : [],
    },
  },
  name: "Ring entity",
  description: "<table>...</table>",
});

// As a primitive
return new cesium.Primitive({
  appearance: new cesium.EllipsoidSurfaceAppearance({
    material: cesium.Material.fromType("Color", {color}),
  }),
  asynchronous: false,
  geometryInstances: [
    {
      attributes: undefined,
      id: "geometry",
      modelMatrix: cesium.Matrix4.IDENTITY,
      geometry: cesium.PolygonGeometry.createGeometry(
        new cesium.PolygonGeometry({
          polygonHierarchy: {
            positions: computeCircle(position, outerRadius),
            holes:
              level !== 0
                ? [
                    {
                      positions: computeCircle(position, radius),
                      holes: [],
                    },
                  ]
                : [],
          },
        })
      )!,
    },
  ],
});

function computeCircle(
  center: cesium.Cartesian3,
  radius: number
): cesium.Cartesian3[] {
  const positions = [];
  for (let i = 0; i < 360; i += 1) {
    const radians = cesium.Math.toRadians(i);
    positions.push(
      new cesium.Cartesian3(
        center.x + radius * Math.cos(radians),
        center.y + radius * Math.sin(radians),
        center.z
      )
    );
  }

  return positions;
}

EDIT: also I don’t understand why but my way of computing the ring polygon doesn’t really create a circular shape:

Hi,

I unfortunately don’t have a solution to your problem. But i saw in your previous post that you are building a drone tracker, which updates its position in realtime. I am trying to do the exact same thing. And i currently have the same issue that you mentioned in your other post, where the line drawn for the drone’s flight path doesn’t correspond to the drone’s actual position due to “HeightReference.RELATIVE_TO_GROUND”.

It looks like you have found a solution in your picture shown above. Could you let me know how you drew the line corresponding to the drone’s ACTUAL position? I would be very grateful.

Hi @Christian_K ,
Welcome to the Cesium community and thanks for your post.

We have a demo in our sandcastle tool Cesium Sandcastle showing how to position entities in the scene relative to the ground, terrain, tiles, etc which is usually helpful for the type of question you are asking.

Please take a look and hopefully that makes setting positions more clear, but certainly let us know if you have further questions.

Thanks,
Luke

ah @Christian_K , I see you resolved your question in another thread. Mapping HeightReference.RELATIVE_TO_GROUND to true height

1 Like

I certainly did. Thank you for the welcome and your response tho :smile:

1 Like