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

Surface Shader - Cellular Procedural Texture (full source code)

Discussion in 'Shaders' started by Przemyslaw_Zaworski, Feb 12, 2019.

  1. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    327
    Basic framework for procedural textures with surface shaders.

    Code (CSharp):
    1. Shader "Cellular"
    2. {
    3.     Properties
    4.     {
    5.         [HideInInspector] _texcoord( "", 2D ) = "white" {}
    6.     }
    7.     Subshader
    8.     {
    9.         Tags { "RenderType" = "Opaque" }
    10.         CGPROGRAM
    11.         #pragma surface SurfaceShader Standard fullforwardshadows addshadow
    12.  
    13.         struct Input
    14.         {
    15.             float2 uv_texcoord;
    16.         };
    17.  
    18.         float shape( float2 p )
    19.         {
    20.             return dot(frac(p)-0.5, frac(p)-0.5);  
    21.         }
    22.  
    23.         float cell( float2 p )
    24.         {
    25.             float c = 0.5;
    26.             c = min(c, shape(p - float2(0.80, 0.61)));
    27.             c = min(c, shape(p - float2(0.36, 0.20)));
    28.             c = min(c, shape(p - float2(0.60, 0.24)));
    29.             c = min(c, shape(p - float2(0.18, 0.82)));
    30.             p *= 1.4142;    
    31.             c = min(c, shape(p - float2(0.45, 0.30)));
    32.             c = min(c, shape(p - float2(0.04, 0.88)));
    33.             c = min(c, shape(p - float2(0.06, 0.54)));
    34.             c = min(c, shape(p - float2(0.64, 0.12)));    
    35.             return sqrt(c*5.);
    36.         }
    37.  
    38.         void SurfaceShader (Input IN, inout SurfaceOutputStandard o)
    39.         {
    40.             float2 uv = float2(2.0 * IN.uv_texcoord.xy - 1.0);
    41.             o.Albedo = float4(cell(uv).xxx + (cell(uv*5.)) * 0.05, 1.0);
    42.             o.Normal = float3(0.0,0.0,1.0);
    43.             o.Metallic = 0.0;
    44.             o.Smoothness = 0.0;
    45.         }
    46.  
    47.         ENDCG
    48.     }
    49. }
    Example with baked lighting:
    upload_2019-2-12_16-28-23.png
     
    bgolus likes this.