Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to add emission map to this shader

Discussion in 'Shaders' started by JamesThornton, Jan 20, 2016.

  1. JamesThornton

    JamesThornton

    Joined:
    Jun 26, 2015
    Posts:
    52
    I've been studying writing shaders. It's going to take some time to understand.

    There's not much documentation...so any assistance is greatly appreciated!


    The following shader reveals a texture along it's alpha using _Cutoff. This is cool, because you can use a gradient, or gradient pattern, to reveal it in unique ways.


    My goal is to have a simple base texture (unlit for now), and reveal an emission map along an alpha using the _Cutoff property. (with same options as Standard Shader emission - texture, intensity, and color) The alpha could be from the base texture, or the emission map, whichever would work best.

    Code (CSharp):
    1. Shader "Custom/Reveal" {
    2.     Properties {
    3.         _MainTex ("Base (RGB) Reveal (A)", 2D) = "white" {}
    4.         _Cutoff ("Cutoff", Range(-0.5, 0.55)) = 0
    5.     }
    6.     SubShader {
    7.         Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    8.         LOD 100
    9.         Alphatest Greater 0
    10.         ZWrite Off
    11.         Blend SrcAlpha OneMinusSrcAlpha
    12.         ColorMask RGB
    13.    
    14.         Pass {
    15.        
    16.             CGPROGRAM
    17.                 #pragma vertex vert
    18.                 #pragma fragment frag
    19.                 #include "UnityCG.cginc"
    20.            
    21.                 struct v2f {
    22.                     float4 pos : SV_POSITION;
    23.                     float2 uv_MainTex : TEXCOORD0;
    24.                 };
    25.            
    26.                 float4 _MainTex_ST;
    27.            
    28.                 v2f vert(appdata_base v) {
    29.                     v2f o;
    30.                     o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    31.                     o.uv_MainTex = TRANSFORM_TEX(v.texcoord, _MainTex);
    32.                     return o;
    33.                 }
    34.            
    35.                 sampler2D _MainTex;
    36.                 fixed _Cutoff;
    37.            
    38.                 float4 frag(v2f IN) : COLOR {
    39.                     half4 c = tex2D (_MainTex, IN.uv_MainTex);
    40.                     c.a = lerp(-20, 20, c.a + _Cutoff);
    41.                     return c;
    42.                 }
    43.             ENDCG
    44.         }
    45.     }
    46. }

    Looking at the Standard Shader source, I found the following Properties:
    _EmissionColor("Color", Color) = (0,0,0)
    _EmissionMap("Emission", 2D) = "white" {}

    As well as the pragma reference. But then it has various cginc files that I believe include the rest of the helper code that I would need to include it.

    I've tried to hack it together, mimicking various shaders with emission, but nothing works quite right, even if it compiles.


    I will continue to study and post if I learn it
     
  2. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    You just need to add to your output color. That's it. You don't have any lighting equation in your shader though so I'm not sure how apparent it will be...

    In a Surface shader all emission does is add color after the lighting calculation is done.
     
  3. JamesThornton

    JamesThornton

    Joined:
    Jun 26, 2015
    Posts:
    52
    Is there a very simple example shader with emission I can reference?

    I've been looking at the source for the legacy self illuminating shaders, the standard shader, and all the cginc files referenced in the standard shader.

    I need it to behave just like the emission in the standard shader (texture, color, intensity, with black automatically 0 intensity)

    Been watching hours of video and reading the docs. It's just not clicking, but I'll keep at it...
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,306
    Properties {
    _MainTex ("Albedo", 2D) = "white" {}
    _EmissionMap ("Emission Map", 2D) = "black" {}
    [HDR] _EmissionColor ("Emission Color", Color) = (0,0,0)
    }

    ...

    fixed4 albedo = tex2D(_MainTex, i.uv);

    ... do some lighting ...
    half4 output = half4(albedo.rgb * lighting.rgb, albedo.a);

    half4 emission = tex2D(_EmissionMap, i.uv) * _EmissionColor;
    output.rgb += emission.rgb;

    return output;
     
  5. ifurkend

    ifurkend

    Joined:
    Sep 4, 2012
    Posts:
    350
    Just curious that when I use multiplication instead of addition, under HDR it results in weird color saturation when the emission value is slightly higher. Any idea?
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,306
    If your object is lit brightly such that with out emission it's brighter than "1.0", then multiplying it by the emission color will result in a brighter effect. The problem is at that point you're just tinting the object, which isn't emission anymore. That's fine if that's what you want to do, but don't call it emission.

    diffuse += emission
    diffuse *= tint
     
    Liam2349 likes this.
  7. Liam2349

    Liam2349

    Joined:
    Mar 10, 2016
    Posts:
    78
    Every shader question ye shall google, the legend bgolus shall have answered.

    Thank you sir.
     
    Raymond_XJTU likes this.
  8. Raymond_XJTU

    Raymond_XJTU

    Joined:
    Jul 24, 2021
    Posts:
    3
    You can also just use ShaderGraph by the same Theory.
     

    Attached Files: