Skip to content

Instantly share code, notes, and snippets.

@da-molchanov
Last active December 23, 2023 16:29
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save da-molchanov/85b95ab3f20fa4eb5624f9b3b959a0ee to your computer and use it in GitHub Desktop.
Save da-molchanov/85b95ab3f20fa4eb5624f9b3b959a0ee to your computer and use it in GitHub Desktop.
Simple custom ACES tonemapper that approximates the default UE4 filmic tonemapper in 11 instructions.
// Copyright 2021 Dmitry Molchanov and Julia Molchanova
// This code is licensed under the MIT license
// Output type: CMOT float 3
// Input name: LinearColor
// This multiplier corresponds to "ExposureCompensation=1" and disabled auto exposure
const float ExposureMultiplier = 1.4;
const float3x3 PRE_TONEMAPPING_TRANSFORM =
{
0.575961650, 0.344143820, 0.079952030,
0.070806820, 0.827392350, 0.101774690,
0.028035252, 0.131523770, 0.840242300
};
const float3x3 EXPOSED_PRE_TONEMAPPING_TRANSFORM = ExposureMultiplier * PRE_TONEMAPPING_TRANSFORM;
const float3x3 POST_TONEMAPPING_TRANSFORM =
{
1.666954300, -0.601741150, -0.065202855,
-0.106835220, 1.237778600, -0.130948950,
-0.004142626, -0.087411870, 1.091555000
};
// Transform color spaces, perform blue correction and pre desaturation
float3 WorkingColor = mul(EXPOSED_PRE_TONEMAPPING_TRANSFORM, LinearColor);
// Apply tonemapping curve
// Narkowicz 2016, "ACES Filmic Tone Mapping Curve"
// https://knarkowicz.wordpress.com/2016/01/06/aces-filmic-tone-mapping-curve/
const float a = 2.51;
const float b = 0.03;
const float c = 2.43;
const float d = 0.59;
const float e = 0.14;
WorkingColor = saturate((WorkingColor * (a * WorkingColor + b)) / (WorkingColor * (c * WorkingColor + d) + e));
// Transform color spaces, apply blue correction and post desaturation
return mul( POST_TONEMAPPING_TRANSFORM, WorkingColor );
@da-molchanov
Copy link
Author

da-molchanov commented Oct 20, 2021

Put the code into a Custom node with a LinearColor input variable and CMOT float 3 output type. Docs on custom nodes.
Looks like this (top right). The spheres have pure R G or B color with different emission values.
Tonemapping_11_lr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment