Skip to content

Commit

Permalink
WebGLRenderer: Add Support for Khronos Neutral Tone Mapping (#27668)
Browse files Browse the repository at this point in the history
* added Commerce tone mapping

* fix test

* Update OutputPass.js

Add support for `CommereceToneMapping`.

* Update OutputShader.js

Add support for `CommerceToneMapping`.

* Update tonemapping_pars_fragment.glsl.js

Add reference for `CommerceToneMapping`.

* rename

* fix typo

---------

Co-authored-by: Michael Herzog <[email protected]>
  • Loading branch information
elalish and Mugen87 authored Feb 9, 2024
1 parent c2b4d2f commit 996903f
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions examples/jsm/postprocessing/OutputPass.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
CineonToneMapping,
AgXToneMapping,
ACESFilmicToneMapping,
KhronosNeutralToneMapping,
SRGBTransfer
} from 'three';
import { Pass, FullScreenQuad } from './Pass.js';
Expand Down Expand Up @@ -61,6 +62,7 @@ class OutputPass extends Pass {
else if ( this._toneMapping === CineonToneMapping ) this.material.defines.CINEON_TONE_MAPPING = '';
else if ( this._toneMapping === ACESFilmicToneMapping ) this.material.defines.ACES_FILMIC_TONE_MAPPING = '';
else if ( this._toneMapping === AgXToneMapping ) this.material.defines.AGX_TONE_MAPPING = '';
else if ( this._toneMapping === KhronosNeutralToneMapping ) this.material.defines.KHRONOS_NEUTRAL_TONE_MAPPING = '';

this.material.needsUpdate = true;

Expand Down
4 changes: 4 additions & 0 deletions examples/jsm/shaders/OutputShader.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ const OutputShader = {
gl_FragColor.rgb = AgXToneMapping( gl_FragColor.rgb );
#elif defined( KHRONOS_NEUTRAL_TONE_MAPPING )
gl_FragColor.rgb = KhronosNeutralToneMapping( gl_FragColor.rgb );
#endif
// color space
Expand Down
1 change: 1 addition & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export const CineonToneMapping = 3;
export const ACESFilmicToneMapping = 4;
export const CustomToneMapping = 5;
export const AgXToneMapping = 6;
export const KhronosNeutralToneMapping = 7;
export const AttachedBindMode = 'attached';
export const DetachedBindMode = 'detached';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,28 @@ vec3 AgXToneMapping( vec3 color ) {
}
// https://modelviewer.dev/examples/tone-mapping
vec3 KhronosNeutralToneMapping( vec3 color ) {
float startCompression = 0.8 - 0.04;
float desaturation = 0.15;
color *= toneMappingExposure;
float x = min(color.r, min(color.g, color.b));
float offset = x < 0.08 ? x - 6.25 * x * x : 0.04;
color -= offset;
float peak = max(color.r, max(color.g, color.b));
if (peak < startCompression) return color;
float d = 1. - startCompression;
float newPeak = 1. - d * d / (peak + d - startCompression);
color *= newPeak / peak;
float g = 1. - 1. / (desaturation * (peak - newPeak) + 1.);
return mix(color, vec3(1, 1, 1), g);
}
vec3 CustomToneMapping( vec3 color ) { return color; }
`;
6 changes: 5 additions & 1 deletion src/renderers/webgl/WebGLProgram.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { WebGLUniforms } from './WebGLUniforms.js';
import { WebGLShader } from './WebGLShader.js';
import { ShaderChunk } from '../shaders/ShaderChunk.js';
import { NoToneMapping, AddOperation, MixOperation, MultiplyOperation, CubeRefractionMapping, CubeUVReflectionMapping, CubeReflectionMapping, PCFSoftShadowMap, PCFShadowMap, VSMShadowMap, AgXToneMapping, ACESFilmicToneMapping, CineonToneMapping, CustomToneMapping, ReinhardToneMapping, LinearToneMapping, GLSL3, LinearSRGBColorSpace, SRGBColorSpace, LinearDisplayP3ColorSpace, DisplayP3ColorSpace, P3Primaries, Rec709Primaries } from '../../constants.js';
import { NoToneMapping, AddOperation, MixOperation, MultiplyOperation, CubeRefractionMapping, CubeUVReflectionMapping, CubeReflectionMapping, PCFSoftShadowMap, PCFShadowMap, VSMShadowMap, AgXToneMapping, ACESFilmicToneMapping, KhronosNeutralToneMapping, CineonToneMapping, CustomToneMapping, ReinhardToneMapping, LinearToneMapping, GLSL3, LinearSRGBColorSpace, SRGBColorSpace, LinearDisplayP3ColorSpace, DisplayP3ColorSpace, P3Primaries, Rec709Primaries } from '../../constants.js';
import { ColorManagement } from '../../math/ColorManagement.js';

// From https://www.khronos.org/registry/webgl/extensions/KHR_parallel_shader_compile/
Expand Down Expand Up @@ -124,6 +124,10 @@ function getToneMappingFunction( functionName, toneMapping ) {
toneMappingName = 'AgX';
break;

case KhronosNeutralToneMapping:
toneMappingName = 'KhronosNeutral';
break;

case CustomToneMapping:
toneMappingName = 'Custom';
break;
Expand Down
1 change: 1 addition & 0 deletions test/unit/src/constants.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export default QUnit.module( 'Constants', () => {
assert.equal( Constants.ACESFilmicToneMapping, 4, 'ACESFilmicToneMapping is equal to 4' );
assert.equal( Constants.CustomToneMapping, 5, 'CustomToneMapping is equal to 5' );
assert.equal( Constants.AgXToneMapping, 6, 'AgXToneMapping is equal to 6' );
assert.equal( Constants.KhronosNeutralToneMapping, 7, 'KhronosNeutralToneMapping is equal to 7' );

assert.equal( Constants.AttachedBindMode, 'attached', 'AttachedBindMode is equal to attached' );
assert.equal( Constants.DetachedBindMode, 'detached', 'DetachedBindMode is equal to detached' );
Expand Down

0 comments on commit 996903f

Please sign in to comment.