我如何在铯查看器中获取模型加载事件

How can i get model loading event in cesium viewer

在cesium中我按如下方式添加3dmodel(url只是.gltf文件的路径)

function load3dmodel(url, x, y) {
    viewer.entities.removeAll();
    viewer.entities.add({
        position: Cesium.Cartesian3.fromDegrees(x, y), model: {
            uri: url
         }
    });
}

在 cesium 查看器中加载 gltf 文件需要 30 到 60 秒,所以我想在加载 3dmodel 时显示处理中的 Gif 图像。为此,我无法找到 3dmodel 加载事件。我的意思是实际加载完成的时间。 我尝试在函数结束后使用 "then" 子句。但它不工作

目前,没有官方方法可以做到这一点。实体 API 层故意隐藏其下方的图形基元层,以防止泄漏抽象。 Cesium 的未来版本应该将 Model.readyModel.readyPromise 暴露给实体 API,但目前尚未实现。

我确实花了一分钟时间看看在 Cesium 1.15 版中找出模型基元需要什么。找到它的代码非常丑陋,它使用 "private"(以 _ 为前缀)变量,这些变量没有记录并且可能会在没有警告的情况下更改。所以这不是一个长期的解决方案,并且可能无法跨版本工作。

function load3dmodel(url, x, y) {
    viewer.entities.removeAll();
    var entity = viewer.entities.add({
        position: Cesium.Cartesian3.fromDegrees(x, y), model: {
            uri: url
        }
    });

    // Use of _private variables is undocumented, subject to change without notice.
    // Do not use this code in production.
    Cesium.requestAnimationFrame(function() {
        viewer.dataSourceDisplay.defaultDataSource._visualizers.reduce(function(a,b) {
            return (a instanceof Cesium.ModelVisualizer) ? a : b; }
        )._modelHash[entity.id].modelPrimitive.readyPromise.then(function() {
            console.log('Your model has loaded.');
        });
    });
}