Sign in to follow this  
Simple0x47

[FUNCTION] DxDrawRoundedRectangle - DxDrawCircle

Recommended Posts

Hello everyone, nothing to say so.

LUA:

function dxDrawCircle( x, y, width, height, color, angleStart, angleSweep, borderWidth )
		height = height or width
		color = color or tocolor(255,255,255)
		borderWidth = borderWidth or 1e9
		angleStart = angleStart or 0
		angleSweep = angleSweep or 360 - angleStart
		if ( angleSweep < 360 ) then
			angleEnd = math.fmod( angleStart + angleSweep, 360 ) + 0
		else
			angleStart = 0
			angleEnd = 360
		end
		x = x - width / 2
		y = y - height / 2
		if not circleShader then
			circleShader = dxCreateShader ( "circle.fx" )
		end
		dxSetShaderValue ( circleShader, "sCircleWidthInPixel", width );
		dxSetShaderValue ( circleShader, "sCircleHeightInPixel", height );
		dxSetShaderValue ( circleShader, "sBorderWidthInPixel", borderWidth );
		dxSetShaderValue ( circleShader, "sAngleStart", math.rad( angleStart ) - math.pi );
		dxSetShaderValue ( circleShader, "sAngleEnd", math.rad( angleEnd ) - math.pi );
		dxDrawImage( x, y, width, height, circleShader, 0, 0, 0, color, true )
	end

	function dxDrawRoundedRectangle( x, y, width, height, radius, color )
		if ( radius >= width ) or ( radius >= height ) then dxDrawCircle( x - ( radius / 2 ), y - ( radius / 2 ) ) end -- The radius is relative to the height and width of the rectangle, so to avoid rectangles if the radius is bigger than the width or height then a complete circle is being drawn.
		dxDrawCircle( x + ( radius / 2 ), y + ( radius / 2 ), radius, radius, color, 270, 90 )
		dxDrawCircle( x + ( radius / 2 ), ( y + height ) - ( radius / 2 ), radius, radius, color, 180, 90 )
		
		dxDrawCircle( ( x + width ) - ( radius / 2 ), y + ( radius / 2 ), radius, radius, color, 0, 90 )
		dxDrawCircle( ( x + width ) - ( radius / 2 ), ( y + height ) - ( radius / 2 ), radius, radius, color, 90, 90 ) 
		
		dxDrawRectangle( x, y + ( radius / 2 ), width, ( height ) - ( radius ), color, true )
		
		dxDrawRectangle( x + ( radius / 2 ), y , ( width ) - ( radius ), ( radius / 2 ), color, true )
		dxDrawRectangle( x + ( radius / 2 ), ( y + height ) - ( radius / 2 ), ( width ) - ( radius ), ( radius / 2 ), color, true )
	end

CIRCLE.FX ( The one from the MTA Wiki )

float sCircleHeightInPixel = 100;
float sCircleWidthInPixel = 100;
float sBorderWidthInPixel = 10;
float sAngleStart = -3.14;
float sAngleEnd = 3.14;

//------------------------------------------------------------------------------------------
// PixelShaderFunction
//  1. Read from PS structure
//  2. Process
//  3. Return pixel color
//------------------------------------------------------------------------------------------
float4 PixelShaderFunction(float4 Diffuse : COLOR0, float2 TexCoord : TEXCOORD0) : COLOR0
{
    float2 uv = float2( TexCoord.x, TexCoord.y ) - float2( 0.5, 0.5 );

    // Clip unwanted pixels from partial pie
    float angle = atan2( -uv.x, uv.y );  // -PI to +PI
    if ( sAngleStart > sAngleEnd )
    {
        if ( angle < sAngleStart && angle > sAngleEnd )
            return 0;
    }
    else
    {
        if ( angle < sAngleStart || angle > sAngleEnd )
            return 0;
    }

    // Calc border width to use
    float2 vec = normalize( uv );
    float CircleRadiusInPixel = lerp( sCircleWidthInPixel, sCircleHeightInPixel, vec.y * vec.y );
    float borderWidth = sBorderWidthInPixel / CircleRadiusInPixel;

    // Check if pixel is inside circle
    float dist =  sqrt( dot( uv, uv ) );
    if ( ( dist > 0.5 ) || ( dist < 0.5 - borderWidth ) )
        return 0;
    else 
        return Diffuse;
}

//------------------------------------------------------------------------------------------
// Techniques
//------------------------------------------------------------------------------------------
technique tec0
{
    pass P0
    {
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}

 

  • Like 2

Share this post


Link to post
On 20.5.2017 at 17:16, #_iMr.[E]coo said:

Please Add Pictures 

I tried it out:

This is with a radius of 10. Is this the right radius? Because i get weird results if i use > 10 or < 10 

a7d291f3c51a4abf8b46395d074bdc3b.png

  • Like 1

Share this post


Link to post

It's not strange, it's normal. don't try to use values bigger than your height or width because that would make a full circle.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.