A question about shaders and removing unwanted variables

Hey everyone:)

For the game I’m working on, we’re going for a very non-realistic style and for that goal I created a basic material for shading the environment. First I made it unlit, but of course that doesn’t work because I do want character shadows. However there are many output pins for the default lit material that I don’t need, for example roughness, metallic and specular. The style is basically very flat. So currently the material is at 31 vertex shader instructions and 59 instructions for the base pass shader with only dynamic lighting.

My question is therefore: could anyone give me a torch to light the jungle that is the shaderfiles and help me figure out if there is a way to disable pins on the output so they don’t have to be calculated?
Or alternatively: Am I thinking too much about this and those three calculations have so minescule of an impact that I shouldn’t bother…

I’ll just also throw in that the only reason I’m using a lit material is because I want the characters shadow to show on the ground.


You can take a look at ShadingModels.usf / BasePassPixelShader.usf and modify existing standard shading to simplify calculations, however I think it would be pretty insignificant without actually changing how GBuffer is arranged and few other minor simplifications, and that would require some coding.

I’d say that not bothering about it would be pretty acceptable in most cases.

Thanks for the pointers, I’ll dig around some and figure out if it is worth it or not. I’m not shy to coding, just don’t know where to start with all these .usfs…

Edit: Could someone enlighten me on what the variables L, V, and N that are everywhere come from/stand for?

  • N - Surface normal at point
  • L - Vector from point to light
  • V - Vector from point to viewer

Aha! Thanks that makes a lot of sense actually.

You should probably use forward shading. If you input variables are static then shader compiler can get rid of all the math is not needed or is constant. In deferred this is not as beneficial because most of the math is deferred to later passes.

Yes, I have stumbled upon some forward shading functions, and also a lot of different BRDF functions in the shader files, but I’m not sure how to use them in my project. Can you please clarify a bit more Kalle? I’d appreciate it enormously!

There is project setting that you want to enable forward shading. This should already help a lot. Then go to code file:
And start to nuke everything that seems to be unrelevant, remember that shader compiler get rid of all dead code anyway. You need to write your own shading model for your custom need but it should be quite simple.

color = BaseColor * lerp(1.0, shadowColor, shadowAmount);

Okay I’m waiting for the engine to compile. Basicly my first try is just gonna have the diffuse color where light shines and shadow color where there is supposed to be shadow. I thought of using the subsurfacecolor pin for this. Later I will try to figure out how to do the shadow clamping for toon effect and a variable for adjusting where the shadow starts. So while I wait for the engine to compile, can anyone point out how horribly stupid this is? Keep in mind I’m completely fresh to writing shaders…

float3 StylizedShading( FGBufferData GBuffer, float3 L, float3 V, half3 N )

float3 shadowColor = ExtractSubsurfaceColor(GBuffer);

float3 H = normalize(V + L);  
float NoH = saturate( dot(N, H) );  
float3 color = lerp(GBuffer.DiffuseColor, shadowColor, NoH);

return color;  


This obviously didn’t work the way I intended it too, gonna look a bit more at it tomorrow(been busy with university)

This is weird, even with a simple return GBuffer.Diffusecolor; I still get roughness and metallic in it. I’m in shadingmodels.usf am I in the wrong spot?

You are correct about GBuffer.DiffuseColor.
If you are confused about GBuffer structure, you can open DeferredShadingCommon.usf and on the line 306( as for version 4.13.1) you can take a look at FGBufferData struct.

Ohh, many thanks! For reference, this is what I’m trying to make my shading model do. Basicly grant access to where shadow begins, what color the lit part is and what color the shadowpart is:)


So far I’ve managed to make it do this: (shadow falloff is behaving really weird, and there’s still some fresnel-action going on hmm)

this is with just a 3float plugged into basecolor and a constant 0 in customdata



Hooray! My GBuffer.CustomData.x now controls where the shadow starts!
Now I have to figure out why it’s glowing, and why it’s still doing that fresnel, how to change the color of the shadowedpart and see if I can make the falloff even less pixelated…