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

How to create a shader with a geometrical pattern?

Discussion in 'Shaders' started by fariazz, May 27, 2017.

  1. fariazz

    fariazz

    Joined:
    Nov 21, 2016
    Posts:
    55
    I'm looking to create a shader to paint the grass/rocks on a low-poly world, where I can specify a few colors, and paint the the floor (or walls, etc) with a geometrical pattern like in the image below.

    Any ideas or recommended resources or good starting point? I'm just starting to learn Shaderlab and CG and I've no idea where to begin for something like this.

    Thanks :)

     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
  3. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    972
    The possibilities seem unlimited, I can just give two examples of shaders based on the Surface Standard shader for a start.
    Example 1:
    pat1.png
    Code 1:
    Code (CSharp):
    1. Shader "Custom/SurfacePat1" {
    2.     Properties {
    3.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    4.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    5.         _Metallic ("Metallic", Range(0,1)) = 0.0
    6.  
    7.         _PatternCentre ("Pattern Centre", Vector) = (0,0,0,0)
    8.         _Plane1 ("Plane1", Vector) = (1,0,0,0)
    9.         _Plane2 ("Plane2", Vector) = (-0.5,0,0.866,0)
    10.         _Plane3 ("Plane3", Vector) = (-0.5,0,-0.866,0)
    11.         _Plane4 ("Plane4", Vector) = (0,-1,0,0)
    12.  
    13.         _TileSize ("TileSize", Range(1,10)) = 5.0
    14.         _n ("n", Range(1,5)) = 4.0
    15.     }
    16.     SubShader {
    17.         Tags { "RenderType"="Opaque" }
    18.         LOD 200
    19.      
    20.         CGPROGRAM
    21.         // Physically based Standard lighting model, and enable shadows on all light types
    22.         #pragma surface surf Standard fullforwardshadows
    23.  
    24.         // Use shader model 3.0 target, to get nicer looking lighting
    25.         #pragma target 3.0
    26.  
    27.         sampler2D _MainTex;
    28.  
    29.         struct Input {
    30.             float2 uv_MainTex;
    31.             float3 worldPos;
    32.         };
    33.  
    34.         half _Glossiness;
    35.         half _Metallic;
    36.         fixed4 _Color;
    37.         uniform float3 _PatternCentre;
    38.         uniform float3 _Plane1;
    39.         uniform float3 _Plane2;
    40.         uniform float3 _Plane3;
    41.         uniform float3 _Plane4;
    42.         float _TileSize;
    43.         float _n;
    44.  
    45.         UNITY_INSTANCING_CBUFFER_START(Props)
    46.             // put more per-instance properties here
    47.         UNITY_INSTANCING_CBUFFER_END
    48.  
    49.         void surf (Input IN, inout SurfaceOutputStandard o) {
    50.  
    51.             float dist1 =  - dot((IN.worldPos - _PatternCentre),_Plane1);
    52.             float dist2 =  - dot((IN.worldPos - _PatternCentre),_Plane2);
    53.             float dist3 =  - dot((IN.worldPos - _PatternCentre),_Plane3);
    54.             float dist4 =  - dot((IN.worldPos - _PatternCentre),_Plane4);
    55.  
    56.             fixed4 _Color = (1,1,1,1);
    57.             _Color.r = frac(dist1/_TileSize) - frac(_n*dist1/_TileSize)/_n;
    58.             _Color.g = frac(dist2/_TileSize) - frac(_n*dist2/_TileSize)/_n;
    59.             _Color.b = frac(dist3/_TileSize) - frac(_n*dist3/_TileSize)/_n;
    60.  
    61.             _Color += (frac(dist4/6) - frac(4*dist4/6)/4)*(0,1,0,0.3);
    62.  
    63.  
    64.             // Albedo comes from a texture tinted by color
    65.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    66.             o.Albedo = c.rgb;
    67.             // Metallic and smoothness come from slider variables
    68.             o.Metallic = _Metallic;
    69.             o.Smoothness = _Glossiness;
    70.             o.Alpha = c.a;
    71.         }
    72.         ENDCG
    73.     }
    74.     FallBack "Diffuse"
    75. }
    Example 2: pat2.png
    Code 2:
    Code (CSharp):
    1. Shader "Custom/SurfacePat2" {
    2.     Properties {
    3.         _Color0 ("Color0", Color) = (1,1,1,1)
    4.         _Color1 ("Color1", Color) = (1,1,1,1)
    5.         _Color2 ("Color2", Color) = (1,1,1,1)
    6.         _Color3 ("Color3", Color) = (1,1,1,1)
    7.         _Color4 ("Color4", Color) = (1,1,1,1)
    8.         _Color5 ("Color5", Color) = (1,1,1,1)
    9.         _Color6 ("Color6", Color) = (1,1,1,1)
    10.         _Color7 ("Color7", Color) = (1,1,1,1)
    11.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    12.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    13.         _Metallic ("Metallic", Range(0,1)) = 0.0
    14.  
    15.         _PatternCentre ("Pattern Centre", Vector) = (0,0,0,0)
    16.         _Plane1 ("Plane1", Vector) = (0.3333,0.9413,0,0)
    17.         _Plane2 ("Plane2", Vector) = (-0.1666,0.9413,0.2887,0)
    18.         _Plane3 ("Plane3", Vector) = (-0.1666,0.9413,-0.2887,0)
    19.  
    20.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    21.  
    22.         _TileSize ("TileSize", Range(1,10)) = 5.0
    23.     }
    24.     SubShader {
    25.         Tags { "RenderType"="Opaque" }
    26.         LOD 200
    27.      
    28.         CGPROGRAM
    29.         // Physically based Standard lighting model, and enable shadows on all light types
    30.         #pragma surface surf Standard fullforwardshadows
    31.  
    32.         // Use shader model 3.0 target, to get nicer looking lighting
    33.         #pragma target 3.0
    34.  
    35.         sampler2D _MainTex;
    36.  
    37.         struct Input {
    38.             float2 uv_MainTex;
    39.             float3 worldPos;
    40.         };
    41.  
    42.         half _Glossiness;
    43.         half _Metallic;
    44.         uniform float3 _PatternCentre;
    45.         uniform float3 _Plane1, _Plane2, _Plane3, _Plane4;
    46.         fixed _TileSize;
    47.  
    48.         fixed4 _Color0, _Color1, _Color2, _Color3, _Color4, _Color5,_Color6,_Color7;
    49.  
    50.         UNITY_INSTANCING_CBUFFER_START(Props)
    51.             // put more per-instance properties here
    52.         UNITY_INSTANCING_CBUFFER_END
    53.  
    54.         void surf (Input IN, inout SurfaceOutputStandard o) {
    55.  
    56.             float dist1 =  - dot((IN.worldPos - _PatternCentre),_Plane1);
    57.             float dist2 =  - dot((IN.worldPos - _PatternCentre),_Plane2);
    58.             float dist3 =  - dot((IN.worldPos - _PatternCentre),_Plane3);
    59.  
    60.             fixed4 _Color = (1,1,1,1);
    61.             fixed i = 8*(frac(dist1/_TileSize) - frac(2*dist1/_TileSize)/2) + 4*(frac(dist2/_TileSize) - frac(2*dist2/_TileSize)/2) + 2*(frac(dist3/_TileSize) - frac(2*dist3/_TileSize)/2);
    62.  
    63.             if(round(i)==0) _Color = _Color0;
    64.             if(round(i)==1) _Color = _Color1;
    65.             if(round(i)==2) _Color = _Color2;
    66.             if(round(i)==3) _Color = _Color3;
    67.             if(round(i)==4) _Color = _Color4;
    68.             if(round(i)==5) _Color = _Color5;
    69.             if(round(i)==6) _Color = _Color6;
    70.             if(round(i)==7) _Color = _Color7;
    71.  
    72.             // Albedo comes from a texture tinted by color
    73.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    74.             o.Albedo = c.rgb;
    75.             // Metallic and smoothness come from slider variables
    76.             o.Metallic = _Metallic;
    77.             o.Smoothness = _Glossiness;
    78.             o.Alpha = c.a;
    79.         }
    80.         ENDCG
    81.     }
    82.     FallBack "Diffuse"
    83. }
     
    larku and fariazz like this.
  4. fariazz

    fariazz

    Joined:
    Nov 21, 2016
    Posts:
    55
    Wow thanks so much! That is super helpful!