Skip to content

Commit

Permalink
Add support for AgX tone mapping
Browse files Browse the repository at this point in the history
Closes #555
  • Loading branch information
vanruesc committed Dec 24, 2023
1 parent b1b5244 commit b15f9a8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
11 changes: 9 additions & 2 deletions src/effects/ToneMappingEffect.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LinearMipmapLinearFilter, Uniform, WebGLRenderTarget } from "three";
import { AdaptiveLuminancePass, LuminancePass } from "../passes/index.js";
import { BlendFunction, ToneMappingMode } from "../enums/index.js";
import { validateToneMappingMode } from "../utils/index.js";
import { Effect } from "./Effect.js";

import fragmentShader from "./glsl/tone-mapping.frag";
Expand All @@ -25,7 +26,7 @@ export class ToneMappingEffect extends Effect {
* @param {Object} [options] - The options.
* @param {BlendFunction} [options.blendFunction=BlendFunction.SRC] - The blend function of this effect.
* @param {Boolean} [options.adaptive=false] - Deprecated. Use mode instead.
* @param {ToneMappingMode} [options.mode=ToneMappingMode.ACES_FILMIC] - The tone mapping mode.
* @param {ToneMappingMode} [options.mode=ToneMappingMode.AGX] - The tone mapping mode.
* @param {Number} [options.resolution=256] - The resolution of the luminance texture. Must be a power of two.
* @param {Number} [options.maxLuminance=4.0] - Deprecated. Same as whitePoint.
* @param {Number} [options.whitePoint=4.0] - The white point.
Expand All @@ -38,7 +39,7 @@ export class ToneMappingEffect extends Effect {
constructor({
blendFunction = BlendFunction.SRC,
adaptive = false,
mode = adaptive ? ToneMappingMode.REINHARD2_ADAPTIVE : ToneMappingMode.ACES_FILMIC,
mode = adaptive ? ToneMappingMode.REINHARD2_ADAPTIVE : ToneMappingMode.AGX,
resolution = 256,
maxLuminance = 4.0,
whitePoint = maxLuminance,
Expand Down Expand Up @@ -123,6 +124,8 @@ export class ToneMappingEffect extends Effect {
this.defines.clear();
this.defines.set("TONE_MAPPING_MODE", value.toFixed(0));

value = validateToneMappingMode(value);

// Use one of the built-in tone mapping operators.
switch(value) {

Expand All @@ -138,6 +141,10 @@ export class ToneMappingEffect extends Effect {
this.defines.set("toneMapping(texel)", "ACESFilmicToneMapping(texel)");
break;

case ToneMappingMode.AGX:
this.defines.set("toneMapping(texel)", "AgXToneMapping(texel)");
break;

default:
this.defines.set("toneMapping(texel)", "texel");
break;
Expand Down
9 changes: 6 additions & 3 deletions src/effects/glsl/tone-mapping.frag
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ uniform float whitePoint;

#if TONE_MAPPING_MODE == 1 || TONE_MAPPING_MODE == 2

// Reinhard 2

uniform float middleGrey;

#if TONE_MAPPING_MODE == 2
Expand Down Expand Up @@ -48,9 +50,10 @@ uniform float whitePoint;

}

#elif TONE_MAPPING_MODE == 5
#elif TONE_MAPPING_MODE == 3

// Uncharted 2: http://filmicworlds.com/blog/filmic-tonemapping-operators

// http://filmicworlds.com/blog/filmic-tonemapping-operators
#define A 0.15
#define B 0.50
#define C 0.10
Expand Down Expand Up @@ -79,7 +82,7 @@ void mainImage(const in vec4 inputColor, const in vec2 uv, out vec4 outputColor)

outputColor = vec4(Reinhard2ToneMapping(inputColor.rgb), inputColor.a);

#elif TONE_MAPPING_MODE == 5
#elif TONE_MAPPING_MODE == 3

outputColor = vec4(Uncharted2ToneMapping(inputColor.rgb), inputColor.a);

Expand Down
10 changes: 6 additions & 4 deletions src/enums/ToneMappingMode.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
* @property {Number} REINHARD2 - Modified Reinhard tone mapping.
* @property {Number} REINHARD2_ADAPTIVE - Simulates the optic nerve responding to the amount of light it is receiving.
* @property {Number} OPTIMIZED_CINEON - Optimized filmic operator by Jim Hejl and Richard Burgess-Dawson.
* @property {Number} UNCHARTED2 - Uncharted 2 tone mapping. http://filmicworlds.com/blog/filmic-tonemapping-operators.
* @property {Number} ACES_FILMIC - ACES tone mapping with a scale of 1.0/0.6.
* @property {Number} UNCHARTED2 - Uncharted 2 tone mapping. http://filmicworlds.com/blog/filmic-tonemapping-operators
* @property {Number} AGX - Filmic tone mapping. Requires three r160 or higher. https://github.com/EaryChow/AgX.
*/

export const ToneMappingMode = {
REINHARD: 0,
REINHARD2: 1,
REINHARD2_ADAPTIVE: 2,
OPTIMIZED_CINEON: 3,
ACES_FILMIC: 4,
UNCHARTED2: 5
UNCHARTED2: 3,
OPTIMIZED_CINEON: 4,
ACES_FILMIC: 5,
AGX: 6
};
23 changes: 22 additions & 1 deletion src/utils/BackCompat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LinearEncoding, REVISION, sRGBEncoding } from "three";
import { LinearSRGBColorSpace, SRGBColorSpace } from "../enums/index.js";
import { LinearSRGBColorSpace, SRGBColorSpace, ToneMappingMode } from "../enums/index.js";

const revision = Number(REVISION.replace(/\D+/g, ""));
const useColorSpace = revision >= 152;
Expand Down Expand Up @@ -128,3 +128,24 @@ export function updateVertexShader(vertexShader) {
return vertexShader;

}

/**
* Validates the given tone mapping mode against the current version of three.
*
* @param {ToneMappingMode} mode - A tone mapping mode.
* @return {ToneMappingMode} The validated tone mapping mode.
* @ignore
*/

export function validateToneMappingMode(mode) {

if(revision < 160 && mode === ToneMappingMode.AGX) {

console.warn("AgX requires three r160 or higher, falling back to ACES filmic");
mode = ToneMappingMode.ACES_FILMIC;

}

return mode;

}

0 comments on commit b15f9a8

Please sign in to comment.