Home Game Development shaders – Prevent color adjustment in ViewportTexture

shaders – Prevent color adjustment in ViewportTexture

0
shaders – Prevent color adjustment in ViewportTexture

[ad_1]

In Godot 4.1.1, I’m trying to use ViewportTextures to capture values generated by some shaders for use in another shader. One SubViewport is capturing the result of an unshaded spatial shader on a full-screen quad. However, when I sample the texture, I don’t get exactly what the shader produced. It looks like the colors are adjusted. How can I prevent that?

Here’s a simplified setup to show the issue:

Node3D
  MeshInstance3D(QuadMesh, postprocess.gdshader)
  Camera3D
  SubViewport2D(disable_3d = true)
    ColorRect(Full Rect, 2d.gdshader)
  SubViewport3D(own_world_3d = true)
    MeshInstance3D(QuadMesh, 3d.gdshader)
    Camera3D

// 2d.gdshader
shader_type canvas_item;
void fragment() { COLOR = vec4(UV, 0.4, 1.0); }

// 3d.gdshader
shader_type spatial;
render_mode unshaded;
void vertex() { POSITION = vec4(VERTEX, 1.0); }
void fragment() { ALBEDO = vec3(SCREEN_UV, 0.4); }

// postprocess.gdshader
shader_type spatial;
render_mode unshaded;

uniform sampler2D SubViewport2D;
uniform sampler2D SubViewport3D;

void vertex() { POSITION = vec4(VERTEX, 1.0); }
void fragment() {
    vec3 col1 = vec3(SCREEN_UV, 0.4);
    vec3 col2 = texture(SubViewport2D, SCREEN_UV).rgb;
    vec3 col3 = texture(SubViewport3D, SCREEN_UV).rgb;
    // Put col2 on the left, col3 on the right.
    bool left = SCREEN_UV.x < 0.5;
    // Put col1 on even pixels.
    bool even = fract((SCREEN_UV * VIEWPORT_SIZE).x * 0.5) < 0.5;
    ALBEDO = even ? col1 : (left ? col2 : col3);
}

In the last shader, I expect col2 and col3 to equal col1. The output indicates that col2 = col1, while col3 is visually similar but not the same:

enter image description here

I tried adding a WorldEnvironment to SubViewport3D but it didn’t make a difference. This issue looks relevant but I’m not sure.

[ad_2]