1
Support / Animated shader
« on: April 19, 2014, 06:09:57 pm »
Hello,
I'm trying to animate a texture on my objects using a time variable, the problem is that it doesn't move.
This is where I update the time variable (in the onDrawFrame method ) :
Here we set the uniforms in the method that creates the object :
ShaderUniform is a class I've made that has a type parameter, and ShaderUtils.setShader is a method that will iterate through the arraylist and choose the right setStaticUniform method according to this type parameter.
And in the animateTextures method, this is how the shader should update the currentTime uniform:
If needed, the fragment shader :
Note that I got this working on another project that used Three.JS and WebGL, I have simply taken this code again and slightly modified it, but the time variable thingy has not changed (in the javascript version, the time uniform was updated in the renderLoop using a delta provided by a ThreeJS method).
It's meant to animate water textures using a noisemap, I use it for a kind of lava effect.
Is it possible to update a uniform in a shader (then the way I do it is probably wrong), or does the problem comes from my time variable ?
I can't figure out yet why this is not working (and I have already searched if someone had the same problem, but it didn't help me that much), any help would be appreciated.
Thanks in advance for your answer.
I'm trying to animate a texture on my objects using a time variable, the problem is that it doesn't move.
This is where I update the time variable (in the onDrawFrame method ) :
Code: [Select]
...
if (System.currentTimeMillis() - time >= 1000) {
Logger.log(fps + "fps");
fps = 0;
time = System.currentTimeMillis();
currentTime = System.nanoTime() - startTime;//startTime is initialized in the class' constructor with : startTime = System.nanoTime();
animateTextures();
}
fps++;
...
Here we set the uniforms in the method that creates the object :
Code: [Select]
...
String uniformTime = mContext.getResources().getString(R.string.global_uniform_time);
ArrayList<ShaderUniform> uniformsTop = new ArrayList<ShaderUniform>();
uniformsTop.add(new ShaderUniform("colorMap", 1));
uniformsTop.add(new ShaderUniform("noiseMap", 1));
uniformsTop.add(new ShaderUniform(uniformTime, currentTime));
uniformsTop.add(new ShaderUniform("alpha", 1.0));
uniformsTop.add(new ShaderUniform("baseSpeed", 0.005));
uniformsTop.add(new ShaderUniform("noiseScale", 0.1337));
uniformsTop.add(new ShaderUniform("invRadius", 0.0005f));
ShaderUtils.setShader(mContext, towerTop, shadersDir+"towers/"+"vertexShader.glsl", shadersDir+"towers/"+"fragmentShader.glsl", uniformsTop);
...
ShaderUniform is a class I've made that has a type parameter, and ShaderUtils.setShader is a method that will iterate through the arraylist and choose the right setStaticUniform method according to this type parameter.
And in the animateTextures method, this is how the shader should update the currentTime uniform:
Code: [Select]
t.getTop().getShader().setUniform(uniformTime, currentTime); //uniformTime is the name of the variable
If needed, the fragment shader :
Code: [Select]
precision mediump float;
varying vec3 lightVec[2];
varying vec3 eyeVec;
varying vec2 texCoord;
uniform sampler2D colorMap;
uniform sampler2D noiseMap;
uniform float time;
uniform float baseSpeed;
uniform float noiseScale;
uniform float invRadius;
uniform float alpha;
uniform vec3 diffuseColors[8];
uniform vec3 specularColors[8];
uniform vec4 ambientColor;
float coef = 10.0;
void main ()
{
//Speed & direction of the texture
vec2 uvTimeShift = texCoord + vec2( -0.4, 0.5 ) * time * baseSpeed;
vec4 noise = texture2D( noiseMap, uvTimeShift );
vec2 uvNoisyTimeShift = texCoord + noiseScale * vec2( noise.r, noise.g ); //noise quantity
vec4 baseColor = texture2D( colorMap, uvNoisyTimeShift );
baseColor.a = alpha;
//...
gl_FragColor = baseColor;
}
Note that I got this working on another project that used Three.JS and WebGL, I have simply taken this code again and slightly modified it, but the time variable thingy has not changed (in the javascript version, the time uniform was updated in the renderLoop using a delta provided by a ThreeJS method).
It's meant to animate water textures using a noisemap, I use it for a kind of lava effect.
Is it possible to update a uniform in a shader (then the way I do it is probably wrong), or does the problem comes from my time variable ?
I can't figure out yet why this is not working (and I have already searched if someone had the same problem, but it didn't help me that much), any help would be appreciated.
Thanks in advance for your answer.