Skip to content

Commit 11e7f63

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 c23b245 commit 11e7f63

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
@@ -343,6 +343,36 @@ <h3>[method:null onBeforeCompile]( [param:Shader shader], [param:WebGLRenderer r
343343
Unlike properties, the callback is not supported by [page:Material.clone .clone](), [page:Material.copy .copy]() and [page:Material.toJSON .toJSON]().
344344
</p>
345345

346+
<h3>[method:String customProgramCacheKey]()</h3>
347+
<p>
348+
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.
349+
</p>
350+
351+
<p>
352+
For example, if onBeforeCompile contains a conditional statement like:<br />
353+
354+
<code>if ( black ) {
355+
356+
shader.fragmentShader = shader.fragmentShader.replace('gl_FragColor = vec4(1)', 'gl_FragColor = vec4(0)')
357+
358+
}
359+
</code>
360+
361+
then customProgramCacheKey should be set like this:<br />
362+
363+
<code>material.customProgramCacheKey = function() {
364+
365+
return black ? '1' : '0';
366+
367+
}
368+
</code>
369+
370+
</p>
371+
372+
<p>
373+
Unlike properties, the callback is not supported by [page:Material.clone .clone](), [page:Material.copy .copy]() and [page:Material.toJSON .toJSON]().
374+
</p>
375+
346376
<h3>[method:null setValues]( [param:object values] )</h3>
347377
<p>
348378
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
@@ -334,6 +334,11 @@ export class Material extends EventDispatcher {
334334
*/
335335
onBeforeCompile ( shader : Shader, renderer : WebGLRenderer ) : void;
336336

337+
/**
338+
* 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.
339+
*/
340+
customProgramCacheKey(): string;
341+
337342
/**
338343
* Sets the properties based on the values.
339344
* @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
@@ -288,7 +288,7 @@ function WebGLPrograms( renderer, extensions, capabilities ) {
288288
rendererExtensionDrawBuffers: isWebGL2 || extensions.get( 'WEBGL_draw_buffers' ) !== null,
289289
rendererExtensionShaderTextureLod: isWebGL2 || extensions.get( 'EXT_shader_texture_lod' ) !== null,
290290

291-
onBeforeCompile: material.onBeforeCompile
291+
customProgramCacheKey: material.customProgramCacheKey()
292292

293293
};
294294

@@ -335,7 +335,7 @@ function WebGLPrograms( renderer, extensions, capabilities ) {
335335

336336
}
337337

338-
array.push( parameters.onBeforeCompile.toString() );
338+
array.push( parameters.customProgramCacheKey );
339339

340340
return array.join();
341341

0 commit comments

Comments
 (0)