Skip to content

Commit

Permalink
fix incorrect position on backdrop shader
Browse files Browse the repository at this point in the history
  • Loading branch information
KurtGokhan committed Oct 6, 2024
1 parent c52c9c8 commit dca1d1f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
20 changes: 12 additions & 8 deletions Assets/Shaders/BackdropFilter.shader
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Shader "ReactUnity/BackdropFilter"
#pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF
#pragma shader_feature_local _GLOSSYREFLECTIONS_OFF
#pragma fragmentoption ARB_precision_hint_fastest
#define GRAB_POS
#include "UnityCG.cginc"
#include "ShaderSetup.cginc"

Expand All @@ -70,11 +71,11 @@ Shader "ReactUnity/BackdropFilter"
float4 _GrabTexture_TexelSize;

float4 frag( v2f i ) : COLOR {
if(_Blur == 0) return tex2D(_GrabTexture, i.uv);
if(_Blur == 0) return float4(0,0,0,0);

float3 sum = float3(0,0,0);

#define GRABPIXEL(weight,kernelx) tex2D( _GrabTexture, UNITY_PROJ_COORD(float2(i.uv.x + _GrabTexture_TexelSize.x * kernelx*_Blur, i.uv.y))) * weight
#define GRABPIXEL(weight,kernelx) tex2D( _GrabTexture, UNITY_PROJ_COORD(float2(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx*_Blur, i.uvgrab.y))) * weight

sum += GRABPIXEL(0.05, -4.0);
sum += GRABPIXEL(0.09, -3.0);
Expand All @@ -101,6 +102,7 @@ Shader "ReactUnity/BackdropFilter"
#pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF
#pragma shader_feature_local _GLOSSYREFLECTIONS_OFF
#pragma fragmentoption ARB_precision_hint_fastest
#define GRAB_POS
#include "UnityCG.cginc"
#include "ShaderSetup.cginc"

Expand All @@ -109,11 +111,11 @@ Shader "ReactUnity/BackdropFilter"
float4 _GrabTexture_TexelSize;

float4 frag( v2f i ) : COLOR {
if(_Blur == 0) return tex2D(_GrabTexture, i.uv);
if(_Blur == 0) return float4(0,0,0,0);

float3 sum = float3(0,0,0);

#define GRABPIXEL(weight,kernely) tex2D( _GrabTexture, UNITY_PROJ_COORD(float2(i.uv.x, i.uv.y + _GrabTexture_TexelSize.y * kernely*_Blur))) * weight
#define GRABPIXEL(weight,kernely) tex2D( _GrabTexture, UNITY_PROJ_COORD(float2(i.uvgrab.x, i.uvgrab.y + _GrabTexture_TexelSize.y * kernely*_Blur))) * weight

sum += GRABPIXEL(0.05, -4.0);
sum += GRABPIXEL(0.09, -3.0);
Expand Down Expand Up @@ -141,6 +143,7 @@ Shader "ReactUnity/BackdropFilter"
#pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF
#pragma shader_feature_local _GLOSSYREFLECTIONS_OFF
#pragma fragmentoption ARB_precision_hint_fastest
#define GRAB_POS
#include "UnityCG.cginc"
#include "UnityUI.cginc"
#include "ShaderSetup.cginc"
Expand Down Expand Up @@ -197,17 +200,18 @@ Shader "ReactUnity/BackdropFilter"

float4 frag(v2f i) : SV_Target
{
// Grab the texture from behind the current object
float3 color = tex2D(_GrabTexture, i.uv).rgb;
float2 uvgrab = i.uvgrab;

// Apply pixelate effect
if (_Pixelate > 0)
{
float4 ts = _GrabTexture_TexelSize * _Pixelate;
float2 uv = round(i.uv / ts.xy) * ts.xy;
color = tex2D(_GrabTexture, uv).rgb;
uvgrab = round(i.uvgrab / ts.xy) * ts.xy;
}

// Grab the texture from behind the current object
float3 color = tex2D(_GrabTexture, uvgrab).rgb;

// Convert to grayscale if needed
if (_Grayscale > 0)
{
Expand Down
7 changes: 7 additions & 0 deletions Assets/Shaders/ShaderSetup.cginc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ struct v2f {
float4 vertex : SV_POSITION;
float4 color : COLOR;
float4 worldPosition : TEXCOORD1;
#ifdef GRAB_POS
float2 uvgrab : TEXCOORD2;
#endif
UNITY_VERTEX_INPUT_INSTANCE_ID
UNITY_VERTEX_OUTPUT_STEREO
};
Expand All @@ -28,6 +31,10 @@ v2f vert (appdata v) {
o.vertex = UnityObjectToClipPos(v.vertex);
// o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.uv = v.uv;
#ifdef GRAB_POS
o.uvgrab = ComputeGrabScreenPos(o.vertex);
#endif

// o.uv1 = v.uv1;
// o.uv2 = v.uv2;
o.color = v.color;
Expand Down

0 comments on commit dca1d1f

Please sign in to comment.