Add Emission to Custom Shader

Discuss modding questions and implementation details.
Post Reply
User avatar
Macadaynu
Posts: 261
Joined: Sun Mar 07, 2021 1:18 pm

Add Emission to Custom Shader

Post by Macadaynu »

I want to be able to use the Emission property found within Unity's standard shader within my custom shader. I have tried a few things I found online but nothing works as of yet.

This is to improve one aspect of the Transparent Windows mod, to allow exterior windows to retain their blue (or yellow) emission depending on the time of day. Right now they are just black as no emission has been added to my custom shader.

Basically I just need the properties highlighted below:
Capture.PNG
Capture.PNG (21.18 KiB) Viewed 2120 times

Here is the code for my custom shader (without emission):

Code: Select all

Shader "Custom/ClipBoxShader" {
    Properties{
        _MainTex("Albedo (RGB)", 2D) = "white" {}
        _Glossiness("Smoothness", Range(0,1)) = 0.5
        _Metallic("Metallic", Range(0,1)) = 0.0
    }

        SubShader{
            Tags { "RenderType" = "Transparent" }
            LOD 200

            CGPROGRAM
            #pragma surface surf Standard fullforwardshadows addshadow
            #pragma target 3.0

            sampler2D _MainTex;
            half _Glossiness;
            half _Metallic;
            float4x4 _WorldToBox;

            struct Input {
                float2 uv_MainTex;
                float3 worldPos;
            };

            void surf(Input IN, inout SurfaceOutputStandard o) {
                float3 boxPosition = mul(_WorldToBox, float4(IN.worldPos, 1));

                if (all(boxPosition > -0.5 && boxPosition < 0.5))
                    discard;                

                fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
                o.Albedo = c.rgb;
                o.Metallic = _Metallic;
                o.Smoothness = _Glossiness;
                o.Alpha = c.a;
            }
            ENDCG
        }
            FallBack "Diffuse"
}

User avatar
BadLuckBurt
Posts: 948
Joined: Sun Nov 05, 2017 8:30 pm

Re: Add Emission to Custom Shader

Post by BadLuckBurt »

This would seem to be a right approach: https://gamedev.stackexchange.com/quest ... -in-shader.

I've added the properties to the shader below but I'll leave it up to you to decide how you want to calculate it. The line I commented out below the o.Alpha line just used the albedo texture which is probably what you don't want. You may need an emission map to control which parts are emissive but lets see how far this gets you.

You can also have a look at Unity's shaders: https://github.com/TwoTailsGames/Unity- ... CGIncludes. The Standard shader appears to be split up into separate parts, Core and Input both seem to deal with emission.

Code: Select all

Shader "Custom/ClipBoxShader" {
    Properties{
        _MainTex("Albedo (RGB)", 2D) = "white" {}
        _Glossiness("Smoothness", Range(0,1)) = 0.5
        _Metallic("Metallic", Range(0,1)) = 0.0
        _Emission("Emission", float) = 0
        _EmissionColor("Emission color", Color) = (1,1,1,1)
    }

        SubShader{
            Tags { "RenderType" = "Transparent" }
            LOD 200

            CGPROGRAM
            #pragma surface surf Standard fullforwardshadows addshadow
            #pragma target 3.0

            sampler2D _MainTex;
            half _Glossiness;
            half _Metallic;
            float _Emission;
            fixed4 _EmissionColor;
            float4x4 _WorldToBox;

            struct Input {
                float2 uv_MainTex;
                float3 worldPos;
            };

            void surf(Input IN, inout SurfaceOutputStandard o) {
                float3 boxPosition = mul(_WorldToBox, float4(IN.worldPos, 1));

                if (all(boxPosition > -0.5 && boxPosition < 0.5))
                    discard;                

                fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
                o.Albedo = c.rgb;
                o.Metallic = _Metallic;
                o.Smoothness = _Glossiness;
                o.Alpha = c.a;
                //o.Emission = c.rgb * tex2D(_MainTex, IN.uv_MainTex).a * _Emission;
            }
            ENDCG
        }
            FallBack "Diffuse"
}
DFU on UESP: https://en.uesp.net/w/index.php?title=T ... fall_Unity
DFU Nexus Mods: https://www.nexusmods.com/daggerfallunity
My github repositories with mostly DFU related stuff: https://github.com/BadLuckBurt

.

User avatar
Macadaynu
Posts: 261
Joined: Sun Mar 07, 2021 1:18 pm

Re: Add Emission to Custom Shader

Post by Macadaynu »

BadLuckBurt wrote: Tue Jun 07, 2022 4:32 am This would seem to be a right approach: https://gamedev.stackexchange.com/quest ... -in-shader.
Hi Burt,

I did try something like that and it wasn't working, I think it's because I still need to include the _EmissionMap property.

So I've included that this time around but the emission map is not being applied, so it just brightens up the whole material rather than the window area.

I'll keep digging

Thanks :)

User avatar
Macadaynu
Posts: 261
Joined: Sun Mar 07, 2021 1:18 pm

Re: Add Emission to Custom Shader

Post by Macadaynu »

Got it!

So as expected I needed to include the emission map with my output, like so if it helps anyone in future:

Code: Select all

	o.Emission = _EmissionColor.rgb * tex2D(_EmissionMap, IN.uv_MainTex).a * _Emission;
So now we can see lit up windows at night
Daggerfall Unity 08-Jun-22 11_13_42 PM.png
Daggerfall Unity 08-Jun-22 11_13_42 PM.png (537.42 KiB) Viewed 2037 times

User avatar
BadLuckBurt
Posts: 948
Joined: Sun Nov 05, 2017 8:30 pm

Re: Add Emission to Custom Shader

Post by BadLuckBurt »

Awesome, that looks really nice. Glad you were able to figure out the rest with the emission map, that was the one area I wasnt sure.
DFU on UESP: https://en.uesp.net/w/index.php?title=T ... fall_Unity
DFU Nexus Mods: https://www.nexusmods.com/daggerfallunity
My github repositories with mostly DFU related stuff: https://github.com/BadLuckBurt

.

Post Reply