Shader Language in Godot 3.0

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Antor
:warning: Old Version Published before Godot 3 was released.

Greetings!

I made this post because I want to learn the new shader language used in Godot 3.0 and help others to learn or fixing possible issues. Don’t misunderstand me, I don’t pretend to complain about it, just learn and help improving the engine :wink: With hat said, let’s start.

I know it’s mostly GLSL, which I like and I’m familiar with it, but I’m having a lot of trouble to make the simplest shaders. Is there any documentation available? I’ve been looking in every place I could, but I found almost no documentation about it, so with a few posts from Juan, my knowledge about GLSL and digging in the source code, I managed to more or less understand the language.

  1. You have to first specify the shader_type (spatial, canvas_item or particles).
  2. Write the uniforms or any other variable.
  3. Define the shaders functions void vertex() and void fragment().

I’m mostly doing spatial shaders, which I assume are for 3D. So my questions are for that type.

I did the simplest shader for make a sphere in red color (the uniform vector equals to (1, 0, 0) ):enter image description here

I have few questions:

  • How can I make the uniform variable to be a color picker? With that code I simply get a window to introduce vector values. In Godot 2.x you introduce uniform color pick_color; and you get a color picker. I’ve seen Juan doing this: enter image description herebut that doesn’t work for me. Is it a feature still in development or can I do it in some way?
  • How can I get the light information ( director vector, specular, diffusse light, etc.) in a spatial shader type? I digged in the source code and the only thing I could find was, for example, a variable called LIGHT_VEC like in Godot 2.x, but is only defined for canvas_shader type!

Finally, I would like to point out some issue. If you try to use COLOR instead of ALBEDO, this is what happens:
enter image description here

I know it’s in alpha development, and there are still missing features and lot of work to do, but I just wanted to make sure if it’s my poor knowledge about it or not ;).

Thank you very much for your time!

For your color, try to write uniform color Color;?

Also about the last issue, do you have any error printed in the console? I don’t see anything wrong with the code besides it doesn’t set the albedo, which should have resulted at least in a black sphere, but it shouldn’t also break the vertex pass like your screenshot shows. Maybe the vec4 construction is wrongly parsed (I think GLSL usually supports this as a “swizzle constructor”). You can search issues on Github or write one with your example, there are multiples issues already recorded about shaders.

Finally, just for contribution, here is a shader I wrote for heightmap displacement: https://github.com/Zylann/godot_heightmap_module/blob/master/default_shader.txt

Zylann | 2017-08-29 12:57

Hello Zylann.

Thank you very much for the fast reply.

  • About the color, I tried it (also withvect4), but the color type doesn’t seem to exist, although I’ve seen it in action in some screenshots from Juan (I downloaded the source code 2 days ago).

  • About the issue, it happens every time you try to declare the diffuse color with COLOR. In the example above I chose one of many forms that I tried: COLOR.rgb = ALBEDO, COLOR.rgb = Color, COLOR = vec4(ALBEDO, ALPHA), etc.
    All of them lead to the same strange white and black thing. The console doesn’t give any problem. Maybe it’s something wrong in the source code declaration of COLOR

And thank you very much for your shader, I’ll look onto it :wink:

And about the light vectors, do you know anything about it?

Thank you again for your help :smiley:

Antor | 2017-08-29 13:56

:bust_in_silhouette: Reply From: Smellyhobo101

There is a reference for the shader language here.

To edit a color uniform in the editor you need to do uniform vec4 color : hint_color;.

You also might want to look at the screen shaders demo.

Not sure about light shaders. According to this issue some of the light data is not accessible right now.

Hey!, sorry for being late.
Thank you very much, I haven’t tried yer, but I’m sure It’ll work. When I can, I’ll test it.

Thank you for the shaders demo also! :smiley:

Antor | 2017-09-18 21:30

:bust_in_silhouette: Reply From: Ace-Dragon

Your issue stems from the fact that ‘Color’ is not an actual shading parameter here, but a value that you can assign to parameters like ‘Albedo’ (it allows the use of vertex color data).

I too am starting to learn the new shader API and I found this will work for a simple shader where a color is set using the picker (in part thanks to the first answer above).

shader_type spatial;
uniform vec4 color : hint_color;

void fragment(){
	ALBEDO = vec3(color[0],color[1],color[2]);
	}

To note, there’s a few things the reference does not make all that clear right now (hopefully, we will soon get more extensive documentation on actual usage).

Hello!
Thank you for your help! I’ll try it as soon as I can :slight_smile:

Antor | 2017-09-18 21:31