I repurposed this water sim shader to do diffusion, aka box blur but the shape is diagonal and I want it uniform. What changes do I need to make?
Shader "Influence/Diffision"
{
Properties
{
_Atten("Attenuation", Range(0.0, 1.0)) = 0.999
}
CGINCLUDE
#include "UnityCustomRenderTexture.cginc"
half _Atten;
float4 frag(v2f_customrendertexture i) : SV_Target
{
float2 uv = i.globalTexcoord;
float du = 1.0 / _CustomRenderTextureWidth;
float dv = 1.0 / _CustomRenderTextureHeight;
float3 duv = float3(du, dv, 0) * _DeltaUV;
float2 c = tex2D(_SelfTexture2D, uv);
c *= tex2D(_SelfTexture2D, uv).a; // pre zeroing out with Obstacle
float2 p = 2*c +
tex2D(_SelfTexture2D, uv - duv.xy) +
tex2D(_SelfTexture2D, uv + duv.xy) +
tex2D(_SelfTexture2D, uv - duv.xz) +
tex2D(_SelfTexture2D, uv + duv.xz) +
tex2D(_SelfTexture2D, uv - duv.x) +
tex2D(_SelfTexture2D, uv + duv.x) +
tex2D(_SelfTexture2D, uv - duv.y) +
tex2D(_SelfTexture2D, uv + duv.y);
p*= .1 * _Atten;
p *= tex2D(_SelfTexture2D, uv).a; // obstacles zero out the color
return float4( p, 0, 1);
}
float4 frag_negative(v2f_customrendertexture i) : SV_Target
{
return float4(-1, 0, 0, 1);
}
float4 frag_positive(v2f_customrendertexture i) : SV_Target
{
return float4(1, 0, 0, 1);
}
float4 frag_obstacle(v2f_customrendertexture i) : SV_Target
{
return float4(0, 0, 0, 0);
}
ENDCG
SubShader
{
Cull Off ZWrite Off ZTest Always
Pass
{
Name "Update"
CGPROGRAM
#pragma vertex CustomRenderTextureVertexShader
#pragma fragment frag
ENDCG
}
Pass
{
Name "Negative"
CGPROGRAM
#pragma vertex CustomRenderTextureVertexShader
#pragma fragment frag_negative
ENDCG
}
Pass
{
Name "Positive"
CGPROGRAM
#pragma vertex CustomRenderTextureVertexShader
#pragma fragment frag_positive
ENDCG
}
Pass
{
Name "Obstacle"
CGPROGRAM
#pragma vertex CustomRenderTextureVertexShader
#pragma fragment frag_obstacle
ENDCG
}
}
}
jvo3dc
January 12, 2018, 12:31pm
2
float3 duv = float3(du, dv, 0) * _DeltaUV;
Might actually be intended as:
float4 duv = float4(du, dv, -dv, 0.0) * _DeltaUV.xyyy;
And then also this:
tex2D(_SelfTexture2D, uv - duv.x) +
tex2D(_SelfTexture2D, uv + duv.x) +
tex2D(_SelfTexture2D, uv - duv.y) +
tex2D(_SelfTexture2D, uv + duv.y);
Should be:
tex2D(_SelfTexture2D, uv - duv.xw) +
tex2D(_SelfTexture2D, uv + duv.xw) +
tex2D(_SelfTexture2D, uv - duv.wy) +
tex2D(_SelfTexture2D, uv + duv.wy);
That last part is probably the main cause for it being diagonal. There are more readable ways to solve it, but this is nice and cryptic
I changed it to that (why is it duv.xw instead of duv.x?) and now it keeps crashing, I use this shader on a material which is used to update a Custom Render Texture, are Custom Render Texture prone to crashitis?
ok I fixed it and halved the number of texture read. And it doesn’t crash anymore (why? who cares, it works, be happy)
Shader "Influence"
{
Properties
{
_Atten("Attenuation", Range(0.0, 1.0)) = 0.999
}
CGINCLUDE
#include "UnityCustomRenderTexture.cginc"
half _Atten;
float4 frag(v2f_customrendertexture i) : SV_Target
{
float2 uv = i.globalTexcoord;
float du = 1.0 / _CustomRenderTextureWidth;
float dv = 1.0 / _CustomRenderTextureHeight;
float3 duv = float3(du, dv, 0);
float c = tex2D(_SelfTexture2D, uv).r;
c *= tex2D(_SelfTexture2D, uv).a; // pre zeroing out with Obstacle
float p = c
+ tex2D(_SelfTexture2D, uv - duv.zy).r
+ tex2D(_SelfTexture2D, uv + duv.zy).r
+ tex2D(_SelfTexture2D, uv - duv.xz).r
+ tex2D(_SelfTexture2D, uv + duv.xz).r
;
p*= .2 * _Atten;
p *= tex2D(_SelfTexture2D, uv).a; // removing the color bleed if it's an obstaclecolor
return float4( p,0, 0, 1);
}
float4 frag_negative(v2f_customrendertexture i) : SV_Target
{
return float4(-1, 0, 0, 1);
}
float4 frag_positive(v2f_customrendertexture i) : SV_Target
{
return float4(1, 0, 0, 1);
}
float4 frag_obstacle(v2f_customrendertexture i) : SV_Target
{
return float4(0, 0, 0, 0);
}
ENDCG
SubShader
{
Cull Off ZWrite Off ZTest Always
Pass
{
Name "Update"
CGPROGRAM
#pragma vertex CustomRenderTextureVertexShader
#pragma fragment frag
ENDCG
}
Pass
{
Name "Negative"
CGPROGRAM
#pragma vertex CustomRenderTextureVertexShader
#pragma fragment frag_negative
ENDCG
}
Pass
{
Name "Positive"
CGPROGRAM
#pragma vertex CustomRenderTextureVertexShader
#pragma fragment frag_positive
ENDCG
}
Pass
{
Name "Obstacle"
CGPROGRAM
#pragma vertex CustomRenderTextureVertexShader
#pragma fragment frag_obstacle
ENDCG
}
}
}
jvo3dc
January 15, 2018, 9:51am
5
Because you only want to add the x component to the x component and keep the y component the same. If you add duv.x to a two component float, it will add x to both components. That’s why I put 0 in w and then added duv.xw. (And .wy in a similar way.)