Skip to content

Commit dca1d1f

Browse files
committed
fix incorrect position on backdrop shader
1 parent c52c9c8 commit dca1d1f

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

Assets/Shaders/BackdropFilter.shader

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Shader "ReactUnity/BackdropFilter"
6262
#pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF
6363
#pragma shader_feature_local _GLOSSYREFLECTIONS_OFF
6464
#pragma fragmentoption ARB_precision_hint_fastest
65+
#define GRAB_POS
6566
#include "UnityCG.cginc"
6667
#include "ShaderSetup.cginc"
6768

@@ -70,11 +71,11 @@ Shader "ReactUnity/BackdropFilter"
7071
float4 _GrabTexture_TexelSize;
7172

7273
float4 frag( v2f i ) : COLOR {
73-
if(_Blur == 0) return tex2D(_GrabTexture, i.uv);
74+
if(_Blur == 0) return float4(0,0,0,0);
7475

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

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

7980
sum += GRABPIXEL(0.05, -4.0);
8081
sum += GRABPIXEL(0.09, -3.0);
@@ -101,6 +102,7 @@ Shader "ReactUnity/BackdropFilter"
101102
#pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF
102103
#pragma shader_feature_local _GLOSSYREFLECTIONS_OFF
103104
#pragma fragmentoption ARB_precision_hint_fastest
105+
#define GRAB_POS
104106
#include "UnityCG.cginc"
105107
#include "ShaderSetup.cginc"
106108

@@ -109,11 +111,11 @@ Shader "ReactUnity/BackdropFilter"
109111
float4 _GrabTexture_TexelSize;
110112

111113
float4 frag( v2f i ) : COLOR {
112-
if(_Blur == 0) return tex2D(_GrabTexture, i.uv);
114+
if(_Blur == 0) return float4(0,0,0,0);
113115

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

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

118120
sum += GRABPIXEL(0.05, -4.0);
119121
sum += GRABPIXEL(0.09, -3.0);
@@ -141,6 +143,7 @@ Shader "ReactUnity/BackdropFilter"
141143
#pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF
142144
#pragma shader_feature_local _GLOSSYREFLECTIONS_OFF
143145
#pragma fragmentoption ARB_precision_hint_fastest
146+
#define GRAB_POS
144147
#include "UnityCG.cginc"
145148
#include "UnityUI.cginc"
146149
#include "ShaderSetup.cginc"
@@ -197,17 +200,18 @@ Shader "ReactUnity/BackdropFilter"
197200

198201
float4 frag(v2f i) : SV_Target
199202
{
200-
// Grab the texture from behind the current object
201-
float3 color = tex2D(_GrabTexture, i.uv).rgb;
203+
float2 uvgrab = i.uvgrab;
202204

203205
// Apply pixelate effect
204206
if (_Pixelate > 0)
205207
{
206208
float4 ts = _GrabTexture_TexelSize * _Pixelate;
207-
float2 uv = round(i.uv / ts.xy) * ts.xy;
208-
color = tex2D(_GrabTexture, uv).rgb;
209+
uvgrab = round(i.uvgrab / ts.xy) * ts.xy;
209210
}
210211

212+
// Grab the texture from behind the current object
213+
float3 color = tex2D(_GrabTexture, uvgrab).rgb;
214+
211215
// Convert to grayscale if needed
212216
if (_Grayscale > 0)
213217
{

Assets/Shaders/ShaderSetup.cginc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ struct v2f {
1414
float4 vertex : SV_POSITION;
1515
float4 color : COLOR;
1616
float4 worldPosition : TEXCOORD1;
17+
#ifdef GRAB_POS
18+
float2 uvgrab : TEXCOORD2;
19+
#endif
1720
UNITY_VERTEX_INPUT_INSTANCE_ID
1821
UNITY_VERTEX_OUTPUT_STEREO
1922
};
@@ -28,6 +31,10 @@ v2f vert (appdata v) {
2831
o.vertex = UnityObjectToClipPos(v.vertex);
2932
// o.uv = TRANSFORM_TEX(v.uv, _MainTex);
3033
o.uv = v.uv;
34+
#ifdef GRAB_POS
35+
o.uvgrab = ComputeGrabScreenPos(o.vertex);
36+
#endif
37+
3138
// o.uv1 = v.uv1;
3239
// o.uv2 = v.uv2;
3340
o.color = v.color;

0 commit comments

Comments
 (0)