Skip to content

Commit 0a22681

Browse files
committed
Move light count into struct
1 parent 4335fa5 commit 0a22681

File tree

6 files changed

+41
-38
lines changed

6 files changed

+41
-38
lines changed

README.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ ptMaterial.materials.updateFrom( materials, textures );
116116

117117
// update the lights
118118
ptMaterial.lights.updateFrom( lights );
119-
ptMaterial.lightCount = lights.length;
120119

121120
// set the environment map
122121
const texture = await new RGBELoader().loadAsync( envMapUrl );
@@ -524,8 +523,7 @@ _extends MaterialBase_
524523
textures: RenderTarget2DArray,
525524

526525
// Light information
527-
lights: LightsTexture,
528-
lightCount = 0: Number,
526+
lights: LightsInfoUniformStruct,
529527
iesProfiles: IESProfilesTexture,
530528

531529
// Environment Map information
@@ -649,11 +647,9 @@ Updates the size and values of the texture to align with the provided set of mat
649647

650648
The "matte" and "side" values must be updated explicitly.
651649

652-
## LightsTexture
650+
## LightsInfoUniformStruct
653651

654-
_extends DataTexture_
655-
656-
Helper texture uniform for encoding lights as texture data.
652+
Helper uniform for encoding lights as texture data with count.
657653

658654
### .updateFrom
659655

example/spotLights.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,6 @@ async function init() {
261261
material.materials.updateFrom( materials, textures );
262262
material.iesProfiles.updateFrom( renderer, iesTextures );
263263
material.lights.updateFrom( lights );
264-
material.lightCount = lights.length;
265264
ptRenderer.material.envMapInfo.updateFrom( scene.environment );
266265

267266
generator.dispose();

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export * from './uniforms/MaterialsTexture.js';
1515
export * from './uniforms/RenderTarget2DArray.js';
1616
export * from './uniforms/EquirectHdrInfoUniform.js';
1717
export * from './uniforms/PhysicalCameraUniform.js';
18-
export * from './uniforms/LightsTexture.js';
18+
export * from './uniforms/LightsInfoUniformStruct.js';
1919
export * from './uniforms/IESProfilesTexture.js';
2020

2121
// utils

src/materials/PhysicalPathTracingMaterial.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { shaderLightSampling } from '../shader/shaderLightSampling.js';
1313
import { shaderUtils } from '../shader/shaderUtils.js';
1414
import { PhysicalCameraUniform } from '../uniforms/PhysicalCameraUniform.js';
1515
import { EquirectHdrInfoUniform } from '../uniforms/EquirectHdrInfoUniform.js';
16-
import { LightsTexture } from '../uniforms/LightsTexture.js';
16+
import { LightsInfoUniformStruct } from '../uniforms/LightsInfoUniformStruct.js';
1717
import { IESProfilesTexture } from '../uniforms/IESProfilesTexture.js';
1818

1919
export class PhysicalPathTracingMaterial extends MaterialBase {
@@ -55,8 +55,7 @@ export class PhysicalPathTracingMaterial extends MaterialBase {
5555
materialIndexAttribute: { value: new UIntVertexAttributeTexture() },
5656
materials: { value: new MaterialsTexture() },
5757
textures: { value: new RenderTarget2DArray().texture },
58-
lights: { value: new LightsTexture() },
59-
lightCount: { value: 0 },
58+
lights: { value: new LightsInfoUniformStruct() },
6059
iesProfiles: { value: new IESProfilesTexture().texture },
6160
cameraWorldMatrix: { value: new Matrix4() },
6261
invProjectionMatrix: { value: new Matrix4() },
@@ -138,8 +137,7 @@ export class PhysicalPathTracingMaterial extends MaterialBase {
138137
uniform int seed;
139138
uniform float opacity;
140139
uniform sampler2D materials;
141-
uniform sampler2D lights;
142-
uniform uint lightCount;
140+
uniform LightsInfo lights;
143141
uniform sampler2DArray iesProfiles;
144142
145143
${ shaderLightSampling }
@@ -420,7 +418,7 @@ export class PhysicalPathTracingMaterial extends MaterialBase {
420418
421419
bool hit = bvhIntersectFirstHit( bvh, rayOrigin, rayDirection, faceIndices, faceNormal, barycoord, side, dist );
422420
423-
LightSampleRec lightHit = lightsClosestHit( lights, lightCount, rayOrigin, rayDirection );
421+
LightSampleRec lightHit = lightsClosestHit( lights.tex, lights.count, rayOrigin, rayDirection );
424422
425423
if ( lightHit.hit && ( lightHit.dist < dist || !hit ) ) {
426424
@@ -433,7 +431,7 @@ export class PhysicalPathTracingMaterial extends MaterialBase {
433431
#if FEATURE_MIS
434432
435433
// weight the contribution
436-
float misWeight = misHeuristic( sampleRec.pdf, lightHit.pdf / float( lightCount + 1u ) );
434+
float misWeight = misHeuristic( sampleRec.pdf, lightHit.pdf / float( lights.count + 1u ) );
437435
gl_FragColor.rgb += lightHit.emission * throughputColor * misWeight;
438436
439437
#else
@@ -463,7 +461,7 @@ export class PhysicalPathTracingMaterial extends MaterialBase {
463461
// get the PDF of the hit envmap point
464462
vec3 envColor;
465463
float envPdf = envMapSample( environmentRotation * rayDirection, envMapInfo, envColor );
466-
envPdf /= float( lightCount + 1u );
464+
envPdf /= float( lights.count + 1u );
467465
468466
// and weight the contribution
469467
float misWeight = misHeuristic( sampleRec.pdf, envPdf );
@@ -788,10 +786,10 @@ export class PhysicalPathTracingMaterial extends MaterialBase {
788786
#if FEATURE_MIS
789787
790788
// uniformly pick a light or environment map
791-
if( rand() > 1.0 / float( lightCount + 1u ) ) {
789+
if( rand() > 1.0 / float( lights.count + 1u ) ) {
792790
793791
// sample a light or environment
794-
LightSampleRec lightSampleRec = randomLightSample( lights, iesProfiles, lightCount, rayOrigin );
792+
LightSampleRec lightSampleRec = randomLightSample( lights.tex, iesProfiles, lights.count, rayOrigin );
795793
796794
bool isSampleBelowSurface = dot( faceNormal, lightSampleRec.direction ) < 0.0;
797795
if ( isSampleBelowSurface ) {
@@ -814,7 +812,7 @@ export class PhysicalPathTracingMaterial extends MaterialBase {
814812
if ( lightMaterialPdf > 0.0 && isValidSampleColor ) {
815813
816814
// weight the direct light contribution
817-
float lightPdf = lightSampleRec.pdf / float( lightCount + 1u );
815+
float lightPdf = lightSampleRec.pdf / float( lights.count + 1u );
818816
float misWeight = misHeuristic( lightPdf, lightMaterialPdf );
819817
gl_FragColor.rgb += lightSampleRec.emission * throughputColor * sampleColor * misWeight / lightPdf;
820818
@@ -854,7 +852,7 @@ export class PhysicalPathTracingMaterial extends MaterialBase {
854852
if ( envMaterialPdf > 0.0 && isValidSampleColor ) {
855853
856854
// weight the direct light contribution
857-
envPdf /= float( lightCount + 1u );
855+
envPdf /= float( lights.count + 1u );
858856
float misWeight = misHeuristic( envPdf, envMaterialPdf );
859857
gl_FragColor.rgb += attenuatedColor * environmentIntensity * envColor * throughputColor * sampleColor * misWeight / envPdf;
860858

src/shader/shaderStructs.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,14 @@ export const shaderLightStruct = /* glsl */ `
212212
#define RECT_AREA_LIGHT_TYPE 0
213213
#define CIRC_AREA_LIGHT_TYPE 1
214214
#define SPOT_LIGHT_TYPE 2
215+
216+
struct LightsInfo {
217+
218+
sampler2D tex;
219+
uint count;
220+
221+
};
222+
215223
struct Light {
216224
217225
vec3 position;

src/uniforms/LightsTexture.js renamed to src/uniforms/LightsInfoUniformStruct.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,39 @@ const LIGHT_PIXELS = 6;
44
const RECT_AREA_LIGHT = 0;
55
const CIRC_AREA_LIGHT = 1;
66
const SPOT_LIGHT = 2;
7-
export class LightsTexture extends DataTexture {
7+
export class LightsInfoUniformStruct {
88

99
constructor() {
1010

11-
super( new Float32Array( 4 ), 1, 1 );
11+
const tex = new DataTexture( new Float32Array( 4 ), 1, 1 );
12+
tex.format = RGBAFormat;
13+
tex.type = FloatType;
14+
tex.wrapS = ClampToEdgeWrapping;
15+
tex.wrapT = ClampToEdgeWrapping;
16+
tex.generateMipmaps = false;
1217

13-
this.format = RGBAFormat;
14-
this.type = FloatType;
15-
this.wrapS = ClampToEdgeWrapping;
16-
this.wrapT = ClampToEdgeWrapping;
17-
this.generateMipmaps = false;
18+
this.tex = tex;
19+
this.count = 0;
1820

1921
}
2022

2123
updateFrom( lights, iesTextures = [] ) {
2224

23-
const pixelCount = lights.length * LIGHT_PIXELS;
25+
const tex = this.tex;
26+
const pixelCount = Math.max( lights.length * LIGHT_PIXELS, 1 );
2427
const dimension = Math.ceil( Math.sqrt( pixelCount ) );
2528

26-
if ( this.image.width !== dimension ) {
29+
if ( tex.image.width !== dimension ) {
2730

28-
this.dispose();
31+
tex.dispose();
2932

30-
this.image.data = new Float32Array( dimension * dimension * 4 );
31-
this.image.width = dimension;
32-
this.image.height = dimension;
33+
tex.image.data = new Float32Array( dimension * dimension * 4 );
34+
tex.image.width = dimension;
35+
tex.image.height = dimension;
3336

3437
}
3538

36-
const floatArray = this.image.data;
39+
const floatArray = tex.image.data;
3740

3841
const u = new Vector3();
3942
const v = new Vector3();
@@ -151,9 +154,8 @@ export class LightsTexture extends DataTexture {
151154

152155
}
153156

154-
window.LIGHT_ARRAY = floatArray;
155-
156-
this.needsUpdate = true;
157+
tex.needsUpdate = true;
158+
this.count = lights.length;
157159

158160
}
159161

0 commit comments

Comments
 (0)