extends Node2D



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
	
	
	
	
	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)
shader_type canvas_item;

vec3 rainbow(float level)
{
	/*
		Target colors
		=============
		
		L  x   color
		0  0.0 vec4(1.0, 0.0, 0.0, 1.0);
		1  0.2 vec4(1.0, 0.5, 0.0, 1.0);
		2  0.4 vec4(1.0, 1.0, 0.0, 1.0);
		3  0.6 vec4(0.0, 0.5, 0.0, 1.0);
		4  0.8 vec4(0.0, 0.0, 1.0, 1.0);
		5  1.0 vec4(0.5, 0.0, 0.5, 1.0);
	*/
	
	float r = float(level <= 2.0) + float(level > 4.0) * 0.5;
	float g = max(1.0 - abs(level - 2.0) * 0.5, 0.0);
	float b = (1.0 - (level - 4.0) * 0.5) * float(level >= 4.0);
	return vec3(r, g, b);
}

vec3 smoothRainbow (float x)
{
    float level1 = floor(x*6.0);
    float level2 = min(6.0, floor(x*6.0) + 1.0);
    
    vec3 a = rainbow(level1);
    vec3 b = rainbow(level2);
    
    return mix(a, b, fract(x*6.0));
}


void fragment() {
	
	vec2 uv = UV;
	vec3 color = smoothRainbow(uv.x);
	
	COLOR = vec4(color,1.0);
	
}
  • xyz replied to this.

    Italians Please format your code properly using ``` tags.
    Also describe more precisely what the problem is. In what way is shader not displayed correctly?

    I used "" but don't understand where ?
    I don't speak English well

      Italians encapsulate whole code with ``` not \"""

      The code block tags are with the button under Esc button "tilde" 🤌

      damn what difference does it make whether the code is highlighted or not
      I need an answer
      and as I sad I don't understand what you sad correctly and what's wrong with my post

      • xyz replied to this.

        Italians It's not about highlighting, it's about indentation which is an important syntax element in GDScript.
        Also when you ask people for free help it's just good manners to make as easy as possible for them to help you.
        So please format your code and describe your problem as specifically as you can.

        Italians If ``` is not working for you, you can try with ~~~
        You can just copy/paste the characters from the above sentence 🙂

        When I add shader to polygon2d shader does not display correctly
        help me good people

        • xyz replied to this.
          Italians changed the title to When Shader added to Polygon2d they don't show correctly .

          Italians Can you show how it displays vs. how you expect it to display? We don't know what "not correctly" means in this case. It does exactly what you programmed it to do 🙂

          The red is polygon2d
          the rainbow is sprite
          so I need the same result on polygon2d

          • xyz replied to this.

            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")

            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?

            • xyz replied to this.

              Italians Well, you'll need to update the UVs to reflect that growth.

              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

              • xyz replied to this.

                but when I change the scale it doesn't repeat the texture

                • xyz replied to this.

                  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.