I’ve got a simple lambert shader and I have to modify it to work with atlases (for texture and normal maps), yet have it looking exactly the same. I did it and my atlas shader compiles and works, but these jaggies appeared around the edges of the blocks (I’m making a voxel based environment). Also, I’m not using any kind of antialiasing.
This shows the two shaders in action and the problem:
Pasted bellow is the code to my two shaders, I can’t find a clue on how to solve this so any contribution is much appreciated.
This is the original (simple lambert) shader:
Shader "Custom/TextureWithNormals"
{
Properties{
_Color("Color", Color) = (1,1,1,1)
_MainTex("Texture", 2D) = "white" {}
_BumpMap("Bumpmap", 2D) = "bump" {}
}
SubShader{
Tags { "RenderType" = "Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf WrapLambert
#pragma target 3.0
half4 LightingWrapLambert(SurfaceOutput s, half3 lightDir, half atten)
{
half NdotL = dot(s.Normal, lightDir);
half diff = NdotL * 0.5 + 0.5;
half4 c;
c.rgb = s.Albedo * _LightColor0.rgb * (diff * atten);
c.a = s.Alpha;
return c;
}
struct Input
{
float2 uv_MainTex;
float2 uv_BumpMap;
};
sampler2D _MainTex;
sampler2D _BumpMap;
fixed4 _Color;
void surf(Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
o.Alpha = c.a;
}
ENDCG
}
Fallback "Diffuse"
}
This is the atlas shader, based on the first one. AtlasX, AtlasY and AtlasRec are set by a script at Awake.
EDIT: Line 58 and 59 altered according to bgolus suggestion, jagged lines are now less noticiable but still present.
Shader "Custom/TexNormAtlas"
{
Properties{
_Color("Color", Color) = (1,1,1,1)
_MainTex("Texture", 2D) = "white" {}
_BumpMap("Bumpmap", 2D) = "bump" {}
}
SubShader
{
Tags { "RenderType" = "Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf WrapLambert
#pragma vertex vert
#pragma target 3.0
sampler2D _MainTex;
sampler2D _BumpMap;
fixed4 _Color;
half4 LightingWrapLambert(SurfaceOutput s, half3 lightDir, half atten)
{
half NdotL = dot(s.Normal, lightDir);
half diff = NdotL * 0.5 + 0.5;
half4 c;
c.rgb = s.Albedo * _LightColor0.rgb * (diff * atten);
c.a = s.Alpha;
return c;
}
struct Input
{
float3 position;
float4 custom_uv;
};
int _AtlasX;
int _AtlasY;
fixed4 _AtlasRec;
void vert (inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input, o);
o.custom_uv = v.texcoord;
o.position = v.vertex;
}
void surf(Input IN, inout SurfaceOutput o) {
fixed2 atlasOffset = IN.custom_uv.zw;
fixed2 scaledUV = IN.custom_uv.xy;
fixed2 atlasUV = scaledUV;
atlasUV.x = (atlasOffset.x * _AtlasRec.x) + frac(atlasUV.x) * _AtlasRec.x;
atlasUV.y = (((_AtlasY - 1) - atlasOffset.y) * _AtlasRec.y) + frac(atlasUV.y) * _AtlasRec.y;
fixed4 c = tex2Dgrad(_MainTex, atlasUV, ddx(scaledUV * _AtlasRec), ddy(scaledUV * _AtlasRec)) * _Color;
o.Normal = UnpackNormal(tex2Dgrad(_BumpMap, atlasUV, ddx(scaledUV * _AtlasRec), ddy(scaledUV * _AtlasRec)));
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
Fallback "Diffuse"
}