Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Any idea how to add normal map and specular map to this custom shader?

Discussion in 'Shaders' started by the_Bad_Brad, May 25, 2018.

  1. the_Bad_Brad

    the_Bad_Brad

    Joined:
    Nov 2, 2014
    Posts:
    278
    I found this online at Unity Answers. I want to add normal and specular maps to each 4 textures.
    This shader mixes 4 textures using red, green, blue and black color maps.
    Code (CSharp):
    1. Shader "todds/Transparent 4Textures" {
    2.      Properties {
    3.         // _Color ("Overall Color", Color) = (1,0.5,0.5,1)
    4.      
    5.          _ac ("alpha cutoff for textures", Range(.0,1)) = 0
    6.          _acm ("alpha cutoff for map", Range(.0,1)) = 0
    7.          _t1 ("texture in black", 2D) = "white" {}
    8.          _tint1 ("Tint1", Color) = (1.0, 1, 1, 0)
    9.      
    10.          _t2 ("texture in red", 2D) = "white" {}
    11.          _tint2 ("Tint2", Color) = (1.0, 1, 1, 0)
    12.          _rc ("amplify red", Range(1,80)) = 0.0
    13.      
    14.          _t3 ("texture in blue", 2D) = "white" {}
    15.          _tint3 ("Tint3", Color) = (1.0,1,1,0)
    16.          _rb ("amplify blue", Range(1,80)) = 0.0
    17.      
    18.          _t5 ("texture in green", 2D) = "white" {}
    19.          _tint5 ("Tint4", Color) = (1.0,1,1,0)
    20.          _rg ("amplify green", Range(1,80)) = 0.0
    21.      
    22.          _t4 ("texture map", 2D) = "white" {}
    23.      
    24.          num ("Number of textures used", Int) = 5
    25.      
    26.      }
    27.      SubShader {
    28.  
    29.          Tags { "Queue"="Transparent" "RenderType"="Transparent" }
    30.          LOD 200
    31.            // extra pass that renders to depth buffer only
    32.     Pass {
    33.         ZWrite On
    34.         ColorMask 0
    35.     }
    36.          CGPROGRAM
    37.               #pragma surface surf Lambert alpha
    38.          int num;
    39.          float _rc;
    40.          float _rb;float _rg;
    41.          float _ac;
    42.          float _acm;
    43.          sampler2D _t1;
    44.          sampler2D _t2;
    45.          sampler2D _t3;
    46.          sampler2D _t4;
    47.           sampler2D _t5;
    48.          fixed4 _tint1;
    49.          fixed4 _tint2;
    50.          fixed4 _tint3;
    51.           fixed4 _tint5;
    52.          struct Input {
    53.          
    54.              float2 uv_t1;
    55.              float2 uv_t2;
    56.              float2 uv_t3;
    57.              float2 uv_t4;
    58.              float2 uv_t5;
    59.          };
    60.    
    61.          void surf (Input IN, inout SurfaceOutput o) {
    62.          
    63.              float f;float f2;
    64.        
    65.              half4 pix2;
    66.              half4 pix = tex2D (_t1, IN.uv_t1)*_tint1;
    67.                    pix.a=tex2D (_t1, IN.uv_t1).a;
    68.              half4 map = tex2D (_t4, IN.uv_t4);
    69.              float alph;
    70.          
    71.                  if(num>1){
    72.                  alph=tex2D (_t2, IN.uv_t2).a;
    73.                  if(alph>_ac){
    74.              
    75.                  f=map.r;
    76.                  if(f>0){
    77.                  f=f*_rc;
    78.              
    79.                  if(f>1){f=1;}
    80.          
    81.                  f*=alph;
    82.                  f2=1-f;
    83.                  pix=pix*f2;
    84.              
    85.                  pix=pix+tex2D (_t2, IN.uv_t2)*f*_tint2;
    86.                  pix.a+=tex2D (_t2, IN.uv_t2).a*f;
    87.                  }}
    88.              
    89.              
    90.                   if(num>2){
    91.                  alph=tex2D (_t3, IN.uv_t3).a;
    92.                  if(alph>_ac){
    93.                  f=map.b;
    94.                  if(f>0){
    95.                  f=f*_rb;
    96.                  if(f>1){f=1;}
    97.                  f*=alph;
    98.                  f2=1-f;
    99.                  pix=pix*f2;
    100.              
    101.                  pix=pix+tex2D (_t3, IN.uv_t3)*f*_tint3;}
    102.                  pix.a+=tex2D (_t3, IN.uv_t3).a*f;
    103.                  }
    104.                  if(num>3){
    105.                  alph=tex2D (_t5, IN.uv_t5).a;
    106.                  if(alph>_ac){
    107.                  f=map.g;
    108.                  if(f>0){
    109.                  f=f*_rg;
    110.                  if(f>1){f=1;}
    111.                  f*=alph;
    112.                  f2=1-f;
    113.                  pix=pix*f2;
    114.              
    115.                  pix=pix+tex2D (_t5, IN.uv_t5)*f*_tint5;}
    116.                  pix.a+=tex2D (_t5, IN.uv_t5).a*f;
    117.                  }
    118.              
    119.              
    120.                    }}
    121.              
    122.                  }
    123.          
    124.              o.Albedo = pix.rgb;
    125.              f=pix.a;
    126.              if(map.a<_acm){map.a=0;}
    127.              if(map.a<pix.a){f=map.a;}
    128.          
    129.              o.Alpha = f;
    130.          
    131.          }
    132.          ENDCG
    133.      }
    134.      FallBack "Diffuse"
    135.  
    136. }
     
    rf007 likes this.
  2. Seyed_Morteza_Kamaly

    Seyed_Morteza_Kamaly

    Joined:
    Nov 18, 2015
    Posts:
    80
    https://docs.unity3d.com/Manual/SL-SurfaceShaderExamples.html

    Code (CSharp):
    1.  Shader "Example/Diffuse Bump" {
    2.     Properties {
    3.       _MainTex ("Texture", 2D) = "white" {}
    4.       _BumpMap ("Bumpmap", 2D) = "bump" {}
    5.     }
    6.     SubShader {
    7.       Tags { "RenderType" = "Opaque" }
    8.       CGPROGRAM
    9.       #pragma surface surf Lambert
    10.       struct Input {
    11.         float2 uv_MainTex;
    12.         float2 uv_BumpMap;
    13.       };
    14.       sampler2D _MainTex;
    15.       sampler2D _BumpMap;
    16.       void surf (Input IN, inout SurfaceOutput o) {
    17.         o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
    18.         o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
    19.       }
    20.       ENDCG
    21.     }
    22.     Fallback "Diffuse"
    23.   }
    Specular & Normal

    Code (CSharp):
    1. Shader "Custom/Normal Gloss Mapped Specular"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Main Color", Color) = (1, 1, 1, 1)
    6.         _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    7.         _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
    8.         _Gloss ("Gloss", Range (0.0, 2.0)) = 1
    9.         _MainTex ("Base Texture", 2D) = "white" {}
    10.         _BumpMap ("Normal Map", 2D) = "bump" {}
    11.         _GlossMap ("Gloss Map", 2D) = "gloss" {}
    12.     }
    13.     SubShader
    14.     {
    15.         Tags { "RenderType"="Opaque" }
    16.      
    17.         CGPROGRAM
    18.  
    19.         #pragma surface surf BlinnPhong
    20.  
    21.         fixed4 _Color;
    22.         half _Shininess;
    23.         half _Gloss;
    24.         sampler2D _MainTex;
    25.         sampler2D _BumpMap;
    26.         sampler2D _GlossMap;
    27.  
    28.         struct Input
    29.         {
    30.             float2 uv_MainTex;
    31.             float2 uv_BumpMap;
    32.             float2 uv_GlossMap;
    33.         };
    34.  
    35.         void surf (Input IN, inout SurfaceOutput o)
    36.         {
    37.             half4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    38.             o.Albedo = c.rgb;
    39.             o.Alpha = c.a;
    40.             o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
    41.             o.Gloss = tex2D (_GlossMap, IN.uv_GlossMap).a * _Gloss;
    42.             o.Specular = _Shininess;
    43.         }
    44.  
    45.         ENDCG
    46.     }
    47.     Fallback "Specular"
    48. }

    your shader is ready:
    Code (CSharp):
    1.     Shader "todds/Transparent 4Textures" {
    2.          Properties {
    3.             // _Color ("Overall Color", Color) = (1,0.5,0.5,1)
    4.        
    5.              _ac ("alpha cutoff for textures", Range(.0,1)) = 0
    6.              _acm ("alpha cutoff for map", Range(.0,1)) = 0
    7.              _t1 ("texture in black", 2D) = "white" {}
    8.              _tint1 ("Tint1", Color) = (1.0, 1, 1, 0)
    9.        
    10.              _t2 ("texture in red", 2D) = "white" {}
    11.              _tint2 ("Tint2", Color) = (1.0, 1, 1, 0)
    12.              _rc ("amplify red", Range(1,80)) = 0.0
    13.        
    14.              _t3 ("texture in blue", 2D) = "white" {}
    15.              _tint3 ("Tint3", Color) = (1.0,1,1,0)
    16.              _rb ("amplify blue", Range(1,80)) = 0.0
    17.        
    18.              _t5 ("texture in green", 2D) = "white" {}
    19.              _tint5 ("Tint4", Color) = (1.0,1,1,0)
    20.              _rg ("amplify green", Range(1,80)) = 0.0
    21.        
    22.              _t4 ("texture map", 2D) = "white" {}
    23.        
    24.              num ("Number of textures used", Int) = 5
    25.  
    26.  
    27.              _Color ("Main Color", Color) = (1, 1, 1, 1)
    28.             _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    29.             _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
    30.             _Gloss ("Gloss", Range (0.0, 2.0)) = 1
    31.             _MainTex ("Base Texture", 2D) = "white" {}
    32.             _BumpMap ("Normal Map", 2D) = "bump" {}
    33.             _GlossMap ("Gloss Map", 2D) = "gloss" {}
    34.        
    35.          }
    36.          SubShader {
    37.    
    38.              Tags { "Queue"="Transparent" "RenderType"="Transparent" }
    39.              LOD 200
    40.                // extra pass that renders to depth buffer only
    41.         Pass {
    42.             ZWrite On
    43.             ColorMask 0
    44.         }
    45.              CGPROGRAM
    46.                 #pragma target 4.0 //Too many texture interpolators would be used for ForwardBase pass (10 out of max 8), so I added  #pragma target 3.0
    47.  
    48.                  #pragma surface surf BlinnPhong
    49.         fixed4 _Color;
    50.         half _Shininess;
    51.         half _Gloss;
    52.         sampler2D _MainTex;
    53.         sampler2D _BumpMap;
    54.         sampler2D _GlossMap;
    55.              int num;
    56.              float _rc;
    57.              float _rb;float _rg;
    58.              float _ac;
    59.              float _acm;
    60.              sampler2D _t1;
    61.              sampler2D _t2;
    62.              sampler2D _t3;
    63.              sampler2D _t4;
    64.               sampler2D _t5;
    65.              fixed4 _tint1;
    66.              fixed4 _tint2;
    67.              fixed4 _tint3;
    68.               fixed4 _tint5;
    69.              struct Input {
    70.            
    71.                  float2 uv_t1;
    72.                  float2 uv_t2;
    73.                  float2 uv_t3;
    74.                  float2 uv_t4;
    75.                  float2 uv_t5;
    76.                  float2 uv_BumpMap;
    77.             float2 uv_GlossMap;
    78.              };
    79.      
    80.              void surf (Input IN, inout SurfaceOutput o) {
    81.            
    82.                  float f;float f2;
    83.          
    84.                  half4 pix2;
    85.                  half4 pix = tex2D (_t1, IN.uv_t1)*_tint1;
    86.                        pix.a=tex2D (_t1, IN.uv_t1).a;
    87.                  half4 map = tex2D (_t4, IN.uv_t4);
    88.                  float alph;
    89.            
    90.                      if(num>1){
    91.                      alph=tex2D (_t2, IN.uv_t2).a;
    92.                      if(alph>_ac){
    93.                
    94.                      f=map.r;
    95.                      if(f>0){
    96.                      f=f*_rc;
    97.                
    98.                      if(f>1){f=1;}
    99.            
    100.                      f*=alph;
    101.                      f2=1-f;
    102.                      pix=pix*f2;
    103.                
    104.                      pix=pix+tex2D (_t2, IN.uv_t2)*f*_tint2;
    105.                      pix.a+=tex2D (_t2, IN.uv_t2).a*f;
    106.                      }}
    107.                
    108.                
    109.                       if(num>2){
    110.                      alph=tex2D (_t3, IN.uv_t3).a;
    111.                      if(alph>_ac){
    112.                      f=map.b;
    113.                      if(f>0){
    114.                      f=f*_rb;
    115.                      if(f>1){f=1;}
    116.                      f*=alph;
    117.                      f2=1-f;
    118.                      pix=pix*f2;
    119.                
    120.                      pix=pix+tex2D (_t3, IN.uv_t3)*f*_tint3;}
    121.                      pix.a+=tex2D (_t3, IN.uv_t3).a*f;
    122.                      }
    123.                      if(num>3){
    124.                      alph=tex2D (_t5, IN.uv_t5).a;
    125.                      if(alph>_ac){
    126.                      f=map.g;
    127.                      if(f>0){
    128.                      f=f*_rg;
    129.                      if(f>1){f=1;}
    130.                      f*=alph;
    131.                      f2=1-f;
    132.                      pix=pix*f2;
    133.                
    134.                      pix=pix+tex2D (_t5, IN.uv_t5)*f*_tint5;}
    135.                      pix.a+=tex2D (_t5, IN.uv_t5).a*f;
    136.                      }
    137.                
    138.                
    139.                        }}
    140.                
    141.                      }
    142.            
    143.                  o.Albedo = pix.rgb;
    144.                  f=pix.a;
    145.                  if(map.a<_acm){map.a=0;}
    146.                  if(map.a<pix.a){f=map.a;}
    147.            
    148.                  o.Alpha = f;
    149.                   o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
    150.                 o.Gloss = tex2D (_GlossMap, IN.uv_GlossMap).a * _Gloss;
    151.                 o.Specular = _Shininess;
    152.            
    153.              }
    154.              ENDCG
    155.          }
    156.          FallBack "Specular"
    157.    
    158.     }
    159.  
     
    Last edited: May 26, 2018
    rf007 likes this.
  3. Hachemi_Hadjallah

    Hachemi_Hadjallah

    Joined:
    Aug 30, 2017
    Posts:
    2
    @Seyed_Morteza_Kamaly please what about this shader

    Code (CSharp):
    1. Shader "CrossSection/OnePlaneBSP" {
    2.     Properties{
    3.         _Color("Color", Color) = (1,1,1,1)
    4.         _CrossColor("Cross Section Color", Color) = (1,1,1,1)
    5.         _MainTex("Albedo (RGB)", 2D) = "white" {}
    6.         _Glossiness("Smoothness", Range(0,1)) = 0.5
    7.         _Metallic("Metallic", Range(0,1)) = 0.0
    8.         _PlaneNormal("PlaneNormal",Vector) = (0,1,0,0)
    9.         _PlanePosition("PlanePosition",Vector) = (0,0,0,1)
    10.         _StencilMask("Stencil Mask", Range(0, 255)) = 255
    11.     }
    12.     SubShader {
    13.         Tags { "RenderType"="Opaque" }
    14.         //LOD 200
    15.         Stencil
    16.         {
    17.             Ref [_StencilMask]
    18.             CompBack Always
    19.             PassBack Replace
    20.  
    21.             CompFront Always
    22.             PassFront Zero
    23.         }
    24.         Cull Back
    25.             CGPROGRAM
    26.             // Physically based Standard lighting model, and enable shadows on all light types
    27. #pragma surface surf Standard fullforwardshadows
    28.  
    29.             // Use shader model 3.0 target, to get nicer looking lighting
    30. #pragma target 3.0
    31.  
    32.             sampler2D _MainTex;
    33.  
    34.         struct Input {
    35.             float2 uv_MainTex;
    36.  
    37.             float3 worldPos;
    38.         };
    39.  
    40.         half _Glossiness;
    41.         half _Metallic;
    42.         fixed4 _Color;
    43.         fixed4 _CrossColor;
    44.         fixed3 _PlaneNormal;
    45.         fixed3 _PlanePosition;
    46.         bool checkVisability(fixed3 worldPos)
    47.         {
    48.             float dotProd1 = dot(worldPos - _PlanePosition, _PlaneNormal);
    49.             return dotProd1 > 0  ;
    50.         }
    51.         void surf(Input IN, inout SurfaceOutputStandard o) {
    52.             if (checkVisability(IN.worldPos))discard;
    53.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    54.             o.Albedo = c.rgb;
    55.             // Metallic and smoothness come from slider variables
    56.             o.Metallic = _Metallic;
    57.             o.Smoothness = _Glossiness;
    58.             o.Alpha = c.a;
    59.         }
    60.         ENDCG
    61.        
    62.             Cull Front
    63.             CGPROGRAM
    64. #pragma surface surf NoLighting  noambient
    65.  
    66.         struct Input {
    67.             half2 uv_MainTex;
    68.             float3 worldPos;
    69.  
    70.         };
    71.         sampler2D _MainTex;
    72.         fixed4 _Color;
    73.         fixed4 _CrossColor;
    74.         fixed3 _PlaneNormal;
    75.         fixed3 _PlanePosition;
    76.         bool checkVisability(fixed3 worldPos)
    77.         {
    78.             float dotProd1 = dot(worldPos - _PlanePosition, _PlaneNormal);
    79.             return dotProd1 >0 ;
    80.         }
    81.         fixed4 LightingNoLighting(SurfaceOutput s, fixed3 lightDir, fixed atten)
    82.         {
    83.             fixed4 c;
    84.             c.rgb = s.Albedo;
    85.             c.a = s.Alpha;
    86.             return c;
    87.         }
    88.  
    89.         void surf(Input IN, inout SurfaceOutput o)
    90.         {
    91.             if (checkVisability(IN.worldPos))discard;
    92.             o.Albedo = _CrossColor;
    93.  
    94.         }
    95.             ENDCG
    96.        
    97.     }
    98.     //FallBack "Diffuse"
    99. }
    100.  
     
  4. Seyed_Morteza_Kamaly

    Seyed_Morteza_Kamaly

    Joined:
    Nov 18, 2015
    Posts:
    80
    Code (CSharp):
    1.  
    2. Shader "CrossSection/OnePlaneBSP" {
    3. Properties{
    4.         _SpecularColor("SpecularColor",Color) = (1,0,0,1)
    5. _Color("Color", Color) = (1,1,1,1)
    6. _CrossColor("Cross Section Color", Color) = (1,1,1,1)
    7. _MainTex("Albedo (RGB)", 2D) = "white" {}
    8.         _BumpMap ("Bumpmap", 2D) = "bump" {}
    9.         _GlossMap ("Gloss Map", 2D) = "gloss" {}
    10. _Glossiness("Smoothness", Range(0,1)) = 0.5
    11. _Emission("Emission", Range(0,1)) = 0.0
    12. _PlaneNormal("PlaneNormal",Vector) = (0,1,0,0)
    13. _PlanePosition("PlanePosition",Vector) = (0,0,0,1)
    14. _StencilMask("Stencil Mask", Range(0, 255)) = 255
    15. }
    16. SubShader {
    17. Tags { "RenderType"="Opaque" }
    18. //LOD 200
    19. Stencil
    20. {
    21. Ref [_StencilMask]
    22. CompBack Always
    23. PassBack Replace
    24.  
    25. CompFront Always
    26. PassFront Zero
    27. }
    28. Cull Back
    29. CGPROGRAM
    30. // Physically based Standard lighting model, and enable shadows on all light types
    31. #pragma surface surf StandardSpecular fullforwardshadows
    32.  
    33. // Use shader model 3.0 target, to get nicer looking lighting
    34. #pragma target 3.0
    35.  
    36. sampler2D _MainTex,_BumpMap,_GlossMap;
    37.  
    38. struct Input {
    39. float2 uv_MainTex;
    40.  
    41. float3 worldPos;
    42.             float2 uv_BumpMap;
    43.             float2 uv_GlossMap;
    44.  
    45. };
    46.  
    47.         half _Glossiness,_Shininess;
    48. half _Emission;
    49. fixed4 _Color;
    50. fixed4 _CrossColor;
    51. fixed3 _PlaneNormal;
    52. fixed3 _PlanePosition;
    53.         float _SpecularColor;
    54. bool checkVisability(fixed3 worldPos)
    55. {
    56. float dotProd1 = dot(worldPos - _PlanePosition, _PlaneNormal);
    57. return dotProd1 > 0 ;
    58. }
    59. void surf(Input IN, inout SurfaceOutputStandardSpecular o) {
    60. if (checkVisability(IN.worldPos))discard;
    61. fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    62. o.Albedo = c.rgb;
    63. // Metallic and smoothness come from slider variables
    64. o.Emission = _Emission;
    65.             o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
    66.             o.Smoothness = tex2D (_GlossMap, IN.uv_GlossMap).a * _Glossiness;
    67. o.Specular = _SpecularColor;
    68. o.Alpha = c.a;
    69. }
    70. ENDCG
    71.  
    72. Cull Front
    73. CGPROGRAM
    74. #pragma surface surf NoLighting noambient
    75.  
    76. struct Input {
    77. half2 uv_MainTex;
    78. float3 worldPos;
    79.  
    80.  
    81. };
    82.  
    83. fixed4 _Color;
    84. fixed4 _CrossColor;
    85. fixed3 _PlaneNormal;
    86. fixed3 _PlanePosition;
    87. bool checkVisability(fixed3 worldPos)
    88. {
    89. float dotProd1 = dot(worldPos - _PlanePosition, _PlaneNormal);
    90. return dotProd1 >0 ;
    91. }
    92. fixed4 LightingNoLighting(SurfaceOutput s, fixed3 lightDir, fixed atten)
    93. {
    94. fixed4 c;
    95. c.rgb = s.Albedo;
    96. c.a = s.Alpha;
    97. return c;
    98. }
    99.  
    100.  
    101. void surf(Input IN, inout SurfaceOutput o)
    102. {
    103. if (checkVisability(IN.worldPos))discard;
    104. o.Albedo = _CrossColor;
    105.  
    106.  
    107. }
    108. ENDCG
    109.  
    110. }
    111. //FallBack "Diffuse"
    112. }
    113.  
     
    Last edited: Mar 18, 2019
  5. Seyed_Morteza_Kamaly

    Seyed_Morteza_Kamaly

    Joined:
    Nov 18, 2015
    Posts:
    80
    I explained this type of shader in below link
    https://gamedev.stackexchange.com/q...ing-water-inside-a-cup-of-glass/163944#163944