Passing Time to a Shader

Hey all,



I feel a bit silly, but I’m having a hell of a time trying to figure out how to do this properly: pass a time float variable to my fragment shader.



I first tried doing a simple:



[java]material.setFloat(“Time”, timeValue);[/java]



… but this really destroys performance as it seems to create a new shader memory object every time this is called.



I then tried doing this:



[java]timeScale += time_per_frame;

Uniform timeval = simpleShader.getUniform(“g_Time”);

timeval.setValue(VarType.Float, timeScale);[/java]



… but it just doesn’t seem to work (the g_Time uniform just stays 0, even though when stepping through debugging, timeScale is != 0).



I have “uniform float g_Time;” defined at the top of my .frag and .vert. Any suggestions? :frowning:



Thanks,

  • Phr00t

Have you added ‘Time’ to the WorldParameters?

For example this j3md

[java]MaterialDef My MaterialDef {



MaterialParameters {

Color Color1

Color Color2

Float Radius

Float Multi

}



Technique {

VertexShader GLSL150: Shaders/CloudShader/cloudShader.vert

FragmentShader GLSL150: Shaders/CloudShader/cloudShader.frag



WorldParameters {

WorldViewProjectionMatrix

Time

}

}



}[/java]



let’s me use such .frag code nicely

[java]

uniform float g_Time;

main(){

float nv1= (snoise(vec4(texCoord4D.xyz*m_Multi/100,g_Time/10)- 0.5));

}

[/java]



With this there is no need to update the time variable.

2 Likes

In your j3md definition file put time in there like this:



[java]



Technique {

VertexShader GLSL130: CustomShader/Stars/sun.vert

FragmentShader GLSL130: CustomShader/Stars/sun.frag



WorldParameters {

WorldViewProjectionMatrix

WorldViewMatrix

ProjectionMatrix

Time // <-- here it is

}



Defines {

BASIC : Basic

}

}

[/java]



In your frag/vert make sure the definition is present. (uniform float g_Time;)



That’s it. Time will be given to your shader.

2 Likes

Gah. Ninja’d!

Woah… I hit refresh, and 4 replies including the perfect answer? Sweeeeet! Thanks! :slight_smile:

1 Like