Unity custom shaders and z-fighting

Posted by Heisenbug on Game Development See other posts from Game Development or by Heisenbug
Published on 2012-09-11T09:57:49Z Indexed on 2012/09/11 15:51 UTC
Read the original article Hit count: 436

Filed under:
|
|

I've just readed a chapter of Unity iOS Essential by Robert Wiebe.

It shows a solution for handling z-figthing problem occuring while rendering a street on a plane with the same y offset.

Basically it modified Normal-Diffuse shader provided by Unity, specifing the (texture?) offset in -1, -1.

Here's basically what the shader looks like:

Shader "Custom/ModifiedNormalDiffuse" {
    Properties {
        _Color ("Main Color", Color) = (1,1,1,1)
        _MainTex ("Base (RGB)", 2D) = "white" {}
    }
    SubShader {
        Offset -1,-1   //THIS IS THE ADDED LINE
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        #pragma surface surf Lambert

        sampler2D _MainTex;
        fixed4 _Color;

        struct Input {
            float2 uv_MainTex;
        };

        void surf (Input IN, inout SurfaceOutput o) {
            half4 c = tex2D (_MainTex, IN.uv_MainTex) *_Color;
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }
        ENDCG
    } 
    FallBack "Diffuse"
}

Ok. That's simple and it works. The author says about it:

...we could use a copy of the shader that draw the road at an Offset of -1, -1 so that whenever the two textures are drawn, the road is always drawn last.

I don't know CG nor GLSL, but I've a little bit of experience with HLSL. Anyway I can't figure out what exactly is going on.

Could anyone explain me what exactly Offset directly does, and how is solves z-fighting problems?

© Game Development or respective owner

Related posts about unity

Related posts about shaders