Pass time to material in render script? (SOLVED)

dl;dr: How do I pass constants to a material enabled in a custom render script?


Hello! I am trying to rebuild a microgame I put out in Godot with Defold, both for learning and because it’s a web game and I want to try to get that download size way down. I’m using Defold 1.9.6.

My current sticking point is shaders. Godot has a really easy way to apply a shader to an arbitrary part of the screen as opposed to an individual sprite.

I’ve used some open source code to compile Godot’s shader language into GLSL and made a fragment program out of that (with some modifications to get it working).

Then I made a new render with a material resource. That material uses the fp. It applies to the whole screen, but it looks correct.

However, I need to pass a time vec4 to the fp in order to get the effect I want. Eventually, I’ll also need to pass a vec4 to describe what part of the screen to apply the effect to, while drawing the rest of the screen normally.

So, how do I pass constants to a material being used in the render script?

Also, is this the correct approach? I had been trying to use render targets, but couldn’t get anything to work that way, and all the examples are out of date for the current version.

Thanks so much!

You can use go.set() to pass values to your shader if the material has fragment/vertex constants defined. Here’s an example:

The same principle applies when you tint a sprite (tint is a constant on the sprite.material):

You can also set constants from your render script using constant buffers:

Yes, the constants buffer got me there, thank you!

I had to get the constants with local constants = render.constant_buffer(), set my time constants (u_time), then add the constants to the draw_options_world local: draw_options_world.constants = constants.

2 Likes