Download

Accessing a material parameter from a custom usf shader

I’m working on a custom shading model, but have been stuck for a while trying to access a value that is defined in C++ from my usf shader code.

I based my implementation on the various values in the Translucency Self Shadowing category of a material as they function in a similar fashion. Each of these has a GetMaterial* function in MaterialTemplate.usf. This function is then used to read the value in the actual shader code.

However when I try to do the same thing I get the following error:


Engine/Shaders/ToonShading.usf(12,20): Shader FPostProcessLpvIndirectPS, VF None:
	 error X3004: undeclared identifier 'GetMaterialToonSmoothing'

Replacing my get function with one of the ones I based my implementation on gives the same result:


Engine/Shaders/ToonShading.usf(12,20): Shader FPostProcessLpvIndirectPS, VF None:
	 error X3004: undeclared identifier 'GetMaterialTranslucencyDirectionalLightingIntensity'

Which seems to indicate I am simply missing an #include. However, I can’t seem to find what I need to include. Directly including MaterialTemplate.usf doesn’t work (and isn’t done in any of the other usf’s that do work) and just randomly trying others either has no effect or causes the following error:


	error: Can't open include file "Material.usf"
    #include "Material.usf"

I have the feeling I’m overlooking something really simple and obvious, but I have been staring at it way too long to find it, so any help would be really appreciated.

Something else odd that might be related: The engine seems to be able to compile a number of other shaders just fine before failing on the LPV post process shader in the above errors. Neither those other shaders, nor this one should even use my custom shading model, so it seems strange that some compile fine while this shader is always the one where compilation fails.

I should admit I’ve only been messing around with the USF shader for a few days now, but maybe I can help.

Yesterday, I was having an issue in my pixel shader where I was getting a similar error but with the ‘GetMaterialBaseColor’ function being the undeclared identifier. This was copy and pasted from an existing shader and very obviously worked there. In the output, C++ was giving I was also getting a warning about truncating a vector variable I had in the PS, this was the actual problem I had to resolve.

Check again the debug information you’re getting, if you have any warnings then fix that and hopefully everything should be fixed.

Weird, I did not get a notification of your reply.

Anyway, thanks for that, I’ll have a look at the rest of the output log if there is something else hidden in it. The shader compiles just fine with the function replaced by a hardcoded value though, so even if there is another error I would expect it to be related to this.

So after a bit more experimenting I got it working when using one of my custom GetMaterial* functions when editing some usf files, but not in others.

I also did not find anything else in the log.

I have also been unable to find any form of online information about the way the shader translator handles this type of function.

Another update on my progress:

I thought I had worked around the problem by using the CustomData GBuffer channel instead of trying to add my own parameters in a similar way as the clear coat shading model works. This worked fine for desktop rendering, but while trying to get mobile working I noticed that CustomData is not available in the mobile path. So I could really use some help with this :confused:

In case you are wondering what I need it for, here’s the WIP forum thread with my current results.