1
wangjuncheng
2025-06-06 7217e02e098b94e421b5a85ec13e0cd2ed35fbeb
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;
/**
@@ -17,20 +20,59 @@
 * 创建水体模拟层
 * @param {Object} options - 可选参数
 * @param {number} options.interval - 水体模拟的时间间隔(单位:毫秒)
 * @param {string} options.baseUrl - 仿真服务地址
 * @param {boolean} options.colorRender - 是否启用颜色渲染
 */
export function createWaterPrimitive(options = {}) {
  const { baseUrl = "/simu/c2h1dc", interval = 1000 } = options; // 默认 baseUrl 和 interval
  water = earthCtrl.simulate.createWaterSimulateLayer({
    baseUrl,     // 仿真服务 URL
    interval,    // 动态设置 interval
    color: new SmartEarth.Cesium.Color.fromCssColorString("#D4F2E7"),
    loop: false, // 是否循环播放
    callback: timeCallback, // 回调函数
  });
  console.log(`Water simulation started with baseUrl: ${baseUrl}, interval: ${interval}ms`);
}
  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"
  ];
  // 构造 waterHeightLevels:从 min 到 max 使用指数增长,确保小值区域更密集
  const levelCount = colorStops.length; // 保持和颜色数量一致
  const waterHeightLevels = [];
  // 指数增长公式:y = base^x
  const base = Math.exp(Math.log(maxFlowRate / minFlowRate) / (levelCount - 1));
  for (let i = 0; i < levelCount; i++) {
    const ratio = i / (levelCount - 1); // 0 ~ 1
    // 使用指数插值,保证低值区域更密集
    const height = minFlowRate * Math.pow(base, i);
    const color = colorStops[i];
    waterHeightLevels.push({
      height: height.toFixed(2), // 可选:保留两位小数
      color
    });
  }
  waterLegendData.value = waterHeightLevels
  console.log(waterLegendData.value,'图里数据');
  // 创建图层
  water = earthCtrl.simulate.createWaterSimulateLayer({
    baseUrl,
    interval,
    color: new SmartEarth.Cesium.Color.fromCssColorString("#D4F2E7"),
    loop: false,
    callback: timeCallback,
    alphaByDepth: -0.3,
    waterHeightLevels,
    colorRender,
  });
  console.log(
    `仿真模拟参数:请求路径 ${baseUrl}, 帧间间隔 ${interval}ms, 是否开启专题渲染 ${colorRender},图例参数${waterHeightLevels}`
  );
}
/**
 * 初始化水体模拟视图
 */
@@ -48,7 +90,7 @@
    },
  };
  viewer.scene.camera.flyTo(view);
  console.log("Camera view initialized for water simulation.");
  // console.log("Camera view initialized for water simulation.");
}
/**
@@ -57,7 +99,7 @@
export function pauseWaterSimulation() {
  if (water) {
    water.pause();
    console.log("Water simulation paused.");
    console.log("暂停仿真");
  } else {
    console.warn("No water simulation to pause.");
  }
@@ -69,7 +111,7 @@
export function resumeWaterSimulation() {
  if (water) {
    water.resume();
    console.log("Water simulation resumed.");
    console.log("继续仿真");
  } else {
    console.warn("No water simulation to resume.");
  }
@@ -77,24 +119,36 @@
/**
 * 跳转到某个时间点的水面状态
 * @param {number} closestIndex - 目标时间戳索引
 */
export function setTimeForWaterSimulation(closestIndex) {
  console.log(closestIndex,'index');
  if (water) {
    const imageList = water.getTimeList(); // 获取所有可用时间戳
    if (imageList.length === 0) {
    const imageList = water.getTimeList();
    if (!imageList.length) {
      console.warn("No timestamps available for water simulation.");
      return;
    }
    const idx = Math.floor(Math.random() * imageList.length); // 随机选择一个时间戳
    // const idx = Math.floor(Math.random() * imageList.length); //随机索引跳转,实际中用不到,只用作演示
    console.log(
      `Jumping to timestamp: count:[${imageList.length}], index:[${idx}]`
      `Jumping to timestamp: count:[${imageList.length}], index:[${closestIndex}]`
    );
    water.setTime(imageList[closestIndex]); // 设置时间戳,跳转到对应时刻
    water.setTime(imageList[closestIndex]);
  } else {
    console.warn("No water simulation to set time for.");
  }
}
/**
 * 设置或关闭颜色渲染
 * @param {boolean} enabled
 */
export function toggleWaterColorRender(enabled) {
  if (water) {
    water.colorRender = enabled;
    console.log(`是否开启专题渲染 ${enabled}`);
  } else {
    console.warn("No water simulation to toggle color rendering.");
  }
}
@@ -104,4 +158,4 @@
 */
function timeCallback(timeStamp) {
  // console.log(`Current timestamp: ${timeStamp}`);
}
}