Skip to content

Commit b5d2bef

Browse files
authored
Merge pull request #572 from gkjohnson/background-properties
Add background properties
2 parents 3be85ba + da4d565 commit b5d2bef

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/core/WebGLPathTracer.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ export class WebGLPathTracer {
196196

197197
// update scene background
198198
material.backgroundBlur = scene.backgroundBlurriness;
199+
material.backgroundIntensity = scene.backgroundIntensity;
200+
material.backgroundRotation.makeRotationFromEuler( scene.backgroundRotation ).multiply( _flipEnvMap );
199201
if ( scene.background === null || scene.background && scene.background.isColor ) {
200202

201203
this._colorBackground = this._colorBackground || new GradientEquirectTexture( 16 );
@@ -231,7 +233,7 @@ export class WebGLPathTracer {
231233
}
232234

233235
// update scene environment
234-
material.environmentIntensity = scene.environmentIntensity || 1;
236+
material.environmentIntensity = scene.environmentIntensity;
235237
material.environmentRotation.makeRotationFromEuler( scene.environmentRotation ).multiply( _flipEnvMap );
236238
if ( this._previousEnvironment !== scene.environment ) {
237239

src/materials/pathtracing/PhysicalPathTracingMaterial.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ export class PhysicalPathTracingMaterial extends MaterialBase {
101101
backgroundBlur: { value: 0.0 },
102102
backgroundMap: { value: null },
103103
backgroundAlpha: { value: 1.0 },
104-
// backgroundIntensity
105-
// backgroundRotation
104+
backgroundIntensity: { value: 1.0 },
105+
backgroundRotation: { value: new Matrix4() },
106106

107107
// randomness uniforms
108108
seed: { value: 0 },
@@ -203,6 +203,8 @@ export class PhysicalPathTracingMaterial extends MaterialBase {
203203
#if FEATURE_BACKGROUND_MAP
204204
205205
uniform sampler2D backgroundMap;
206+
uniform mat4 backgroundRotation;
207+
uniform float backgroundIntensity;
206208
207209
#endif
208210
@@ -237,6 +239,7 @@ export class PhysicalPathTracingMaterial extends MaterialBase {
237239
// globals
238240
mat3 envRotation3x3;
239241
mat3 invEnvRotation3x3;
242+
mat3 backgroundRotation3x3;
240243
float lightsDenom;
241244
242245
// sampling
@@ -265,14 +268,16 @@ export class PhysicalPathTracingMaterial extends MaterialBase {
265268
266269
vec3 sampleBackground( vec3 direction, vec2 uv ) {
267270
268-
vec3 sampleDir = normalize( direction + sampleHemisphere( direction, uv ) * 0.5 * backgroundBlur );
271+
vec3 sampleDir = sampleHemisphere( direction, uv ) * 0.5 * backgroundBlur;
269272
270273
#if FEATURE_BACKGROUND_MAP
271274
272-
return sampleEquirectColor( backgroundMap, sampleDir );
275+
sampleDir = normalize( backgroundRotation3x3 * direction + sampleDir );
276+
return backgroundIntensity * sampleEquirectColor( backgroundMap, sampleDir );
273277
274278
#else
275279
280+
sampleDir = normalize( envRotation3x3 * direction + sampleDir );
276281
return environmentIntensity * sampleEquirectColor( envMapInfo.map, sampleDir );
277282
278283
#endif
@@ -299,6 +304,7 @@ export class PhysicalPathTracingMaterial extends MaterialBase {
299304
// inverse environment rotation
300305
envRotation3x3 = mat3( environmentRotation );
301306
invEnvRotation3x3 = inverse( envRotation3x3 );
307+
backgroundRotation3x3 = mat3( backgroundRotation );
302308
lightsDenom =
303309
( environmentIntensity == 0.0 || envMapInfo.totalSum == 0.0 ) && lights.count != 0u ?
304310
float( lights.count ) :
@@ -371,7 +377,7 @@ export class PhysicalPathTracingMaterial extends MaterialBase {
371377
372378
if ( state.firstRay || state.transmissiveRay ) {
373379
374-
gl_FragColor.rgb += sampleBackground( envRotation3x3 * ray.direction, rand2( 2 ) ) * state.throughputColor;
380+
gl_FragColor.rgb += sampleBackground( ray.direction, rand2( 2 ) ) * state.throughputColor;
375381
gl_FragColor.a = backgroundAlpha;
376382
377383
} else {

0 commit comments

Comments
 (0)