using UnityEngine; using UnityEngine.UI; public class HeatMap : MonoBehaviour { public int width = 256; public int height = 256; public RawImage heatMapDisplay; // 用于显示热力图的UI元素 public float diffusionRate = 0.1f; // 热扩散系数 private Texture2D heatMapTexture; private float[,] heatValues; private float[,] heatValuesBuffer; void Start() { heatMapTexture = new Texture2D(width, height); heatValues = new float[width, height]; heatValuesBuffer = new float[width, height]; heatMapDisplay.texture = heatMapTexture; InitializeHeatSources(); UpdateHeatMapTexture(); } void Update() { SimulateHeatDiffusion(); UpdateHeatMapTexture(); } void InitializeHeatSources() { // 示例:在指定位置设置热源 SetHeatSource(width / 2, height / 2, 1.0f); // 可以根据模块的位置和热量设置多个热源 } void SetHeatSource(int x, int y, float heat) { if (x >= 0 && x < width && y >= 0 && y < height) { heatValues[x, y] = heat; } } void SimulateHeatDiffusion() { for (int x = 1; x < width - 1; x++) { for (int y = 1; y < height - 1; y++) { float currentHeat = heatValues[x, y]; float surroundingHeat = heatValues[x + 1, y] + heatValues[x - 1, y] + heatValues[x, y + 1] + heatValues[x, y - 1]; float newHeat = currentHeat + diffusionRate * (surroundingHeat - 4 * currentHeat); heatValuesBuffer[x, y] = Mathf.Clamp01(newHeat); } } // 交换缓冲区 (heatValues, heatValuesBuffer) = (heatValuesBuffer, heatValues); } void UpdateHeatMapTexture() { for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { float heatValue = heatValues[x, y]; Color color = GetColorFromHeatValue(heatValue); heatMapTexture.SetPixel(x, y, color); } } heatMapTexture.Apply(); } Color GetColorFromHeatValue(float value) { // 使用渐变颜色映射热量值 return Color.Lerp(Color.blue, Color.red, value); } }