Hello,
Coming from here: Proper way to animate a texture attached to a mesh?
I’m treating to do the whole “play animation” logic of a pseudo-sprite within the shader code (switching texture coordinates of a plane), to relieve JS of doing it.
To do so, I’m using a ShaderMaterial on the mesh, which is going to update the UV of the texture.
I need the timer tick to do the whole logic of the animation, and at the moment I didn’t found a way to get the timet tick (Date.now()) within the shader code.
I’ve found this topic in stackoverflow: How to set a time uniform in webgl - Stack Overflow
So I would need something like the WebGL getUniformLocation and gl.uniform1f to set the u_time uniform value in one step for several ShaderMaterials (one per sprite animation), but I haven’t found something similar in Babylon.
Is it possible to set the value of a global uniform used in several ShaderMatereials?
My idea is this:
In each ShaderMaterial:
BABYLON.Effect.ShadersStore[`spriteMesh${this.idShader}FragmentShader`] = `
precision highp float;
varying vec2 vUv;
uniform sampler2D textureSampler;
uniform int initialTime; // This is local, I use shaderMaterial.setFloat
uniform int frameStart; // This is local, I use shaderMaterial.setInt
uniform int frameEnd; // This is local, I use shaderMaterial.setInt
uniform int timerTick; // This is global and common to all sprites ShaderMaterials
void main() {
// Do the play animation logic
}
`
In the main render loop:
// This should be done in Babylon instead WebGL
var timeLocation = context.getUniformLocation(program, "u_time");
function renderLoop(timeStamp) {
// This should be done in Babylon instead WebGL
gl.uniform1f(timeLocation, timeStamp/1000.0);
}