[UE4] Adding a custom shading model (Final)


Material instance of the material we are going to make

For the final part of this guide I will write about the few things I added to the material and my approach to do outlines which take in account the base color.
This part is optional you already have everything you need.
If you are still interested continue reading this part will have more pictures in it. :>



The material

This is the whole basic material. There is not much stuff in it yet and it’s up to you to extend it. On the left side we can see the color parameter or the base texture. Let’s component mask it so we don’t include our alpha in further calculations. The alpha goes directly into opacity / opacity mask. Even if the output pin is deactivated we can still make use of it later by overwriting the blending model inside of the material instance.

For the normal input either plug in a normal map or leave it at default (0, 0, 1). You could also do static switches for baseTexture and normalMap that switch between color input and texture input.

Create parameters for Specular, Metallic, Roughness and Smoothness and plug them into their pins. Smoothness goes into the ClearCoat pin.



Adding the fresnel

The fresnel gets connected as above. For it’s values I used exponent: 2.0 and base reflect fraction: 0.0. The smoothstep node doesn’t exist so we have to create it ourselves. Add a custom node and set it’s output type to float1. Add two inputs. Add the following code into it:

return smoothstep(0.5 - Threshold, 0.5 + Threshold, IN);

The multiply after this takes the base color / texture we masked as second input. Also add another scalar parameter called FresnelIntensity so that we can turn down it’s value.



The shadow coloring

For the colored shadow add a scalar parameter called ColoredShadow. This will control how much color will be added to the emissive. Keep in mind the closer rgb gets to 1 the smaller the effect of the power node will be. (Multiplying 1 x 1 = 1) Play around with the exp value of the power node. For me a value of 2.5  looked best for characters having light skin tone textures.

Lastly add the results of fresnel and colored shadow together and plug them into the emissive input. As you might have guessed this will make not only the shadow brighter but also the base texture but for now I don’t know of a better way to colorize shadows except doing that in a post process by somehow faking the calculated light.



Outline post process overview

Now for the colored outlines. This is basically a modified version of the outline highlighter post process that Epic used in the example content.



Lower left of the material

This part is for creating the offset of the outline. If you ever did any sobel outline or bluring or something similar you should be familiar with the concept. By comparing the basic renderTexture to offsetted versions of it we can find out the difference between neighbouring pixels.



Lower right of the material

Here we will do exactly that to compare the scene depth. We use the scene depth instead of the custom depth so that our outlines are not rendered in front of other geometry.



Upper half of the material

This part is quite a mess. We offset the base color and the custom depth. The custom depth is used as a mask for the color so that we can separate the objects that should have the outline from the ones that shouldn’t. I decided to combine the colors by using the max node so that the brighter color will prevail. Once we have the offsetted colored silhouette of the base color we do some calculations to make it darker. We also combine and clamp all the custom depths to make a mask of the colored area. This part is probably not optimal as it uses quite a lot of texture lookups but I don’t now another way around it so if someone has a suggestion feel free to post a comment. :>

The final part of the post process is simple: We multiply the lower half with the custom depth mask we just created and use this as alpha for a lerp between our outline color and a SceneTexture that renders the scene as it is (PostProcessInput0). At the end we plug in everything into our emissive pin.



Material test on the stanford dragon

And that’s it. We created a custom shading model, wrote it’s shader code, made changes to it’s material and set up an outline. I hope someone finds this helpfulsomehow and would feel glad for any feedback may that be suggestions, critique or additions.

2 thoughts on “[UE4] Adding a custom shading model (Final)

  1. SuperMoMo

    Thank you, blogger, I follow your tutorial and get a nice result. But I have some confused about your last part, I have implemented your material and associated it with a post process volume, but I can only get a black screen. I don’t know what’s wrong with it. Could you help me about how to use this material?
    Thank you very much!

    1. FelixK Post author

      There are two materials described on this part one for the custom shading model from the previous posts and one for an outline post process.
      The outline material itself should work by simply adding it as “blendable” in a Postprocess Volume. (like you probably did)
      If it just displays everything as black it sounds like one of the ScreenTexture nodes doesn’t do it’s job properly.
      Finding the problem atm is kind of hard without seeing your complete setup or knowing at which node it fails.

      Possible solutions might be:
      1) Have you set your meshes to render custom depth (shouldn’t be the cause of the blackscreen)
      2) Are there any other PostProcesses around that might interfere?
      3) Try connecting the different ScreenTexture types in the material directly to the emissive output and see if any gives a black screen
      4) If all ScreenTextures work try connecting only parts of the material to the emissive to find out where exactly it turns black


Leave a Reply

Your email address will not be published. Required fields are marked *