From 5a93ff9c70a25e09d77aff8e9175022b63b3060f Mon Sep 17 00:00:00 2001 From: wangjuncheng <1> Date: 星期五, 18 七月 2025 10:54:23 +0800 Subject: [PATCH] 1 --- src/utils/water.js | 329 ++++++++++++++++++++++++++++++++++++++---------------- 1 files changed, 233 insertions(+), 96 deletions(-) diff --git a/src/utils/water.js b/src/utils/water.js index 333b865..3d7de0b 100644 --- a/src/utils/water.js +++ b/src/utils/water.js @@ -1,103 +1,240 @@ -import { cartesianToXY } from "@/utils/map" -let water = null +import { useSimStore } from "@/store/simulation"; +import { storeToRefs } from "pinia"; +const simStore = useSimStore(); +const { waterLegendData } = storeToRefs(simStore); +let water = null; + +/** + * 閿�姣佹按浣撴ā鎷熷眰 + */ export function destoryWaterPrimitive() { - if (water) { - water.destroy() - water = null - } + if (water) { + enableWaterArrowFlow(false); + water.destroy(); + water = null; + // console.log("Water simulation destroyed."); + } } -export function createWaterPrimitive(rainfall) { - if (water) { - water.rainfall = rainfall - return - } - let positionArray = [ - { - x: -2172418.4199554003, - y: 4339741.174232391, - z: 4124707.9687457774, - }, - { - x: -2173054.1197314346, - y: 4339420.091643301, - z: 4124710.896091218, - }, - { - x: -2172832.10576698, - y: 4338665.615224519, - z: 4125615.341361447, - }, - { - x: -2171464.714295606, - y: 4337884.628113293, - z: 4127145.919793182, - }, - { - x: -2170738.824348358, - y: 4338621.865525843, - z: 4126755.442367371, - }, - { - x: -2171270.181257778, - y: 4338967.11477535, - z: 4126117.1692552743, - }, - { - x: -2171120.444962885, - y: 4339310.237335228, - z: 4125837.0031219805, - }, - { - x: -2171911.8873938583, - y: 4339707.853907823, - z: 4125007.7462886167, - }, - { - x: -2171934.494218292, - y: 4339685.285760623, - z: 4125019.506657141, - }, - ] - const coordinates = [] - positionArray.forEach(item => { - const result = cartesianToXY(item) - coordinates.push(result.longitude, result.latitude, result.height) - }) - water = earthCtrl.analysis.createWaterSimulateGPU({ - //isDraw: true, // 鏄惁缁樺埗姘撮潰 - height: -5, - maxHeight: 2000, - coordinates: coordinates, - // coordinates: [ - // 116.57628914253382, 40.5654461866556, 0, 116.57628914253382, 40.552898897029046, 0, - // 116.60855621443835, 40.552898897029046, 0, 116.60855621443835, 40.5654461866556, 0, - // ], - // coordinates: [ - // 116.568865, 40.590729, 0, 116.568865, 40.533505, 0, - // 116.608505, 40.533505, 0, 116.608505, 40.590729, 0, - // ], - }) - console.log("water", water) +/** + * 鍒涘缓姘翠綋妯℃嫙灞� + * @param {Object} options - 鍙�夊弬鏁� + * @param {number} options.interval - 姘翠綋妯℃嫙鐨勬椂闂撮棿闅旓紙鍗曚綅锛氭绉掞級 + * @param {string} options.baseUrl - 浠跨湡鏈嶅姟鍦板潃 + * @param {boolean} options.colorRender - 鏄惁鍚敤棰滆壊娓叉煋 + */ +export async function createWaterPrimitive(options = {}) { + const { + baseUrl = "/simu/c2h1dc", + interval = 1000, + colorRender = true, + minFlowRate = 0.1, // 鏂板鍙傛暟 + maxFlowRate = 12, // 鏂板鍙傛暟 + } = options; - // water.rainfall = 0.0001; // 璋冭妭闆ㄩ噺 - // water.rainPointMax = 2.0; // 娓楁按闃堝�� - // water.attenuation = 0.995; // 琛板噺 - // water.strenght = 0.25 // 姘存祦寮哄害 + // 瀹氫箟姘存繁棰滆壊鏄犲皠鐨勮壊鏍� + const colorStops = [ + "#09a2dc", + "#58c196", + "#bedf74", + "#d7f06e", + "#ffe930", + "#fdd10a", + "#feb652", + "#fd7f06", + "#fe2b07", + "#4d0a08", + ]; + + const levelCount = colorStops.length; + const minAllowed = 0.05; // 鏈�灏忓厑璁稿�� + 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 = await earthCtrl.simulate.createWaterSimulateLayer({ + baseUrl, + interval, + color: SmartEarth.Cesium.Color.fromCssColorString("#D4F2E7"), + loop: false, + callback: timeCallback, + alphaByDepth: -0.3, + waterHeightLevels, + colorRender, + sizeIndex: 0, + }); + //闃叉缂╂斁瀵艰嚧鍦板舰鍙樺姩鍘嬬洊姘撮潰锛屾按闈㈠鍔犺缃� + water.clampMinHeight = 0; //鐩告満鐩稿浜庢按闈㈡渶灏忛珮搴� + water.clampMaxHeight = 1000; //鐩告満鐩稿浜庢按闈㈡渶澶ч珮搴� + water.offsetMinHeight = 0; //姘村亸绉绘渶灏忛珮搴� + water.offsetMaxHeight = 100; //姘村亸绉绘渶澶ч珮搴� + // 鏄惁寮�鍚澶� + enableWaterArrowFlow(false); + // 鏄惁寮�鍚按闈㈤槾褰� + toggleWaterShadow(false); + + // console.log( + // `浠跨湡妯℃嫙鍙傛暟锛氳姹傝矾寰� ${baseUrl}, 甯ч棿闂撮殧 ${interval}ms, 鏄惁寮�鍚笓棰樻覆鏌� ${colorRender}` + // ); } - +/** + * 鍒濆鍖栨按浣撴ā鎷熻鍥� + */ export function initeWaterPrimitiveView() { - let view = { - destination: { - x: -2173603.2294639186, - y: 4338938.333124211, - z: 4128027.401463165, - }, - orientation: { - pitch: -0.6208443477400212, - roll: 0.000049799989940702005, - heading: 3.6294612473618644, - }, - } - viewer.scene.camera.flyTo(view) + let view = { + destination: { + x: -2173603.2294639186, + y: 4338938.333124211, + z: 4128027.401463165, + }, + orientation: { + pitch: -0.6208443477400212, + roll: 0.000049799989940702005, + heading: 3.6294612473618644, + }, + }; + viewer.scene.camera.flyTo(view); + // console.log("Camera view initialized for water simulation."); +} +/** + * 鏇存崲姘撮�忔槑搴� + */ +export function updateWaterColor(color, alpha) { + if (water) { + water.color = Cesium.Color.fromCssColorString(color); + water.alphaByDepth = alpha; + } else { + console.warn("No water simulation to pause."); + } +} +/** + * 鏆傚仠姘翠綋妯℃嫙 + */ +export function pauseWaterSimulation() { + if (water) { + water.pause(); + // console.log("鏆傚仠浠跨湡"); + } else { + console.warn("No water simulation to pause."); + } +} + +/** + * 鎭㈠姘翠綋妯℃嫙 + */ +export function resumeWaterSimulation() { + if (water) { + water.resume(); + // console.log("缁х画浠跨湡"); + } else { + console.warn("No water simulation to resume."); + } +} + +/** + * 璺宠浆鍒版煇涓椂闂寸偣鐨勬按闈㈢姸鎬� + * @param {number} closestIndex - 鐩爣鏃堕棿鎴崇储寮� + */ +export function setTimeForWaterSimulation(closestIndex) { + if (water) { + const imageList = water.getTimeList(); + + if (!imageList.length) { + 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}]` + // ); + 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."); + } +} +// ==================銆怱DK 鏂板鍔熻兘 - 绠ご娴佸悜 & 闃村奖鎺у埗銆�================== + +/** + * 寮�鍚�/鍏抽棴绠ご娴佸悜鍔ㄧ敾 + * @param {boolean} enabled - 鏄惁鍚敤绠ご鍔ㄧ敾 + */ +export function enableWaterArrowFlow(enabled) { + if (water) { + // 榛樿鍏抽棴鐘舵�� + water.flowEnabled = enabled; // 鍋囪 SDK 鏀寔姝ゅ睘鎬� + // console.log(`绠ご娴佸悜鍔ㄧ敾宸�${enabled ? "寮�鍚�" : "鍏抽棴"}`); + } else { + console.warn("鏈壘鍒版按浣撴ā鎷熷浘灞傦紝璇峰厛鍚姩娲按妯℃嫙"); + } +} + +/** + * 寮�鍚�/鍏抽棴鍏ㄥ眬闃村奖鏁堟灉 + * @param {boolean} enabled - 鏄惁鍚敤闃村奖 + */ +export function toggleWaterShadow(enabled) { + if (!viewer) { + console.warn("Cesium Viewer 鏈垵濮嬪寲锛屾棤娉曡缃槾褰�"); + return; + } + + try { + earthCtrl.shadows = enabled; + if (enabled) { + earthCtrl.shadowMap.maximumDistance = 10000.0; //鏈�澶ц窛绂� + earthCtrl.shadowMap.pointLightRadius = 50.0; //鐐瑰厜婧愬崐寰� + } + // console.log(`闃村奖鏁堟灉宸�${enabled ? "寮�鍚�" : "鍏抽棴"}`); + } catch (error) { + console.error("璁剧疆闃村奖澶辫触:", error); + } +} + +// ================================================== +/** + * 鏃堕棿鎴冲洖璋冨嚱鏁� + * @param {number} timeStamp - 褰撳墠鏃堕棿鎴� + */ +function timeCallback(timeStamp) { + // console.log(`Current timestamp: ${timeStamp}`); } -- Gitblit v1.9.3