-
-
Notifications
You must be signed in to change notification settings - Fork 36.1k
Closed
Labels
Description
Over the years, a common feature request has been to be able to modify the built-in materials. Today I realised that it could be implemented in a similar way to Object3D's onBeforeRender().
WebGLPrograms adds onBeforeCompile.toString() to the program hash so this shouldn't affect other built-in materials used in the scene.
Here's an example of the feature in action:
http://rawgit.com/mrdoob/three.js/dev/examples/webgl_materials_modified.html
material.onBeforeCompile = function ( shader ) {
// console.log( shader )
shader.uniforms.time = { value: 0 };
shader.vertexShader = 'uniform float time;\n' + shader.vertexShader;
shader.vertexShader = shader.vertexShader.replace(
'#include <begin_vertex>',
'vec3 transformed = vec3( position.x + sin( time + position.y ) / 2.0, position.y, position.z );'
);
materialShader = shader;
};Not only we can mess with the shader code, but we can add custom uniforms.
if ( materialShader ) {
materialShader.uniforms.time.value = performance.now() / 1000;
}Is it too hacky?
Mugen87, AdmiralPotato, i-tengfei, dotellie, shrekshao and 12 morepailheadMugen87, michaelgiles, caracal7, karesztrk and phishchiang