PS:文章是2018年写的,保存的不是很好,图片已经很模糊了,建议直接去看我的视频,一共四期:
https://www.bilibili.com/video/BV1544y177sh?spm_id_from=333.999.0.0&vd_source=ac34bbe36af835db8452b6636f002bd2
This dairy was modified on Sep. sixth 2018.
Hello everyone ,we met again.This article will tell everyone how to make the custom shadow of directional light in Unreal Engine 4.Next I did not talk about any trashy things.Let’s start to make it.
I need to say if you want to make custom shadow,you have to get some knowledge about the Matrix. There is a nice book everyone can read.Its name is 3D math primer for Graphics and Game development.This article did not talk about the Matrix knowledge for the 3D math.So if you can not understand,when you read below article, you will be giddy.
First Create the new level and delete some things about Unreal Engine default lighting.Create the other simple shape for our reference.

Create the new material for custom shadow.And Set shading model to Unlit.

Now we need a render target to capture the depth of the scene.It work like the tutorial in internet.I did not talk about its theory,because there are so many tutorials about it.Below URL are the tutorials of the DirectX 11 shadow map:
①(Chinese)
https://blog.csdn.net/qq_29523119/article/details/53056755
②(English)
https://www.youtube.com/watch?v=3AdLu0PHOnE
③(Cantonese!?!??OMG 广州话)
https://www.youtube.com/watch?v=oPxY1eTrrOo
Create render target and set


Create a blueprint of Actor.And Create a scene capture component 2D.Set it for the render target.The projection Type is orthographic.because we only make the directional light shadow.


Sample the render target and watch the effect.it is the strong light in the scene.Because it is scene depth,the vertexes distance the camera.Not the from zero to one.So we should divide the distance.Create a parameter collection,and create some parameter as we will need.One position and two matrix parameters.Create a custom event in blueprint and run it on the construction script.








Alright, now we can see the capture map was pasted the shape.But show type was wrong.So this is why we need the Matrix to transform.My matrix constructions are build in blueprint.Unreal Engine as same as many Engines are the left handed coordinate system. So the Y axis is the up vector,X axis is the right vector,Z axis is the forward vector.We can not get the camera space space and its projection space easily in blueprint.So we only make these matrixes by ourselves.There are two matrixes function in the DirectXMath source code.They can help us to transform the space.


Projection Matrix ↓
However in the projection space the zero point of the axis system is on the left-down side.We can debug the mouse position.We can see the upleft side is the zero point.So we shoud add a minus sign at the Y axis.And here, in the projection space,the W vector is not matter,we can discard it.I only folllow the equation to make the matrix.

Debug the mouse position

(I am sorry i cant catch the arrows of the mouse.I only draw it. XD)


Material Editor

Result

Next we use the scene depth to contract the shadow map,and draw the shadow.

Shadow Acne(Bias 0.0)

Bias 0.001


If I have some things to update for this article,I will follow up.
Have fun 🙂