Cesium通过 feature ids来操作3dtiles瓦片集中的要素

547 阅读1分钟

在3dtiles瓦片集中,如果知道某一个要素/图元的ids,而大多数情况下这个ids更业务相关,为此通过ids来设置模型中要素的一些属性,比如颜色、显示隐藏等都很有必要。 而在Cesium提供的关于Cesium3DTileset接口中并没有直接的接口,但可以通过Cesium3DTileStyle来实现。

 * 设置该图层id为1的图元颜色 为红色
 * Cesium3DTileset.setObjsColor([1],Cesium.Color.RED)
 */
Cesium.Cesium3DTileset.prototype.setObjsColor = function (ids, color) {
    let currentStyle = this.style && this.style.style || {};
    this.style = new Cesium.Cesium3DTileStyle({
        ...currentStyle,
        color: {
            evaluateColor: function (feature) {
                if (ids.includes(feature._batchId)) {
                    return Cesium.Color.clone(feature.color, color);
                }
            }
        }
    })
};
Cesium.Cesium3DTileset.prototype.removeObjsColor = function (ids) {

    let currentStyle = this.style && this.style.style || {};
    this.style = new Cesium.Cesium3DTileStyle({
        ...currentStyle,
        color: {
            evaluateColor: function (feature) {
                if (ids.includes(feature._batchId)) {

                    return undefined
                }
            }
        }
    })
};
Cesium.Cesium3DTileset.prototype.setObjsExtendHeight = function () {
    // 在这里实现您的自定义方法
    console.log('This is my custom method!');
};
Cesium.Cesium3DTileset.prototype.setObjsOffset = function () {
    // 在这里实现您的自定义方法
    console.log('This is my custom method!');
};
Cesium.Cesium3DTileset.prototype.setObjsTranslate = function () {
    // 在这里实现您的自定义方法
    console.log('This is my custom method!');
};

/**
 * 设置该图层id为1的图元显示,其余所有图元全部不可见。
 * Cesium3DTileset.setObjsVisible([1],true);
 * 设置图元id=1的隐藏,其余所有图元可见。
 * Cesium3DTileset.setObjsVisible([1],false);
 */
Cesium.Cesium3DTileset.prototype.setObjsVisible = function (ids, boolean) {
    let currentStyle = this.style && this.style.style || {};
    this.style = new Cesium.Cesium3DTileStyle({
        ...currentStyle,
        show: {
            evaluate: function (feature) {
                let ishow = boolean
                if (ids.includes(feature._batchId)) {
                    return ishow
                } else {
                    return !ishow
                }

            }
        }
    })

};
/**
 * //设置id为1和2的图元为不可见,其余图元的可见状态不变。
 * Cesium3DTileset.setOnlyObjsVisible([1,2],false);
 * //设置id为1和2的图元为可见,其余图元的可见状态不变。
 * Cesium3DTileset.setOnlyObjsVisible([1,2],true);
 */
Cesium.Cesium3DTileset.prototype.setOnlyObjsVisible = function (ids, boolean) {
    let currentStyle = this.style && this.style.style || {};
    this.style = new Cesium.Cesium3DTileStyle({
        ...currentStyle,
        show: {
            evaluate: function (feature) {
                let ishow = boolean
                ids.includes(feature._batchId) ? ishow = ishow
                    : ishow = !ishow
                return ishow
            }
        }
    })
};