Hi,
I’m trying to use a shader to render a repeating texture in a screen-space way.
See the screenshot for what I’m trying to achieve.
But as you can see, there is some distortion, the pixelly texture is being bent.
This is using the standard Unity cube model.
- Is there a way I can fix this?
- What am I doing wrong?
The shader I am using, for reference:
Shader "Ben/ScreenSpaceBasic" {
Properties {
_MainTex ("Texture", 2D) = "white" { }
_Color ("Main Color", Color) = (1,1,1,0.5)
}
SubShader {
Tags { "RenderType" = "Opaque" }
Cull Off
Lighting Off
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
uniform float4 _MainTex_ST;
uniform float4 _Color;
struct VertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float2 uv_screen : TEXCOORD1;
};
v2f vert(VertexInput v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
float4 clipSpace = o.pos;
clipSpace.xy /= clipSpace.w;
clipSpace.xy = 0.5*(clipSpace.xy+1.0);
o.uv_screen = clipSpace.xy;
o.uv_screen *= _MainTex_ST.xy + _MainTex_ST.zw;
return o;
}
half4 frag(v2f i) : COLOR {
half4 texcol = tex2D(_MainTex, i.uv_screen);
return texcol * _Color;
}
ENDCG
}
}
Fallback "VertexLit"
}