首先感谢该文章作者提供的代码
https://blog.csdn.net/qq_39142804/article/details/122825831?utm_source=app&app_version=5.3.1
本文是在其基础上做了部分完善,由于entity没有水面动态材质,primitive的exhight无法使用property的形式进行改变,因此独立出该primitive 将exhight属性抛出 可以动态修改
原文中的水面上涨是通过类外部的函数执行的,我添加了grow函数,进一步让代码封装 独立化
代码:
import * as Cesium from “cesium”;
import movingRiver from “@/assets/images/map/water_normals.jpg”;
export class WaterPrimitive {
constructor(options) {
this._positions = options.positions;
this._height = options.height;
this._extrudedHeight = options.extrudedHeight;
this._targetHeight = Cesium.defaultValue(options.targetHeight, options.extrudedHeight);
this._grow = Cesium.defaultValue(options.grow, false);
this._upGrow = Cesium.defaultValue(options.upGrow, true);
this.init();
}
}
Object.defineProperties(WaterPrimitive.prototype, {
extrudedHeight: {
get() {
return this._extrudedHeight;
},
set(newVal) {
if (Object.prototype.toString.call(newVal) !== “[object Number]”) return;
if (this._primitive) {
this._primitive._state = 3;
this._primitive._appearance = undefined;
this._primitive.geometryInstances.geometry = this.getGeometry();
this._extrudedHeight = newVal;
}
}
}
});
WaterPrimitive.prototype.getGeometry = function () {
return new Cesium.PolygonGeometry({
polygonHierarchy: new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArray(this._positions)),
height: this._height,
extrudedHeight: this._extrudedHeight,
vertexFormat: Cesium.EllipsoidSurfaceAppearance.VERTEX_FORMAT
});
};
WaterPrimitive.prototype.update = function (frameState) {
if (this._primitive) {
let primitive = this._primitive;
primitive.update(frameState);
}
};
WaterPrimitive.prototype.destroy = function () {
let primitive = this._primitive;
primitive.destroy()
}
WaterPrimitive.prototype.init = function () {
let geometry = this.getGeometry();
if (!geometry) return;
this._primitive = new Cesium.Primitive({
releaseGeometryInstances: false,
geometryInstances: new Cesium.GeometryInstance({
geometry
}),
asynchronous: false,
appearance: new Cesium.EllipsoidSurfaceAppearance({
material: new Cesium.Material({
fabric: {
type: “Water”,
uniforms: {
baseWaterColor: new Cesium.Color(0 / 255.0, 255 / 255.0, 237 / 255.0, 0.1),
normalMap: movingRiver,
frequency: 1000.0,
animationSpeed: 0.1,
amplitude: 10,
specularIntensity: 10
}
}
}),
})
});
if (this._grow) {
this.grow();
}
};
WaterPrimitive.prototype.grow = function () {
if (this._upGrow) {
if (this.extrudedHeight < this._targetHeight) {
this.extrudedHeight += 0.01;
Cesium.requestAnimationFrame(() => this.grow());
}
} else {
if (this.extrudedHeight > this._targetHeight) {
this.extrudedHeight -= 0.01;
Cesium.requestAnimationFrame(() => this.grow());
}
}
}
Cesium.WaterPrimitive = WaterPrimitive;
后记:我对封装的理解可能不够 可以留言一起改进22.6.11 6:39