This is a UE5 version of my shading model for UE4 that I built for my military academy dating simulator/military warfare tactical rpg/tank washing simulator.
A fair amount of stuff has changed since the previous thread, including the first number in the engine version it’s for so may as well make a new thread to contain that information.
In summary, this shading model allows you to specify exactly what unreal displays in shadow and lit areas. This doesnt have to follow any sort of realistic lighting logic, you can create materials that are brighter in shadow than where they are actually lit. Whatever you put in the shadow colour pin is what is displayed in shadow.
Here is Nicholas Cage to demonstrate.
and some basic spheres
Here’s how it works.
Material and Lighting Setup
This Shading Model hijacks the Clear Coat (default lit in source versions) Shading Model.
There are two options that this is geared towards.
#1 Standard material workflow
Most of this works the same as stock Unreal.
Base Colour works the same as stock Unreal, however I do not use the colour of light actors on the shading side. So if you have a surface and you want to apply a light colour to it you need to multiply that onto the base colour on the material side. I might change this back later but I’m working on some effect ideas that require this kind of setup. It’s also a nonissue unless you want to use more than just a single directional light which isn’t a purpose this shading model is really geared towards.
Metallic is shadow range, this defines how hard your shadow is. 1 is soft, 0 is hard.
Custom Data 0 is Shadow Colour. This is what renders when a surface is in shadow.
Custom Data 1 isn’t used for anything but it is exposed and will write a single channel to the gbuffer so it can be used to provide information to a post process material.
All that is needed on the lighting front is a single white moveable directional light with an intensity of 1. You can affect the size of the shadow by raising/lowering this, but this value is what I use.
Multiple lights is SORTA supported but they are tricky, their influence must not overlap or else you will get incorrect results. HOWEVER, if you enable the command r.UseClusteredDeferredShading you can stack point and spot lights to your hearts content. This isn’t a magic bullet right now unfortunately, they will still misbehave in certain circumstances, and if they touch a directional light/rect light and you should use lighting channels to prevent this from happening, but this opens up many lighting possibilities that were not possible to do correctly in previous versions.
Left is without clustered shading enabled, right is with.
This requires a post process to correct unlit pixels, it is included in the example project as PP_PointLightCorrection.
#2 Replace final output with a true lighting buffer for more advanced post process shaders.
This approach works by turning the final output Unreal creates into just the lighting information, and as such should be compatible with all lighting features, Lumen, Ray Tracing, etc. The downside, is that you’re still stuck with the limitations of working in the post process space, but if the first approach’s ability to control lighting is too limited for you, this should cover your needs.
In DeferredLightPixelShader.usf uncomment this line:
OutColor.rgb = Radiance.rgb;
and comment out
ShadowMask = smoothstep(0.5 - ShadowRange, 0.5 + ShadowRange, Luminance(Radiance));
OutColor.rgb = lerp(ShadowColour, PixelColour, ShadowMask);
I’m not using this approach, but people interested in creating their own custom shading through post processes will find this interesting given the significant drawbacks of the traditional Scenecolour divided by diffuse colour to get a crude approximation of the lighting information approach. This is just outright better in every single way than trying to do this with stock Unreal.
Installation
You can find my repository here: https://github.com/envieous/UnrealEngine-SelShader
Before you ask why you got a 404 after clicking that link, read this: Unreal Engine 4 on GitHub - Unreal Engine
For 5.3(Launcher branch) and onwards
Download my repository, and overwrite Engine/Shaders/Private in your LAUNCHER engine installation folder with the Engine/Shaders/Private folder from MY REPOSITORY.
For 5.3 (Source branch) and previous
Setup
These changes aren’t explicitly required, but if you’re trying to achieve a look similar to mine this is how I’m doing it.
Disable Static Lighting
Disable exposure adjustment here.
You’ll also likely want to look at your other post process settings, bloom and vignette in particular and pick values you like.
The tonemapper in Unreal is notorious for washing out/desaturating/and just generally ruining your colours in the name of physical accuracy. I have disabled the tonemapper and Unreal now outputs colours very close to what is inputted.
If for some reason you actually want Unreal to tOnEmAp your colours you can undo this change by changing this line in PostProcessTonemap.usf
half3 OutDeviceColor = LinearColor.rgb;
back to
half3 OutDeviceColor = ColorLookupTable(LinearColor);
Unreal also tonemaps UMG elements. I have no idea why ANYONE would desire this behavior, and unfortunately I can not remove this functionality without severely compromising the usability of the editor as it affects the ENTIRE editor UI as well.
For now the best way I’ve found to correct this is to create this material function to correct UMG colours.
This isn’t an ideal way of doing this and will probably cause problems, but it’s good enough for development, and can be removed at the end while also removing UMG tonemaping for the final build to achieve correct results.
To remove UMG tonemapping open SlateElementPixelShader.usf and comment out the lines:
OutColor = ApplyContrast(OutColor);
OutColor.rgb = GammaCorrect(OutColor.rgb);
Misc
I should probably mention Lumen. I’ve played with it a bit but it doesn’t seem feasible from a both a performance and art standpoint to utilize it in my project so if you want to use it I can’t really offer any suggestions or help, but if you make something cool with it in this shading model I’m very interested to see what people come up with.
Example Project(s)
This project has all the settings setup and contains further example content. There’s two folders in the archive, use launcher example for launcher version, source for source.
Download here.
Acknowledgements
Tom Looman for his Dr Facilier shadow demonstration that I used initially as the basis for my shading system.
Doomfest and Arnage , for their custom shading models. I studied a lot of their changes to figure out what was going on in Unreal’s USF files.
FelixK and Matt Hoffman for their articles on creating a custom shading model in Unreal. These articles despite being dated were still incredibly helpful for figuring all this out.
I’m pretty sure that covers everything. Enjoy.
You can find my Twitter here, and my Discord here.
Also if you’re interested or want to support this project, you can wishlist my game on Steam, each wishlist helps <3
Also consider leaving a post and vote here in the off chance of getting Epic to make custom shading models not require wasting hours of everyone’s time building the engine from source just to enable a couple material pins.
Finally, if you want to directly support further development of this shading model, you can do so on Patreon here: