Exposing the BRDF Models to the Material Editor?

So as promised here is a quick rundown of adding a new lighting model ID. In the code below, the new lighting model ID will be called Foliage

====================================================================================================================================
DeferredShadingCommon.usf

[Line 128] or [Line 253 in v4.2]
This is where you define new lighting models. Make sure to update the MAX any time you add a new one


#define LIGHTINGMODELID_FOLIAGE 4
#define LIGHTINDMODELID_NUM 5

[Line 212] or [Line 340 in v4.2]
This line encodes the lighting model ID into the range of 0.f - 1.f for storing in the GBuffer


OutGBufferA.a = Data.LightingModelId / ( (float) LIGHTINGMODELID_NUM - 1.f );

[Line 247] or [Line 374 in v4.2]
This function decodes the lighting model ID from the GBuffer


int DecodeLightingModelId( float4 InGBufferA )
{
	return (int) ( InGBufferA.a * ( (float) LIGHTINGMODELID_NUM - 0.001f ) );
}

====================================================================================================================================
Engine\Source\Runtime\Engine\Classes\Engine\EngineTypes.h

[Line 109]
Engine side definition of the Material Lighting Model. This allows it to be selectable in the Material Editor


MLM_Foliage UMETA( DisplayName = "Foliage" ),
MLM_MAX

====================================================================================================================================
Engine\Source\Runtime\Engine\Private\Materials\MaterialShared.cpp

[Line 1131] or [Line 1146 in v4.2]
Ensure that the shader compiler sets our new custom lighting model if selected


case MLM_Foliage: OutEnvironment.SetDefine( TEXT( "MATERIAL_LIGHTINGMODEL_FOLIAGE" ), TEXT( "1" ) ); break;
default:

====================================================================================================================================
BasePassPixelShader.usf

[Line 711] or [Line 731 in v4.2]
Set the Lighting Model ID based on the Material Lighting Model selected


#elif MATERIAL_LIGHTINGMODEL_FOLIAGE
   Data.LightingModelId = LIGHTINGMODELID_FOLIAGE;
#else

====================================================================================================================================
DeferredLightingCommon.usf

[Line 262] or [Line 353 in v4.2]

GetDynamicLighting
This function is in charge of calculating lighting for a given position, normal, etc. and this is where you would
calculate your new lighting based on the LightingModelId

====================================================================================================================================

26e06ad8ed9b6be420e842ee0d10950b11c86ad2.jpeg
You should see a new option in the material editor for your new lighting model



To ensure your new lighting model ID is being applied, you can use the Lighting Model buffer visualization. Your new lighting model will be white. If you plan on having addition lighting models, you will need to update MaterialTemplate.usf at Line 797 to include new colors to be able to differentiate between the different models (Otherwise they will all appear white)


Here is an example of applying Oren-Nayar diffuse to the new lighting model, compared to the existing Default Lit lighting model