Error when quickly turning on/off the 3D terrain after panning or zoom

1. A concise explanation of the problem you’re experiencing.

I always encounter this error whenever I tried to turn on the 3D terrain then do panning or zooming then turn the 3D terrain off again. I am assuming that it might be because the terrain is still loading and then I abruptly turned it off since when I do panning or zoom it updates the terrain tiles. But I might be wrong as well, so I wanted to know your thoughts on this and how I can prevent this error on my application.

CesiumWidget.js:726 An error occurred while rendering.  Rendering has stopped.
DeveloperError: This object was destroyed, i.e., destroy() was called.
Error
    at new DeveloperError (webpack-internal:///./src/sdk/cesium/packages/engine/Source/Core/DeveloperError.js:41:11)
    at Imagery.throwOnDestroyed (webpack-internal:///./src/sdk/cesium/packages/engine/Source/Core/destroyObject.js:41:11)
    at TileImagery.freeResources (webpack-internal:///./src/sdk/cesium/packages/engine/Source/Scene/TileImagery.js:31:23)
    at GlobeSurfaceTile.freeResources (webpack-internal:///./src/sdk/cesium/packages/engine/Source/Scene/GlobeSurfaceTile.js:207:20)
    at QuadtreeTile.freeResources (webpack-internal:///./src/sdk/cesium/packages/engine/Source/Scene/QuadtreeTile.js:468:15)
    at invalidateAllTiles (webpack-internal:///./src/sdk/cesium/packages/engine/Source/Scene/QuadtreePrimitive.js:234:25)
    at QuadtreePrimitive.beginFrame (webpack-internal:///./src/sdk/cesium/packages/engine/Source/Scene/QuadtreePrimitive.js:338:5)
    at Globe.beginFrame (webpack-internal:///./src/sdk/cesium/packages/engine/Source/Scene/Globe.js:938:13)
    at render (webpack-internal:///./src/sdk/cesium/packages/engine/Source/Scene/Scene.js:2956:17)
    at tryAndCatchError (webpack-internal:///./src/sdk/cesium/packages/engine/Source/Scene/Scene.js:2973:5)

2. A minimal code example. If you’ve found a bug, this helps us reproduce and repair it.
I created 2 functions in order to turn on & off the 3D terrain. If the value that is passed on the setTerrain3d function is true, it will set a terrain otherwise it will remove it.

        async enableTerrain3d() {
            // this.viewer.terrainProvider = await Cesium.createWorldTerrainAsync();
            let terrain = Cesium.Terrain.fromWorldTerrain();
            this.viewer.scene.setTerrain(terrain);
            terrain.errorEvent.addEventListener(error => {
                alert(`Encountered an error while creating terrain! ${error}`);
            });
            this.viewer.scene.globe.depthTestAgainstTerrain = false;
        },
        setTerrain3d(value) {
            if (value) {
                this.enableTerrain3d();
            } else {
                this.viewer.scene.terrainProvider = new Cesium.EllipsoidTerrainProvider();
                this.viewer.scene.globe.depthTestAgainstTerrain = true;
            }
        },

3. Context. Why do you need to do this? We might know a better way to accomplish your goal.

I need to smoothly enable or disable the 3D terrain without any error. Even if I do panning or zoom and then quickly turn on/off the 3D terrain, I wouldn’t get the error above.

4. The Cesium version you’re using, your operating system and browser.

1.109

Hi @Sarah_Osay ,
Thanks for your post and for continuing to use Cesium.

We have this sandcastle demo showing how to switch terrain providers for the scene Cesium Sandcastle

In addition, it looks like in one function you use scene.setTerrain but in the other you try to set the scene.terrainProvider property directly. Can you try using setTerrain for both?

I hope one of those 2 items is helpful in resolving your issue, please let us know if the issues persist.

Thank you,
Luke

Hi @Luke_McKinstry, thanks for your quick response! I tried updating my code based on the Cesium Sandcastle link you shared, but unfortunately, I’m still encountering the destroy error. However, I did manage to gather more information about the issue. Here are the steps I took to reproduce the error:

  • Turn on terrain
  • Zoom in to the tileset until it reaches the blank space
  • Zoom out (Error happens)

Please refer to the GIF below for the example of how the error happened:
chrome-capture-2024-10-10

It appears that I’m encountering a persistent error when assigning terrain data, regardless of whether I utilize the setTerrain function or directly manipulate the terrainProvider. This issue consistently arises in the specific scenario outlined above.

Could you offer any advice on how to address this issue?

Hi @Sarah_Osay,

Sorry the issue is still persisting.

You could try to destroy the primitive before switching terrain and then adding it back after. I am not sure all the technical details behind this practice but it may work for your use case.

If that does not work perhaps you could provide us a copy of your tileset and we could try to debug locally.

Thanks and we hope to get your issue resolved soon.
Best,
Luke

Hello!

I’ve identified two main causes of the “destroy” error after more investigation:

  1. Custom Wheel Function: My custom wheel function interfered with terrain navigation, allowing zooming beyond the terrain and into the black space, which triggered the error. Reverting to the default wheel event prevents this over-zoom and resolves this instance of the error.

  2. Pointcloud Placement: Incorrect placement of the pointcloud within the terrain map, particularly below the ground level where the black space is visible, also intermittently caused the “destroy” error.

I’m currently running more tests to confirm that the ‘destroy’ error has been completely eliminated. I’ll update this thread and mark this as the solution once I’ve verified the fix.