mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Subtextures
This commit is contained in:
@@ -618,9 +618,27 @@ namespace GlitchyEngine.Renderer
|
||||
s_circleInstanceQueue.Clear();
|
||||
}
|
||||
|
||||
/// A specialized function that calculates the 2D transform matrix
|
||||
private static Matrix Calculate2DTransform(Vector3 translation, Vector2 scale, float rotation)
|
||||
{
|
||||
float sin = 0.0f;
|
||||
float cos = 1.0f;
|
||||
|
||||
if (rotation != 0.0f)
|
||||
{
|
||||
sin = Math.Sin(rotation);
|
||||
cos = Math.Cos(rotation);
|
||||
}
|
||||
|
||||
return .(cos * scale.X, -sin * scale.Y, 0, translation.X,
|
||||
sin * scale.X, cos * scale.Y, 0, translation.Y,
|
||||
0 , 0 , 1, translation.Z,
|
||||
0 , 0 , 0, 1);
|
||||
}
|
||||
|
||||
// Primitives
|
||||
|
||||
// Quad
|
||||
// Colored Quad
|
||||
|
||||
public static void DrawQuad(Vector2 position, Vector2 size, float rotation, ColorRGBA color)
|
||||
{
|
||||
@@ -648,33 +666,64 @@ namespace GlitchyEngine.Renderer
|
||||
DrawQuad(transform, s_whiteTexture, color);
|
||||
}
|
||||
|
||||
// Quad Subtexture
|
||||
|
||||
[Inline]
|
||||
private static Vector4 CalculateSubTexcoords(Vector4 texCoords, Vector4 innerTexcoords)
|
||||
{
|
||||
Vector4 uv = texCoords;
|
||||
uv.XY += innerTexcoords.XY * uv.ZW;
|
||||
uv.ZW *= innerTexcoords.ZW;
|
||||
|
||||
return uv;
|
||||
}
|
||||
|
||||
// Subtex only
|
||||
|
||||
public static void DrawQuad(Vector2 position, Vector2 size, float rotation, SubTexture2D texture, ColorRGBA color = .White)
|
||||
{
|
||||
DrawQuad(Vector3(position, 0.0f), size, rotation, texture.Texture, .White, texture.TexCoords);
|
||||
}
|
||||
|
||||
public static void DrawQuad(Vector3 position, Vector2 size, float rotation, SubTexture2D texture, ColorRGBA color = .White)
|
||||
{
|
||||
DrawQuad(position, size, rotation, texture.Texture, .White, texture.TexCoords);
|
||||
}
|
||||
|
||||
public static void DrawQuad(Matrix transform, SubTexture2D texture, ColorRGBA color = .White)
|
||||
{
|
||||
DrawQuad(transform, texture.Texture, .White, texture.TexCoords);
|
||||
}
|
||||
|
||||
// Subtex + Texcoords
|
||||
|
||||
public static void DrawQuad(Vector2 position, Vector2 size, float rotation, SubTexture2D subtexture, ColorRGBA color = .White, Vector4 uvTransform = .(0, 0, 1, 1))
|
||||
{
|
||||
Vector4 uv = CalculateSubTexcoords(subtexture.TexCoords, uvTransform);
|
||||
|
||||
DrawQuad(Vector3(position, 0.0f), size, rotation, subtexture.Texture, .White, uv);
|
||||
}
|
||||
|
||||
public static void DrawQuad(Vector3 position, Vector2 size, float rotation, SubTexture2D subtexture, ColorRGBA color = .White, Vector4 uvTransform = .(0, 0, 1, 1))
|
||||
{
|
||||
Vector4 uv = CalculateSubTexcoords(subtexture.TexCoords, uvTransform);
|
||||
|
||||
DrawQuad(position, size, rotation, subtexture.Texture, .White, uv);
|
||||
}
|
||||
|
||||
public static void DrawQuad(Matrix transform, SubTexture2D subtexture, ColorRGBA color = .White, Vector4 uvTransform = .(0, 0, 1, 1))
|
||||
{
|
||||
Vector4 uv = CalculateSubTexcoords(subtexture.TexCoords, uvTransform);
|
||||
|
||||
DrawQuad(transform, subtexture.Texture, .White, uv);
|
||||
}
|
||||
|
||||
// Textured Quad
|
||||
|
||||
public static void DrawQuad(Vector2 position, Vector2 size, float rotation, Texture2D texture, ColorRGBA color = .White, Vector4 uvTransform = .(0, 0, 1, 1))
|
||||
{
|
||||
DrawQuad(Vector3(position, 0.0f), size, rotation, texture, color, uvTransform);
|
||||
}
|
||||
|
||||
public static void DrawQuadPivotCorner(Vector2 position, Vector2 size, float rotation, Texture2D texture, ColorRGBA color = .White, Vector4 uvTransform = .(0, 0, 1, 1))
|
||||
{
|
||||
DrawQuadPivotCorner(Vector3(position, 0.0f), size, rotation, texture, color, uvTransform);
|
||||
}
|
||||
|
||||
/// A specialized function that calculates the 2D transform matrix
|
||||
private static Matrix Calculate2DTransform(Vector3 translation, Vector2 scale, float rotation)
|
||||
{
|
||||
float sin = 0.0f;
|
||||
float cos = 1.0f;
|
||||
|
||||
if (rotation != 0.0f)
|
||||
{
|
||||
sin = Math.Sin(rotation);
|
||||
cos = Math.Cos(rotation);
|
||||
}
|
||||
|
||||
return .(cos * scale.X, -sin * scale.Y, 0, translation.X,
|
||||
sin * scale.X, cos * scale.Y, 0, translation.Y,
|
||||
0 , 0 , 1, translation.Z,
|
||||
0 , 0 , 0, 1);
|
||||
}
|
||||
|
||||
public static void DrawQuad(Vector3 position, Vector2 size, float rotation, Texture2D texture, ColorRGBA color = .White, Vector4 uvTransform = .(0, 0, 1, 1))
|
||||
{
|
||||
@@ -699,6 +748,13 @@ namespace GlitchyEngine.Renderer
|
||||
}
|
||||
}
|
||||
|
||||
// Textured quad pivot
|
||||
|
||||
public static void DrawQuadPivotCorner(Vector2 position, Vector2 size, float rotation, Texture2D texture, ColorRGBA color = .White, Vector4 uvTransform = .(0, 0, 1, 1))
|
||||
{
|
||||
DrawQuadPivotCorner(Vector3(position, 0.0f), size, rotation, texture, color, uvTransform);
|
||||
}
|
||||
|
||||
public static void DrawQuadPivotCorner(Vector3 position, Vector2 size, float rotation, Texture2D texture, ColorRGBA color = .White, Vector4 uvTransform = .(0, 0, 1, 1))
|
||||
{
|
||||
DrawQuad(position + Vector3(size.X / 2, size.Y / -2, 0), size, rotation, texture, color, uvTransform);
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
using GlitchyEngine.Core;
|
||||
using GlitchyEngine.Math;
|
||||
|
||||
namespace GlitchyEngine.Renderer
|
||||
{
|
||||
class SubTexture2D : RefCounter
|
||||
{
|
||||
protected Texture2D _texture ~ _.ReleaseRef();
|
||||
protected Vector4 _texCoords;
|
||||
|
||||
public Texture2D Texture => _texture;
|
||||
public Vector4 TexCoords => _texCoords;
|
||||
|
||||
public this(Texture2D texture) : this(_texture, .(0, 0, 1, 1)) { }
|
||||
|
||||
public this(Texture2D texture, Int2 topLeft, Int2 size) :
|
||||
this(texture,
|
||||
{
|
||||
Vector2 texSize = Vector2(texture.Width, texture.Height);
|
||||
Vector4 texCoords = Vector4((Vector2)topLeft, (Vector2)size) / texSize.XYXY;
|
||||
texCoords
|
||||
}) { }
|
||||
|
||||
public this(Texture2D texture, Vector2 topLeft, Vector2 size) : this(texture, Vector4(topLeft, size)) { }
|
||||
|
||||
public this(Texture2D texture, Vector4 texCoords)
|
||||
{
|
||||
_texture = texture..AddRef();
|
||||
_texCoords = texCoords;
|
||||
}
|
||||
|
||||
public static SubTexture2D CreateFromGrid(Texture2D texture, Vector2 coords, Vector2 gridSize, Vector2 spriteSize = .One)
|
||||
{
|
||||
Vector2 textureSize = .(texture.Width, texture.Height);
|
||||
|
||||
Vector4 uv = .(coords * gridSize, gridSize * spriteSize) / textureSize.XYXY;
|
||||
|
||||
return new SubTexture2D(texture, uv);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user