|
|
|
|
Reply From: |
timoschwarzer |
-
Create a new CanvasItemMaterial:
-
Edit the new CanvasItemMaterial:
-
Create a new CanvasItemShader:
-
Use this code as Fragment Shader:
COLOR.rgb = vec3(dot(COLOR.rgb, vec3(0.299, 0.587, 0.114)));
Here you’ll find an explanation of the calculation above.
Enabling/Disabling grayscaling at runtime
You can extend your shader code to have an uniform variable to control whether applying the grayscale filter or not. This would look like this:
uniform bool grayscale = false;
if (grayscale) {
COLOR.rgb = vec3(dot(COLOR.rgb, vec3(0.299, 0.587, 0.114)));
}
After changing your shader code to the above, you should have a new option in your CanvasItemMaterial:
Better solution than mine! Colours don’t have the same gamma contribution and this really makes use of it (for example, green is brighter than blue on a CRT screen)
Small warning though: use if
with care in a shader, for some graphic cards it means compiling as many binaries as combinations since GPUs work differently than CPUs.
Zylann | 2016-07-16 15:19
Thank you for your answer! Really great effect, what I was searching for!
Is this process possible to be made in GDScript? I would like to create the shader and attach it to all TextureFrames I have.
I would like to know too, is this the only way to get that effect? I expected something like “TextureFrame.set_color(“grey”)”, because as far as I know, shaders are code that are always running and I’m afraid about that.
Not_a_Robot | 2016-07-16 17:16
Grey is not a color, it is the absence of color (or the presence of all of them, depends on the brightness :p), so you cannot really do set_color(grey)
.
When you do set_color
you’ll apply a tint over the base texture, but you cannot remove colors this way. You could just use that if your texture is monochrome, but otherwise you need a shader.
The GPU executes shaders tens of thousands times faster than GDScript, and this shader is so simple it might even be faster than the default one. Don’t worry too much about that
Zylann | 2016-07-16 17:45
This shader is nothing for a GPU…
So, the optimal way would be writing that shader, save the shader (NOT the material) as .shd (Go into shader edtiting mode and click the save icon at the upper left hand of the inspector). Then set the disabled-property with GDScript
timoschwarzer | 2016-07-16 18:14
I rewrote this shader in VIsualShader for Godot 3.1 over on my blog. Thanks for making such a simple shader that I could use as a base for learning about VisualShader!
Well, this doesn’t work with godot 3.
I messaged it into this :
shader_type canvas_item;
uniform bool grayscale = false;
void fragment() {
if (grayscale) {
COLOR.rgb = vec3(dot(COLOR.rgb, vec3(0.299, 0.587, 0.114)));
}
}
but all that does is gives me a completely grey square.
AllenKll | 2021-09-11 16:38