Download

How to utilize .usf shaders correctly

Been wondering how to create shaders within a source build of UE4. I know how to write HLSL, and have limited knowledge of UE4’s .usf shaders. However, I have no idea what to do with .usf files I create. Various sources say that all I have to do is create an #include of my .usf shader within MaterialTemplate.usf and then add an #include within a custom node in the material editor, however as far as I know MaterialTemplate.usf does not exist. I’ve found MaterialTemplate.ush, however that doesn’t seem to be the right file. Ultimately, I just want to create a simple passthrough shader and somehow use it on an object with a custom shading model I’ve written.

Alternatively: instead of mucking around with .usf files, is it possible to create an HLSL shader and use that?

Create your USF. Inside of the material editor, place a custom node and within it, add the following code:

#include “/Project/YOURSHADERHERE.ush”
return 0;

You’ll need to create node inputs for all the various inputs to your struct

Within the shader file, you’ll need a basic setup like this:

#pragma once

struct SomeKindOfEffectWithLotsOfFunctions
{
float3 TestFunction(float3 A, float3 B, float3 C)
{
//more math stuff here
return A+B+C;
}
};

SomeKindOfEffectWithLotsOfFunctions material;
return material.TestFunction(Alpha,Beta,Charley); //these are named after your pin names on the custom node, but need to line up with the function input order

Realistically, you’ll have a ton of other functions within the struct. Just make sure to define them before the main function that gives the final output.

Awesome, I’ll be sure to try this out when I get the chance. Also, am I able to retrieve all lighting information with .usf? For example, within the material editor I cannot get all the lighting information so a Cel material’s shading is limited to only the directional light. Could I get each light affecting the object so I am able to apply the Cel shading per light, so point lighting and the likes on the object also get the Cel treatment?

Thanks, dude! :cool:

To the best of my knowledge, no, you won’t be able to do it with the default shading model. With a custom model, yes. The material is rendered before the lighting pass. The lighting pass reads the gbuffers for things like color, roughness, normal, metallic, etc, then works its magic from there. You’d need to do a little half-pass and feed in the lighting information during the material pass where you’d get things like the directions, cone angles, and so on. It would likely add on a cost similar to adding in a transparency pass(happens in the forward pass, after deferred pass).

What is the 4.26 way of using includes?

I get crashes and errors no matter what file path I use when attempting to add a custom.usf file to the include path of a custom node.

bump, what’s 4.26 way of including?