Time-related shaders and direction for learning shaders? (SOLVED)

Hello, I just wanted to try a shader that updates according the time. In The Book of Shaders something called u_time is used, but in Defold such a variable won’t work. Following the book, I wrote at the beginning part of the code:

uniform float u_time;

And I got the following warning:

WARNING:RENDER: Type for uniform u_time is not supported (6)

Perhaps a little silly - but why did this happen? How can I import some variable telling the running time (and the cursor position, and so on)? Do I need to firstly add a “Fragment Constant” in the material editing interface, and use a Lua script to get the time and transport it to that “constant”?

Besides, since it’s a shader inside Defold there’re definitely specific usages, but I can’t find out where to learn such things systematically. Are there some recommended tutorials and documents?

(I have read the nice tutorial Shaders for beginners but it’s only a start. In addition I have had a look at the Shader Manual, The Book of Shaders, OpenGL ES 2.0 Online Reference Pages., Shader Toy, … But the logical relationship among them is not so clear to me. It seems like that the shaders are more or less different everywhere …)

2 Likes

This tutorial on porting a Shadertoy shader helped me learn how they work in Defold, plus it has a section on adding time to a shader.

1 Like

There is also a tutorial by @Pawel on shaders here: Shaders for Beginners

1 Like

@britzl Hi britzl, have already read this tutorial, as described

1 Like

From the documentation:

CONSTANT_TYPE_USER

A vector4 constant that you can use for any custom data you want to pass into your shader programs. You can set the initial value of the constant in the constant definition, but it is mutable through the functions [go.set()](https://defold.com/ref/stable/go/#go.set) / [go.animate()](https://defold.com/ref/stable/go/#go.animate). You can also retrieve the value with [go.get()](https://defold.com/ref/stable/go/#go.get). Changing a material constant of a single component instance [breaks render batching and will result in additional draw calls](https://defold.com/manuals/render/#draw-calls-and-batching).
Example: ```lua go.set(“#sprite”, “tint”, vmath.vector4(1,0,0,1))

(It’s also visible from our list of supported types in the code.)

So, if you replace your “float”, with a “vec4”, you should be good to go.

2 Likes

Uniforms in Defold can only be described as vectors of 4 elements, as Matthias mentioned above. This means, if you want to pass something that represents time you will need to add such value in 1 of the 4 places in the vector, for example:

local time = vmath.vector4(0)
    

function update(self, dt)
    time.x = time.x + 1
    go.set("#sprite", "u_time", time)
end

This plus assumgin you hav the uniform set properly in your material should allow you to have a increasing with every update time value in your fragment program :wink:

You can check out “blink_effect_trigger” uniform rm setup in my tutorial :wink:

Regarding the differences between shaders - what are your impressions up to now and what could be your expectations from a tutorial that could fill the gap? :blush:

3 Likes

Guys thanks a lot! In fact I knew how the uniform work (although I ignored the type problem), I just wanted to ask whether people get time information inside the shader program or by using uniforms. Seems like the latter is the right way! (Also, thank you for the tutorials)

2 Likes

Time should be increased by frame’s dt for the effect to look fitted to the updating frames, so yes, I think it is the best to increase it via a uniform :wink:
Also, depending on your setup, you could either increase it in some game object’s script’s update function (for example for individual sprites) or in render script’s update function (for example for drawing textures on quads)

2 Likes