@@ -13,8 +13,7 @@ import { shaderLightSampling } from '../shader/shaderLightSampling.js';
1313import { shaderUtils } from '../shader/shaderUtils.js' ;
1414import { PhysicalCameraUniform } from '../uniforms/PhysicalCameraUniform.js' ;
1515import { EquirectHdrInfoUniform } from '../uniforms/EquirectHdrInfoUniform.js' ;
16- import { LightsTexture } from '../uniforms/LightsTexture.js' ;
17- import { SpotLightsTexture } from '../uniforms/SpotLightsTexture.js' ;
16+ import { LightsInfoUniformStruct } from '../uniforms/LightsInfoUniformStruct.js' ;
1817import { IESProfilesTexture } from '../uniforms/IESProfilesTexture.js' ;
1918
2019export class PhysicalPathTracingMaterial extends MaterialBase {
@@ -56,10 +55,7 @@ export class PhysicalPathTracingMaterial extends MaterialBase {
5655 materialIndexAttribute : { value : new UIntVertexAttributeTexture ( ) } ,
5756 materials : { value : new MaterialsTexture ( ) } ,
5857 textures : { value : new RenderTarget2DArray ( ) . texture } ,
59- lights : { value : new LightsTexture ( ) } ,
60- lightCount : { value : 0 } ,
61- spotLights : { value : new SpotLightsTexture ( ) } ,
62- spotLightCount : { value : 0 } ,
58+ lights : { value : new LightsInfoUniformStruct ( ) } ,
6359 iesProfiles : { value : new IESProfilesTexture ( ) . texture } ,
6460 cameraWorldMatrix : { value : new Matrix4 ( ) } ,
6561 invProjectionMatrix : { value : new Matrix4 ( ) } ,
@@ -141,10 +137,7 @@ export class PhysicalPathTracingMaterial extends MaterialBase {
141137 uniform int seed;
142138 uniform float opacity;
143139 uniform sampler2D materials;
144- uniform sampler2D lights;
145- uniform uint lightCount;
146- uniform sampler2D spotLights;
147- uniform uint spotLightCount;
140+ uniform LightsInfo lights;
148141 uniform sampler2DArray iesProfiles;
149142
150143 ${ shaderLightSampling }
@@ -425,7 +418,7 @@ export class PhysicalPathTracingMaterial extends MaterialBase {
425418
426419 bool hit = bvhIntersectFirstHit( bvh, rayOrigin, rayDirection, faceIndices, faceNormal, barycoord, side, dist );
427420
428- LightSampleRec lightHit = lightsClosestHit( lights, lightCount , rayOrigin, rayDirection );
421+ LightSampleRec lightHit = lightsClosestHit( lights.tex, lights.count , rayOrigin, rayDirection );
429422
430423 if ( lightHit.hit && ( lightHit.dist < dist || !hit ) ) {
431424
@@ -437,15 +430,23 @@ export class PhysicalPathTracingMaterial extends MaterialBase {
437430
438431 #if FEATURE_MIS
439432
440- // weight the contribution
441- float misWeight = misHeuristic( sampleRec.pdf, lightHit.pdf / float( lightCount + 1u ) );
442- gl_FragColor.rgb += lightHit.emission * throughputColor * misWeight;
433+ // NOTE: we skip MIS for spotlights since we haven't fixed the forward
434+ // path tracing code path, yet
435+ if ( lightHit.type == SPOT_LIGHT_TYPE ) {
436+
437+ gl_FragColor.rgb += lightHit.emission * throughputColor;
438+
439+ } else {
440+
441+ // weight the contribution
442+ float misWeight = misHeuristic( sampleRec.pdf, lightHit.pdf / float( lights.count + 1u ) );
443+ gl_FragColor.rgb += lightHit.emission * throughputColor * misWeight;
444+
445+ }
443446
444447 #else
445448
446- gl_FragColor.rgb +=
447- lightHit.emission *
448- throughputColor;
449+ gl_FragColor.rgb += lightHit.emission * throughputColor;
449450
450451 #endif
451452
@@ -468,7 +469,7 @@ export class PhysicalPathTracingMaterial extends MaterialBase {
468469 // get the PDF of the hit envmap point
469470 vec3 envColor;
470471 float envPdf = envMapSample( environmentRotation * rayDirection, envMapInfo, envColor );
471- envPdf /= float( lightCount + 1u );
472+ envPdf /= float( lights.count + 1u );
472473
473474 // and weight the contribution
474475 float misWeight = misHeuristic( sampleRec.pdf, envPdf );
@@ -789,57 +790,14 @@ export class PhysicalPathTracingMaterial extends MaterialBase {
789790 bool isBelowSurface = dot( rayDirection, faceNormal ) < 0.0;
790791 rayOrigin = point + faceNormal * ( maxPoint + 1.0 ) * ( isBelowSurface ? - RAY_OFFSET : RAY_OFFSET );
791792
792- // spot light sampling
793- LightSampleRec lightSampleRec = randomSpotLightSample( spotLights, iesProfiles, spotLightCount, rayOrigin );
794-
795- bool isSampleBelowSurface = dot( faceNormal, lightSampleRec.direction ) < 0.0;
796- if ( isSampleBelowSurface ) {
797-
798- lightSampleRec.pdf = 0.0;
799-
800- }
801-
802- // check if a ray could even reach the light area
803- if (
804- lightSampleRec.pdf > 0.0 &&
805- isDirectionValid( lightSampleRec.direction, normal, faceNormal ) &&
806- ! anyCloserHit( bvh, rayOrigin, lightSampleRec.direction, lightSampleRec.dist )
807- ) {
808-
809- // get the material pdf
810- vec3 sampleColor;
811- float lightMaterialPdf = bsdfResult( outgoing, clearcoatOutgoing, normalize( invBasis * lightSampleRec.direction ), normalize( clearcoatInvBasis * lightSampleRec.direction ), surfaceRec, sampleColor );
812- bool isValidSampleColor = any( greaterThanEqual( sampleColor, vec3( 0.0 ) ) );
813- if ( lightMaterialPdf > 0.0 && isValidSampleColor ) {
814-
815- #if FEATURE_MIS
816-
817- // weight the direct light contribution
818- float lightPdf = lightSampleRec.pdf;// / float( spotLightCount );
819- float misWeight = misHeuristic( lightPdf, lightMaterialPdf );
820- gl_FragColor.rgb += lightSampleRec.emission * throughputColor * sampleColor * misWeight / lightPdf;
821-
822- #else
823-
824- gl_FragColor.rgb +=
825- lightSampleRec.emission *
826- throughputColor *
827- sampleColor;
828-
829- #endif
830-
831- }
832-
833- }
834-
835793 // direct env map sampling
836794 #if FEATURE_MIS
837795
838796 // uniformly pick a light or environment map
839- if( rand() > 1.0 / float( lightCount + 1u ) ) {
797+ if( rand() > 1.0 / float( lights.count + 1u ) ) {
840798
841799 // sample a light or environment
842- LightSampleRec lightSampleRec = randomLightSample( lights, lightCount , rayOrigin );
800+ LightSampleRec lightSampleRec = randomLightSample( lights.tex, iesProfiles, lights.count , rayOrigin );
843801
844802 bool isSampleBelowSurface = dot( faceNormal, lightSampleRec.direction ) < 0.0;
845803 if ( isSampleBelowSurface ) {
@@ -862,7 +820,7 @@ export class PhysicalPathTracingMaterial extends MaterialBase {
862820 if ( lightMaterialPdf > 0.0 && isValidSampleColor ) {
863821
864822 // weight the direct light contribution
865- float lightPdf = lightSampleRec.pdf / float( lightCount + 1u );
823+ float lightPdf = lightSampleRec.pdf / float( lights.count + 1u );
866824 float misWeight = misHeuristic( lightPdf, lightMaterialPdf );
867825 gl_FragColor.rgb += lightSampleRec.emission * throughputColor * sampleColor * misWeight / lightPdf;
868826
@@ -902,7 +860,7 @@ export class PhysicalPathTracingMaterial extends MaterialBase {
902860 if ( envMaterialPdf > 0.0 && isValidSampleColor ) {
903861
904862 // weight the direct light contribution
905- envPdf /= float( lightCount + 1u );
863+ envPdf /= float( lights.count + 1u );
906864 float misWeight = misHeuristic( envPdf, envMaterialPdf );
907865 gl_FragColor.rgb += attenuatedColor * environmentIntensity * envColor * throughputColor * sampleColor * misWeight / envPdf;
908866
0 commit comments