Skip to content

Commit df64dc6

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 a6b2e37 commit df64dc6

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

docs/api/en/materials/Material.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,36 @@ <h3>[method:null onBeforeCompile]( [param:Shader shader], [param:WebGLRenderer r
346346
Unlike properties, the callback is not supported by [page:Material.clone .clone](), [page:Material.copy .copy]() and [page:Material.toJSON .toJSON]().
347347
</p>
348348

349+
<h3>[method:String customProgramCacheKey]()</h3>
350+
<p>
351+
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 for this material as needed.
352+
</p>
353+
354+
<p>
355+
For example, if onBeforeCompile contains a conditional statement like:<br />
356+
357+
<code>if ( black ) {
358+
359+
shader.fragmentShader = shader.fragmentShader.replace('gl_FragColor = vec4(1)', 'gl_FragColor = vec4(0)')
360+
361+
}
362+
</code>
363+
364+
then customProgramCacheKey should be set like this:<br />
365+
366+
<code>material.customProgramCacheKey = function() {
367+
368+
return black ? '1' : '0';
369+
370+
}
371+
</code>
372+
373+
</p>
374+
375+
<p>
376+
Unlike properties, the callback is not supported by [page:Material.clone .clone](), [page:Material.copy .copy]() and [page:Material.toJSON .toJSON]().
377+
</p>
378+
349379
<h3>[method:null setValues]( [param:object values] )</h3>
350380
<p>
351381
values -- a container with parameters.<br />

src/materials/Material.d.ts

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

319+
/**
320+
* 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.
321+
*/
322+
customProgramCacheKey(): string;
323+
319324
/**
320325
* Sets the properties based on the values.
321326
* @param values A container with parameters.

src/materials/Material.js

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

8787
onBeforeCompile: function () {},
8888

89+
customProgramCacheKey: function () {
90+
91+
return this.onBeforeCompile.toString();
92+
93+
},
94+
8995
setValues: function ( values ) {
9096

9197
if ( values === undefined ) return;

src/renderers/webgl/WebGLPrograms.js

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

273273
}
274274

275-
array.push( material.onBeforeCompile.toString() );
276-
277275
array.push( renderer.gammaOutput );
278276

279277
array.push( renderer.gammaFactor );
280278

279+
array.push( material.customProgramCacheKey() );
280+
281281
return array.join();
282282

283283
};

0 commit comments

Comments
 (0)