Skip to content

Commit 92f26f7

Browse files
authored
Merge pull request #17567 from higharc/custom-program-code
Material: Added callback to identify customized programs
2 parents 843270c + 11e7f63 commit 92f26f7

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

337+
<h3>[method:String customProgramCacheKey]()</h3>
338+
<p>
339+
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.
340+
</p>
341+
342+
<p>
343+
For example, if onBeforeCompile contains a conditional statement like:<br />
344+
345+
<code>if ( black ) {
346+
347+
shader.fragmentShader = shader.fragmentShader.replace('gl_FragColor = vec4(1)', 'gl_FragColor = vec4(0)')
348+
349+
}
350+
</code>
351+
352+
then customProgramCacheKey should be set like this:<br />
353+
354+
<code>material.customProgramCacheKey = function() {
355+
356+
return black ? '1' : '0';
357+
358+
}
359+
</code>
360+
361+
</p>
362+
363+
<p>
364+
Unlike properties, the callback is not supported by [page:Material.clone .clone](), [page:Material.copy .copy]() and [page:Material.toJSON .toJSON]().
365+
</p>
366+
337367
<h3>[method:null setValues]( [param:object values] )</h3>
338368
<p>
339369
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
@@ -327,6 +327,11 @@ export class Material extends EventDispatcher {
327327
*/
328328
onBeforeCompile ( shader : Shader, renderer : WebGLRenderer ) : void;
329329

330+
/**
331+
* 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.
332+
*/
333+
customProgramCacheKey(): string;
334+
330335
/**
331336
* Sets the properties based on the values.
332337
* @param values A container with parameters.

src/materials/Material.js

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

8686
onBeforeCompile: function ( /* shaderobject, renderer */ ) {},
8787

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

9096
if ( values === undefined ) return;

src/renderers/webgl/WebGLPrograms.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ function WebGLPrograms( renderer, extensions, capabilities ) {
289289
rendererExtensionDrawBuffers: isWebGL2 || extensions.get( 'WEBGL_draw_buffers' ) !== null,
290290
rendererExtensionShaderTextureLod: isWebGL2 || extensions.get( 'EXT_shader_texture_lod' ) !== null,
291291

292-
onBeforeCompile: material.onBeforeCompile
292+
customProgramCacheKey: material.customProgramCacheKey()
293293

294294
};
295295

@@ -336,7 +336,7 @@ function WebGLPrograms( renderer, extensions, capabilities ) {
336336

337337
}
338338

339-
array.push( parameters.onBeforeCompile.toString() );
339+
array.push( parameters.customProgramCacheKey );
340340

341341
return array.join();
342342

0 commit comments

Comments
 (0)