Unity 生成可控可视化三维热力图

Unity 生成可控可视化三维热力图

最终生成效果

带高度的热力图

1.生成相应的Mesh

vertices = new Vector3[(xSize + 1) * (ySize + 1)];
for (int i = 0, y = 0; y <= ySize; y++)
{
     for (int x = 0; x <= xSize; x++, i++)
       {
                vertices[i] = new Vector3(x,0,y);
       }
}
mesh.vertices = vertices;
int[] triangles = new int[xSize * ySize * 6];
for (int ti = 0, vi = 0, y = 0; y < ySize; y++, vi++)
{
    for (int x = 0; x < xSize; x++, ti += 6, vi++)
    {
           triangles[ti] = vi;
           triangles[ti + 3] = triangles[ti + 2] = vi + 1;
           triangles[ti + 4] = triangles[ti + 1] = vi + xSize + 1;
           triangles[ti + 5] = vi + xSize + 2;
    }
}

2.构建所有顶点所对应的数据值

/// <param name="MaxPeopleNum">最大人数</param>
/// <param name="range">影响范围</param>
/// <param name="pox">x位置</param>
/// <param name="temperatures">点属性数据</param>
 private void SetPonitValue(float MaxPeopleNum, int range, Vector3 pos, ref List<float> temperatures)
 {
      for (int i = 0; i < temperatures.Count; i++)
      {
           int num = i;
           float distance = Mathf.Sqrt(Mathf.Pow(Vector3.Distance(heatPointVec[i], pos), 2)); //算距离
           if (distance <= range)
           {
                float ratio = 1 - (distance / range);//ratio等于0到1
                float temp = ratio * MaxPeopleNum;
                // 只有比当前点人数高才选择覆盖
                if (temp > heatPointValue[num])
                    heatPointValue[num] = temp;
           }
      
      }
 }

3.取相应的颜色值

Color SetColor(float temperature)
 {
        Color colorOne = new Color();
        if (temperature <=40)
        {
           colorOne = Color.Lerp(new Color(1, 0, 1, 0), new Color(0, 1, 1, 0f), (temperature) * 0.025f);//无
        }
       else if (temperature >40 && temperature < 80) 
       {
           colorOne = Color.Lerp(new Color(0, 1, 1, 0f), new Color(0, 1, 0), (temperature - 40) * 0.025f);//蓝=>绿
       }
       else if (temperature >= 80 && temperature < 120)
       {
           colorOne = Color.Lerp(new Color(0, 1, 0), new Color(1, 1, 0), (temperature - 80) * 0.025f); //绿=>黄
       }
       else if (temperature >= 120 && temperature < 160)
       {
            colorOne = Color.Lerp(new Color(1f, 1f, 0f), new Color(1f, 0f, 0f), (temperature - 120) * 0.025f); //黄=>红
       }
        else
        {
            colorOne = new Color(1f, 0f, 0f, 1f); //红
        }
        return colorOne;
}

里面颜色和具体范围数值可以根据自己项目需要自行修改。

具体知识讨论和源码需求可以关注私聊哦~

猜你喜欢

转载自blog.csdn.net/s4158303581/article/details/106860646