extra draw gui

add the extra functions for drawing gui elements
RoundedRectangle:

All draw rect functions now pass through roundedRectangle which uses a shader and can draw borders, and rounds the corners

Draw thick line:
draws a line thicker than 1 pixel, uses a geometry shader to do this

Draw Circle:
Draws a circle with a border parameter.
This commit is contained in:
marauder2k7 2024-03-06 17:27:18 +00:00
parent 39c2cc09fc
commit c9d70de609
9 changed files with 654 additions and 170 deletions

View file

@ -149,4 +149,32 @@ singleton ShaderData( CubemapSaveShader )
samplerNames[0] = "$cubemapTex";
pixVersion = 3.0;
};
//-----------------------------------------------------------------------------
// GUI shaders
//-----------------------------------------------------------------------------
singleton ShaderData( RoundedRectangleGUI )
{
DXVertexShaderFile = $Core::CommonShaderPath @ "/fixedFunction/colorV.hlsl";
DXPixelShaderFile = $Core::CommonShaderPath @ "/fixedFunction/roundedRectangleP.hlsl";
pixVersion = 3.0;
};
singleton ShaderData( CircularGUI )
{
DXVertexShaderFile = $Core::CommonShaderPath @ "/fixedFunction/colorV.hlsl";
DXPixelShaderFile = $Core::CommonShaderPath @ "/fixedFunction/circleP.hlsl";
pixVersion = 3.0;
};
singleton ShaderData( ThickLineGUI )
{
DXVertexShaderFile = $Core::CommonShaderPath @ "/fixedFunction/colorV.hlsl";
DXGeometryShaderFile = $Core::CommonShaderPath @ "/fixedFunction/thickLineG.hlsl";
DXPixelShaderFile = $Core::CommonShaderPath @ "/fixedFunction/thickLineP.hlsl";
pixVersion = 3.0;
};

View file

@ -0,0 +1,66 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "../shaderModel.hlsl"
struct Conn
{
float4 HPOS : TORQUE_POSITION;
float4 color : COLOR;
};
uniform float2 sizeUni;
uniform float radius;
uniform float2 rectCenter;
uniform float borderSize;
uniform float4 borderCol;
float circle(float2 p, float radius)
{
float dist = length(p - float2(0.5,0.5));
return 1.0 - smoothstep(radius - (radius*0.01),
radius + (radius*0.01),
dot(dist,dist) * 4.0);
}
float4 main(Conn IN) : TORQUE_TARGET0
{
float distance = length(IN.HPOS.xy - rectCenter);
if(distance > radius)
{
discard;
}
if(distance < radius)
{
if(distance < (radius - borderSize))
{
return IN.color;
}
else
{
return borderCol;
}
}
return IN.color;
}

View file

@ -0,0 +1,109 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "../shaderModel.hlsl"
struct Conn
{
float4 HPOS : TORQUE_POSITION;
float4 color : COLOR;
};
uniform float2 sizeUni;
uniform float2 rectCenter;
uniform float radius;
uniform float borderSize;
uniform float4 borderCol;
float RoundedRectSDF(float2 p, float2 size, float radius)
{
float2 halfSize = size / 2.0;
// Calculate distance to each side of the rectangle
float2 dist = abs(p) - halfSize + radius;
// Compute the distance to the rounded corners
float cornerDist = length(max(abs(p) - (halfSize - float2(radius, radius)), 0.0));
// Return the minimum distance (negative inside, positive outside)
return min(max(dist.x, dist.y), 0.0) + cornerDist - radius;
}
float4 main(Conn IN) : TORQUE_TARGET0
{
float2 p = IN.HPOS.xy;
float2 halfSize = sizeUni * 0.5;
float halfBorder = borderSize * 0.5;
p -= rectCenter;
// Calculate signed distance field for rounded rectangle
float4 fromColor = IN.color;
// alpha
float4 toColor = float4(0.0, 0.0, 0.0, 0.0);
float cornerRadius = radius;
// if ((p.y < 0.0 && p.x < 0.0) || // top left corner
// (p.y < 0.0 && p.x > 0.0) || // top right corner
// (p.y > 0.0 && p.x > 0.0) || // bottom right corner.
// (p.y > 0.0 && p.x < 0.0)) // bottom left corner
// {
// cornerRadius = radius;
// }
if(cornerRadius > 0.0 || halfBorder > 0.0)
{
float sdf = RoundedRectSDF(p, sizeUni, cornerRadius - halfBorder);
clip(0.01 - sdf);
if(halfBorder > 0.0)
{
if(sdf < 0.0)
{
// if ((p.y >= -halfSize.y - radius + halfBorder && p.y <= -halfSize.y + radius - halfBorder) || // top border
// (p.y >= halfSize.y - radius + halfBorder && p.y <= halfSize.y + radius - halfBorder) || // bottom border
// (p.x >= -halfSize.x - radius + halfBorder && p.x <= -halfSize.x + radius - halfBorder) || // left border
// (p.x >= halfSize.x - radius + halfBorder && p.x <= halfSize.x + radius - halfBorder) ) { // right border
// }
toColor = borderCol;
}
sdf = abs(sdf) - halfBorder;
// Apply smoothing to create rounded effect
float blending = smoothstep(1.0, -1.0, sdf);
return lerp(fromColor, toColor, blending);
}
float alpha = smoothstep(1.0, 0.0, sdf);
return float4(IN.color.rgb, IN.color.a * alpha);
}
else
{
return IN.color;
}
}

View file

@ -0,0 +1,77 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "../shaderModel.hlsl"
struct Conn
{
float4 HPOS : TORQUE_POSITION;
float4 color : COLOR;
};
struct PSConn
{
float4 HPOS : TORQUE_POSITION;
float4 color : COLOR;
};
uniform float thickness;
uniform float2 oneOverViewport;
[maxvertexcount(4)]
void main(line Conn lineSegment[2], inout TriangleStream<PSConn> outstream)
{
// Calculate the direction of the line segment
float2 direction = normalize(lineSegment[1].HPOS.xy - lineSegment[0].HPOS.xy);
// Calculate perpendicular direction
float2 perpendicular = normalize(float2(-direction.y, direction.x));
// Calculate offset for thickness
float2 offset = float2(thickness * oneOverViewport.x, thickness * oneOverViewport.y) * perpendicular;
// Calculate vertices for the line with thickness
float2 p0 = lineSegment[0].HPOS.xy + offset;
float2 p1 = lineSegment[0].HPOS.xy - offset;
float2 p2 = lineSegment[1].HPOS.xy + offset;
float2 p3 = lineSegment[1].HPOS.xy - offset;
PSConn output;
output.HPOS = float4(p0, 0.0f, 1.0f);
output.color = lineSegment[0].color;
outstream.Append(output);
output.HPOS = float4(p1, 0.0f, 1.0f);
output.color = lineSegment[0].color;
outstream.Append(output);
output.HPOS = float4(p2, 0.0f, 1.0f);
output.color = lineSegment[1].color;
outstream.Append(output);
output.HPOS = float4(p3, 0.0f, 1.0f);
output.color = lineSegment[1].color;
outstream.Append(output);
outstream.RestartStrip();
}

View file

@ -0,0 +1,34 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "../shaderModel.hlsl"
struct PSConn
{
float4 HPOS : TORQUE_POSITION;
float4 color : COLOR;
};
float4 main(PSConn IN) : TORQUE_TARGET0
{
return IN.color;
}