GMS 2 Draw Sprite Part Extended Need Help

Petrik33

Member
So I am not very familiar with shaders but I have watched and read most neccesary tutorials to understand how they work. I need my shader for drawing laser weapon aim. The laser weapon needs to be charged before shooting, this is why I want my aim to show the current charge by filling the inside of the circle (except from the dot in the center) with the color in gradient from green to red(the color can be made by simple GML code if you don't know how to make the gradient), but the shader should fill not the whole space inside the aim, but the percentage of charging from down to top(e.g. if the weapon is half charged the bottom half of the aim should be filled with color between green and red). If the idea is not clear enough write here please.
 
There's a number of different ways of doing this, but I'm feeling a bit out of it, so I'll just tell you about the easiest way.

Si, I'm presuming your aim circle is a sprite. If you put that sprite on its own texture page, then the texture coordinate on the y axis will be 0 at the top of the sprite, and 1 at the bottom of the sprite. So, in the fragment shader, the tex coord varying variable will tell you the current position within the sprite. If your circle doesn't take up the whole sprite, you may need to do a bit of simple math. Now, you can do a comparison between the charge proportion (make it in the range 0 to 1), and the tex coord y coordinate. Cast the result as a float to use in mix functions. example: col = mix(col, new_col, float(charge_proportion < v_vTexcoord.y)); If you need to invert the y coordinate, use (1.0 - v_vTexcoord.y). Flipping from < to > or vice versa will invert the fill proportion.
 
Last edited:
You know what, you can probably get just as good results without having to use a shader at all. If you had a white sprite you could use the blend attribute to color it. And draw_sprite_part to change the "charge" level.
 

Petrik33

Member
You know what, you can probably get just as good results without having to use a shader at all. If you had a white sprite you could use the blend attribute to color it. And draw_sprite_part to change the "charge" level.
man, that's a nice suggestion about using non-shader solution, thanks a lot!
 

Petrik33

Member
You know what, you can probably get just as good results without having to use a shader at all. If you had a white sprite you could use the blend attribute to color it. And draw_sprite_part to change the "charge" level.
Hey man, could you please help me with the draw_sprite_part_ext function cause this is the code I have now, it leads to background circle drawn on the neccesary place only when the charge progress is maximum(1), otherwise it draws it over the aim, here are the screenshots and code:
GML:
/// @description Draw Charge Progress
var chargeProgress=Weapon.LaserWeaponCharge/Weapon.LaserWeaponMaxCharge;
chargeProgress=clamp(chargeProgress,0,1);
var progressColor=merge_color(c_green,c_red,chargeProgress)
if(chargeProgress!=0)
{
    draw_sprite_part_ext
    (
        spr_aim_background,0,    //Spr,SubImage

        0, floor(sprite_height*(1-chargeProgress)),  //Left,Top
        sprite_width, floor(sprite_height*chargeProgress),   //Width,Height

        x-sprite_xoffset,y-sprite_yoffset,1,1,  //X,Y,XScale,YScale
        progressColor,   //Color
        0.5   //Alpha
    )
}
draw_self();
 

Attachments

That's because when it gets to it's full size that position is right, but when it's in the "growing" stage, there's an extra offset that is needed. That extra offset starts at some value and shrinks to 0 in conjunction with the growth of the sprite. The extra offset that is needed is probably the inverse of the "top" value you have (so the y position should be something like
Code:
y-sprite_yoffset+floor(sprite_height*(1-chargeProgress))
Though, it might be subtracting instead of adding. It'll just require a little experimentation on your part to see exactly what you need to do to ensure the position stay consistent with the growth.
 

Petrik33

Member
That's because when it gets to it's full size that position is right, but when it's in the "growing" stage, there's an extra offset that is needed. That extra offset starts at some value and shrinks to 0 in conjunction with the growth of the sprite. The extra offset that is needed is probably the inverse of the "top" value you have (so the y position should be something like
Code:
y-sprite_yoffset+floor(sprite_height*(1-chargeProgress))
Though, it might be subtracting instead of adding. It'll just require a little experimentation on your part to see exactly what you need to do to ensure the position stay consistent with the growth.
Hey man, this worked exactly, with the first try, thank you very much, I can't even explain how much you helped me
 
Top