今天更新一个大众功能的 shader 热力图,相信很多小伙伴会有这个需求,先来个效果图
包含了二维热力图和三维热力图,背景图片混合,热力透明度等。
微信号:ITComputerGraph
更多精彩内容,关注公众号《IT木子李》
热力图的思路:先获取热力点,然后有热点的影响半径,根据半径可算出周围象素的热图情况
v2f vert(a2v input)
{
v2f o;
half3 worldPos = mul(unity_ObjectToWorld,input.pos).xyz;
half heat = 0;
for( int i = 0 ; i < _FactorCount;i++ )
{
half dis = distance(worldPos, _Factors[i].xyz);
float radius = _FactorsProperties.x;
float intensity = _FactorsProperties.y;
half ratio = 1 - saturate(dis / radius);
heat += intensity * ratio;
}
o.pos = UnityObjectToClipPos(input.pos + half3(0,heat*_Height,0));
o.worldPos = mul(unity_ObjectToWorld,input.pos).xyz;
o.uv = input.uv * _MainTex_ST.xy + _MainTex_ST.zw;
return o;
}
fixed4 frag(v2f input):COLOR
{
half heat = 0;
for( int i = 0 ; i < _FactorCount;i++ )
{
half dis = distance(input.worldPos, _Factors[i].xyz);
float radius = _FactorsProperties.x;
float intensity = _FactorsProperties.y;
half ratio = 1 - saturate(dis / radius);
heat += intensity * ratio;
heat = clamp(heat, 0.05, 0.95);
}
half4 color = tex2D(_HeatMapTex, fixed2(heat, 0.5));
half aplha = (heat - 0.05) / 0.9;
color.a = pow( aplha * _Alpha, _AlphaStep);
half4 tex = tex2D(_MainTex, input.uv);
if (_TexVisible == 0)
{
return color;
}
else
{
return fixed4(color.rgb * color.a + tex.rgb * (1 - color.a), tex.a);
}
}
以上是顶点着色器和片元着色器代码。