Satellite Connection Code Causing Performance Issues

Here’s the section of the code responsible for connecting satellites with lines, which appears to be causing the performance issues:

function updateSatelliteConnection(connection, satellite1, satellite2) {
    var position1 = satellite1.position.getValue(viewer.clock.currentTime);
    var position2 = satellite2.position.getValue(viewer.clock.currentTime);

    if (!position1 || !position2) {
        connection.polyline.positions = [new Cesium.Cartesian3(), new Cesium.Cartesian3()];
    } else {
        connection.polyline.positions = [position1, position2];
        if (isOccluded(position1, position2)) {
            connection.polyline.material = new Cesium.PolylineDashMaterialProperty({
                color: connection.polyline.material.color.getValue(),
                dashLength: 16.0
            });
        } else {
            connection.polyline.material = new Cesium.ColorMaterialProperty(connection.polyline.material.color.getValue());
        }
    }
}

// Example usage:
var connection1 = viewer.entities.add({
    polyline: {
        positions: [],
        width: 2,
        material: new Cesium.PolylineDashMaterialProperty({
            color: Cesium.Color.RED,
            dashLength: 16.0
        })
    }
});

setInterval(function() {
    updateSatelliteConnection(connection1, earthSatellite, moonSatellite);
}, 300); // Update every 300 ms

Strangely, while the earlier parts of the code exhibit high CPU usage without causing freezes, adding even a single section of this connection line code causes significant slowdowns. This occurs despite the connection lines themselves not consuming a large amount of resources.

Why does it cause page lag even if the resource usage is not high?

When creating a polyline, if there is a model blocking the middle or if the positions of the two points are not appropriate, is there a possibility that the web page could freeze directly?

weird

Hi there,

It appears you are re-creating the positions array every update, which can cause lag from garbage collection. Maybe consider modifying the positions in place, and use Cesium methods like Cartesian2.clone.