unity 温度云图

image.png

image.png

借鉴:https://blog.csdn.net/x100568/article/details/51219023
https://zhuanlan.zhihu.com/p/158039963

我是个shader新手,门都没入那种,项目需要必须要搞个温度云图。我项目的需求是:我这个是固定点位产生温度变化。

这个shader是通过UV计算的。所以第一步,是需要把世界坐标转换成uv坐标。额,本人对shader一敲不通,只能用一个笨办法。发射射线返回一个uv坐标。

shader的话可以通过上面的地址复制一份,但注意需要自己手动修改一番

C# 代码

  public Transform plane;
    public Material currentMaterial;
    [Header("热力图点位")]
    public List pointsList = new List();
    [Header("温度")]
    public List temperaturesList = new List();
    public List pointsVector2List = new List();


    private void Start()
    {
        currentMaterial = plane.GetComponent().material;
        for (int i = 0; i < pointsList.Count; i++)
        {
            temperaturesList.Add(Random.Range(22,26));
        }
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            SetHeatmap();
        }
        if (Input.GetKeyDown(KeyCode.A))
        {
            pointsVector2List.Clear();
            currentMaterial.SetInt("_Points_Length", 0);
        }
    }

  
    RaycastHit hit;
    private void SetHeatmap()
    {
        pointsVector2List.Clear();
        for (int i = 0; i < pointsList.Count; i++)
        {
            int index = i;
            Vector3 down = pointsList[index].TransformDirection(Vector3.up);
            if (Physics.Linecast(pointsList[index].position+ down*5, pointsList[index].position - down * 5, out hit))
            {
                Vector3 tempPos = hit.textureCoord;
                pointsVector2List.Add(tempPos);
                Debug.Log(":" + tempPos);
            }
        }
        currentMaterial.SetFloatArray("_temperatures", temperaturesList);
        if(pointsVector2List.Count>0)
        currentMaterial.SetVectorArray("_Points", pointsVector2List.ToArray());
        currentMaterial.SetInt("_Points_Length", pointsList.Count);  //设置数量
    }

    private void OnDrawGizmos()
    {
        Gizmos.color = Color.red;

        for (int i = 0; i < pointsList.Count; i++)
        {
            int index = i;
            Vector3 down = pointsList[index].TransformDirection(Vector3.up);

            Gizmos.DrawLine(pointsList[index].position + down * 5, pointsList[index].position - down * 5);
        }
    }

你可能感兴趣的:(unity 温度云图)