When Shader added to Polygon2d they don't show correctly
The red is polygon2d
the rainbow is sprite
so I need the same result on polygon2d
- Edited
Italians Polygon2D's UVs work in a bit of a weird way. They're not really meant to be used with a custom shader. UVs are actually scaled to match pixel size of a texture assigned to the polygon. Without a texture, they'll always be (0,0). The quickest workaround for your current setup would be to just create a 1x1 pixels dummy texture and assign it to the polygon. So in your create_polygon()
function add:
polygon.texture = preload("res://my_dummy_1x1_texture.png")
- Edited
Thank man!!!
how to make the texture not stretch
and multiplied
that is, as in the example in the code above
we hold the button and the polygon grows down
texture shader just stretches
How can I make it repeat?
- Edited
for me its dosnt work
can you help with this code
shader
shader_type canvas_item;
vec4 square(vec2 uv,float width,vec2 position){
uv = uv * 2.0 - 1.0;
vec2 abs_uv = abs(uv.xy + position);
float square = step(width,max(abs_uv.x, abs_uv.y));
return vec4(vec3(square),1.0);
}
void fragment() {
COLOR = vec4(square(UV,0.2, vec2(0.5,0.5)).r,square(UV,0.4,vec2(0.8,-0.8)).g,square(UV,0.4,vec2(-0.3,0.3)).b,1.0);
}
and the main code
extends Node2D
@onready var polygon_2d: Polygon2D = $Polygon2D
var polygon: Polygon2D
var packed_array: PackedVector2Array
var test_pos: float = 2.0
var shader_tex
var shader
func _ready() -> void:
create_polygon()
func create_polygon():
polygon = Polygon2D.new()
polygon.polygon = [
Vector2(100, 100),
Vector2(100,300 + test_pos),
Vector2(300,300 + test_pos),
Vector2(300,100),
]
shader_tex = preload("res://new.gdshader")
shader = ShaderMaterial.new()
shader.shader = shader_tex
var dummy_tex: Texture2D = preload("res://test/dummy_tex.png")
polygon.texture = dummy_tex
polygon.TEXTURE_REPEAT_ENABLED
polygon.material = shader
#polygon.uv = [
#Vector2(0, 0),
#Vector2(0,1),
#Vector2(1,1),
#Vector2(1,0),
#
#]
add_child(polygon)
func _process(delta: float) -> void:
if Input.is_action_pressed("action"):
test_pos += 200 * delta
polygon.polygon = [
Vector2(100, 100),
Vector2(100,300 + test_pos),
Vector2(300,300 + test_pos),
Vector2(300,100),
]
print("test", test_pos)
polygon.uv = [
Vector2(0, 0),
Vector2(0,1),
Vector2(1,1),
Vector2(1,0),
]
expected result where the pattern repeats
and in a column and the real result of the shader is stretched
how to make it not stretch but repeat
placed the polygon.uv into the process function but nothing changed
Italians Play with the polygon.texture_scale property. Adjust it according to the new size of your polygon.
but when I change the scale it doesn't repeat the texture
- Edited
Italians There's no texture in your case. You're just tricking the engine to send UVs to your shader. For debugging, you can render the UVs directly out from the shader as r and g components of your output color. It'll visualize what you're getting in the UV builtin so you can adjust it additionally as needed by your shader code. This may include translating, scaling or wrapping, depending on how your shader utilizes the UVs.