From 71a6cf313e4e6dbb4b325325f71096d2b828b6e2 Mon Sep 17 00:00:00 2001 From: Garrett Johnson Date: Thu, 21 Mar 2024 23:35:11 +0900 Subject: [PATCH 1/2] Add support for backgroundIntensity and backgroundRotation --- .../pathtracing/PhysicalPathTracingMaterial.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/materials/pathtracing/PhysicalPathTracingMaterial.js b/src/materials/pathtracing/PhysicalPathTracingMaterial.js index 497096ffb..468531d2b 100644 --- a/src/materials/pathtracing/PhysicalPathTracingMaterial.js +++ b/src/materials/pathtracing/PhysicalPathTracingMaterial.js @@ -101,8 +101,8 @@ export class PhysicalPathTracingMaterial extends MaterialBase { backgroundBlur: { value: 0.0 }, backgroundMap: { value: null }, backgroundAlpha: { value: 1.0 }, - // backgroundIntensity - // backgroundRotation + backgroundIntensity: { value: 1.0 }, + backgroundRotation: { value: new Matrix4() }, // randomness uniforms seed: { value: 0 }, @@ -203,6 +203,8 @@ export class PhysicalPathTracingMaterial extends MaterialBase { #if FEATURE_BACKGROUND_MAP uniform sampler2D backgroundMap; + uniform mat4 backgroundRotation; + uniform float backgroundIntensity; #endif @@ -237,6 +239,7 @@ export class PhysicalPathTracingMaterial extends MaterialBase { // globals mat3 envRotation3x3; mat3 invEnvRotation3x3; + mat3 backgroundRotation3x3; float lightsDenom; // sampling @@ -265,14 +268,16 @@ export class PhysicalPathTracingMaterial extends MaterialBase { vec3 sampleBackground( vec3 direction, vec2 uv ) { - vec3 sampleDir = normalize( direction + sampleHemisphere( direction, uv ) * 0.5 * backgroundBlur ); + vec3 sampleDir = sampleHemisphere( direction, uv ) * 0.5 * backgroundBlur; #if FEATURE_BACKGROUND_MAP - return sampleEquirectColor( backgroundMap, sampleDir ); + sampleDir = normalize( backgroundRotation3x3 * direction + sampleDir ); + return backgroundIntensity * sampleEquirectColor( backgroundMap, sampleDir ); #else + sampleDir = normalize( envRotation3x3 * direction + sampleDir ); return environmentIntensity * sampleEquirectColor( envMapInfo.map, sampleDir ); #endif @@ -299,6 +304,7 @@ export class PhysicalPathTracingMaterial extends MaterialBase { // inverse environment rotation envRotation3x3 = mat3( environmentRotation ); invEnvRotation3x3 = inverse( envRotation3x3 ); + backgroundRotation3x3 = mat3( backgroundRotation ); lightsDenom = ( environmentIntensity == 0.0 || envMapInfo.totalSum == 0.0 ) && lights.count != 0u ? float( lights.count ) : @@ -371,7 +377,7 @@ export class PhysicalPathTracingMaterial extends MaterialBase { if ( state.firstRay || state.transmissiveRay ) { - gl_FragColor.rgb += sampleBackground( envRotation3x3 * ray.direction, rand2( 2 ) ) * state.throughputColor; + gl_FragColor.rgb += sampleBackground( ray.direction, rand2( 2 ) ) * state.throughputColor; gl_FragColor.a = backgroundAlpha; } else { From da4d5657ad5bcea55cdf984437e2f3919f4b0978 Mon Sep 17 00:00:00 2001 From: Garrett Johnson Date: Thu, 21 Mar 2024 23:39:03 +0900 Subject: [PATCH 2/2] Add support for webgl path tracer --- src/core/WebGLPathTracer.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/core/WebGLPathTracer.js b/src/core/WebGLPathTracer.js index 41c11f03f..0685bfdab 100644 --- a/src/core/WebGLPathTracer.js +++ b/src/core/WebGLPathTracer.js @@ -196,6 +196,8 @@ export class WebGLPathTracer { // update scene background material.backgroundBlur = scene.backgroundBlurriness; + material.backgroundIntensity = scene.backgroundIntensity; + material.backgroundRotation.makeRotationFromEuler( scene.backgroundRotation ).multiply( _flipEnvMap ); if ( scene.background === null || scene.background && scene.background.isColor ) { this._colorBackground = this._colorBackground || new GradientEquirectTexture( 16 ); @@ -231,7 +233,7 @@ export class WebGLPathTracer { } // update scene environment - material.environmentIntensity = scene.environmentIntensity || 1; + material.environmentIntensity = scene.environmentIntensity; material.environmentRotation.makeRotationFromEuler( scene.environmentRotation ).multiply( _flipEnvMap ); if ( this._previousEnvironment !== scene.environment ) {