Avoid looping in shader
Hi everyone,
I am creating a heatmap for my application, I am simply visualizing it over a world map such as this example here
I've used a shader applied on a quad as big as my camera frustum to visualize it and I was able to do it.
The issue is that for this shader to work it needs to loop every single pixel of the quad for many times as all the points I want to visualize, which is actually a big bottleneck, since it can be like 100x100x300 loops, this obviously weighs too hard on the app performance. Does anyone know how to avoid this behaviour?
Down here I'm posting my shader code, to pass the points I'm simply using things like Material.SetVectorArray() etc
struct vertInput
{
float4 pos : POSITION;
};
struct vertOutput
{
float4 pos : POSITION;
fixed3 worldPos : TEXCOORD1;
};
vertOutput vert(vertInput input)
{
vertOutput o;
o.pos = UnityObjectToClipPos(input.pos);
o.worldPos = mul(unity_ObjectToWorld, input.pos).xyz;
return o;
}
half4 frag(vertOutput output) : COLOR
{
half h = 0;
for (int i = 0; i < _Points_Length; i ++)
{
half dist = distance(output.worldPos, _Points[i].xyz);//_Points[i].xyz is the point I'm passing to my shader
half radi = _Properties[i].x; //this is the radius of the area around the actual point
half hi = 1 - saturate(dist / radi);
h += hi * _Properties[i].y; //Properties[i].y is just an intensity modifier
}
h = saturate(h);
half4 color = tex2D(_HeatTex, fixed2(h, 0.5));
return color;
}
Your answer
Follow this Question
Related Questions
Making a pixelation shader to change resolutions in different areas of the screen 0 Answers
How do you check the next pixels color? 2 Answers
How can I have a uniform struct in a Shader? 0 Answers
Is there a way to fetch albedo/spec/roughness/AO and normals (RTs) in a deferred frag. shader? 0 Answers
Fragment shader shows alpha, surface shader doesn't 0 Answers