当前位置:网站首页>Unity 热力图建立方法
Unity 热力图建立方法
2022-06-24 13:00:00 【小生云木】
Unity 热力图建立方法
实现效果
基本思路
之前有写过用网格顶点赋值颜色生成热力图的方法。有一个很大的缺点,需要大量的网格点支持,提高了GPU的渲染压力。所以换了一个思路,直接创建并修改图片像素的颜色,这样只需要四个顶点的网格就可以承载热力图的生成。
计算前fbs→97.5

计算后fbs→96.5

基本不受影响。
解决问题及办法
世界坐标和像素坐标的转换;
像素的初始化及像素的加权值;
色块的颜色和影响范围关系的配置;
分辨率和性能的调节。
世界坐标和像素坐标的转换
这里有两个注意点,一是网格的轴心点,二是uv的坐标轴。网格的原点是模型美术规定的,但是在世界坐标转像素坐标时,需要将原点对应,所以要改变一下网格的原点。改变原点有两种方式,1.让美术修改,将网格的原点改成uv坐标系的原点;2.给网格加一个父物体,父物体的位置在网格的uv坐标原点。如下图所示


完成原点对齐后,自需要求出比例就可以。
Vector3 modelSize = gameObject.GetComponent<Collider>().bounds.size;
float MapRatio = heatMapBase.Resolution / modelSize.x;
//heatMapBase.Resolution是定义的图片像素分辨率。
//MapRatio是图片坐标和世界坐标转换的比例
像素的初始化及像素的加权值
运行之前要先把所以像素的权值全部归零。带权的参考点会对周围的像素点进行影响,影响的范围和自身的权值有关,这里还可以引入一个变量DisRatio,用于改变权值的影响范围。给像素赋权值的时,只能值覆盖小值。
//每个像素初始化
List<Vector2> my_Pixels = new List<Vector2>();
List<float> my_Values = new List<float>();
for (int y = 0; y < texture.height; y++)
{
for (int x = 0; x < texture.width; x++)
{
Vector2 pixel = new Vector2(x, y);
my_Pixels.Add(pixel);
my_Values.Add(0);
}
}
int allLength = my_Pixels.Count;//所以像素点数量
int pointLength = heatMapInfos.Count;//热力图兴趣点点数量
int colorLength = heatMapBase.HeatMapInfos.Count;//色块等级
//每个像素加权值
for (int p = 0; p < pointLength; p++)
{
for (int a = 0; a < allLength; a++)
{
float my_Distance = Vector2.Distance(heatMapInfos[p].Pixel* MapRatio, my_Pixels[a]);
float my_MaxDis = heatMapBase.DisRatio * heatMapInfos[p].Amount;
if (my_Distance < my_MaxDis)
{
float value = (1 - (Mathf.Pow(my_Distance, 2) / Mathf.Pow(my_MaxDis, 2))) * heatMapInfos[p].Amount;
if (value > my_Values[a])
{
my_Values[a] = value;
}
}
}
}
色块的颜色和影响范围关系的配置
色块的颜色选取,权值,图片的分辨率和影响范围是动态可以配置的。这里加一个模板用来控制。为了方便像素颜色的赋值,规定权值由大到小填入。

public class HeatMapBase : ScriptableObject
{
public float DisRatio;//距离比例
public float Resolution;//分辨率
public List<HeatMapInfo> HeatMapInfos;
[Serializable]
public class HeatMapInfo
{
public float MaxAmount;//最大权值
public Color Color;//颜色
}
}
分辨率和性能的调节
在编写之初,考虑动态改变分辨率,网格越大分辨率越大。但这个无疑是增加了性能的消耗。所以考虑将分辨率写入配置表。转而出现了图片大范围的锯齿如下图。后面引入了color的插值运算,完美解决问题。
不加插值

加插值

//每个像素赋值颜色
for (int i = 0; i < allLength; i++)
{
for (int j = 0; j < colorLength; j++)
{
float my_CurMaxAmount = heatMapBase.HeatMapInfos[j].MaxAmount;
if (my_Values[i] >= my_CurMaxAmount)
{
//当前块的颜色
Color my_CurColor = heatMapBase.HeatMapInfos[j].Color;
if (j != 0)
{
float my_UpDiffValue = heatMapBase.HeatMapInfos[j - 1].MaxAmount - my_CurMaxAmount;
Color my_UpColor = heatMapBase.HeatMapInfos[j - 1].Color;
float t = (my_Values[i] - my_CurMaxAmount) / my_UpDiffValue;
texture.SetPixel((int)my_Pixels[i].x, (int)my_Pixels[i].y, Color.Lerp(my_CurColor, my_UpColor, t));
break;
}
else
{
texture.SetPixel((int)my_Pixels[i].x, (int)my_Pixels[i].y, my_CurColor);
break;
}
}
}
}
边栏推荐
- **Unity中莫名其妙得小问题-灯光和天空盒
- [sdx62] wcn685x IPA failure analysis and solution
- 初识云原生安全:云时代的最佳保障
- 杰理之串口接收 IO 需要设置数字功能【篇】
- Daily question 8-515 Find the maximum value in each tree row
- Jericho After sleep, the system will wake up regularly and continue to run without resetting [chapter]
- Eight major trends in the industrial Internet of things (iiot)
- 2022煤矿瓦斯抽采操作证考试题及模拟考试
- Huawei PC grows against the trend, and product power determines everything
- Can a team do both projects and products?
猜你喜欢

Autorf: learn the radiation field of 3D objects from single view (CVPR 2022)

Research and development practice of Kwai real-time data warehouse support system

#21Set经典案例

杰理之TIMER0 用默认的 PA13 来检测脉宽【篇】

图扑软件数字孪生海上风电 | 向海图强,奋楫争先

Vulnerability management mistakes that CIOs still make

HarmonyOS. two

Google waymo proposed r4d: remote distance estimation using reference target
![NPM package [details] (including NPM package development, release, installation, update, search, uninstall, view, version number update rules, package.json details, etc.)](/img/b0/85ac6274b239e42c9543fa296df456.png)
NPM package [details] (including NPM package development, release, installation, update, search, uninstall, view, version number update rules, package.json details, etc.)

常识知识点
随机推荐
HarmonyOS. two
In the era of knowledge economy, it will teach you to do well in knowledge management
发扬连续作战优良作风 全力以赴确保北江大堤安全
杰理之增加一个输入捕捉通道【篇】
源碼解析 Handler 面試寶典
【5G NR】5G NR系统架构
[5g NR] 5g NR system architecture
kotlin 组合挂起函数
2022 recurrent training question bank and answers for hoisting signal Rigger (special type of construction work)
华为 PC 逆势增长,产品力决定一切
杰理之在所有模式下打开喊话增加 mic 自动 mute【篇】
unity 等高线创建方法
Operation of simulated test question bank and simulated test platform for safety production management personnel of fireworks and firecrackers production units in 2022
redis 数据类型详解
These default routes and static routes can not be configured and deployed. What kind of network workers are they!
2022 construction elevator driver (construction special type of work) examination questions and online simulation examination
详解kubernetes备份恢复利器 Velero | 深入了解Carina系列第三期
4 reasons for "safe left shift"
快手实时数仓保障体系研发实践
Docker installation redis
