自定义cesium ImageryProvider: 优化loadImage获取瓦片资源

需积分: 5 1 下载量 149 浏览量 更新于2024-08-03 收藏 1KB TXT 举报
在Cesium.js这个强大的3D地球渲染库中,ImageryProvider是一个核心组件,它负责提供地图或地球表面的图像数据。如果你想自定义加载瓦片资源的行为,特别是优化图片加载流程或者处理特定错误情况,你可能会选择重写ImageryProvider类中的`loadImage`方法。在这个例子中,我们看到一个名为`RewriteImageryProvider`的自定义类,它是对cesium.ImageryProvider的一个扩展。 首先,`RewriteImageryProvider`类的构造函数`constructor()`中调用了`init()`方法,这是对原有ImageryProvider初始化过程的扩展,主要目标是重载`loadImage`函数。这个重写的方法允许你控制图片的加载逻辑,例如: 1. 异步加载和回调处理: - 使用`new Promise`来实现异步操作,这样当图片加载成功时,通过`resolve`回调将加载完成的图片对象存储在`_tiles`数组中。如果图片加载失败,则通过`reject`抛出错误,并打印错误信息。 2. 图片加载源: - `img.src`被设置为传入的URL,这是获取瓦片资源的基本步骤。`crossOrigin`属性设置为"Anonymous",确保可以在跨域情况下访问图片。 3. 事件监听: - 当图片加载成功时,`onload`事件被监听,将图片的URL添加到一个Set(去重)中,最后通过`Array.from(new Set(this._tiles))`返回所有加载成功的瓦片URL。 4. 错误处理: - 如果图片加载过程中出现错误,如网络问题或其他原因,`onerror`事件会被触发,捕获错误并记录下来,防止程序崩溃。 通过这种方式,`RewriteImageryProvider`提供了对Cesium默认`loadImage`功能的增强,允许开发者根据自己的需求定制瓦片资源的加载策略。这在处理大量瓦片、优化网络请求、错误处理或缓存等方面具有显著的优势。这种重写方式展示了如何深入理解Cesium的工作原理,并利用其API扩展其功能。

分析下面代码的作用:/* * @Description: 飞线效果(参考开源代码) * @Version: 1.0 * @Author: Julian * @Date: 2022-03-05 16:13:21 * @LastEditors: Julian * @LastEditTime: 2022-03-05 17:39:38 */ class LineFlowMaterialProperty { constructor(options) { this._definitionChanged = new Cesium.Event(); this._color = undefined; this._speed = undefined; this._percent = undefined; this._gradient = undefined; this.color = options.color; this.speed = options.speed; this.percent = options.percent; this.gradient = options.gradient; }; get isConstant() { return false; } get definitionChanged() { return this._definitionChanged; } getType(time) { return Cesium.Material.LineFlowMaterialType; } getValue(time, result) { if (!Cesium.defined(result)) { result = {}; } result.color = Cesium.Property.getValueOrDefault(this._color, time, Cesium.Color.RED, result.color); result.speed = Cesium.Property.getValueOrDefault(this._speed, time, 5.0, result.speed); result.percent = Cesium.Property.getValueOrDefault(this._percent, time, 0.1, result.percent); result.gradient = Cesium.Property.getValueOrDefault(this._gradient, time, 0.01, result.gradient); return result } equals(other) { return (this === other || (other instanceof LineFlowMaterialProperty && Cesium.Property.equals(this._color, other._color) && Cesium.Property.equals(this._speed, other._speed) && Cesium.Property.equals(this._percent, other._percent) && Cesium.Property.equals(this._gradient, other._gradient)) ) } } Object.defineProperties(LineFlowMaterialProperty.prototype, { color: Cesium.createPropertyDescriptor('color'), speed: Cesium.createPropertyDescriptor('speed'), percent: Cesium.createPropertyDescriptor('percent'), gradient: Cesium.createPropertyDescriptor('gradient'), }) Cesium.LineFlowMaterialProperty = LineFlowMaterialProperty; Cesium.Material.LineFlowMaterialProperty = 'LineFlowMaterialProperty'; Cesium.Material.LineFlowMaterialType = 'LineFlowMaterialType'; Cesium.Material.LineFlowMaterialSource = ` uniform vec4 color; uniform float speed; uniform float percent; uniform float gradient; czm_material czm_getMaterial(czm_materialInput materialInput){ czm_material material = czm_getDefaultMaterial(materialInput); vec2 st = materialInput.st; float t =fract(czm_frameNumber * speed / 1000.0); t *= (1.0 + percent); float alpha = smoothstep(t- percent, t, st.s) * step(-t, -st.s); alpha += gradient; material.diffuse = color.rgb; material.alpha = alpha; return material; } ` Cesium.Material._materialCache.addMaterial(Cesium.Material.LineFlowMaterialType, { fabric: { type: Cesium.Material.LineFlowMaterialType, uniforms: { color: new Cesium.Color(1.0, 0.0, 0.0, 1.0), speed: 10.0, percent: 0.1, gradient: 0.01 }, source: Cesium.Material.LineFlowMaterialSource }, translucent: function(material) { return true; } })

2023-03-02 上传