Skip to content

Commit

Permalink
Add callback to identify customized programs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Oletus committed Sep 17, 2019
1 parent c5323d1 commit a304a9b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/materials/Material.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,11 @@ export class Material extends EventDispatcher {
*/
onBeforeCompile ( shader : Shader, renderer : WebGLRenderer ) : void;

/**
* 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.
*/
customProgramCode(): string;

/**
* Sets the properties based on the values.
* @param values A container with parameters.
Expand Down
6 changes: 6 additions & 0 deletions src/materials/Material.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ Material.prototype = Object.assign( Object.create( EventDispatcher.prototype ),

onBeforeCompile: function () {},

customProgramCode: function () {

return this.onBeforeCompile.toString();

},

setValues: function ( values ) {

if ( values === undefined ) return;
Expand Down
4 changes: 2 additions & 2 deletions src/renderers/webgl/WebGLPrograms.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,12 @@ function WebGLPrograms( renderer, extensions, capabilities ) {

}

array.push( material.onBeforeCompile.toString() );

array.push( renderer.gammaOutput );

array.push( renderer.gammaFactor );

array.push( material.customProgramCode() );

return array.join();

};
Expand Down

0 comments on commit a304a9b

Please sign in to comment.