【cesium】水面动态上涨下跌且带有动态水面材质

本文介绍了一种在Cesium中实现水面动态材质的方法,通过封装WaterPrimitive类,独立出exhight属性并提供grow函数,实现水体高度的动态增长或降低。代码示例详细展示了如何创建和更新水体几何,并使用requestAnimationFrame实现平滑动画效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先感谢该文章作者提供的代码
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

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gis分享

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值