Skip to content

Commit 187baae

Browse files
Added 3D commerce tone mapping
1 parent 824ef31 commit 187baae

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

Components/src/ToneMapping.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ bool ToneMappingUpdateUI(HLSL::ToneMappingAttribs& Attribs, float* AverageLogLum
8686
{
8787
bool AttribsChanged = false;
8888
{
89-
std::array<const char*, 11> ToneMappingMode{};
89+
std::array<const char*, 12> ToneMappingMode{};
9090
ToneMappingMode[TONE_MAPPING_MODE_NONE] = "None";
9191
ToneMappingMode[TONE_MAPPING_MODE_EXP] = "Exp";
9292
ToneMappingMode[TONE_MAPPING_MODE_REINHARD] = "Reinhard";
@@ -98,6 +98,7 @@ bool ToneMappingUpdateUI(HLSL::ToneMappingAttribs& Attribs, float* AverageLogLum
9898
ToneMappingMode[TONE_MAPPING_MODE_AGX] = "AgX";
9999
ToneMappingMode[TONE_MAPPING_MODE_AGX_CUSTOM] = "AgX Custom";
100100
ToneMappingMode[TONE_MAPPING_MODE_PBR_NEUTRAL] = "PBR Neutral";
101+
ToneMappingMode[TONE_MAPPING_MODE_COMMERCE] = "Commerce";
101102
if (ImGui::Combo("Tone Mapping Mode", &Attribs.iToneMappingMode, ToneMappingMode.data(), static_cast<int>(ToneMappingMode.size())))
102103
AttribsChanged = true;
103104
}

Shaders/PostProcess/ToneMapping/public/ToneMapping.fxh

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ float3 ToneMap(in float3 f3Color, ToneMappingAttribs Attribs, float fAveLogLum)
117117
}
118118
# else
119119
{
120-
fToneMappedLum = L_xy * (1.0 + L_xy / (whitePoint*whitePoint)) / (1.0 + L_xy);
120+
fToneMappedLum = L_xy * (1.0 + L_xy / (whitePoint*whitePoint)) / (1.0 + L_xy);
121121
}
122122
# endif
123-
return fToneMappedLum * pow(f3Color / fInitialPixelLum, Attribs.fLuminanceSaturation * float3(1.0, 1.0, 1.0));
123+
return fToneMappedLum * pow(f3Color / fInitialPixelLum, Attribs.fLuminanceSaturation * float3(1.0, 1.0, 1.0));
124124
}
125125
#elif TONE_MAPPING_MODE == TONE_MAPPING_MODE_UNCHARTED2
126126
{
@@ -143,7 +143,7 @@ float3 ToneMap(in float3 f3Color, ToneMappingAttribs Attribs, float fAveLogLum)
143143
{
144144
// http://www.mpi-inf.mpg.de/resources/tmo/logmap/logmap.pdf
145145
float fToneMappedLum = log10(1.0 + fScaledPixelLum) / log10(1.0 + whitePoint);
146-
return fToneMappedLum * pow(f3Color / fInitialPixelLum, Attribs.fLuminanceSaturation * float3(1.0, 1.0, 1.0));
146+
return fToneMappedLum * pow(f3Color / fInitialPixelLum, Attribs.fLuminanceSaturation * float3(1.0, 1.0, 1.0));
147147
}
148148
#elif TONE_MAPPING_MODE == TONE_MAPPING_MODE_ADAPTIVE_LOG
149149
{
@@ -152,7 +152,7 @@ float3 ToneMap(in float3 f3Color, ToneMappingAttribs Attribs, float fAveLogLum)
152152
float fToneMappedLum =
153153
1.0 / log10(1.0 + whitePoint) *
154154
log(1.0 + fScaledPixelLum) / log( 2.0 + 8.0 * pow( fScaledPixelLum / whitePoint, log(Bias) / log(0.5)) );
155-
return fToneMappedLum * pow(f3Color / fInitialPixelLum, Attribs.fLuminanceSaturation * float3(1.0, 1.0, 1.0));
155+
return fToneMappedLum * pow(f3Color / fInitialPixelLum, Attribs.fLuminanceSaturation * float3(1.0, 1.0, 1.0));
156156
}
157157
#elif TONE_MAPPING_MODE == TONE_MAPPING_MODE_AGX
158158
{
@@ -176,7 +176,7 @@ float3 ToneMap(in float3 f3Color, ToneMappingAttribs Attribs, float fAveLogLum)
176176
// https://github.com/KhronosGroup/ToneMapping/blob/main/PBR_Neutral/pbrNeutral.glsl
177177
float StartCompression = 0.8 - 0.04;
178178
float Desaturation = 0.15;
179-
179+
180180
float x = min(f3Color.r, min(f3Color.g, f3Color.b));
181181
float Offset = x < 0.08 ? x - 6.25 * x * x : 0.04;
182182
f3Color -= Offset;
@@ -191,7 +191,28 @@ float3 ToneMap(in float3 f3Color, ToneMappingAttribs Attribs, float fAveLogLum)
191191
float g = 1.0 - 1.0 / (Desaturation * (Peak - NewPeak) + 1.0);
192192
f3Color = lerp(f3Color, float3(NewPeak, NewPeak, NewPeak), g);
193193
}
194-
return f3Color;
194+
return f3Color;
195+
}
196+
#elif TONE_MAPPING_MODE == TONE_MAPPING_MODE_COMMERCE
197+
{
198+
// https://github.com/google/model-viewer/pull/4495
199+
float StartCompression = 0.8;
200+
float Desaturation = 0.5;
201+
202+
float d = 1.0 - StartCompression;
203+
float Peak = max(f3Color.r, max(f3Color.g, f3Color.b));
204+
if (Peak >= StartCompression)
205+
{
206+
float NewPeak = 1.0 - d * d / (Peak + d - StartCompression);
207+
float InvPeak = 1.0 / Peak;
208+
209+
float ExtraBrightness = dot(f3Color * (1.0 - StartCompression * InvPeak), float3(1.0, 1.0, 1.0));
210+
211+
f3Color *= NewPeak * InvPeak;
212+
float g = 1.0 - 3.0 / (Desaturation * ExtraBrightness + 3.0);
213+
f3Color = lerp(f3Color, float3(1.0, 1.0, 1.0), g);
214+
}
215+
return f3Color;
195216
}
196217
#else
197218
{

Shaders/PostProcess/ToneMapping/public/ToneMappingStructures.fxh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define TONE_MAPPING_MODE_AGX 8
2020
#define TONE_MAPPING_MODE_AGX_CUSTOM 9
2121
#define TONE_MAPPING_MODE_PBR_NEUTRAL 10
22+
#define TONE_MAPPING_MODE_COMMERCE 11
2223

2324
struct AgXAttribs
2425
{

0 commit comments

Comments
 (0)