Skip to content

Commit a304a9b

Browse files
committed
Add callback to identify customized programs
In case onBeforeCompile has conditional statements in it, the parameter values affecting the shader code can be returned from customProgramCode so the shader program is correctly fetched from cache or recompiled as needed.
1 parent c5323d1 commit a304a9b

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

src/materials/Material.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,11 @@ export class Material extends EventDispatcher {
321321
*/
322322
onBeforeCompile ( shader : Shader, renderer : WebGLRenderer ) : void;
323323

324+
/**
325+
* In case onBeforeCompile is used, this callback can be used to identify values of settings used in onBeforeCompile, so three.js can reuse a cached shader or recompile the shader as needed.
326+
*/
327+
customProgramCode(): string;
328+
324329
/**
325330
* Sets the properties based on the values.
326331
* @param values A container with parameters.

src/materials/Material.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ Material.prototype = Object.assign( Object.create( EventDispatcher.prototype ),
8787

8888
onBeforeCompile: function () {},
8989

90+
customProgramCode: function () {
91+
92+
return this.onBeforeCompile.toString();
93+
94+
},
95+
9096
setValues: function ( values ) {
9197

9298
if ( values === undefined ) return;

src/renderers/webgl/WebGLPrograms.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,12 @@ function WebGLPrograms( renderer, extensions, capabilities ) {
257257

258258
}
259259

260-
array.push( material.onBeforeCompile.toString() );
261-
262260
array.push( renderer.gammaOutput );
263261

264262
array.push( renderer.gammaFactor );
265263

264+
array.push( material.customProgramCode() );
265+
266266
return array.join();
267267

268268
};

0 commit comments

Comments
 (0)