Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/api/en/materials/Material.html
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ <h3>[method:null dispose]()</h3>
<h3>[method:null onBeforeCompile]( [param:Shader shader], [param:WebGLRenderer renderer] )</h3>
<p>
An optional callback that is executed immediately before the shader program is compiled.
This function is called with the shader source code as a parameter. Useful for the modification of built-in materials.
This function is called with the shader source code as a parameter. Useful for the modification of built-in materials. Default is null.
</p>

<h3>[method:null setValues]( [param:object values] )</h3>
Expand Down
2 changes: 1 addition & 1 deletion src/materials/Material.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ export class Material extends EventDispatcher {
dispose(): void;

/**
* An optional callback that is executed immediately before the shader program is compiled. This function is called with the shader source code as a parameter. Useful for the modification of built-in materials.
* An optional callback that is executed immediately before the shader program is compiled. This function is called with the shader source code as a parameter. Useful for the modification of built-in materials. Default is null.
* @param shader Source code of the shader
* @param renderer WebGLRenderer Context that is initializing the material
*/
Expand Down
2 changes: 1 addition & 1 deletion src/materials/Material.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Material.prototype = Object.assign( Object.create( EventDispatcher.prototype ),

isMaterial: true,

onBeforeCompile: function () {},
onBeforeCompile: null,

setValues: function ( values ) {

Expand Down
10 changes: 7 additions & 3 deletions src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1510,10 +1510,14 @@ function WebGLRenderer( parameters ) {

}

material.onBeforeCompile( materialProperties.shader, _this );
if ( material.onBeforeCompile ) {

// Computing code again as onBeforeCompile may have changed the shaders
code = programCache.getProgramCode( material, parameters );
material.onBeforeCompile( materialProperties.shader, _this );

// Computing code again as onBeforeCompile may have changed the shaders
code = programCache.getProgramCode( material, parameters );

}

program = programCache.acquireProgram( material, materialProperties.shader, parameters, code );

Expand Down
6 changes: 5 additions & 1 deletion src/renderers/webgl/WebGLPrograms.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,11 @@ function WebGLPrograms( renderer, extensions, capabilities ) {

}

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

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

}

array.push( renderer.gammaOutput );

Expand Down