- Home /
Texture coordinates in shader are negative
I was expecting that texture coordinates in a surface shader would be in the range [0, 1]. However I get negative values. Why is that? The following shader I would expect an output of all green, however it outputs some red on edges. I am new to shaders in Unity, so please let me know if I can provide any more information.
Shader "Custom/MyShader" {
Properties {
<...>
_Image("Image", 2D) = "white" {}
<...>
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Standard fullforwardshadows
#pragma target 3.0
sampler2D _Image;
struct Input {
float2 uv_Image;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutputStandard o) {
fixed4 c = float4(0, 1, 0, 1);
if (IN.uv_Image.x < 0 || IN.uv_Image.y < 0) {
c = float4(1, 0, 0, 1);
}
o.Albedo = c.rgb;
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
Answer by Buckslice · Feb 06, 2018 at 12:46 AM
This is due to Anti Aliasing. If you go to Edit->Project Settings->Quality then disable AA you should no longer be able to see red pixels on the left and bottom edges.
I am not exactly sure of the details, but I think it has to do with the fact that Unity's MSAA smooths out visible pixelated edges by multisampling nearby pixels around them and averaging it, so it ends up sampling at points at the edge that are negative uv which your shader's if statement triggers and colors red.
That did not change things. $$anonymous$$ultisampling is a post fragment shader process, right?
Hmm, I set up your shader on a quad in a fresh project and that fixed it for me... What are your render settings? I think $$anonymous$$SAA is the only anti aliasing solution Unity offers that isn't image-based (performed on the final image like a post processing effect). Heres a article explaining the details.