2023西安数博会CIM演示-【前端】-Web
AdaKing88
2023-08-21 bc03b832caa49bbcd2674fe4cae3701b5059bf95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * @Descripttion:
 * @version: 1.0.0
 * @Author: glc
 * @Date: 2022-04-29 16:13:29
 * @LastEditors: Andy
 * @LastEditTime: 2022-09-16 20:55:05
 */
//自定义水体primitive
export function WaterPrimitive(options) {
  //debugger;
  this._positions = options.positions;
  this._height = options.height;
  this._extrudedHeight = options.extrudedHeight;
  this._primitive = null;
  this._waterImg = options.waterimg;
  Object.defineProperty(this, "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;
      }
    },
  });
  this.init(); // 调用init函数
}
 
WaterPrimitive.prototype.getGeometry = function () {
  return new Cesium.PolygonGeometry({
    polygonHierarchy: new Cesium.PolygonHierarchy(this._positions),
    height: this._height, // 底部高度
    extrudedHeight: this._extrudedHeight, // 水面高度
    vertexFormat: Cesium.EllipsoidSurfaceAppearance.VERTEX_FORMAT,
  });
};
 
WaterPrimitive.prototype.init = function () {
  this._primitive = new Cesium.Primitive({
    show: true, // 默认隐藏
    geometryInstances: new Cesium.GeometryInstance({
      geometry: new Cesium.PolygonGeometry({
        polygonHierarchy: new Cesium.PolygonHierarchy(this._positions),
        extrudedHeight: this._extrudedHeight, //注释掉此属性可以只显示水面
        //perPositionHeight: true, //注释掉此属性水面就贴地了
      }),
    }),
    // 可以设置内置的水面shader
    appearance: new Cesium.EllipsoidSurfaceAppearance({
      translucent: true,
      material: new Cesium.Material({
        fabric: {
          type: "Water",
          uniforms: {
            baseWaterColor: new Cesium.Color(0.0, 0.0, 1.0, 0.5),
            blendColor: new Cesium.Color(0.0, 0.0, 1.0, 0.5),
            normalMap: this._waterImg,
            frequency: 1000.0,
            animationSpeed: 0.01,
            amplitude: 10.0,
          },
        },
      }),
    }),
  });
};
WaterPrimitive.prototype.update = function (frameState) {
  if (this._primitive) {
    let primitive = this._primitive;
    primitive.update(frameState);
  }
};