146 lines
4.3 KiB
C#
146 lines
4.3 KiB
C#
//using System.Drawing;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using Yarn.Unity;
|
|
|
|
public class HeatMapController : MonoBehaviour
|
|
{
|
|
public Material heatMaterial; // 需要传递数据的材质
|
|
|
|
public Transform[]Pos;
|
|
|
|
[HideInInspector]
|
|
private Vector4[] points = new Vector4[7]; // 存储6个点的数组,(x, y, z) = center, w = temperature
|
|
|
|
[HideInInspector]
|
|
private Vector4[] properties = new Vector4[7]; // 存储6个盒子大小的数组, (x,y) = boxsize
|
|
|
|
public Slider slider;
|
|
private float tempNum=0;
|
|
|
|
public float tempNumMax;
|
|
private float uftempture=1;
|
|
private float backtemture=1;
|
|
|
|
private float modulesTempture=1;
|
|
|
|
public float uf=1;
|
|
public float back=1;
|
|
|
|
public float module=0.7f;
|
|
//public Texture2D hotspotShapeTexture; // 引用热点形状纹理
|
|
|
|
|
|
private float tempture=1;
|
|
|
|
public Slider xSlider; // 拖入X轴控制的Slider
|
|
public Slider ySlider; // 拖入Y轴控制的Slider
|
|
|
|
private float xMin = -4.5f;
|
|
private float xMax = 2.1f;
|
|
private float yMin = -3.3f;
|
|
private float yMax = 2.9f;
|
|
|
|
public RectTransform targetObject; // 拖入要控制的物体的Transform
|
|
|
|
void Start()
|
|
{
|
|
xSlider.minValue = 1;
|
|
xSlider.maxValue = 10;
|
|
ySlider.minValue = 1;
|
|
ySlider.maxValue = 10;
|
|
|
|
// 注册Slider值改变事件
|
|
xSlider.onValueChanged.AddListener(UpdatePosition);
|
|
ySlider.onValueChanged.AddListener(UpdatePosition);
|
|
|
|
// 初始调用,设置初始位置
|
|
UpdatePosition(0);
|
|
//tempture=1;
|
|
//tempture=tempture;
|
|
// 初始化数据,这里是示例,你可以根据实际需求设置
|
|
SetDataToMaterial();
|
|
|
|
}
|
|
private void UpdatePosition(float value)
|
|
{
|
|
// 使用InverseLerp将Slider的值映射到0到1范围,然后再使用Lerp进行插值
|
|
float xPos = Mathf.Lerp(xMin, xMax, Mathf.InverseLerp(1, 10, xSlider.value));
|
|
float yPos = Mathf.Lerp(yMin, yMax, Mathf.InverseLerp(1, 10, ySlider.value));
|
|
|
|
// 更新物体位置
|
|
targetObject.position = new Vector3(xPos, yPos, targetObject.position.z);
|
|
}
|
|
|
|
public void SetTemture(float value)
|
|
{
|
|
tempture=value;
|
|
|
|
}
|
|
public void SetDataToMaterial()
|
|
{
|
|
|
|
// uftempture=tempture;
|
|
// uftempture=Map(uftempture,0,3,1.5f,2.6f);
|
|
|
|
// backtemture=tempture;
|
|
// backtemture=Map(backtemture,0,3,1.5f,3.6f);
|
|
|
|
// modulesTempture=tempture;
|
|
// modulesTempture=Map(modulesTempture,0,3f,1.5f,2.3f);
|
|
|
|
if (tempture <= 1)
|
|
{
|
|
uftempture = Mathf.Lerp(1.5f, 2.4f, tempture ); // 线性插值
|
|
modulesTempture = Mathf.Lerp(1.5f, 2.1f, tempture)-tempNum;
|
|
backtemture = Mathf.Lerp(1.5f, 2.1f, tempture);
|
|
}
|
|
else if (tempture <= 2)
|
|
{
|
|
uftempture = Mathf.Lerp(2.4f, 2.7f, tempture - 1);
|
|
modulesTempture = Mathf.Lerp(2.1f, 2.3f, tempture - 1);
|
|
backtemture = Mathf.Lerp(2.1f, 3.6f, tempture - 1);
|
|
}
|
|
else
|
|
{
|
|
uftempture =2.7f;
|
|
modulesTempture = 2.6f;
|
|
backtemture = 3.8f;
|
|
}
|
|
|
|
|
|
for (int i = 0; i < 7; i++)
|
|
{
|
|
RectTransform transform=Pos[i].GetComponent<RectTransform>();
|
|
|
|
if(i==0)
|
|
{
|
|
points[i] = new Vector4(Pos[i].position.x, Pos[i].position.y, uftempture, 0);
|
|
|
|
}
|
|
else if(i==6)
|
|
{
|
|
points[i] = new Vector4(Pos[i].position.x, Pos[i].position.y, backtemture, 0);
|
|
}
|
|
else
|
|
{
|
|
points[i] = new Vector4(Pos[i].position.x, Pos[i].position.y, modulesTempture, 0); // (x, y, z, temperature)
|
|
|
|
}
|
|
|
|
|
|
properties[i] = new Vector4(transform.rect.width*1, transform.rect.height*1, 0.0f, 0.0f); // (boxsize x, boxsize y, unused, unused)
|
|
}
|
|
|
|
// 将数据传递给 Shader
|
|
heatMaterial.SetVectorArray("_Points", points);
|
|
heatMaterial.SetVectorArray("_Properties", properties);
|
|
|
|
}
|
|
float Map(float value, float minSource, float maxSource, float minTarget, float maxTarget)
|
|
{
|
|
return minTarget + (value - minSource) * (maxTarget - minTarget) / (maxSource - minSource);
|
|
}
|
|
}
|
|
|