long story short: I'm trying to flip the texture of a particle horizontally, and I'm using the code below. The code below does not work because a particles shader does not "recognize" FRAGCOORDS like a canvas type. Can someone help me?

shader_type particles;

uniform bool flip_texture = true;

void fragment() {
    // Get the particle's local texture coordinates
    vec2 uv = FRAGCOORD.xy; // Use actual texture coordinates

// Flip the texture horizontally if the uniform is set to true
if (flip_texture) {
    uv.x = 1.0 - uv.x;
}

// Sample the texture using the modified UV coordinates
vec4 tex_color = texture(TEXTURE, uv);

// Set the final color of the particle
COLOR = tex_color;
}
  • xyz replied to this.

    xyz when I use UV or UV.xy it throws an error "Unknown identifier in expression: UV"

    • xyz replied to this.

      UpsetChicken Why are you doing this in the particle shader? Particle shaders are used to animate particles not to draw them. They don't have the fragment() function nor do they have UV or FRAGCOORD built ins. You need to do the flipping in the canvas item shader that draws particles.

        xyz You mean the code? I am trying to flip the texture of a particle horizontally, so when my character is facing left, it flips, otherwise, it doesn't. It's probaly worth pointing that I am using Godot 3.6(not sure if this helps)

        • xyz replied to this.

          xyz
          tried writing like this:

          shader_type canvas_item;
          
          uniform bool flip_texture = true;
          
          void fragment() {
              // Get the texture coordinates (UV) passed to the shader
              vec2 uv = UV; // Use the UV coordinates for texture sampling
          
          // Flip the texture horizontally if the uniform is set to true
          if (flip_texture) {
              uv.x = 1.0 - uv.x;
          }
          
          // Sample the texture using the modified UV coordinates
          vec4 tex_color = texture(TEXTURE, uv);
          
          // Set the final color of the fragment (particle or sprite)
          COLOR = tex_color;
          }
          
          

          changed the code to this, but it had no visible effect

          xyz So, I have a "Character" node, that node has a child node called "Particles", and that code is assigned to "Particles" Process Material (Shader)

          • xyz replied to this.

            xyz Dude I've been slamming my dumb head into the keyboard for the last two days and you literally made me solve my problem. Thanks!

            @xyz
            hey! so, I managed to flip the particle horizontally, and made a small snippet to flip, animate and display the animation of the particle, but when I do this with shaders, the frame becomes stretched. What am I doing wrong? (sorry to ping you again)

            shader_type canvas_item;
            
            uniform int total_frames = 4;  // Total frames in the sprite sheet (4 horizontal frames)
            uniform float animation_speed = 1.0;  // Speed of the animation (higher value = faster animation)
            uniform bool flip = false;  // Uniform to control horizontal flip of the texture
            uniform vec2 sprite_size;  // Sprite size in texture space
            
            void fragment() {
                // Get the texture coordinates (UV) passed to the shader
                vec2 uv = UV;
            
            // Calculate the width of each frame (each frame takes up 1/4th of the texture width)
            float frame_width = 1.0 / float(total_frames);
            
            // Calculate the current frame based on time and animation speed
            float time = TIME * animation_speed;  // TIME is the built-in variable that provides the elapsed time in seconds
            int current_frame = int(mod(time, float(total_frames)));  // Modulo to cycle through frames
            
            // Calculate the X offset for the current frame
            float frame_x_offset = float(current_frame) * frame_width;
            
            // Adjust the UV.x to point to the correct frame in the texture
            uv.x = frame_x_offset + uv.x * frame_width;
            
            // If flip is enabled, invert the UV.x to flip the texture horizontally
            if (flip) {
                uv.x = 1.0 - uv.x;
            }
            
            // UV.y based on sprite height to prevent stretching
            uv.y = uv.y * sprite_size.y;  // Ensure UV.y stays within the correct height range
            
            // Sample the texture using the modified UV coordinates
            vec4 tex_color = texture(TEXTURE, uv);
            
            // Set the final color of the fragment (particle or sprite)
            COLOR = tex_color;
            }
            
            
            • xyz replied to this.

              xyz horizontally streched. the width of the frames surpass the height. The result is an image that looks like "squashed"

              • xyz replied to this.

                UpsetChicken Post the image of the whole texture, and the rendered result. Also what's the value of sprite_size uniform?

                  xyz

                  this is the result. my particle has four frames like this one. 4 columns and 1 row.

                  Also what's the value of sprite_size uniform?

                  x = 1 and y = 1

                  • xyz replied to this.

                    UpsetChicken Maybe the particle itself is scaled. How do particles look if you just render them as opaque quads without the texture?

                      xyz
                      the frames will render with the "normal" scale, but there will be four frames displayed on-screen

                      • xyz replied to this.

                        UpsetChicken Not sure I understand what you mean. Post more screenshots that show particle bounding boxes not just textures.

                          UpsetChicken What does the white rectangle represent? What do you get when the shader's last line is COLOR = vec4(1.0)?