[HLSL CODE] 3D LUT
Posted: 14 Jun 2015, 16:26
UE4, Unity compatible 256x16 look up table(LUT)
https://docs.unrealengine.com/latest/IN ... orGrading/
https://docs.unity3d.com/Manual/script- ... ookup.html
the neutral lut here:
dx11: ( Skyrim SE, Fallout 4 )
dx9: (Skrim, Fallout 3, Fallout NV)
other dimension will require modifying some constant.
https://docs.unrealengine.com/latest/IN ... orGrading/
https://docs.unity3d.com/Manual/script- ... ookup.html
the neutral lut here:
dx11: ( Skyrim SE, Fallout 4 )
Code: Select all
//////////////////////////////////////////////////////////////////////////////
// loading lut textures
//////////////////////////////////////////////////////////////////////////////
//ignore this if using DNI 3d lut instead
Texture2D LUTtex <string ResourceName="LUT.png"; >;
//DNI luts, ignore these if not going for DNI 3d lut
Texture2D LUTtex_ExtNight <string ResourceName="LUT_ExtNight.png"; >; // for exterior night
Texture2D LUTtex_ExtDay <string ResourceName="LUT_ExtDay.png"; >; // for exterior day
Texture2D LUTtex_Int <string ResourceName="LUT_Int.png"; >; // for interior
//////////////////////////////////////////////////////////////////////////////
//3d lut function
//////////////////////////////////////////////////////////////////////////////
static const float2 CLut_Size = float2(256.0, 16.0);
float3 CLutFunc( float3 colorIN, Texture2D lutTexIn ) {
float2 CLut_pSize = 1.0 / CLut_Size;
float4 CLut_UV;
colorIN = saturate(colorIN) * ( CLut_Size.y - 1.0);
CLut_UV.w = floor(colorIN.b);
CLut_UV.xy = (color.rg + 0.5) * CLut_pSize;
CLut_UV.x += CLut_UV.w * CLut_pSize.y;
CLut_UV.z = CLut_UV.x + CLut_pSize.y;
return = lerp(lutTexIn.SampleLevel(Sampler1, CLut_UV.xy, 0).rgb,
lutTexIn.SampleLevel(Sampler1, CLut_UV.zy, 0).rgb, color.b - CLut_UV.w);
}
//////////////////////////////////////////////////////////////////////////////
//pixel shader
//////////////////////////////////////////////////////////////////////////////
float4 PS_Draw(VS_OUTPUT_POST IN, float4 v0 : SV_Position0) : SV_Target
{
//... other stuff
// without DNI:
color.rgb = ClutFunc(color.rgb, LUTtex);
// DNI example:
color.rgb = lerp(lerp(CLutFunc(color.rgb, LUTtex_ExtNight),
CLutFunc(color.rgb, LUTtex_ExtDay), ENightDayFactor),
CLutFunc(color.rgb, LUTtex_Int), EInteriorFactor);
//... other stuff
};
Code: Select all
//////////////////////////////////////////////////////////////////////////////
//texture helper
//////////////////////////////////////////////////////////////////////////////
#define LOAD_TEXTURE( Name, Path, sampler, UV ) \
texture2D tex##Name <string ResourceName= #Path ; >; \
sampler2D Sampler##Name = sampler_state { \
Texture = < tex##Name >; SRGBTexture=FALSE; \
MinFilter = sampler; MagFilter = sampler; MipFilter = NONE; \
AddressU = UV; AddressV = UV; }
//////////////////////////////////////////////////////////////////////////////
// loading lut textures
//////////////////////////////////////////////////////////////////////////////
//ignore this if using DNI 3d lut instead
LOAD_TEXTURE( LUT, LUT.bmp, LINEAR, CLAMP );
//DNI luts, ignore these if not going for DNI 3d lut
LOAD_TEXTURE( LUT_ExtNight, LUT_ExtNight.bmp, LINEAR, CLAMP ); // exterior night
LOAD_TEXTURE( LUT_ExtDay, LUT_ExtDay.bmp, LINEAR, CLAMP ); // exterior day
LOAD_TEXTURE( LUT_Int, LUT_Int.bmp, LINEAR, CLAMP ); // interior
//////////////////////////////////////////////////////////////////////////////
//Lut Function
//////////////////////////////////////////////////////////////////////////////
static const float2 CLut_Size = float2(256.0, 16.0);
float3 CLutFunc( float3 colorIN, sampler2D LutSampler ) {
float2 CLut_pSize = 1.0 / CLut_Size;
float4 CLut_UV;
colorIN = saturate(colorIN) * ( CLut_Size.y - 1.0);
CLut_UV.w = floor(colorIN.b);
CLut_UV.xy = (colorIN.rg + 0.5) * CLut_pSize;
CLut_UV.x += CLut_UV.w * CLut_pSize.y;
CLut_UV.z = CLut_UV.x + CLut_pSize.y;
return lerp( tex2Dlod(LutSampler, CLut_UV.xyzz).rgb,
tex2Dlod(LutSampler, CLut_UV.zyzz).rgb, colorIN.b - CLut_UV.w);
}
//////////////////////////////////////////////////////////////////////////////
//Pixel Shader
//////////////////////////////////////////////////////////////////////////////
// float4 PS_D6EC7DD1(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR //Skyrim LE
float4 PS_C1DAE3F7(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR //Fallout 3
{
//... other stuff
// without DNI:
color.rgb = ClutFunc(color.rgb, SamplerLUT);
// DNI example:
color.rgb = lerp( lerp( CLutFunc(color.rgb, SamplerLUT_ExtNight),
CLutFunc(color.rgb, SamplerLUT_ExtDay), ENightDayFactor),
CLutFunc(color.rgb, SamplerLUT_Int), EInteriorFactor);
//... other stuff
};