Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Tiling Shader

Discussion in 'Shaders' started by ivkoni, Mar 16, 2011.

  1. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    for all your current and future tiling needs. (taken from nvidia's website)
    Code (csharp):
    1.  
    2. Shader "Tiles" {
    3.     Properties {
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.         _gNumTiles ("Number Of Tiles", Range (1,100)) = 1
    6.         _gThreshhold ("Edge Width", Range (0.0,2.0)) = 0.15
    7.         _gEdgeColor ("Color", Color)  = (.5, .5, .5, 1)
    8.     }
    9.     SubShader {
    10.         Tags { "RenderType"="Opaque" }
    11.         LOD 200
    12.        
    13.         CGPROGRAM
    14.         #pragma surface surf Lambert
    15.  
    16.         sampler2D _MainTex;
    17.  
    18.         struct Input {
    19.             float2 uv_MainTex;
    20.         };
    21.        
    22.         float _gNumTiles;
    23.         float _gThreshhold;
    24.         float4 _gEdgeColor;
    25.        
    26.     void surf (Input IN, inout SurfaceOutput o) {
    27.            
    28.         half size = 1.0/_gNumTiles;
    29.         half2 Pbase = IN.uv_MainTex - fmod(IN.uv_MainTex,size.xx);
    30.         half2 PCenter = Pbase + (size/2.0).xx;
    31.         half2 st = (IN.uv_MainTex - Pbase)/size;
    32.         half4 c1 = (half4)0;
    33.         half4 c2 = (half4)0;
    34.         half4 invOff = half4((1-_gEdgeColor.xyz),1);
    35.         if (st.x > st.y) { c1 = invOff; }
    36.         half threshholdB =  1.0 - _gThreshhold;
    37.         if (st.x > threshholdB) { c2 = c1; }
    38.         if (st.y > threshholdB) { c2 = c1; }
    39.         half4 cBottom = c2;
    40.         c1 = (half4)0;
    41.         c2 = (half4)0;
    42.         if (st.x > st.y) { c1 = invOff; }
    43.         if (st.x < _gThreshhold) { c2 = c1; }
    44.         if (st.y < _gThreshhold) { c2 = c1; }
    45.         half4 cTop = c2;
    46.         half4 tileColor = tex2D(_MainTex,PCenter);
    47.         half4 result = tileColor + cTop - cBottom;
    48.         o.Albedo = result.rgb;
    49.         o.Alpha = result.a;
    50.            
    51.         }
    52.         ENDCG
    53.     }
    54.     FallBack "Diffuse"
    55. }
    56.