ScriptCore: Added more properties to components

- Added CircleRenderer
- Transform: Added Rotation, RotationEuler, RotationAxisAngle and Scale
- RigidBody: Added BodyType and GravityScale
- Added ColorRGBA
This commit is contained in:
Simon Lübeß
2024-01-27 19:46:37 +01:00
parent bc535ddc26
commit 8ff35d3967
12 changed files with 566 additions and 22 deletions
+53
View File
@@ -1,3 +1,4 @@
using System.Numerics;
using GlitchyEngine.Math;
namespace GlitchyEngine.Core;
@@ -35,4 +36,56 @@ public class Transform : Component
}
set => ScriptGlue.Transform_SetTranslation(Entity.UUID, in value);
}
/// <summary>
/// Gets or sets the rotation of the entity.
/// </summary>
public Quaternion Rotation
{
get
{
ScriptGlue.Transform_GetRotation(Entity.UUID, out Quaternion rotation);
return rotation;
}
set => ScriptGlue.Transform_SetRotation(Entity.UUID, in value);
}
/// <summary>
/// Gets or sets the rotation of the entity in radians around each axis.
/// </summary>
public float3 RotationEuler
{
get
{
ScriptGlue.Transform_GetRotationEuler(Entity.UUID, out float3 rotationEuler);
return rotationEuler;
}
set => ScriptGlue.Transform_SetRotationEuler(Entity.UUID, in value);
}
/// <summary>
/// Gets or sets the rotation of the entity as an axis of rotation and the angle in radians.
/// </summary>
public RotationAxisAngle RotationAxisAngle
{
get
{
ScriptGlue.Transform_GetRotationAxisAngle(Entity.UUID, out RotationAxisAngle rotationEuler);
return rotationEuler;
}
set => ScriptGlue.Transform_SetRotationAxisAngle(Entity.UUID, value);
}
/// <summary>
/// Gets or sets the scale of the entity.
/// </summary>
public float3 Scale
{
get
{
ScriptGlue.Transform_GetScale(Entity.UUID, out float3 scale);
return scale;
}
set => ScriptGlue.Transform_SetScale(Entity.UUID, in value);
}
}
+51
View File
@@ -0,0 +1,51 @@
using GlitchyEngine.Core;
using GlitchyEngine.Math;
namespace GlitchyEngine.Graphics;
/// <summary>
/// Component that renders a circle.
/// </summary>
public class CircleRenderer : Component
{
// TODO: Texture2D Sprite
/// <summary>
/// Gets or sets the tint color of the circle.
/// </summary>
public ColorRGBA Color
{
get
{
ScriptGlue.CircleRenderer_GetColor(_uuid, out ColorRGBA color);
return color;
}
set => ScriptGlue.CircleRenderer_SetColor(_uuid, value);
}
/// <summary>
/// Gets or sets the UV transform for the texture. XY are an offset, ZW are scaling
/// </summary>
public float4 UvTransform
{
get
{
ScriptGlue.CircleRenderer_GetUvTransform(_uuid, out float4 uvTransform);
return uvTransform;
}
set => ScriptGlue.CircleRenderer_SetUvTransform(_uuid, value);
}
/// <summary>
/// Gets or sets the inner radius of the torus. 0 means a circle is rendered.
/// </summary>
public float InnerRadius
{
get
{
ScriptGlue.CircleRenderer_GetInnerRadius(_uuid, out float innerRadius);
return innerRadius;
}
set => ScriptGlue.CircleRenderer_SetInnerRadius(_uuid, value);
}
}
+65
View File
@@ -0,0 +1,65 @@
using System.Runtime.InteropServices;
namespace GlitchyEngine.Math;
using static GlitchyEngine.Math.Math;
/// <summary>
/// Represents a color with red, blue, green color channels and alpha.
/// <br/>Each channel is represented by a <see cref="float"/>
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct ColorRGBA
{
internal const float SrgbToLin = 2.2f;
internal const float LinToSRGB = (float)(1.0 / 2.2);
/// <summary>
/// The red component of the color.
/// </summary>
public float R;
/// <summary>
/// The green component of the color.
/// </summary>
public float G;
/// <summary>
/// The blue component of the color.
/// </summary>
public float B;
/// <summary>
/// The alpha component of the color.
/// </summary>
public float A;
/// <summary>
/// Creates a new instance of ColorRGBA with all components set to 0.
/// </summary>
public ColorRGBA() { }
/// <summary>
/// Creates a new instance of ColorRGBA with the specified values.
/// </summary>
/// <param name="r">The red component of the color.</param>
/// <param name="g">The green component of the color.</param>
/// <param name="b">The blue component of the color.</param>
/// <param name="a">The alpha component of the color.</param>
public ColorRGBA(float r, float g, float b, float a = 1.0f)
{
R = r;
G = g;
B = b;
A = a;
}
public static explicit operator float4(ColorRGBA color) => new float4(color.R, color.G, color.B, color.A);
public static explicit operator ColorRGBA(float4 vector) => new ColorRGBA(vector.X, vector.Y, vector.Z, vector.W);
// Converts a Color from sRGB color space to Linear color space.
public static ColorRGBA SRgbToLinear(ColorRGBA sRGB) => new ColorRGBA(pow(sRGB.R, SrgbToLin), pow(sRGB.G, SrgbToLin), pow(sRGB.B, SrgbToLin), sRGB.A);
// Converts a Color from linear color space to sRGB color space.
public static ColorRGBA LinearToSRGB(ColorRGBA linear) => new ColorRGBA(pow(linear.R, LinToSRGB), pow(linear.G, LinToSRGB), pow(linear.B, LinToSRGB), linear.A);
}
+30
View File
@@ -0,0 +1,30 @@
using System.Runtime.InteropServices;
namespace GlitchyEngine.Math;
/// <summary>
/// Represents a rotation around an axis by a specific angle.
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct RotationAxisAngle
{
/// <summary>
/// The axis around which the rotation is applied.
/// </summary>
public float3 Axis;
/// <summary>
/// The angle in radians around the <see cref="Axis"/>.
/// </summary>
public float Angle;
/// <summary>
/// Creates a new instance of an axis angle rotation.
/// </summary>
/// <param name="axis">The axis.</param>
/// <param name="angle">The angle in radians.</param>
public RotationAxisAngle(float3 axis, float angle)
{
Axis = axis;
Angle = angle;
}
}
+32
View File
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace GlitchyEngine.Physics;
/// <summary>
/// Describes whether the rigid body is static, dynamic or kinematic.
/// </summary>
public enum BodyType : byte
{
/// <summary>
/// A static body does not move under simulation and behaves as if it has infinite mass.
/// Internally, Box2D stores zero for the mass and the inverse mass
/// Static bodies can be moved manually by the user. A static body has zero velocity.
/// Static bodies do not collide with other static or kinematic bodies.
/// </summary>
Static = 0,
/// <summary>
/// A dynamic body is fully simulated. They can be moved manually by the user, but normally they move according to forces.
/// A dynamic body can collide with all body types. A dynamic body always has finite, non-zero mass.
/// If you try to set the mass of a dynamic body to zero, it will automatically acquire a mass of one kilogram and it won't rotate.
/// </summary>
Dynamic = 1,
/// <summary>
/// A kinematic body moves under simulation according to its velocity.
/// Kinematic bodies do not respond to forces. They can be moved manually by the user, but normally a kinematic body is moved by setting its velocity.
/// A kinematic body behaves as if it has infinite mass, however, Box2D stores zero for the mass and the inverse mass.
/// Kinematic bodies do not collide with other kinematic or static bodies.
/// </summary>
Kinematic = 2
}
+30
View File
@@ -101,4 +101,34 @@ public class Rigidbody2D : Component
}
set => ScriptGlue.Rigidbody2D_SetFixedRotation(_uuid, value);
}
/// <summary>
/// Gets or sets the body type of the rigidbody.
/// </summary>
public BodyType BodyType
{
get
{
ScriptGlue.Rigidbody2D_GetBodyType(_uuid, out BodyType bodyType);
return bodyType;
}
set => ScriptGlue.Rigidbody2D_SetBodyType(_uuid, value);
}
/// <summary>
/// Gets or sets the gravity scale of this rigidbody.
/// E.g. a value of 0.0 means, that the rigidbody is not affected by gravity and a value of -1.0 means, that it gravity is inverted.
/// </summary>
public float GravityScale
{
get
{
ScriptGlue.Rigidbody2D_GetGravityScale(_uuid, out float gravityScale);
return gravityScale;
}
set => ScriptGlue.Rigidbody2D_SetGravityScale(_uuid, value);
}
}
+60
View File
@@ -1,7 +1,9 @@
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using GlitchyEngine.Core;
using GlitchyEngine.Math;
using GlitchyEngine.Physics;
using GlitchyEngine.Serialization;
namespace GlitchyEngine;
@@ -78,6 +80,30 @@ internal static class ScriptGlue
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Transform_SetTranslation(UUID entityId, in float3 translation);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Transform_GetRotation(UUID entityId, out Quaternion rotation);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Transform_SetRotation(UUID entityId, in Quaternion rotation);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Transform_GetRotationEuler(UUID entityId, out float3 rotationEuler);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Transform_SetRotationEuler(UUID entityId, in float3 rotationEuler);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Transform_GetRotationAxisAngle(UUID entityId, out RotationAxisAngle translation);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Transform_SetRotationAxisAngle(UUID entityId, RotationAxisAngle translation);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Transform_GetScale(UUID entityId, out float3 scale);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Transform_SetScale(UUID entityId, in float3 scale);
#endregion TransformComponent
@@ -113,12 +139,24 @@ internal static class ScriptGlue
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Rigidbody2D_GetAngularVelocity(UUID entityId, out float velocity);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Rigidbody2D_GetBodyType(UUID entityId, out BodyType bodyType);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Rigidbody2D_SetBodyType(UUID entityId, in BodyType bodyType);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Rigidbody2D_IsFixedRotation(UUID entityId, out bool isFixedRotation);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Rigidbody2D_SetFixedRotation(UUID entityId, in bool isFixedRotation);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Rigidbody2D_GetGravityScale(UUID entityId, out float gravityScale);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Rigidbody2D_SetGravityScale(UUID entityId, in float gravityScale);
#endregion RigidBody2D
#region Camera
@@ -182,6 +220,28 @@ internal static class ScriptGlue
#endregion Physics2D
#region CircleRenderer
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void CircleRenderer_GetColor(UUID entityId, out ColorRGBA color);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void CircleRenderer_SetColor(UUID entityId, ColorRGBA color);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void CircleRenderer_GetUvTransform(UUID entityId, out float4 uvTransform);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void CircleRenderer_SetUvTransform(UUID entityId, float4 uvTransform);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void CircleRenderer_GetInnerRadius(UUID entityId, out float innerRadius);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void CircleRenderer_SetInnerRadius(UUID entityId, float innerRadius);
#endregion
#region Math
[MethodImpl(MethodImplOptions.InternalCall)]