Skip to content

Commit 6255b45

Browse files
committed
Define cache key and handle custom materials
1 parent 8df734d commit 6255b45

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

src/passes/GeometryPass.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
RGFormat,
1717
SRGBColorSpace,
1818
Scene,
19+
ShaderMaterial,
1920
TextureDataType,
2021
UnsignedByteType,
2122
UnsignedInt248Type,
@@ -355,6 +356,7 @@ export class GeometryPass extends Pass implements GeometryPassOptions, Selective
355356

356357
/* eslint-disable @typescript-eslint/unbound-method */
357358
const onBeforeCompile = material.onBeforeCompile;
359+
const customProgramCacheKey = material.customProgramCacheKey;
358360

359361
material.onBeforeCompile = (shader: WebGLProgramParametersWithUniforms, renderer: WebGLRenderer) => {
360362

@@ -365,13 +367,39 @@ export class GeometryPass extends Pass implements GeometryPassOptions, Selective
365367

366368
}
367369

368-
if(this.gBuffer !== null) {
370+
if(this.gBuffer === null) {
369371

370-
const outputDefinitions = extractOutputDefinitions(this.gBuffer);
371-
shader.fragmentShader = outputDefinitions + "\n\n" + shader.fragmentShader;
372+
return;
372373

373374
}
374375

376+
if(material instanceof ShaderMaterial) {
377+
378+
shader.fragmentShader = shader.fragmentShader.replace(
379+
/(^ *void\s+main\(\)\s+{.*)/m,
380+
"$1\n\n#include <pp_default_output_fragment>"
381+
);
382+
383+
}
384+
385+
const outputDefinitions = extractOutputDefinitions(this.gBuffer);
386+
shader.fragmentShader = outputDefinitions + "\n\n" + shader.fragmentShader;
387+
388+
};
389+
390+
material.customProgramCacheKey = () => {
391+
392+
let key = this.gBuffer?.texture?.uuid ?? "";
393+
394+
// Workaround for troika-three-text, see #660.
395+
if(material.customProgramCacheKey !== customProgramCacheKey) {
396+
397+
key += customProgramCacheKey.call(material);
398+
399+
}
400+
401+
return key;
402+
375403
};
376404

377405
}

0 commit comments

Comments
 (0)