CesiumGS / cesium

An open-source JavaScript library for world-class 3D globes and maps :earth_americas:
https://cesium.com/cesiumjs/
Apache License 2.0
12.92k stars 3.48k forks source link

Rendering error for entity at origin #12150

Open javagl opened 2 months ago

javagl commented 2 months ago

When creating an entity at (0,0,0), it crashes with

TypeError: Cannot read properties of undefined (reading 'longitude')

Sandcastle:

https://sandcastle.cesium.com/index.html#c=dY/BSsQwEIZfZeiphZK6eFu7RehV8CB46iVtRh02nZRM2nWVfXeT9rLoCnPI/Pm+n2RwLAEWwhN6OADjCVoUmkf1umZ5lw3r3joOmhh9lxUPHW+GQg4UCEVpY/LvjgEmJzFxvL/uarUP8aT5Pr8rIU1RJrh3n3tYNQBDI7JEU/5TdyWkKRJ/if4lPSQrs1rC2WKz1QA80jg5H2D2NleqCjhOVseOqp+HIwY1iCQxoXV1rdaGFiBzuPFlGKwWiTdvs7Uv9IVd1tRV5P+o1mlD/P68oLf6nLCPXfO0hUqpuorrbTM4Z3vtfzX/AA

It's pretty simple:

const viewer = new Cesium.Viewer("cesiumContainer");
viewer.entities.add({
  position: new Cesium.Cartesian3(0, 0, 0),
  box: {
    dimensions: new Cesium.Cartesian3(1, 1, 1)
  },
});

This is caused here: https://github.com/CesiumGS/cesium/blob/89d15af5d2c0338a59f91f3dfa666ad8478f694b/packages/engine/Source/Scene/Primitive.js#L1530

The cartesianToCartographic function is returning undefined when receiving the center.

(I'll just put it at (0, 0, 1e-15) for now...)

ggetz commented 2 months ago

Here's another Sandcastle which breaks with the same stack

javagl commented 2 months ago

Adding a

osmBuildingsTileset.modelMatrix = Cesium.Matrix4.fromTranslation(
  new Cesium.Cartesian3(0.001, 0.001, 0.001));

"fixes" that. (Not sure which smiley to insert here).

I think a viable approach for a first "triage" pass on this issue would be to search for all calls to cartesianToCartographic, see what is done with the result, and handle the undefined return case accordingly. In both cases described here so far, it was passed to the respective projection, and I could imagine that some of that could be solved somewhat pragmatically, with something like

if (!defined(cartographic)) {
    // Input was origin...
    cartesian.x = 0.0;    
    cartesian.y = 0.0;
} else { ... project ... }