Search Unity

"Checkerboard" diffuse transparency.

Discussion in 'Shaders' started by darkhog, May 29, 2015.

  1. darkhog

    darkhog

    Joined:
    Dec 4, 2012
    Posts:
    2,218
    I'm trying to achieve oldschool looking, checkerboard-style transparency. However I'm a complete noob when it comes to writing shaders and all I've achieved is to make object not render at all.

    Here's mockup made in blender of what I'm trying to achieve (put a sphere in the background to show that those darker areas are transparent):


    I've started from Unity's legacy diffuse transparent shader, trying to enhance it to do this, but failed miserably. Here's the shader code:

    Code (csharp):
    1. Shader "Custom/Transparent/Checkers" {
    2. Properties {
    3.    _Color ("Main Color", Color) = (1,1,1,1)
    4.    _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    5. }
    6.  
    7. SubShader {
    8.    Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    9.    LOD 200
    10.  
    11. CGPROGRAM
    12. #pragma surface surf Lambert alpha:blend
    13.  
    14. sampler2D _MainTex;
    15. fixed4 _Color;
    16.  
    17. struct Input {
    18.    float2 uv_MainTex;
    19. };
    20.  
    21. void surf (Input IN, inout SurfaceOutput o) {
    22.    fixed4 alcol = _Color;
    23.    alcol.a=0;
    24.    fixed4 c;
    25.    if ((fmod(IN.uv_MainTex.x,0.1f)!=0)&&(fmod(IN.uv_MainTex.y,0.1f)!=1)){
    26.      c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    27.    } else {
    28.      c = tex2D(_MainTex, IN.uv_MainTex) * alcol;
    29.    }
    30.    o.Albedo = c.rgb;
    31.    o.Alpha = c.a;
    32. }
    33. ENDCG
    34. }
    35.  
    36. Fallback "Legacy Shaders/Transparent/VertexLit"
    37. }
    Also it should be independent from model's UV, so squares would be always of the same size and scale won't affect them (it'll just show more squares).

    Any help on that?
     
    Last edited: May 30, 2015
  2. darkhog

    darkhog

    Joined:
    Dec 4, 2012
    Posts:
    2,218
    Any help?
     
  3. echo4papa

    echo4papa

    Joined:
    Mar 26, 2015
    Posts:
    158
    You can use the world space position of your certs to give you texcoords. The downside would be that the texture changes as the object moves.