Skip to content

Commit 9720a54

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 fa2a44d commit 9720a54

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

353+
<h3>[method:String customProgramCacheKey]()</h3>
354+
<p>
355+
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.
356+
</p>
357+
358+
<p>
359+
For example, if onBeforeCompile contains a conditional statement like:<br />
360+
361+
<code>if ( black ) {
362+
363+
shader.fragmentShader = shader.fragmentShader.replace('gl_FragColor = vec4(1)', 'gl_FragColor = vec4(0)')
364+
365+
}
366+
</code>
367+
368+
then customProgramCacheKey should be set like this:<br />
369+
370+
<code>material.customProgramCacheKey = function() {
371+
372+
return black ? '1' : '0';
373+
374+
}
375+
</code>
376+
377+
</p>
378+
379+
<p>
380+
Unlike properties, the callback is not supported by [page:Material.clone .clone](), [page:Material.copy .copy]() and [page:Material.toJSON .toJSON]().
381+
</p>
382+
353383
<h3>[method:null setValues]( [param:object values] )</h3>
354384
<p>
355385
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
@@ -328,6 +328,11 @@ export class Material extends EventDispatcher {
328328
*/
329329
onBeforeCompile ( shader : Shader, renderer : WebGLRenderer ) : void;
330330

331+
/**
332+
* 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.
333+
*/
334+
customProgramCacheKey(): string;
335+
331336
/**
332337
* Sets the properties based on the values.
333338
* @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
@@ -267,12 +267,12 @@ function WebGLPrograms( renderer, extensions, capabilities ) {
267267

268268
}
269269

270-
array.push( material.onBeforeCompile.toString() );
271-
272270
array.push( renderer.outputEncoding );
273271

274272
array.push( renderer.gammaFactor );
275273

274+
array.push( material.customProgramCacheKey() );
275+
276276
return array.join();
277277

278278
};

0 commit comments

Comments
 (0)