Download

How to cast shadow in material in UE4?

Hi, I’m trying to write a simple light model in the material blueprint.
and I don’t know which node can cast the shadow of objects, or which node is equal to the Light Attenuation node in Unity?
02

As far as I know, there isn’t one. We don’t have direct access to the lights in the scene or their shadow depths unfortunately. In my opinion it’s one of the biggest things holding Unreal back in non-photorealistic rendering.

There are some ways around this, for example you can pass light properties to materials via blueprint and you can render out a depth map for them using render targets, but it’s involved to say the least and really only works well on unlit materials.

Another option is to implement a new shading model, but to my knowledge this requires building the engine from source.

I’m not sure what you’re trying to do but if this is just for a simple light effect (for example a flickering light or fake cloud shadowing) then you should look into using a light function instead.

I’m trying to write a simple light model shader, the formula is (Lambert + Phong)*Shadow+ 3 Color Ambient Occlusion. I completed this in unity, and I want to copy this effect to UE4. But I don’t know how to get the shadow message in UE4, so which node in UE4 is equal to the node Light Attenuation node in unity?


This is the shader code in unity:

// Shader created with Shader Forge v1.40 
// Shader Forge (c) Freya Holmer - http://www.acegikmo.com/shaderforge/
// Note: Manually altering this data may prevent you from opening it in Shader Forge
/*SF_DATA;ver:1.40;sub:START;pass:START;ps:flbk:,iptp:0,cusa:False,bamd:0,cgin:,cpap:True,lico:1,lgpr:1,limd:0,spmd:1,trmd:0,grmd:0,uamb:True,mssp:True,bkdf:False,hqlp:False,rprd:False,enco:False,rmgx:True,imps:True,rpth:0,vtps:0,hqsc:True,nrmq:1,nrsp:0,vomd:0,spxs:False,tesm:0,olmd:1,culm:0,bsrc:0,bdst:1,dpts:2,wrdp:True,dith:0,atcv:False,rfrpo:True,rfrpn:Refraction,coma:15,ufog:False,aust:True,igpj:False,qofs:0,qpre:1,rntp:1,fgom:False,fgoc:False,fgod:False,fgor:False,fgmd:0,fgcr:0.5,fgcg:0.5,fgcb:0.5,fgca:1,fgde:0.01,fgrn:0,fgrf:300,stcl:False,atwp:False,stva:128,stmr:255,stmw:255,stcp:6,stps:0,stfa:0,stfz:0,ofsf:0,ofsu:0,f2p0:False,fnsp:False,fnfb:False,fsmp:False;n:type:ShaderForge.SFN_Final,id:3138,x:33262,y:32433,varname:node_3138,prsc:2|emission-300-OUT;n:type:ShaderForge.SFN_ViewVector,id:9077,x:31183,y:32977,varname:node_9077,prsc:2;n:type:ShaderForge.SFN_NormalVector,id:2610,x:31143,y:32374,prsc:2,pt:False;n:type:ShaderForge.SFN_LightVector,id:4240,x:31143,y:32528,varname:node_4240,prsc:2;n:type:ShaderForge.SFN_Dot,id:3337,x:31387,y:32433,varname:node_3337,prsc:2,dt:0|A-2610-OUT,B-4240-OUT;n:type:ShaderForge.SFN_Max,id:6767,x:31579,y:32433,varname:node_6767,prsc:2|A-3337-OUT,B-2888-OUT;n:type:ShaderForge.SFN_Vector1,id:2888,x:31579,y:32568,varname:node_2888,prsc:2,v1:0;n:type:ShaderForge.SFN_Multiply,id:2845,x:31765,y:32422,varname:node_2845,prsc:2|A-589-RGB,B-6767-OUT;n:type:ShaderForge.SFN_Color,id:589,x:31747,y:32264,ptovrint:False,ptlb:BaseColor,ptin:_BaseColor,varname:node_589,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,c1:1,c2:1,c3:1,c4:1;n:type:ShaderForge.SFN_LightVector,id:2988,x:30505,y:32817,varname:node_2988,prsc:2;n:type:ShaderForge.SFN_Multiply,id:8673,x:30721,y:32817,varname:node_8673,prsc:2|A-2988-OUT,B-4551-OUT;n:type:ShaderForge.SFN_Vector1,id:4551,x:30505,y:32950,varname:node_4551,prsc:2,v1:-1;n:type:ShaderForge.SFN_Vector1,id:5466,x:31379,y:32977,varname:node_5466,prsc:2,v1:0;n:type:ShaderForge.SFN_Reflect,id:9267,x:30909,y:32817,varname:node_9267,prsc:2|A-8673-OUT,B-8241-OUT;n:type:ShaderForge.SFN_NormalVector,id:8241,x:30909,y:32948,prsc:2,pt:False;n:type:ShaderForge.SFN_Dot,id:7301,x:31183,y:32819,varname:node_7301,prsc:2,dt:0|A-9267-OUT,B-9077-OUT;n:type:ShaderForge.SFN_Max,id:2721,x:31525,y:32819,varname:node_2721,prsc:2|A-7301-OUT,B-5466-OUT;n:type:ShaderForge.SFN_Power,id:6179,x:31759,y:32819,varname:node_6179,prsc:2|VAL-2721-OUT,EXP-4639-OUT;n:type:ShaderForge.SFN_Slider,id:4639,x:31602,y:32987,ptovrint:False,ptlb:SpecuarPower,ptin:_SpecuarPower,varname:node_4639,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,min:1,cur:30,max:1000;n:type:ShaderForge.SFN_Multiply,id:6970,x:32585,y:32431,varname:node_6970,prsc:2|A-9980-OUT,B-7904-OUT;n:type:ShaderForge.SFN_LightAttenuation,id:7904,x:32585,y:32559,varname:node_7904,prsc:2;n:type:ShaderForge.SFN_NormalVector,id:806,x:30251,y:33280,prsc:2,pt:False;n:type:ShaderForge.SFN_ComponentMask,id:4417,x:30439,y:33280,varname:node_4417,prsc:2,cc1:1,cc2:-1,cc3:-1,cc4:-1|IN-806-OUT;n:type:ShaderForge.SFN_Max,id:6880,x:30809,y:33285,varname:node_6880,prsc:2|A-4417-OUT,B-7616-OUT;n:type:ShaderForge.SFN_Vector1,id:7616,x:30809,y:33411,varname:node_7616,prsc:2,v1:0;n:type:ShaderForge.SFN_Multiply,id:9950,x:30809,y:33539,varname:node_9950,prsc:2|A-4417-OUT,B-3364-OUT;n:type:ShaderForge.SFN_Vector1,id:3364,x:30798,y:33669,varname:node_3364,prsc:2,v1:-1;n:type:ShaderForge.SFN_Max,id:4771,x:31029,y:33393,varname:node_4771,prsc:2|A-9950-OUT,B-7616-OUT;n:type:ShaderForge.SFN_Subtract,id:3955,x:31243,y:33273,varname:node_3955,prsc:2|A-9955-OUT,B-6880-OUT;n:type:ShaderForge.SFN_Vector1,id:9955,x:31243,y:33212,varname:node_9955,prsc:2,v1:1;n:type:ShaderForge.SFN_Subtract,id:141,x:31623,y:33480,varname:node_141,prsc:2|A-3955-OUT,B-4771-OUT;n:type:ShaderForge.SFN_Multiply,id:7023,x:31006,y:33125,varname:node_7023,prsc:2|A-3600-RGB,B-6880-OUT;n:type:ShaderForge.SFN_Color,id:3600,x:30701,y:33122,ptovrint:False,ptlb:UpAmbientColor,ptin:_UpAmbientColor,varname:_BaseColor_copy_copy_copy_copy_copy_copy,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,c1:1,c2:1,c3:1,c4:1;n:type:ShaderForge.SFN_Multiply,id:4813,x:31243,y:33495,varname:node_4813,prsc:2|A-4771-OUT,B-2071-RGB;n:type:ShaderForge.SFN_Multiply,id:7040,x:31712,y:33320,varname:node_7040,prsc:2|A-2657-RGB,B-141-OUT;n:type:ShaderForge.SFN_Add,id:7610,x:31915,y:33286,varname:node_7610,prsc:2|A-7023-OUT,B-7040-OUT;n:type:ShaderForge.SFN_Add,id:1112,x:32126,y:33286,varname:node_1112,prsc:2|A-7610-OUT,B-4813-OUT;n:type:ShaderForge.SFN_Multiply,id:1413,x:32336,y:33286,varname:node_1413,prsc:2|A-1112-OUT,B-4329-OUT;n:type:ShaderForge.SFN_Slider,id:4329,x:32271,y:33440,ptovrint:False,ptlb:AmbientColorIn,ptin:_AmbientColorIn,varname:node_4329,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,min:0,cur:1,max:5;n:type:ShaderForge.SFN_Multiply,id:2844,x:32585,y:33266,varname:node_2844,prsc:2|A-2461-RGB,B-1413-OUT;n:type:ShaderForge.SFN_Tex2d,id:2461,x:32295,y:33097,ptovrint:False,ptlb:OcclusionTex,ptin:_OcclusionTex,varname:node_2461,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,tex:7677f2ee59474384fad484574cc3ba17,ntxv:0,isnm:False;n:type:ShaderForge.SFN_Multiply,id:2421,x:32812,y:33247,varname:node_2421,prsc:2|A-589-RGB,B-2844-OUT;n:type:ShaderForge.SFN_Multiply,id:4735,x:32049,y:32820,varname:node_4735,prsc:2|A-6179-OUT,B-5188-RGB;n:type:ShaderForge.SFN_Color,id:5188,x:32049,y:32967,ptovrint:False,ptlb:SpecularColor,ptin:_SpecularColor,varname:_BaseColor_copy,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,c1:1,c2:1,c3:1,c4:1;n:type:ShaderForge.SFN_Color,id:2071,x:31243,y:33653,ptovrint:False,ptlb:DownAmbientColor,ptin:_DownAmbientColor,varname:node_2071,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,c1:1,c2:1,c3:1,c4:1;n:type:ShaderForge.SFN_Color,id:2657,x:31476,y:33320,ptovrint:False,ptlb:SideAmbientColor,ptin:_SideAmbientColor,varname:node_2657,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,c1:1,c2:1,c3:1,c4:1;n:type:ShaderForge.SFN_Add,id:3175,x:32219,y:32684,varname:node_3175,prsc:2|A-2845-OUT,B-4735-OUT;n:type:ShaderForge.SFN_Add,id:300,x:32907,y:32591,varname:node_300,prsc:2|A-6970-OUT,B-2421-OUT;n:type:ShaderForge.SFN_Multiply,id:9980,x:32414,y:32435,varname:node_9980,prsc:2|A-8962-RGB,B-3175-OUT;n:type:ShaderForge.SFN_Color,id:8962,x:32197,y:32435,ptovrint:False,ptlb:LightColor,ptin:_LightColor,varname:node_8962,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,c1:1,c2:1,c3:1,c4:1;proporder:589-8962-3600-2657-2071-4329-2461-4639-5188;pass:END;sub:END;*/

Shader "Shader Forge/L07_OldLightMode" {
    Properties {
        _BaseColor ("BaseColor", Color) = (1,1,1,1)
        _LightColor ("LightColor", Color) = (1,1,1,1)
        _UpAmbientColor ("UpAmbientColor", Color) = (1,1,1,1)
        _SideAmbientColor ("SideAmbientColor", Color) = (1,1,1,1)
        _DownAmbientColor ("DownAmbientColor", Color) = (1,1,1,1)
        _AmbientColorIn ("AmbientColorIn", Range(0, 5)) = 1
        _OcclusionTex ("OcclusionTex", 2D) = "white" {}
        _SpecuarPower ("SpecuarPower", Range(1, 1000)) = 30
        _SpecularColor ("SpecularColor", Color) = (1,1,1,1)
    }
    SubShader {
        Tags {
            "RenderType"="Opaque"
        }
        Pass {
            Name "FORWARD"
            Tags {
                "LightMode"="ForwardBase"
            }
            
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_instancing
            #include "UnityCG.cginc"
            #include "AutoLight.cginc"
            #include "Lighting.cginc"
            #pragma multi_compile_fwdbase_fullshadows
            #pragma target 3.0
            uniform sampler2D _OcclusionTex; uniform float4 _OcclusionTex_ST;
            UNITY_INSTANCING_BUFFER_START( Props )
                UNITY_DEFINE_INSTANCED_PROP( float4, _BaseColor)
                UNITY_DEFINE_INSTANCED_PROP( float, _SpecuarPower)
                UNITY_DEFINE_INSTANCED_PROP( float4, _UpAmbientColor)
                UNITY_DEFINE_INSTANCED_PROP( float, _AmbientColorIn)
                UNITY_DEFINE_INSTANCED_PROP( float4, _SpecularColor)
                UNITY_DEFINE_INSTANCED_PROP( float4, _DownAmbientColor)
                UNITY_DEFINE_INSTANCED_PROP( float4, _SideAmbientColor)
                UNITY_DEFINE_INSTANCED_PROP( float4, _LightColor)
            UNITY_INSTANCING_BUFFER_END( Props )
            struct VertexInput {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
                float2 texcoord0 : TEXCOORD0;
            };
            struct VertexOutput {
                float4 pos : SV_POSITION;
                UNITY_VERTEX_INPUT_INSTANCE_ID
                float2 uv0 : TEXCOORD0;
                float4 posWorld : TEXCOORD1;
                float3 normalDir : TEXCOORD2;
                LIGHTING_COORDS(3,4)
            };
            VertexOutput vert (VertexInput v) {
                VertexOutput o = (VertexOutput)0;
                UNITY_SETUP_INSTANCE_ID( v );
                UNITY_TRANSFER_INSTANCE_ID( v, o );
                o.uv0 = v.texcoord0;
                o.normalDir = UnityObjectToWorldNormal(v.normal);
                o.posWorld = mul(unity_ObjectToWorld, v.vertex);
                o.pos = UnityObjectToClipPos( v.vertex );
                TRANSFER_VERTEX_TO_FRAGMENT(o)
                return o;
            }
            float4 frag(VertexOutput i) : COLOR {
                UNITY_SETUP_INSTANCE_ID( i );
                i.normalDir = normalize(i.normalDir);
                float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
                float3 normalDirection = i.normalDir;
                float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
////// Lighting:
                float attenuation = LIGHT_ATTENUATION(i);
////// Emissive:
                float4 _LightColor_var = UNITY_ACCESS_INSTANCED_PROP( Props, _LightColor );
                float4 _BaseColor_var = UNITY_ACCESS_INSTANCED_PROP( Props, _BaseColor );
                float _SpecuarPower_var = UNITY_ACCESS_INSTANCED_PROP( Props, _SpecuarPower );
                float4 _SpecularColor_var = UNITY_ACCESS_INSTANCED_PROP( Props, _SpecularColor );
                float4 _OcclusionTex_var = tex2D(_OcclusionTex,TRANSFORM_TEX(i.uv0, _OcclusionTex));
                float4 _UpAmbientColor_var = UNITY_ACCESS_INSTANCED_PROP( Props, _UpAmbientColor );
                float node_4417 = i.normalDir.g;
                float node_7616 = 0.0;
                float node_6880 = max(node_4417,node_7616);
                float4 _SideAmbientColor_var = UNITY_ACCESS_INSTANCED_PROP( Props, _SideAmbientColor );
                float node_4771 = max((node_4417*(-1.0)),node_7616);
                float4 _DownAmbientColor_var = UNITY_ACCESS_INSTANCED_PROP( Props, _DownAmbientColor );
                float _AmbientColorIn_var = UNITY_ACCESS_INSTANCED_PROP( Props, _AmbientColorIn );
                float3 emissive = (((_LightColor_var.rgb*((_BaseColor_var.rgb*max(dot(i.normalDir,lightDirection),0.0))+(pow(max(dot(reflect((lightDirection*(-1.0)),i.normalDir),viewDirection),0.0),_SpecuarPower_var)*_SpecularColor_var.rgb)))*attenuation)+(_BaseColor_var.rgb*(_OcclusionTex_var.rgb*((((_UpAmbientColor_var.rgb*node_6880)+(_SideAmbientColor_var.rgb*((1.0-node_6880)-node_4771)))+(node_4771*_DownAmbientColor_var.rgb))*_AmbientColorIn_var))));
                float3 finalColor = emissive;
                return fixed4(finalColor,1);
            }
            ENDCG
        }
        Pass {
            Name "FORWARD_DELTA"
            Tags {
                "LightMode"="ForwardAdd"
            }
            Blend One One
            
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_instancing
            #include "UnityCG.cginc"
            #include "AutoLight.cginc"
            #include "Lighting.cginc"
            #pragma multi_compile_fwdadd_fullshadows
            #pragma target 3.0
            uniform sampler2D _OcclusionTex; uniform float4 _OcclusionTex_ST;
            UNITY_INSTANCING_BUFFER_START( Props )
                UNITY_DEFINE_INSTANCED_PROP( float4, _BaseColor)
                UNITY_DEFINE_INSTANCED_PROP( float, _SpecuarPower)
                UNITY_DEFINE_INSTANCED_PROP( float4, _UpAmbientColor)
                UNITY_DEFINE_INSTANCED_PROP( float, _AmbientColorIn)
                UNITY_DEFINE_INSTANCED_PROP( float4, _SpecularColor)
                UNITY_DEFINE_INSTANCED_PROP( float4, _DownAmbientColor)
                UNITY_DEFINE_INSTANCED_PROP( float4, _SideAmbientColor)
                UNITY_DEFINE_INSTANCED_PROP( float4, _LightColor)
            UNITY_INSTANCING_BUFFER_END( Props )
            struct VertexInput {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
                float2 texcoord0 : TEXCOORD0;
            };
            struct VertexOutput {
                float4 pos : SV_POSITION;
                UNITY_VERTEX_INPUT_INSTANCE_ID
                float2 uv0 : TEXCOORD0;
                float4 posWorld : TEXCOORD1;
                float3 normalDir : TEXCOORD2;
                LIGHTING_COORDS(3,4)
            };
            VertexOutput vert (VertexInput v) {
                VertexOutput o = (VertexOutput)0;
                UNITY_SETUP_INSTANCE_ID( v );
                UNITY_TRANSFER_INSTANCE_ID( v, o );
                o.uv0 = v.texcoord0;
                o.normalDir = UnityObjectToWorldNormal(v.normal);
                o.posWorld = mul(unity_ObjectToWorld, v.vertex);
                o.pos = UnityObjectToClipPos( v.vertex );
                TRANSFER_VERTEX_TO_FRAGMENT(o)
                return o;
            }
            float4 frag(VertexOutput i) : COLOR {
                UNITY_SETUP_INSTANCE_ID( i );
                i.normalDir = normalize(i.normalDir);
                float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
                float3 normalDirection = i.normalDir;
                float3 lightDirection = normalize(lerp(_WorldSpaceLightPos0.xyz, _WorldSpaceLightPos0.xyz - i.posWorld.xyz,_WorldSpaceLightPos0.w));
////// Lighting:
                float attenuation = LIGHT_ATTENUATION(i);
                float3 finalColor = 0;
                return fixed4(finalColor * 1,0);
            }
            ENDCG
        }
    }
    FallBack "Diffuse"
    CustomEditor "ShaderForgeMaterialInspector"
}

Answer’s still the same, there is no node for this. You have two options:

  1. Implement a new shading model and build the engine from source
  2. Build the shadows yourself inside the material using a depth map captured from a render target