How can I print variables output in godot shader?

Godot Version

4.2.2

Question

How can I print variables output in godot shader? like print() in gdscript. It is so hard to learn without it…
I know I can get uniform variables in gdscript and print it but I want to get like TEXTURE, UV, COLOR

Since the code runs on your GPU, there is no console to print from back to the CPU. Searching around, many artists leverage visual tests from the output of the shader itself.

As far as Godot shader goes I have not read of any print functionality in the documentation.

1 Like

Unfortunately you can’t.

Uniform variables are one way only. You can assign them via script but the shader can’t change them. Also, since the shader runs in your GPU you can’t print() or do any console related stuff.

There are several workarounds though. The first one is to print a meaningful color in the shader, for instance, if you’re trying to detect an area where your shader works, you can print full red vec4(1,0,0,1). But it all depends on what you’re trying to do.

I think, there are some tools, glslDevil, but I haven’t tried it.

Specifically, what are you trying to do?

1 Like

You cannot. It’s not possible. Shaders run on the GPU and have no access to any output console.
There is a workaround, you can add a function that displays digits on the screen, but you must do it the “shader” way. This is an example: Shader - Shadertoy BETA

1 Like

Oh… Thanks for letting me know.
I looked at the official documentation, but it was hard to understand.
So I prefer to put in some variables and print them out to see exactly how the function works rather than just read the docs.
For shader, it was much much harder than gdscript. Usually I just copy from godotshaders.com, but mostly it is not exactly what I wanted. so I have to edit it.
I created a shader that dynamically changes color and shape at the same time. it is just three lines of codes, but it took too much time. because I don’t know exactly how COLOR = texture(TEXTURE, UV) works…
I think I need to watch for more tutorials on YouTube and I just found good tutorials

Any GLSL/opengl shader tutorials will mostly work. Keep in mind Godot puts its own flavor on things but the concepts are the same.

For canvas shaders that use the COLOR variable. You must think of the fragment function running for each pixel on the screen individually. COLOR will represent the color of one pixel at a time. This my seem inefficient but, GPUs have many cores that work in parallel so it goes by extremely fast. This also puts constraints on how you may think about writing shader code. It doesn’t work quite the same as writing regular code on a CPU due to hardware differences.

The Texture function is just a helper function that uses a sampler2d and a coordinate to return a pixel from the sampler2d (also usually referred to as texture).

You can think of UV as a coordinate system similar to XY but for the subject of the shader itself. UV will usually represent a point on the object you are wanting to color. Although the UV map can be manipulated differently from the sprite/mesh itself. Its similar to COLOR variable in that it will have many values in parallel for fragments that needs to be drawn.

1 Like

Yeah, shaders are a bit harder than gdscript, it’s a different world, but they’re more simple (in that there are limited things you can do). Once you get a grip on them, they’re a powerful tool.
Don’t get discouraged by that, just follow a few tutorials and you’ll learn them quickly, as I said, they’re a bit harder to understand, but once you learn like 3 or 4 basic things, you will be able to read and modify any shader.

1 Like

Maybe this will help. :smiley: I have just published a new book, this time dedicated to creating shaders in Godot. Learn how to program your own visual effects!

1 Like

Thanks. but actually I’m not good at English. Reading text is harder than watching video for me :joy:

Thanks guys. It’s really helpful for me. I just find tutorials about godot shader texture and uv. :smile:

Heh, no worries. :sunglasses:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.