1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-12-24 17:29:23 +01:00

made texture atlases have an underlying texture region as well

This commit is contained in:
Ell 2020-12-19 14:34:30 +01:00
parent 1f40129ad3
commit 9895be2250
2 changed files with 63 additions and 12 deletions

View file

@ -72,7 +72,8 @@ namespace MLEM.Textures {
/// Creates a new texture region that spans the entire texture
/// </summary>
/// <param name="texture">The texture to use</param>
public TextureRegion(Texture2D texture) : this(texture, new Rectangle(0, 0, texture.Width, texture.Height)) {
public TextureRegion(Texture2D texture) :
this(texture, new Rectangle(0, 0, texture.Width, texture.Height)) {
}
/// <summary>
@ -83,7 +84,8 @@ namespace MLEM.Textures {
/// <param name="v">The y coordinate of the top left corner of this area</param>
/// <param name="width">The width of this area</param>
/// <param name="height">The height of this area</param>
public TextureRegion(Texture2D texture, int u, int v, int width, int height) : this(texture, new Rectangle(u, v, width, height)) {
public TextureRegion(Texture2D texture, int u, int v, int width, int height) :
this(texture, new Rectangle(u, v, width, height)) {
}
/// <summary>
@ -92,7 +94,39 @@ namespace MLEM.Textures {
/// <param name="texture">The texture to use</param>
/// <param name="uv">The top left corner of this area</param>
/// <param name="size">The size of this area</param>
public TextureRegion(Texture2D texture, Point uv, Point size) : this(texture, new Rectangle(uv, size)) {
public TextureRegion(Texture2D texture, Point uv, Point size) :
this(texture, new Rectangle(uv, size)) {
}
/// <summary>
/// Creates a new texture region which is a sub-region of the given texture region
/// </summary>
/// <param name="region">The texture region to create a sub-region of</param>
/// <param name="area">The new texture region area</param>
public TextureRegion(TextureRegion region, Rectangle area) :
this(region, area.Location, area.Size) {
}
/// <summary>
/// Creates a new texture region which is a sub-region of the given texture region
/// </summary>
/// <param name="region">The texture region to create a sub-region of</param>
/// <param name="u">The x coordinate of the top left corner of this area</param>
/// <param name="v">The y coordinate of the top left corner of this area</param>
/// <param name="width">The width of this area</param>
/// <param name="height">The height of this area</param>
public TextureRegion(TextureRegion region, int u, int v, int width, int height) :
this(region, new Point(u, v), new Point(width, height)) {
}
/// <summary>
/// Creates a new texture region which is a sub-region of the given texture region
/// </summary>
/// <param name="region">The texture region to create a sub-region of</param>
/// <param name="uv">The top left corner of this area</param>
/// <param name="size">The size of this area</param>
public TextureRegion(TextureRegion region, Point uv, Point size) :
this(region.Texture, region.Position + uv, size) {
}
}

View file

@ -12,9 +12,10 @@ namespace MLEM.Textures {
public class UniformTextureAtlas : GenericDataHolder {
/// <summary>
/// The texture to use for this atlas
/// The <see cref="TextureRegion"/> that this uniform texture atlas uses as its basis.
/// In most cases, <see cref="Region"/> has the full area of the underlying <see cref="Texture"/>.
/// </summary>
public readonly Texture2D Texture;
public readonly TextureRegion Region;
/// <summary>
/// The amount of sub-regions this atlas has in the x direction
/// </summary>
@ -32,6 +33,11 @@ namespace MLEM.Textures {
/// </summary>
public readonly int RegionHeight;
/// <summary>
/// The texture to use for this atlas.
/// Note that <see cref="Region"/> stores the actual area that we depend on.
/// </summary>
public Texture2D Texture => this.Region.Texture;
/// <summary>
/// Returns the <see cref="TextureRegion"/> at this texture atlas's given index.
/// The index is zero-based, where rows come first and columns come second.
/// </summary>
@ -55,24 +61,35 @@ namespace MLEM.Textures {
private readonly Dictionary<Rectangle, TextureRegion> regions = new Dictionary<Rectangle, TextureRegion>();
/// <summary>
/// Creates a new uniform texture atlas with the given texture region and region amount.
/// This atlas will only ever pull information from the given <see cref="TextureRegion"/> and never exit the region's bounds.
/// </summary>
/// <param name="region">The texture region to use for this atlas</param>
/// <param name="regionAmountX">The amount of texture regions in the x direction</param>
/// <param name="regionAmountY">The amount of texture regions in the y direction</param>
public UniformTextureAtlas(TextureRegion region, int regionAmountX, int regionAmountY) {
this.Region = region;
this.RegionAmountX = regionAmountX;
this.RegionAmountY = regionAmountY;
this.RegionWidth = region.Width / regionAmountX;
this.RegionHeight = region.Height / regionAmountY;
}
/// <summary>
/// Creates a new uniform texture atlas with the given texture and region amount.
/// </summary>
/// <param name="texture">The texture to use for this atlas</param>
/// <param name="regionAmountX">The amount of texture regions in the x direction</param>
/// <param name="regionAmountY">The amount of texture regions in the y direction</param>
public UniformTextureAtlas(Texture2D texture, int regionAmountX, int regionAmountY) {
this.Texture = texture;
this.RegionAmountX = regionAmountX;
this.RegionAmountY = regionAmountY;
this.RegionWidth = texture.Width / regionAmountX;
this.RegionHeight = texture.Height / regionAmountY;
public UniformTextureAtlas(Texture2D texture, int regionAmountX, int regionAmountY) :
this(new TextureRegion(texture), regionAmountX, regionAmountY) {
}
private TextureRegion GetOrAddRegion(Rectangle rect) {
if (this.regions.TryGetValue(rect, out var region))
return region;
region = new TextureRegion(this.Texture,
region = new TextureRegion(this.Region,
rect.X * this.RegionWidth, rect.Y * this.RegionHeight,
rect.Width * this.RegionWidth, rect.Height * this.RegionHeight);
this.regions.Add(rect, region);