Skip to content

Add background properties #572

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

Merged
merged 2 commits into from
Mar 21, 2024
Merged
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
4 changes: 3 additions & 1 deletion src/core/WebGLPathTracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 ) {

Expand Down
16 changes: 11 additions & 5 deletions src/materials/pathtracing/PhysicalPathTracingMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -203,6 +203,8 @@ export class PhysicalPathTracingMaterial extends MaterialBase {
#if FEATURE_BACKGROUND_MAP

uniform sampler2D backgroundMap;
uniform mat4 backgroundRotation;
uniform float backgroundIntensity;

#endif

Expand Down Expand Up @@ -237,6 +239,7 @@ export class PhysicalPathTracingMaterial extends MaterialBase {
// globals
mat3 envRotation3x3;
mat3 invEnvRotation3x3;
mat3 backgroundRotation3x3;
float lightsDenom;

// sampling
Expand Down Expand Up @@ -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
Expand All @@ -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 ) :
Expand Down Expand Up @@ -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 {
Expand Down