Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebGLRenderer: Add .setLookCDL() #28544

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,13 @@ class WebGLRenderer {

this._outputColorSpace = SRGBColorSpace;

// tone mapping
// image formation

this.toneMapping = NoToneMapping;
this.toneMappingExposure = 1.0;

this._lookCDL = new Vector4( 1, 0, 1, 1 );

// internal properties

const _this = this;
Expand Down Expand Up @@ -501,6 +503,12 @@ class WebGLRenderer {

};

this.setLookCDL = function ( slope = 1, offset = 0, power = 1, saturation = 1 ) {

this._lookCDL.set( slope, offset, power, saturation );

};

// Clearing

this.getClearColor = function ( target ) {
Expand Down Expand Up @@ -1643,6 +1651,7 @@ class WebGLRenderer {
materialProperties.fog = scene.fog;
materialProperties.envMap = ( material.isMeshStandardMaterial ? cubeuvmaps : cubemaps ).get( material.envMap || materialProperties.environment );
materialProperties.envMapRotation = ( materialProperties.environment !== null && material.envMap === null ) ? scene.environmentRotation : material.envMapRotation;
materialProperties.lookCDL = _this._lookCDL;

if ( programs === undefined ) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ export default /* glsl */`
#endif

uniform float toneMappingExposure;
uniform vec4 lookCDL;

// Applies ASC CDL v1.2 color grade to input color in an unspecified log or linear space.
vec3 applyLookCDL( vec3 color ) {

float slope = lookCDL.x;
float offset = lookCDL.y;
float power = lookCDL.z;
float saturation = lookCDL.w;

// Fixed Rec. 709 weights for saturation as specified by ASC CDL v1.2.
float luma = dot( color, vec3( 0.2126, 0.7152, 0.0722 ) );

color = pow( max( color * slope + offset, 0.0 ), vec3( power ) );

return max( luma + saturation * ( color - luma ), 0.0 );

}

// exposure only
vec3 LinearToneMapping( vec3 color ) {
Expand Down Expand Up @@ -63,6 +81,9 @@ vec3 ACESFilmicToneMapping( vec3 color ) {

color = ACESInputMat * color;

// TODO: Convert to ACEScc or ACEScct, apply CDL, and convert back.
// color = applyLookCDL( color );

// Apply RRT and ODT
color = RRTAndODTFit( color );

Expand Down Expand Up @@ -149,7 +170,7 @@ vec3 AgXToneMapping( vec3 color ) {
color = agxDefaultContrastApprox( color );

// Apply AgX look
// v = agxLook(v, look);
color = applyLookCDL( color );

color = AgXOutsetMatrix * color;

Expand Down
5 changes: 4 additions & 1 deletion src/renderers/shaders/UniformsLib.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Color } from '../../math/Color.js';
import { Vector2 } from '../../math/Vector2.js';
import { Matrix3 } from '../../math/Matrix3.js';
import { Vector4 } from '../../math/Vector4.js';

/**
* Uniforms library for shared webgl shaders
Expand All @@ -19,7 +20,9 @@ const UniformsLib = {
alphaMap: { value: null },
alphaMapTransform: { value: /*@__PURE__*/ new Matrix3() },

alphaTest: { value: 0 }
alphaTest: { value: 0 },

lookCDL: { value: /*@__PURE__*/ new Vector4() },

},

Expand Down
6 changes: 4 additions & 2 deletions src/renderers/webgl/WebGLMaterials.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ function WebGLMaterials( renderer, properties ) {

uniforms.opacity.value = material.opacity;

const materialProperties = properties.get( material );

uniforms.lookCDL.value.copy( materialProperties.lookCDL );

if ( material.color ) {

uniforms.diffuse.value.copy( material.color );
Expand Down Expand Up @@ -214,8 +218,6 @@ function WebGLMaterials( renderer, properties ) {

}

const materialProperties = properties.get( material );

const envMap = materialProperties.envMap;
const envMapRotation = materialProperties.envMapRotation;

Expand Down