Last active
April 13, 2020 22:10
-
-
Save AlainBarrios/bcb3bf1521c57fbaadf99d47e7925897 to your computer and use it in GitHub Desktop.
Modify the shaders before compiling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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