keywords:UE4, Shadow Optimization, Scene Optimization, 阴影, 场景, 优化

The latest article: UE5 Shadow Notes

Shadow Optimization

Enable Dynamic Shadow Distance

Testing Case: 500 Actors are rendering in screen, Camera height is 4000, DirectionalLight’s property Dynamic Shadow Distance StationaryLight (Default value is 0, which means disabled) should be larger than the straight-line distance from Camera to Actor (Beacause the distance need to cover all Actors, so the distance would be better be larger ), otherwise frame rate would drop down from 200 fps to 100 fps.

How to Cast Shadow by Camera Distance

Three factors:

  • RayTraced Distance Field Soft Shadows
  • Scalability of Shadow
  • Max Draw Distance
RayTraced Distance Field Soft Shadows
  1. Project Settings -> Engine -> Rendering -> Lighting -> enable Generate Mesh Distance Fields (Need to restart Editor)

  2. Modify DirectionalLight’s properties: DistanceField Shadow Distance and RayTraced DistanceField Shadows

    Effect

Relationship between DistanceField Shadow Distance & Dynamic Shadow Distance
  • Stationary

  • Moveable

Reference:RayTraced Distance Field Soft Shadows
https://docs.unrealengine.com/en-us/Engine/Rendering/LightingAndShadows/RayTracedDistanceFieldShadowing

Scalability of Shadow
  • Cinematic

  • Medium

  • Low

Scalability Reference
https://docs.unrealengine.com/en-US/engine/performance/scalability/scalabilityreference

Max Draw Distance

Max Draw Distance = 5000
Max Distance Fade Range = 0

Max Draw Distance = 5000
Max Distance Fade Range = 2500

Cast Shadow by Level of Detail (LOD)

Console command:

//LOD level to force for the shadow map generation only, -1 is off.
r.ForceLODShadow

It works only for non-dynamic lights.

Dynamic Shadows for Dense Geometry

How to optimize the performance of dynamic shadows?
Steps:

  1. Disable Cast Shadow in original static mesh.
  2. Add Proxy Geometry settings in Proxy Mesh tab of Merge Actor tool.
  3. Leave Cast Shadows enabled on the proxy shadow caster.
  4. In the Details panel under Rendering , disable Render in Main Pass on the proxy shadow caster.

Proxy Geometry Shadows
https://docs.unrealengine.com/4.27/en-US/TestingAndOptimization/ProxyGeoTool/ProxyGeoShadows/
TIP: Shadow Optimization - Proxy Shadows
https://dev.epicgames.com/documentation/en-us/unreal-engine/proxy-geometry-tool-overview-in-unreal-engine

ShadowMap Memory Size

Testing case:
Place 1024 cubes in an empty level, casting shadow by stationary DirectionalLight, and check the memory cost of shadow map. (console cmd:stat rhi)

Shadows quality: Low

Shadows quality: Cinematic

Conclusion:
The higher the shadow quality, the larger the memory cost of shadow map.

Another factor affected the memory cost for shadow computing is the light map resolution:

The larger the light map resolution, and higher of texture atlas res which packed and stored for light map and shadow map is.
Sample of texture atlas:

What’s the difference between light map and shadow map?
Light map (created by Lightmass) is used to precompute indirect lighting (interreflection, e.g. GI) of lights with stationary and static mobility, while shadow map is ued to store surface’s depth value of object which can be irradiated by direct lighting (stationary light or movable light).

Shadow Optimization for Foliage

Contact Shadow is screen space, distance of the shadow has no difference in performance. It’s used in combination with other shadow methods for cheap extra detail.

References:
Lighting: Unreal 5 Features Reference
https://medium.com/@shinsoj/lighting-features-cheat-sheet-5b81b63b3ab7
Virtual Shadow Maps in Fortnite Battle Royale Chapter 4
https://www.unrealengine.com/en-US/tech-blog/virtual-shadow-maps-in-fortnite-battle-royale-chapter-4

How to cast character shadow on mobile device

1st way: Movable Directional Light

How to Enable Dynamic Shadows & Correct Reflection Maps on Mobile
https://forums.unrealengine.com/development-discussion/android-development/41661-tip-how-to-enable-dynamic-shadows-correct-reflection-maps-on-mobile

Lighting for Mobile Platforms
https://docs.unrealengine.com/latest/INT/Platforms/Mobile/Lighting/

4.7 release mobile shadows
https://answers.unrealengine.com/questions/180482/47-release-mobile-shadows.html

2nd way: Stationary Directional Light

Enable Cast Modulated Shadows:

Casting Shadow (Common questions)

How to Cast Shadow for SkeletalMesh

Constructor:

UPrimitiveComponent->CastShadow = true;

Run-time:

UPrimitiveComponent->SetCastShadow(true);

UPrimitiveComponent->CastShadow = true; would not work out of Constructor.

How to cast shadow when Actor hidden in game
USkeletalMeshComponent->SetHiddenInGame(true);
USkeletalMeshComponent->bCastHiddenShadow = true;
How to prevent mesh from receiving shadows

Set material’s Shading Model to Unlit, then set you base color to go to the emissive(so its not totally black), but Matellic and Specular lost.

Reference
https://answers.unrealengine.com/questions/258748/how-to-disable-receiving-shadows-on-a-particular-m.html

Reference

Documents

Dynamic Scene Shadows
https://docs.unrealengine.com/en-us/Resources/ContentExamples/DynamicSceneShadows

Cascaded Shadows
https://docs.unrealengine.com/en-us/Platforms/Mobile/Lighting/HowTo/CascadedShadow

RayTraced Distance Field Soft Shadows
https://docs.unrealengine.com/en-us/Engine/Rendering/LightingAndShadows/RayTracedDistanceFieldShadowing

Shadow Casting
https://docs.unrealengine.com/en-us/Engine/Rendering/LightingAndShadows/Shadows

Unreal Engine 4 的 光和影
https://www.unrealengine.com/zh-CN/blog/chn-unreal-engine-4-light-and-shadow

[UE4] Introduction to Shadow Rendering function
https://qiita.com/EGJ-Yutaro_Sawada/items/2a8a1467ebaaa9795fe0

Shadow Casting: High level overview of shadows. (Recommended)
https://docs.unrealengine.com/4.27/en-US/BuildingWorlds/LightingAndShadows/Shadows/

UDK(UE3) Shadowing Reference (Recommended)
https://docs.unrealengine.com/udk/Three/ShadowingReference.html

Fake Dynamic Lighting

Using modulate blend material to make a light
https://twitter.com/klemen_lozar/status/1033409819116359681

How to Create Blob Shadows in Unreal Engine 4 (Part 1)
https://www.youtube.com/watch?v=qMYpAFGpNv0

Shadow Types

UE中的Shadow技术 (Recommended)
https://www.jianshu.com/p/982776b93d2b

Capsule Shadows
https://docs.unrealengine.com/en-US/Engine/Rendering/LightingAndShadows/CapsuleShadows/Overview/index.html

Screen Space Contact Shadows
https://docs.unrealengine.com/4.27/en-US/BuildingWorlds/LightingAndShadows/ContactShadows/


人会长大三次。第一次是在发现自己不是世界中心的时候。第二次是在发现即使再怎么努力,终究还是有些事令人无能为力的时候。第三次是在,明知道有些事可能会无能为力,但还是会尽力争取的时候。