Skip to content

Instantly share code, notes, and snippets.

@AlainBarrios
Last active April 13, 2020 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlainBarrios/bcb3bf1521c57fbaadf99d47e7925897 to your computer and use it in GitHub Desktop.
Save AlainBarrios/bcb3bf1521c57fbaadf99d47e7925897 to your computer and use it in GitHub Desktop.
Modify the shaders before compiling
this.material.onBeforeCompile = (shader) => {
shader.uniforms.time = { value: 0 };
shader.uniforms.texture = { value: texture };
/******************** Vertex Shader ********************/
shader.vertexShader = `
#define TAU 6.28318530718
uniform float time;
attribute vec3 instPosition;
attribute vec2 instUV;
varying vec2 vInstUV;
${snoise3d}
${shader.vertexShader}`;
const token = "#include <begin_vertex>";
const customVertex = `
vec3 offset = instPosition;
offset.y = snoise( vec3( instPosition.xz * 6.0, time ) ) * 2.0;
vec3 transformed = position + offset;
vInstUV = instUV;
`;
shader.vertexShader = shader.vertexShader.replace(token, customVertex);
/******************** Fragment Shader ********************/
shader.fragmentShader = `
uniform sampler2D texture;
varying vec2 vInstUV;
${shader.fragmentShader}`;
const token2 = "#include <dithering_fragment>";
const customFragment = `
vec2 instUV = vInstUV;
vec4 color = texture2D(texture, instUV);
gl_FragColor = color;
`;
shader.fragmentShader = shader.fragmentShader.replace(
token2,
customFragment
);
this.materialShader = shader;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment