Jump to content

x[ مساعدة ]x فاكشن dxDrawCircle


iiv03

Recommended Posts

2 hours ago, N3xT said:

مشكلتك بالشادر نفسه, دور لك على شادر أفضل

وعدل شوي على اكوادك وبيزبط معك

عدلت الآكواد الحين وكل شوي ارور وصلحته مشآكل بس الحين مزبط لي anti alias

ذي محاولتي

float sCircleHeightInPixel = 100;
float sCircleWidthInPixel = 100;
float sBorderWidthInPixel = 10;
float sAngleStart = -3.14;
float sAngleEnd = 3.14;
float4x4 gWorldViewProjection : WORLDVIEWPROJECTION;
struct VSInput
{
    float3 Position : POSITION0;
    float4 Diffuse : COLOR0;
    float2 TexCoord : TEXCOORD0;
};
struct PSInput
{
    float4 Position : POSITION0;
    float4 Diffuse : COLOR0;
    float4 Diffuse2 : COLOR1;
    float2 TexCoord : TEXCOORD0;
    float2 Settings : TEXCOORD1;
};


float4 PixelShaderFunction(float4 Diffuse : COLOR0, float2 TexCoord : TEXCOORD0) : COLOR0
{
    float2 uv = float2( TexCoord.x, TexCoord.y ) - float2( 0.5, 0.5 );

    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;
		
}

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

PSInput VertexShaderFunction(VSInput VS)
{
    PSInput PS = (PSInput)0;
    PS.Position = mul(float4(VS.Position, 1), gWorldViewProjection);

    int bAntialias = floor(VS.Diffuse.a*255+0.5)%2;
    int bTwoColors = floor(VS.Diffuse.a*127+0.5)%2;
    [branch] if(bTwoColors == 1){
        float aComp = floor(VS.Diffuse.a*63+0.5)%64;;
        PS.Diffuse = float4((floor(VS.Diffuse.rgb*15+0.5)%16)/15, (aComp%8)/7);
        PS.Diffuse2 = float4(floor((VS.Diffuse.rgb*255+0.5)%16)/15, ((aComp/8)%8)/7);
    } else {
        PS.Diffuse = float4(VS.Diffuse.rgb, floor(VS.Diffuse.a*63+0.5)%64/63);
        PS.Diffuse2 = PS.Diffuse;
    }

    PS.TexCoord = VS.TexCoord;
    PS.Settings = float2(bTwoColors,bAntialias);

    return PS;
}


float4 PixelShaderFunction(PSInput PS) : COLOR0
{
    const float dist = length(PS.TexCoord*2-1);
    float4 color = PS.Settings.x > 0.5 ? lerp(PS.Diffuse, PS.Diffuse2, dist) : PS.Diffuse;

    if (PS.Settings.y < 0.5){
        return dist <= 1 ? color : float4(0,0,0,0);
    }

    return float4(color.rgb, smoothstep(1, 1 - fwidth(dist) * 2, dist)*color.a);
}
technique Technique1
{
    pass Pass1
    {
        SrcBlend = SrcAlpha;
        DestBlend = InvSrcAlpha;
        VertexShader = compile vs_3_0 VertexShaderFunction();
    }
}

local floor = math.floor
function fromColor(col)
	local r,g,b,a
	b = col%256
	g = floor(col/0x100)%256
	r = floor(col/0x10000)%256
	a = floor(col/0x1000000)%256
	return r,g,b,a
end

function dxDrawShaderCircle( x, y, width, height, color1, color2, angleStart, angleSweep, borderWidth, antialiasing)
local shaderColCache = {}
    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 
	assert(type(color1) == "number", "Expected number as color argument to dxDrawCircleShader")
	if (not color2) or (color2 == color1) then color2 = "def" end

	local finColor
	local col1 = shaderColCache[color1]
	if col1 then
		local col2 = col1[color2]
		if col2 then
			finColor = col2
		end
	end

	if not finColor then
		shaderColCache[color1] = shaderColCache[color1] or {}
		local twoColors = (color2 ~= "def")
		if twoColors then
			local r1,g1,b1,a1 = fromColor(color1)
			local r2,g2,b2,a2 = fromColor(color2)
			local finR = floor((r1/256)*16)*16 + floor((r2/256)*16)
			local finG = floor((g1/256)*16)*16 + floor((g2/256)*16)
			local finB = floor((b1/256)*16)*16 + floor((b2/256)*16)
			local finA = floor((a1/256)*8)*32 + floor((a2/256)*8)*4 + 2
			finColor = tocolor( finR, finG, finB, finA )
		else
			local r,g,b,a = fromColor(color1)
			local finA = (floor((a/256)*64)*4)%256
			finColor = tocolor( r,g,b, finA )
		end
		shaderColCache[color1][color2] = finColor
	end

	local antialiasComp = 16777216
	if not antialias then antialiasComp = 0 end
	finColor = finColor + antialiasComp
    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 ) 
end 


addEventHandler("onClientRender",root,
	function()
	dxDrawShaderCircle(500,400,100,height,tocolor(0,0,255),tocolor(0,0,255),0,360,3,1) 
	end
)

ديبوق مطلع ولا ارور والمود شغال

anti alias مآزبط

Edited by liwahadri
Link to comment

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...