Added circle rendering to Renderer2D

This commit is contained in:
Simon Lübeß
2021-10-14 23:01:08 +02:00
parent 84df8c111b
commit dcb8424c2d
3 changed files with 301 additions and 34 deletions
+59
View File
@@ -0,0 +1,59 @@
Texture2D Texture : register(t0);
SamplerState Sampler : register(s0);
cbuffer Constants : register(b0)
{
float4x4 ViewProjection;
};
struct VS_Input
{
float2 Position : POSITION;
float2 Texcoord : TEXCOORD0;
float4x4 Transform : TRANSFORM;
float4 Color : COLOR;
float4 UVTransform : TEXCOORD1;
float InnerRadius : TEXCOORD2;
};
struct PS_Input
{
float4 Position : SV_Position;
float2 RawPos : TEXCOORD0;
float2 Texcoord : TEXCOORD1;
float4 Color : COLOR;
float InnerRadius : TEXCOORD2;
};
PS_Input VS(VS_Input input)
{
PS_Input output;
output.Position = mul(ViewProjection, mul(input.Transform, float4(input.Position, 0.0f, 1.0f)));
output.Texcoord = input.UVTransform.xy + input.UVTransform.zw * input.Texcoord;
output.RawPos = input.Position;
output.Color = input.Color;
output.InnerRadius = input.InnerRadius;
return output;
}
float4 PS(PS_Input input) : SV_Target0
{
float2 uv = input.RawPos * 2;
float distance = 1.0f - length(uv);
// Smoothing edges based on derivative (not sure if RawPos is the best value for this)
float f = fwidth(input.RawPos.x) + fwidth(input.RawPos.y);
float amount = smoothstep(0.0f, f, distance);
amount *= smoothstep(input.InnerRadius + f, input.InnerRadius, distance);
float4 color = Texture.Sample(Sampler, input.Texcoord) * input.Color;
color.a *= amount;
return color;
}
#effect[VS=VS, PS=PS]
+8
View File
@@ -108,11 +108,17 @@ namespace Sandbox
Renderer2D.DrawQuad(Vector3(0, 0, 1), Vector2(10), 0, _checkerTexture, .White, .(0, 0, 1, 1));
Renderer2D.DrawCircle(Vector3(0, 0, 0f), Vector2(5), _checkerTexture, .LightBlue, innerRadius, .(0, 0, 1, 1));
Renderer2D.DrawCircle(Vector3(-2, -2, -2f), Vector2(1), .GreenYellow);
FontRenderer.DrawText(fonty, "Hallo! gjy", 0, 0, 64, .White, .White);
Renderer2D.EndScene();
}
float innerRadius = 0.5f;
public override void OnEvent(Event event)
{
EventDispatcher dispatcher = EventDispatcher(event);
@@ -128,6 +134,8 @@ namespace Sandbox
{
ImGui.Begin("Test");
ImGui.DragFloat("Inner Radius", &innerRadius, 0.01f);
ImGui.ColorPicker4("Color", *(float[4]*)&_squareColor0);
_squareColor1 = ColorRGBA.White - _squareColor0;