111
wangjuncheng
2025-06-18 05c576538f3fc7ce1dec09f3cee07581203c79af
src/utils/water.js
@@ -1,5 +1,8 @@
import { cartesianToXY } from "@/utils/map";
import { useSimStore } from "@/store/simulation";
import { storeToRefs } from "pinia";
const simStore = useSimStore();
const { waterLegendData } = storeToRefs(simStore);
let water = null;
/**
@@ -21,29 +24,71 @@
 * @param {boolean} options.colorRender - 是否启用颜色渲染
 */
export function createWaterPrimitive(options = {}) {
  const { baseUrl = "/simu/c2h1dc", interval = 1000, colorRender = true } = options;
  const {
    baseUrl = "/simu/c2h1dc",
    interval = 1000,
    colorRender = true,
    minFlowRate = 0.1,   // 新增参数
    maxFlowRate = 12     // 新增参数
  } = options;
  // 定义水深颜色映射的色标
  const colorStops = [
    "#09a2dc", "#58c196", "#bedf74", "#d7f06e",
    "#ffe930", "#fdd10a", "#feb652", "#fd7f06",
    "#fe2b07", "#4d0a08"
  ];
  const levelCount = colorStops.length;
  const minAllowed = 0.01; // 最小允许值
  const threshold = 1;     // 小值与大值分界点
  let effectiveMin = Math.max(minFlowRate, minAllowed); // 最小不能小于 0.01
  const waterHeightLevels = [];
  // 分两段构造高度数组
  for (let i = 0; i < levelCount; i++) {
    let ratio = i / (levelCount - 1); // 0 ~ 1
    let height;
    if (ratio <= 0.5) {
      // 前半段:低值区域,使用强指数增长,从 effectiveMin 到 threshold
      const localRatio = ratio * 2; // 映射到 0~1
      const expRatio = Math.pow(localRatio, 2); // 更强调低值区域密度
      height = effectiveMin + (threshold - effectiveMin) * expRatio;
    } else {
      // 后半段:高值区域,从 threshold 到 maxFlowRate,使用指数增长
      const localRatio = (ratio - 0.5) * 2; // 映射到 0~1
      const expBase = Math.exp(Math.log(maxFlowRate / threshold) / 1);
      height = threshold * Math.pow(expBase, localRatio);
    }
    waterHeightLevels.push({
      height: parseFloat(height.toFixed(2)), // 保留两位小数
      color: colorStops[i]
    });
  }
  waterLegendData.value = waterHeightLevels;
  console.log(waterLegendData.value, '图例数据');
  // 创建图层
  water = earthCtrl.simulate.createWaterSimulateLayer({
    baseUrl,
    interval,
    color: new SmartEarth.Cesium.Color.fromCssColorString("#D4F2E7"),
    color: SmartEarth.Cesium.Color.fromCssColorString("#D4F2E7"),
    loop: false,
    callback: timeCallback,
    waterHeightLevels: [
      { height: 0.5, color: "#09a2dc" },
      { height: 1.0, color: "#58c196" },
      { height: 1.5, color: "#bedf74" },
      { height: 2.0, color: "#d7f06e" },
      { height: 2.5, color: "#ffe930" },
      { height: 3.0, color: "#fdd10a" },
      { height: 4.0, color: "#feb652" },
      { height: 5.0, color: "#fd7f06" },
      { height: 10.0, color: "#fe2b07" },
    ],
    colorRender, // 控制是否启用颜色渲染
    alphaByDepth: -0.3,
    waterHeightLevels,
    colorRender,
    sizeIndex:0,
  });
  console.log(`Water simulation started with baseUrl: ${baseUrl}, interval: ${interval}ms, colorRender: ${colorRender}`);
  console.log(
    `仿真模拟参数:请求路径 ${baseUrl}, 帧间间隔 ${interval}ms, 是否开启专题渲染 ${colorRender}`
  );
}
/**
 * 初始化水体模拟视图
@@ -62,7 +107,7 @@
    },
  };
  viewer.scene.camera.flyTo(view);
  console.log("Camera view initialized for water simulation.");
  // console.log("Camera view initialized for water simulation.");
}
/**
@@ -71,7 +116,7 @@
export function pauseWaterSimulation() {
  if (water) {
    water.pause();
    console.log("Water simulation paused.");
    console.log("暂停仿真");
  } else {
    console.warn("No water simulation to pause.");
  }
@@ -83,7 +128,7 @@
export function resumeWaterSimulation() {
  if (water) {
    water.resume();
    console.log("Water simulation resumed.");
    console.log("继续仿真");
  } else {
    console.warn("No water simulation to resume.");
  }
@@ -101,9 +146,10 @@
      console.warn("No timestamps available for water simulation.");
      return;
    }
    const idx = Math.floor(Math.random() * imageList.length);
    console.log(`Jumping to timestamp: count:[${imageList.length}], index:[${closestIndex}]`);
    // const idx = Math.floor(Math.random() * imageList.length); //随机索引跳转,实际中用不到,只用作演示
    console.log(
      `Jumping to timestamp: count:[${imageList.length}], index:[${closestIndex}]`
    );
    water.setTime(imageList[closestIndex]);
  } else {
    console.warn("No water simulation to set time for.");
@@ -117,7 +163,7 @@
export function toggleWaterColorRender(enabled) {
  if (water) {
    water.colorRender = enabled;
    console.log(`Water color render set to: ${enabled}`);
    console.log(`是否开启专题渲染 ${enabled}`);
  } else {
    console.warn("No water simulation to toggle color rendering.");
  }
@@ -129,4 +175,4 @@
 */
function timeCallback(timeStamp) {
  // console.log(`Current timestamp: ${timeStamp}`);
}
}