123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- /**************************************************
- 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<float> heatPointValue = new List<float>(); //所有顶点对应的颜色的值
- List<Vector3> heatPointVec = new List<Vector3>();//所有顶点
- MeshFilter meshFilter;
- public void mapBigen(List<HeatMapDate> heatMapDate, float High, GameObject self)
- {
- ReadMash(self); //得到所有的顶点
- Inject(HeatPointValue(), heatMapDate, High);
- }
- public void Inject(List<float> temperature, List<HeatMapDate> 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<MeshFilter>();
- 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<float> HeatPointValue()
- {
- heatPointValue.Clear();
- for (int i = 0; i < heatPointVec.Count; i++)
- {
- // 假定正常人数为40人低于40人均为正常温度
- heatPointValue.Add(40);
- }
- return heatPointValue;
- }
- /// <summary>
- /// 得到点属性数据
- /// </summary>
- /// <param name="MaxPeopleNum">最大人数</param>
- /// <param name="range">影响范围</param>
- /// <param name="pox">x位置</param>
- /// <param name="temperatures">点属性数据</param>
- private void RandomTeamperatureone(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 (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;
- // }
- //}
- }
- /// <summary>
- /// 增加颜色数据
- /// </summary>
- /// <param name="meshFilter"></param>
- 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;
- }
- /// <summary>
- /// 生成mesh
- /// </summary>
- /// <param name="high">基本高度</param>
- 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;
- }
- }
|