Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

How to add emission to a custom shader?

Discussion in 'Unity 5 Pre-order Beta' started by AustinRichards, Feb 11, 2015.

  1. AustinRichards

    AustinRichards

    Joined:
    Apr 4, 2013
    Posts:
    321
    I put together a custom splatmap shader. I'm trying to add emission to it. How can I add the unity 5 emission to a custom shader? Don't need to exact code for my exact shader relating to each splat, I just need to be pointed in the right direction on how a custom shader can take advantage of the unity 5 emission.

    Code (CSharp):
    1. //
    2. //   Author: Evan Pipta "747834"
    3. //   License: GNU GPL: http://www.gnu.org/copyleft/gpl.html
    4. //
    5. Shader "Custom/Splatmapped UV FirstPass"
    6. {
    7.     Properties
    8.     {
    9.         _Color ("Main Color", Color)                                 = (0.8, 0.8, 0.8, 0)
    10.         _Spec ("Specular Power", Range(0.01, 1) )                             = 0.2
    11.         _Shininess ("Glossiness", Range(0.01, 4) )                             = 0.15
    12.         _Parallax ("Height", Range (0.005, 0.108)) = 0.03
    13.         _BlendSoft ("Texture Blend Softness", Range(0, 1))                    = 0.1
    14.         _Tex_Color ("Color map (RGB)", 2D)                             = "white" {}
    15.         _Tex_Splat ("Splat map (RGBA)", 2D)                         = "white" {}
    16.         _Tex_DiffuseR ("Splat Diffuse R (RGB), Height (A)", 2D)                 = "white" {}
    17.         _Tex_NormalR ("Splat Normal R", 2D)                         = "bump" {}
    18.         _Tex_DispR ("Splat Displacement R (RGB), Height (A)", 2D)                 = "grey" {}
    19.         _Tex_DiffuseG ("Splat Diffuse G (RGB), Height (A)", 2D)                 = "white" {}
    20.         _Tex_NormalG ("Splat Normal G", 2D)                         = "bump" {}
    21.         _Tex_DispG ("Splat Displacement G (RGB), Height (A)", 2D)                 = "grey" {}
    22.         _Tex_DiffuseB ("Splat Diffuse B (RGB), Height (A)", 2D)                 = "white" {}
    23.         _Tex_NormalB ("Splat Normal B", 2D)                         = "bump" {}
    24.         _Tex_DispB ("Splat Displacement B (RGB), Height (A)", 2D)                 = "grey" {}
    25.         _Tex_DiffuseA ("Splat Diffuse A (RGB), Height (A)", 2D)                 = "white" {}
    26.         _Tex_NormalA ("Splat Normal A", 2D)                         = "bump" {}
    27.         _Tex_DispA ("Splat Displacement A (RGB), Height (A)", 2D)                 = "grey" {}
    28.     }
    29.  
    30.     SubShader
    31.     {
    32.         Tags { "RenderType"="Opaque" }
    33.         LOD 1000
    34.      
    35.         CGPROGRAM
    36.  
    37.             #pragma surface surf BlinnPhongSpecMap addshadow
    38.             #pragma target 3.0
    39.          
    40.             sampler2D _Tex_Splat;
    41.             sampler2D _Tex_Color;
    42.             sampler2D _Tex_DiffuseR;
    43.             sampler2D _Tex_DiffuseG;
    44.             sampler2D _Tex_DiffuseB;
    45.             sampler2D _Tex_DiffuseA;
    46.             sampler2D _Tex_NormalR;
    47.             sampler2D _Tex_NormalG;
    48.             sampler2D _Tex_NormalB;
    49.             sampler2D _Tex_NormalA;
    50.             sampler2D _Tex_DispR;
    51.             sampler2D _Tex_DispG;
    52.             sampler2D _Tex_DispB;
    53.             sampler2D _Tex_DispA;
    54.  
    55.             fixed4 _Color;
    56.             half _Spec;
    57.             half _Shininess;
    58.             half _DetailTileX;
    59.             half _DetailTileY;
    60.             half _BlendSoft;
    61.             float _Parallax;
    62.  
    63.             static const float _PI = 3.14159265359f;
    64.          
    65.             struct Input
    66.             {
    67.                 float2 uv_Tex_Splat;
    68.                 float2 uv_Tex_Color;
    69.                 float2 uv_Tex_DiffuseR;
    70.                 float2 uv_Tex_DiffuseG;
    71.                 float2 uv_Tex_DiffuseB;
    72.                 float2 uv_Tex_DiffuseA;
    73.                 float3 viewDir   : TEXCOORD5;
    74.                 // float2 uv_BumpMap;
    75.             };
    76.          
    77.             struct SurfaceOut
    78.             {
    79.                 fixed3 Albedo;
    80.                 fixed3 Normal;
    81.                 fixed3 Emission;
    82.                 half Specular;
    83.                 fixed3 Gloss;
    84.                 fixed Alpha;
    85.             };
    86.  
    87.             //compares input against compares. returns 1 if input is greater than ALL compares, else 0
    88.             float cutoff( float input, float compare1, float compare2, float compare3 )
    89.             {
    90.                 return ( input > compare1 && input > compare2 && input > compare3 ) ? 1 : 0;
    91.             }
    92.  
    93.             float3 blend_overlay( float3 base, float3 blend )
    94.             {
    95.                 return lerp((base*blend*2),(1.0-(2.0*(1.0-base)*(1.0-blend))),round(base));
    96.             }
    97.  
    98.             void surf (Input IN, inout SurfaceOut o)
    99.             {
    100.                 //o.Albedo = 0;
    101.                 float3 c = {0, 0, 0};
    102.              
    103.                 // Parallax
    104.                 half h;
    105.                 float2 offset;
    106.            
    107.                 h = tex2D (_Tex_DispR, IN.uv_Tex_DiffuseR).w;
    108.                 offset = ParallaxOffset (h, _Parallax, IN.viewDir);
    109.                 IN.uv_Tex_DiffuseR += offset;
    110.            
    111.                 h = tex2D (_Tex_DispG, IN.uv_Tex_DiffuseG).w;
    112.                 offset = ParallaxOffset (h, _Parallax, IN.viewDir);
    113.                 IN.uv_Tex_DiffuseG += offset;
    114.            
    115.                 h = tex2D (_Tex_DispB, IN.uv_Tex_DiffuseB).w;
    116.                 offset = ParallaxOffset (h, _Parallax, IN.viewDir);
    117.                 IN.uv_Tex_DiffuseB += offset;
    118.            
    119.                 h = tex2D (_Tex_DispA, IN.uv_Tex_DiffuseA).w;
    120.                 offset = ParallaxOffset (h, _Parallax, IN.viewDir);
    121.                 IN.uv_Tex_DiffuseA += offset;
    122.  
    123.                 //Diffuse
    124.                 float4 splat = tex2D( _Tex_Splat, IN.uv_Tex_Splat );
    125.                 float4 colormap = tex2D( _Tex_Color, IN.uv_Tex_Color );
    126.              
    127.          
    128.                 float4 dr = tex2D( _Tex_DiffuseR, IN.uv_Tex_DiffuseR );
    129.                 float4 dg = tex2D( _Tex_DiffuseG, IN.uv_Tex_DiffuseG );
    130.                 float4 db = tex2D( _Tex_DiffuseB, IN.uv_Tex_DiffuseB );
    131.                 float4 da = tex2D( _Tex_DiffuseA, IN.uv_Tex_DiffuseA );
    132.                 dr.a *= splat.r;
    133.                 dg.a *= splat.g;
    134.                 db.a *= splat.b;
    135.                 da.a *= splat.a;
    136.  
    137.                 //Combine all alphas to equal 1
    138.                 float sum = dr.a + dg.a + db.a + da.a;
    139.                 if ( sum > 0 )
    140.                 {
    141.                     dr.a /= sum;
    142.                     dg.a /= sum;
    143.                     db.a /= sum;
    144.                     da.a /= sum;
    145.                 }
    146.                 else
    147.                 {
    148.                     dr.a = 1;
    149.                 }
    150.  
    151.                 //Cutoff each alpha, comparing against each other
    152.                 float dr_cutoff = cutoff( dr.a, dg.a, db.a, da.a );
    153.                 float dg_cutoff = cutoff( dg.a, dr.a, db.a, da.a );
    154.                 float db_cutoff = cutoff( db.a, dr.a, dg.a, da.a );
    155.                 float da_cutoff = cutoff( da.a, dr.a, dg.a, db.a );
    156.  
    157.                 //Lerp between the smooth alpha and cutoff alpha by the softness value
    158.                 //Amount to lerp should increase nonlinearly as the difference between the cutoff and smooth increases
    159.                 //That way we "round the corners" as we lerp.
    160.                 dr.a = lerp( dr_cutoff, dr.a, saturate( _BlendSoft ) );
    161.                 dg.a = lerp( dg_cutoff, dg.a, saturate( _BlendSoft ) );
    162.                 db.a = lerp( db_cutoff, db.a, saturate( _BlendSoft ) );
    163.                 da.a = lerp( da_cutoff, da.a, saturate( _BlendSoft ) );
    164.  
    165.                 c = saturate( (dr.rgb*dr.a + dg.rgb*dg.a + db.rgb*db.a + da.rgb*da.a) * _Color );
    166.                 c = blend_overlay( c.rgb, colormap.rgb );
    167.  
    168.                 o.Albedo = c;
    169.              
    170.  
    171.                 //--------------------------------------------
    172.                 //Specular
    173.                 o.Gloss = _Spec;
    174.                 o.Specular = _Shininess;
    175.  
    176.  
    177.                 //--------------------------------------------
    178.                 //Normal
    179.                 float3 nr = UnpackNormal( tex2D( _Tex_NormalR, IN.uv_Tex_DiffuseR ) );
    180.                 float3 ng = UnpackNormal( tex2D( _Tex_NormalG, IN.uv_Tex_DiffuseG ) );
    181.                 float3 nb = UnpackNormal( tex2D( _Tex_NormalB, IN.uv_Tex_DiffuseB ) );
    182.                 float3 na = UnpackNormal( tex2D( _Tex_NormalA, IN.uv_Tex_DiffuseA ) );
    183.                 o.Normal = clamp( (nr.rgb*dr.a + ng.rgb*dg.a + nb.rgb*db.a + na.rgb*da.a), -1, 1 );
    184.             }
    185.          
    186.             inline fixed4 LightingBlinnPhongSpecMap (SurfaceOut s, fixed3 lightDir, half3 viewDir, fixed atten)
    187.             {
    188.                 half3 h = normalize (lightDir + viewDir);
    189.                 fixed diff = max (0, dot (s.Normal, lightDir));
    190.                 float nh = max (0, dot (s.Normal, h));
    191.                 float spec = pow (nh, s.Specular*128.0);
    192.                 fixed4 c;
    193.                 c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * spec * s.Gloss) * (atten * 2);
    194.                 c.a = s.Alpha + _LightColor0.a * (0.2989f * s.Gloss.r + 0.5870f * s.Gloss.g + 0.1140f * s.Gloss.b) * spec * atten;
    195.                 return c;
    196.             }
    197.  
    198.         ENDCG
    199.     }
    200.  
    201. }
    202.  
     
  2. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    664
    It's always worth to have a look at the Built-In shaders when you create custom shaders.
    Just pick those which behave the way you want and see what they do.

    Here is an example for the BlinnPhong surface:
    Code (csharp):
    1.  
    2. //Properties
    3.    _EmissionLM ("Emission (Lightmapper)", Float) = 0
    4.    [Toggle] _DynamicEmissionLM ("Dynamic Emission (Lightmapper)", Int) = 0
    5.  
    6. //Output
    7.    o.Emission = c.rgb * tex2D(_Illum, IN.uv_Illum).a;
    8.  
     
  3. AustinRichards

    AustinRichards

    Joined:
    Apr 4, 2013
    Posts:
    321
    Thank you so much. Got it working :)