Skip to content

Instantly share code, notes, and snippets.

@mootari
Last active April 29, 2018 22:44
Show Gist options
  • Save mootari/8a833089efe5d6375b92552eede001f8 to your computer and use it in GitHub Desktop.
Save mootari/8a833089efe5d6375b92552eede001f8 to your computer and use it in GitHub Desktop.
function getMaterial(uniformsCallback) {
const material = new THREE.MeshStandardMaterial(/* ... */);
material.onBeforeCompile = shader => {
shader.uniforms.time = { value: 0 };
if(uniformsCallback) uniformsCallback(shader.uniforms);
shader.fragmentShader = applyPatches(shader.fragmentShader, {
'varying vec3 vViewPosition;': `$&
uniform float time;
`
});
}
return material;
}
let uniforms;
const material = getMaterial(u => uniforms = u);
const mesh = initMesh(material);
mesh.onBeforeRender = (renderer, scene, camera, geometry, material, group) => {
if(!uniforms) renderer.compile(scene, camera);
uniforms.time.value = performance.now() / 1000;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment