Fixed circle rendering and added 2D circle collider

This commit is contained in:
Simon Lübeß
2022-11-26 12:17:31 +01:00
parent 0155a38ae6
commit 8f22cbb8bc
7 changed files with 240 additions and 17 deletions
+64 -3
View File
@@ -18,7 +18,8 @@
Y = 0,
Z = 1,
W = 1
}
},
IsCircle = false
},
TransformComponent = {
Position = {
@@ -79,7 +80,8 @@
Y = 0,
Z = 1,
W = 1
}
},
IsCircle = false
},
TransformComponent = {
Position = {
@@ -160,9 +162,68 @@
OrthographicHeight = 10,
OrthographicNearPlane = 0,
OrthographicFarPlane = 10,
AspectRatio = 2.864979,
AspectRatio = 2.01173,
FixedAspectRatio = false
}
},
{
Id = 15710354273720487680,
NameComponent = {
Name = "Circle"
},
SpriterRendererComponent = {
Color = {
R = 1,
G = 0,
B = 0,
A = 1
},
UvTransform = {
X = 0,
Y = 0,
Z = 1,
W = 1
},
IsCircle = true
},
TransformComponent = {
Position = {
X = -0.121203,
Y = 0.66016,
Z = 0
},
Rotation = {
X = 0,
Y = 0,
Z = 0,
W = 1
},
Scale = {
X = 1,
Y = 1,
Z = 1
},
EditorEulerRotation = {
X = 0,
Y = 0,
Z = 0
}
},
Rigidbody2D = {
BodyType = .Dynamic,
FixedRotation = false
},
CircleCollider2D = {
Offset = {
X = 0,
Y = 0
},
Radius = 0.5,
Density = 1,
Friction = 0.5,
Restitution = 0,
RestitutionThreshold = 0.5
}
}
]
}
+30 -4
View File
@@ -1,3 +1,5 @@
#define EDITOR
Texture2D Texture : register(t0);
SamplerState Sampler : register(s0);
@@ -14,6 +16,9 @@ struct VS_Input
float4 Color : COLOR;
float4 UVTransform : TEXCOORD1;
float InnerRadius : TEXCOORD2;
#ifdef EDITOR
uint EntityId : ENTITYID;
#endif
};
struct PS_Input
@@ -22,6 +27,9 @@ struct PS_Input
float2 RawPos : TEXCOORD0;
float2 Texcoord : TEXCOORD1;
float4 Color : COLOR;
#ifdef EDITOR
nointerpolation uint EntityId : ENTITYID;
#endif
float InnerRadius : TEXCOORD2;
};
@@ -35,11 +43,25 @@ PS_Input VS(VS_Input input)
output.Color = input.Color;
output.InnerRadius = input.InnerRadius;
#ifdef EDITOR
output.EntityId = input.EntityId;
#endif
return output;
}
float4 PS(PS_Input input) : SV_Target0
struct PS_Output
{
float4 Color : SV_Target0;
#ifdef EDITOR
uint EntityId : SV_TARGET1;
#endif
};
PS_Output PS(PS_Input input)
{
PS_Output output;
float2 uv = input.RawPos * 2;
float distance = 1.0f - length(uv);
@@ -53,10 +75,14 @@ float4 PS(PS_Input input) : SV_Target0
// Discard invisible pixels
clip(amount - 0.5f);
float4 color = Texture.Sample(Sampler, input.Texcoord) * input.Color;
color.a *= amount;
output.Color = Texture.Sample(Sampler, input.Texcoord) * input.Color;
output.Color.a *= amount;
return color;
#ifdef EDITOR
output.EntityId = input.EntityId;
#endif
return output;
}
#pragma Effect[VS=VS; PS=PS]
@@ -66,6 +66,7 @@ namespace GlitchyEditor.EditWindows
ShowComponentEditor<MeshComponent>("Mesh", entity, => ShowMeshComponentEditor, => ShowComponentContextMenu<MeshComponent>);
ShowComponentEditor<Rigidbody2DComponent>("Rigidbody 2D", entity, => ShowRigidBody2DComponentEditor, => ShowComponentContextMenu<Rigidbody2DComponent>);
ShowComponentEditor<BoxCollider2DComponent>("Box collider 2D", entity, => ShowBoxCollider2DComponentEditor, => ShowComponentContextMenu<BoxCollider2DComponent>);
ShowComponentEditor<CircleCollider2DComponent>("Circle collider 2D", entity, => ShowCircleCollider2DComponentEditor, => ShowComponentContextMenu<CircleCollider2DComponent>);
ShowAddComponentButton(entity);
}
@@ -278,6 +279,8 @@ namespace GlitchyEditor.EditWindows
ImGui.EditVector<4>("UV Transform", ref *(float[4]*)&spriteRendererComponent.UvTransform);
ImGui.Checkbox("Is Circle", &spriteRendererComponent.IsCircle);
}
private static void ShowMeshRendererComponentEditor(Entity entity, MeshRendererComponent* meshRendererComponent)
@@ -471,6 +474,37 @@ namespace GlitchyEditor.EditWindows
boxCollider.RestitutionThreshold = restitutionThreshold;
}
private static void ShowCircleCollider2DComponentEditor(Entity entity, CircleCollider2DComponent* circleCollider)
{
float textWidth = ImGui.CalcTextSize("Offset".CStr()).x;
textWidth += ImGui.GetStyle().FramePadding.x * 3.0f;
Vector2 offset = circleCollider.Offset;
if (ImGui.EditVector2("Offset", ref offset, .Zero, 0.1f, textWidth))
circleCollider.Offset = offset;
float radius = circleCollider.Radius;
if (ImGui.DragFloat("Radius", &radius, 0.0f, 0.1f, textWidth))
circleCollider.Radius = radius;
float density = circleCollider.Density;
if (ImGui.DragFloat("Density", &density, 0.0f, 0.1f, textWidth))
circleCollider.Density = density;
float friction = circleCollider.Friction;
if (ImGui.DragFloat("Friction", &friction, 0.0f, 0.1f, textWidth))
circleCollider.Friction = friction;
float restitution = circleCollider.Restitution;
if (ImGui.DragFloat("Restitution", &restitution, 0.0f, 0.1f, textWidth))
circleCollider.Restitution = restitution;
float restitutionThreshold = circleCollider.RestitutionThreshold;
if (ImGui.DragFloat("RestitutionThreshold", &restitutionThreshold, 0.0f, 0.1f, textWidth))
circleCollider.RestitutionThreshold = restitutionThreshold;
}
private static void LabelColumn(StringView label)
{
ImGui.TextUnformatted(label);
@@ -613,6 +647,7 @@ namespace GlitchyEditor.EditWindows
ShowComponentButton<LightComponent>("Light");
ShowComponentButton<Rigidbody2DComponent>("Rigidbody 2D");
ShowComponentButton<BoxCollider2DComponent>("Box collider 2D");
ShowComponentButton<CircleCollider2DComponent>("Circle collider 2D");
ImGui.EndCombo();
}