Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | rnnr |
I’m trying to use simple shader to draw square texture split to quarters - top right and bottom left red, the rest white.
I crated scene wit root CanvasLayer and TextureRect in it, and I associated a shader with the texture:
shader_type canvas_item;
render_mode unshaded;
// rectangle dimensions
uniform vec2 position;
uniform vec2 size;
void vertex() {
}
void fragment() {
// relative coordinates (inside the rectangle)
float x = FRAGCOORD.x-position.x;
float y = FRAGCOORD.y-position.y;
if
(
//(x > size.x / 2.0) // whole right half
(y < size.y / 2.0) // should be lower half
)
{
COLOR = vec4(1.0, 0.0, 0.0, 1.0)
}
else
{
COLOR = vec4(1.0);
}
}
The condition is reduced here to make the code short.
I set the position and size from GD script:
extends TextureRect
func _ready():
self.material.set("shader_param/position", self.rect_position)
self.material.set("shader_param/size", self.rect_size)
This way I should be able to move the textured rectangle around the scene and draw the desired “pattern”.
The problem is may code works for me only for x axis ( the condition with "whole right half " comment) – regardless of where the textured rectangle is, the right half is red as desired.
For y axis (commented “should be lower half”) it does not work - it seems to take y coordinate relatively to screen, not the textured rectangle, in other words the lower half is red only when the thing is in the center of the screen.
What am I doing wrong here ?