How to solve the clustering bug in Cesium?

800 views Asked by At

I have found a bug in 1.28:

I produced it by creating lots of entities with label. Howeveer, when I set the clustering enabled, some labels were invisible in 2D.

The screenshot of bug

enter image description here

the code as follows:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport"
      content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<meta name="description"
      content="Use Viewer to start building new applications or easily embed Cesium into existing applications.">
<meta name="cesium-sandcastle-labels" content="Beginner, Showcases">
<title>Cesium Demo</title>
<script type="text/javascript" src="../Sandcastle-header.js"></script>
<script type="text/javascript" src="../../../ThirdParty/requirejs-2.1.20/require.js"></script>
<style>
    #convert {
        z-index: 100;
        top: 30px;
        position: absolute;
    }
</style>
<script type="text/javascript">

    require.config({
        baseUrl: '../../../Source',
        waitSeconds: 60
    });
</script>
</head>
<body class="sandcastle-loading" data-sandcastle-bucket="bucket-requirejs.html">
<style>
@import url(../templates/bucket.css);
</style>
<div id="cesiumContainer" class="fullSize"></div>
<div id="loadingOverlay"><h1>Loading...</h1></div>
<div id="toolbar"></div>
<div>
<button id="convert">2/3D转换</button>
</div>
<script id="cesium_sandcastle_script">
var viewer;
function startup(Cesium) {
    'use strict';
//Sandcastle_Begin
    viewer = new Cesium.Viewer('cesiumContainer');
    document.getElementById("convert").onclick = function () {
        if (viewer.scene.mode == Cesium.SceneMode.SCENE3D) {
            viewer.scene.mode = Cesium.SceneMode.SCENE2D
        } else {
            viewer.scene.mode = Cesium.SceneMode.SCENE3D
        }
    };


    var modelLayer = new Cesium.CustomDataSource();
    for (var i = 0; i < 50; i++) {
        var lon = Math.random() * 1000;
        var lat = Math.random() * 500;
        modelLayer.entities.add({
            position: Cesium.Cartesian3.fromDegrees(-75.1641667 + lon, -39.9522222 + lat),
            label: {
                text: '东海舰队超级无敌',
                font: '13px Verdana',
                position: Cesium.Cartesian3.fromDegrees(-75.1641667 + lon, -39.9522222 + lat, 100000),
                verticalOrigin: Cesium.VerticalOrigin.TOP,
                eyeOffset: Cesium.Cartesian3(0, 0, -10000)
            },
            model: {
                position: Cesium.Cartesian3.fromDegrees(-75.1641667 + lon, -39.9522222 + lat, 0),
                uri: '../../SampleData/models/CesiumAir/Cesium_Air.glb',
                minimumPixelSize: 128,
                maximumScale: 20000
            }
        });
    }


    var pixelRange = 15;
    var minimumClusterSize = 3;
    var enabled = true;

    modelLayer.clustering.enabled = enabled;
    modelLayer.clustering.pixelRange = pixelRange;
    modelLayer.clustering.minimumClusterSize = minimumClusterSize;

    var removeListener;

    var pinBuilder = new Cesium.PinBuilder();
    var pin50 = pinBuilder.fromText('50+', Cesium.Color.RED, 48).toDataURL();
    var pin40 = pinBuilder.fromText('40+', Cesium.Color.ORANGE, 48).toDataURL();
    var pin30 = pinBuilder.fromText('30+', Cesium.Color.YELLOW, 48).toDataURL();
    var pin20 = pinBuilder.fromText('20+', Cesium.Color.GREEN, 48).toDataURL();
    var pin10 = pinBuilder.fromText('10+', Cesium.Color.BLUE, 48).toDataURL();

    var singleDigitPins = new Array(8);
    for (var i = 0; i < singleDigitPins.length; ++i) {
        singleDigitPins[i] = pinBuilder.fromText('' + (i + 2), Cesium.Color.VIOLET, 48).toDataURL();
    }
    customStyle();

    function customStyle() {
        if (Cesium.defined(removeListener)) {
            removeListener();
            removeListener = undefined;
        } else {
            removeListener = modelLayer.clustering.clusterEvent.addEventListener(function (clusteredEntities, cluster) {
                cluster.label.show = true;
                cluster.billboard.show = true;
                cluster.billboard.verticalOrigin = Cesium.VerticalOrigin.BOTTOM;

                if (clusteredEntities.length >= 50) {
                    cluster.billboard.image = pin50;
                } else if (clusteredEntities.length >= 40) {
                    cluster.billboard.image = pin40;
                } else if (clusteredEntities.length >= 30) {
                    cluster.billboard.image = pin30;
                } else if (clusteredEntities.length >= 20) {
                    cluster.billboard.image = pin20;
                } else if (clusteredEntities.length >= 10) {
                    cluster.billboard.image = pin10;
                } else {
                    cluster.billboard.image = singleDigitPins[clusteredEntities.length - 2];
                }
            });
        }

        // force a re-cluster with the new styling
        var pixelRange = modelLayer.clustering.pixelRange;
        modelLayer.clustering.pixelRange = 0;
        modelLayer.clustering.pixelRange = pixelRange;
    }

    viewer.dataSources.add(modelLayer);
//Sandcastle_End
    Sandcastle.finishedLoading();
}
if (typeof Cesium !== "undefined") {
    startup(Cesium);
} else if (typeof require === "function") {
    require(["Cesium"], startup);
}
</script>
</body>
</html>

1

There are 1 answers

0
Brendan On

This used to work in Cesium v1.26. I have just encountered this same issue with the current Cesium v1.34 and have logged a bug. The labels will appear if you disable clustering.