0 votes

I'm attempting to write a spatial fragment shader that makes half of an object one color, and the other half another color. I've tried the following below, but the results aren't what I expected:

shader_type spatial;

void fragment() {
    if(UV.x < 0.5) {
        ALBEDO = vec3(0.35, 0.83, 0.32);
        ALPHA = 0.75;
        EMISSION = vec3(0.35, 0.83, 0.32);
    }
    else {
        ALBEDO = vec3(250, 250, 0);
    }
}

This resulted in the following:
enter image description here

Now, I want the color of this green rectangle to match the exact color of the one further back in the picture. The albedo, alpha, and emission colors are set exactly the same, but the image is still not the same color.

In addition to this, the color is not split evenly across the rectangle. You'll notice the front strip of the rectangle is green instead of half green and half yellow.

For some additional context, the green rectangle in the background has the follow properties set (not with a custom spatial shader, just through the GUI):

Flags Transparent: True
Albedo RGBA: (0.35, 0.83, 0.32, 0.75)
Emission Enabled: True
Emission RGB: (0.35, 0.83, 0.32)

Any help is greatly appreciated, thanks!

Godot version 3.2.3
in Engine by (30 points)

1 Answer

0 votes
Best answer

Another person from a separate chat client suggested this solution and it ended up working.

Assigning ALBEDO a vec3 of 88, 211, 81 caused the color to immediately turn white (I believe this is because I should've been using the raw values bounded between [0, 1], though these weren't generating the correct colors either), but using the newly declared color variable's r,g,b,a members and setting them to the values (not-raw) that I selected for the green rectangle in the background got the colors to match.

Also, switching from UV to VERTEX worked for me since my camera doesn't move and is focused/centered on the 0 line of the x axis:

shader_type spatial;

uniform vec4 color : hint_color = vec4(88, 211, 81, 190);

void fragment() {
    if(VERTEX.x < 0.0) {
        vec3 oricol = vec3(color.r, color.g, color.b);
        ALBEDO = oricol;
        EMISSION = oricol;
        ALPHA = color.a;
    }
    else {
        ALBEDO = vec3(250, 250, 0);
    }
}

enter image description here

by (30 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.