/************************************************** FileName:ObjectPool Date:19/12/5 Version:0.1 Function List:热力图颜色赋值 **************************************************** ****************************************************/ using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class heatControl : MonoBehaviour { List heatPointValue = new List(); //所有顶点对应的颜色的值 List heatPointVec = new List();//所有顶点 MeshFilter meshFilter; public void mapBigen(List heatMapDate, float High, GameObject self) { ReadMash(self); //得到所有的顶点 Inject(HeatPointValue(), heatMapDate, High); } public void Inject(List temperature, List LayerDate, float High) { // 算法添加高温点 for (int i = 0; i < LayerDate.Count; i++) { RandomTeamperatureone(LayerDate[i].People, LayerDate[i].Range, new Vector3(LayerDate[i].Xpos, 0, LayerDate[i].Zpos), ref temperature); } Generate(0.1f); Genereat(0.1f, LayerDate[0].Range, new Vector3(LayerDate[0].Xpos, 0, LayerDate[0].Zpos)); // // 赋值颜色 AddVertexColor(meshFilter); } public void CreateMesh() { } //得到所有的顶点 void ReadMash(GameObject self) { meshFilter = self.transform.GetComponent(); int Length = meshFilter.mesh.vertices.Length; for (int i = 0; i < Length; i++) { heatPointVec.Add(meshFilter.mesh.vertices[i]); //Debug.Log(heatPointVec[i]); } } // 初始化算法 private List HeatPointValue() { heatPointValue.Clear(); for (int i = 0; i < heatPointVec.Count; i++) { // 假定正常人数为40人低于40人均为正常温度 heatPointValue.Add(40); } return heatPointValue; } /// /// 得到点属性数据 /// /// 最大人数 /// 影响范围 /// x位置 /// 点属性数据 private void RandomTeamperatureone(float MaxPeopleNum, int range, Vector3 pos, ref List 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 (MaxPeopleNum > 160) { temp = ratio * MaxPeopleNum*1.5f ; //1.1f是基数,调整美观的 } if (temp > MaxPeopleNum) { temp = MaxPeopleNum; } // 只有比当前点人数高才选择覆盖 if (temp > heatPointValue[num]) heatPointValue[num] = temp; } } //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 * 1.5f && distance > range) // { // float ratio = 1 - (distance / range);//ratio等于0到1 // float temp = ratio * MaxPeopleNum; // // 只有比当前点人数高才选择覆盖 // if (temp > heatPointValue[num]) // heatPointValue[num] = temp; // } // else if (distance < range) // { // float ratio = 1 - (distance / range);//ratio等于0到1 // if (ratio < 0.7f) // { // ratio = 0.7f; // } // float temp = ratio * MaxPeopleNum; // // 只有比当前点人数高才选择覆盖 // if (temp > heatPointValue[num]) // heatPointValue[num] = temp; // } //} } /// /// 增加颜色数据 /// /// private void AddVertexColor(MeshFilter meshFilter) { Color[] colors = new Color[heatPointValue.Count]; for (int i = 0; i < heatPointValue.Count; i++) { colors[i] = CalcColor(heatPointValue[i]); } meshFilter.mesh.colors = colors; } Color CalcColor(float temperature) { Color colorOne = new Color(); if (temperature <=40) { //colorOne = Color.Lerp(new Color(1, 0, 1, 0), new Color(0, 1, 1, 0), (temperature) * 0.025f);//无 colorOne = Color.Lerp(new Color(0, 1, 1, 0f), new Color(0, 1, 0), (temperature - 30) * 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 < 100) { // Debug.Log((temperature - 80) * 0.025f); colorOne = Color.Lerp(new Color(0, 1, 0), new Color(1, 1, 0), (temperature - 80) * 0.025f); //绿 } else if (temperature >= 100 && 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; } /// /// 生成mesh /// /// 基本高度 public void Generate(float high) { Vector3[] vertices = new Vector3[heatPointVec.Count]; for (int i = 0; i < heatPointVec.Count; i++) { vertices[i] = new Vector3(heatPointVec[i].x, heatPointValue[i] *high, heatPointVec[i].z); } meshFilter.mesh.vertices = vertices; } public void Genereat(float high, float range ,Vector3 pos) { Vector3[] vertices = new Vector3[heatPointVec.Count]; for (int i = 0; i < heatPointVec.Count; i++) { vertices[i] = new Vector3(heatPointVec[i].x, heatPointValue[i] * high, heatPointVec[i].z); float distance = Mathf.Sqrt(Mathf.Pow(Vector3.Distance(heatPointVec[i], pos), 2)); //算距离 if (heatPointValue[i]>40&& distance<= range) { // 生成球形的原理,是求出这个点在球体上的高度。 //range 球的半径(斜边) distance 是平面上点到圆心的距离(其中一条直角边) 求出高(另一条直角边) float y = Mathf.Sqrt((Mathf.Pow(range, 2) - Mathf.Pow(distance, 2))); vertices[i] = new Vector3(heatPointVec[i].x, y, heatPointVec[i].z); } } meshFilter.mesh.vertices = vertices; } }