render gamma

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JKTech

how do i get and set the screen shader inside a gdscript function?
my plan is to change the gamma, ive seen some tutorials but im not sure how to get the screen shader atm

:bust_in_silhouette: Reply From: estebanmolca

To access the shader parameters, use material.set_shader_param (" uniform_name ", value)
To make a shader that affects the entire screen, you have to have a node with an opaque color that covers the entire screen and is drawn last.
For example a ColorRect in the last position of the tree.
In the shader SCREEN_TEXTURE is used, this is the texture of everything that the node has behind. I leave you an example:


Node2d —> whit script
—ColorRect —> whit shader, cover all the screen


extends Node2D

func _ready():
	$ColorRect.material.set_shader_param("brightness", 1.4)
    $ColorRect.material.set_shader_param("contrast", 0.8)

ShaderMaterial in ColorRect:

shader_type canvas_item;

uniform float brightness = 1.0;
uniform float contrast = 1.0;
uniform float saturation = 1.0;

void fragment() {
    vec3 c = textureLod(SCREEN_TEXTURE, SCREEN_UV, 0.0).rgb;

    c.rgb = mix(vec3(0.0), c.rgb, brightness);
    c.rgb = mix(vec3(0.5), c.rgb, contrast);
    c.rgb = mix(vec3(dot(vec3(1.0), c.rgb) * 0.33333), c.rgb, saturation);

    COLOR.rgb = c;
:bust_in_silhouette: Reply From: NeedleValve

Godot Shading language has a special texture, SCREEN_TEXTURE (and DEPTH_TEXTURE for depth, in the case of 3D). It takes as argument the UV of the screen and returns a vec3 RGB with the color. A special built-in varying: SCREEN_UV can be used to obtain the UV for the current fragment. As a result, this simple canvas_item fragment shader:

void fragment() {
results in an invisible object, because it just shows what lies behind.

The reason why textureLod must be used is because, when Godot copies back a chunk of the screen, it also does an efficient separatable gaussian blur to its mipmaps.

This allows for not only reading from the screen, but reading from it with different amounts of blur at no cost.NTGD Needle Valve