From 96cd6709985b89bd731f904c3fdedc781efb09df Mon Sep 17 00:00:00 2001 From: Tobias Knerr Date: Thu, 14 Nov 2024 16:56:08 +0100 Subject: [PATCH] Offer easy access to a texture's aspect ratio --- .../core/target/common/material/BlankTexture.java | 8 +++++++- .../core/target/common/material/TextureAtlas.java | 13 +++++++++++-- .../core/target/common/material/TextureData.java | 5 +++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/osm2world/core/target/common/material/BlankTexture.java b/src/main/java/org/osm2world/core/target/common/material/BlankTexture.java index a1fc02f51..d7dc64a36 100644 --- a/src/main/java/org/osm2world/core/target/common/material/BlankTexture.java +++ b/src/main/java/org/osm2world/core/target/common/material/BlankTexture.java @@ -9,6 +9,7 @@ public final class BlankTexture extends RuntimeTexture { public static final BlankTexture INSTANCE = new BlankTexture(); + private static final Resolution RESOLUTION = new Resolution(128, 128); private BlankTexture(TextureDataDimensions dimensions) { super(dimensions, Wrap.REPEAT, NamedTexCoordFunction.GLOBAL_X_Z); @@ -25,7 +26,12 @@ protected BufferedImage createBufferedImage(Resolution resolution) { @Override protected BufferedImage createBufferedImage() { - return getBufferedImage(new Resolution(128, 128)); + return getBufferedImage(RESOLUTION); + } + + @Override + public float getAspectRatio() { + return RESOLUTION.getAspectRatio(); } @Override diff --git a/src/main/java/org/osm2world/core/target/common/material/TextureAtlas.java b/src/main/java/org/osm2world/core/target/common/material/TextureAtlas.java index 4eaf34696..860263be8 100644 --- a/src/main/java/org/osm2world/core/target/common/material/TextureAtlas.java +++ b/src/main/java/org/osm2world/core/target/common/material/TextureAtlas.java @@ -51,8 +51,7 @@ public TextureAtlas(List textures) { @Override protected BufferedImage createBufferedImage() { - BufferedImage result = new BufferedImage(numTexturesX * TEXTURE_RESOLUTION.width, - numTexturesZ * TEXTURE_RESOLUTION.height, BufferedImage.TYPE_INT_ARGB); + BufferedImage result = new BufferedImage(getResolution().width, getResolution().height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = result.createGraphics(); @@ -98,6 +97,16 @@ VectorXZ mapTexCoord(TextureData texture, VectorXZ texCoord) { } + private Resolution getResolution() { + return new Resolution(numTexturesX * TEXTURE_RESOLUTION.width, + numTexturesZ * TEXTURE_RESOLUTION.height); + } + + @Override + public float getAspectRatio() { + return getResolution().getAspectRatio(); + } + @Override public String toString() { return "TextureAtlas " + textures; diff --git a/src/main/java/org/osm2world/core/target/common/material/TextureData.java b/src/main/java/org/osm2world/core/target/common/material/TextureData.java index 599306078..f71351525 100644 --- a/src/main/java/org/osm2world/core/target/common/material/TextureData.java +++ b/src/main/java/org/osm2world/core/target/common/material/TextureData.java @@ -176,6 +176,11 @@ public void writeRasterImageToStream(OutputStream stream) throws IOException { writeRasterImageToStream(stream, 0.75f); } + /** returns this texture's aspect ratio (same definition as {@link Resolution#getAspectRatio()}) */ + public float getAspectRatio() { + return Resolution.of(getBufferedImage()).getAspectRatio(); + } + /** averages the color values (in linear color space) */ public LColor getAverageColor() {