Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mapwide texture on maps that are not the full size #378

Merged
merged 3 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ public void setTextures() {
map.setTextureMasksScaled(map.getTextureMasksLow(), texturesLowMask.getFinalMask());
map.setTextureMasksScaled(map.getTextureMasksHigh(), texturesHighMask.getFinalMask());
map.setTerrainType(map.getTerrainType(), terrainType.getFinalMask());
map.setRawMapTexture(
ImageUtil.getMapwideTextureBytes(normals.getFinalMask(), scaledWaterDepth.getFinalMask(),
map.setMapwideTexture(
ImageUtil.getMapwideTexture(normals.getFinalMask(), scaledWaterDepth.getFinalMask(),
shadows.getFinalMask()));
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public void setTextures() {
DebugUtil.timedRun("com.faforever.neroxis.map.generator", "generateTextures", () -> {
map.setTextureMasksScaled(map.getTextureMasksLow(), texturesLowMask.getFinalMask());
map.setTextureMasksScaled(map.getTextureMasksHigh(), texturesHighMask.getFinalMask());
map.setRawMapTexture(
ImageUtil.getMapwideTextureBytes(normals.getFinalMask(), scaledWaterDepth.getFinalMask(),
map.setMapwideTexture(
ImageUtil.getMapwideTexture(normals.getFinalMask(), scaledWaterDepth.getFinalMask(),
shadows.getFinalMask()));
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,13 @@ public static void exportPreview(Path folderPath, SCMap map) throws IOException
}

public static void exportMapwideTexture(Path folderPath, SCMap map) throws IOException {
byte[] rawTexture = map.getRawMapTexture();
BufferedImage image = map.getMapwideTexture();
Path textureDirectory = Paths.get("env", "texture");
Path filePath = textureDirectory.resolve(MAPWIDE_DDS);
Path writingPath = folderPath.resolve(filePath);
Files.createDirectories(writingPath.getParent());
try {
Files.write(writingPath, rawTexture, StandardOpenOption.CREATE);
ImageUtil.writeRawDDS(image, writingPath);
} catch (IOException e) {
System.out.print("Could not write the map-wide texture\n" + e);
}
Expand Down
9 changes: 8 additions & 1 deletion shared/src/main/java/com/faforever/neroxis/map/SCMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public class SCMap {
private final List<AIMarker> largeExpansionAIMarkers;
private final List<AIMarker> navalAreaAIMarkers;
private final List<AIMarker> navalRallyMarkers;
private byte[] rawMapTexture;
private float heightMapScale = 1f / 128f;
private String name = "";
@Setter(AccessLevel.NONE)
Expand Down Expand Up @@ -91,6 +90,7 @@ public class SCMap {
private BufferedImage waterFlatnessMap;
private BufferedImage waterDepthBiasMap;
private BufferedImage terrainType;
private BufferedImage mapwideTexture;
private int cartographicContourInterval = 100;
private int cartographicDeepWaterColor = new Color(71, 140, 181).getRGB();
private int cartographicMapContourColor = new Color(0, 0, 0).getRGB();
Expand Down Expand Up @@ -478,13 +478,16 @@ private void scaleMapContent(float contentScale) {
StrictMath.round(textureMasksHigh.getHeight() * contentScale));
textureMasksLow = scaleImage(textureMasksLow, StrictMath.round(textureMasksLow.getWidth() * contentScale),
StrictMath.round(textureMasksLow.getHeight() * contentScale));
mapwideTexture = scaleImage(mapwideTexture, StrictMath.round(mapwideTexture.getWidth() * contentScale),
StrictMath.round(mapwideTexture.getHeight() * contentScale));
}

private void scaleMapBounds(float boundsScale, Vector2 topLeftOffset) {
float normalMapScale = (float) normalMap.getWidth() / size;
float waterMapScale = (float) waterMap.getWidth() / size;
float textureMaskHighScale = (float) textureMasksHigh.getWidth() / size;
float textureMaskLowScale = (float) textureMasksLow.getWidth() / size;
float mapwideTextureScale = (float) mapwideTexture.getWidth() / size;
preview = scaleImage(preview, StrictMath.round(256 / boundsScale), StrictMath.round(256 / boundsScale));
Vector2 previewOffset = boundsScale > 1 ? new Vector2(128 - 128 / boundsScale,
128 - 128 / boundsScale) : new Vector2(-64 / boundsScale,
Expand Down Expand Up @@ -524,6 +527,10 @@ private void scaleMapBounds(float boundsScale, Vector2 topLeftOffset) {
StrictMath.round(textureMasksLow.getWidth() * boundsScale),
StrictMath.round(textureMasksLow.getHeight() * boundsScale),
new Vector2(topLeftOffset).multiply(textureMaskLowScale));
mapwideTexture = insertImageIntoNewImageOfSize(mapwideTexture,
StrictMath.round(mapwideTexture.getWidth() * boundsScale),
StrictMath.round(mapwideTexture.getHeight() * boundsScale),
new Vector2(topLeftOffset).multiply(mapwideTextureScale));
}

private void moveObjects(float contentScale, Vector2 offset) {
Expand Down
41 changes: 33 additions & 8 deletions shared/src/main/java/com/faforever/neroxis/util/ImageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,29 +141,54 @@ public static void writeCompressedDDS(BufferedImage image, Path path) throws IOE
Files.write(path, compressedData, StandardOpenOption.APPEND);
}

public static byte[] getMapwideTextureBytes(NormalMask normalMask, FloatMask waterDepth, FloatMask shadowMask) {
public static void writeRawDDS(BufferedImage image, Path path) throws IOException {
int size = image.getHeight();
int length = size * size * 4;
Raster imageRaster = image.getData();
ByteBuffer imageBytes = ByteBuffer.allocate(length).order(ByteOrder.LITTLE_ENDIAN);
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
int[] values = imageRaster.getPixel(x, y, new int[4]);
for (int val : values) {
imageBytes.put((byte) val);
}
}
}
DDSHeader ddsHeader = new DDSHeader();
ddsHeader.setWidth(size);
ddsHeader.setHeight(size);
ddsHeader.setRGBBitCount(32);
ddsHeader.setRBitMask(0x000000FF);
ddsHeader.setGBitMask(0x0000FF00);
ddsHeader.setBBitMask(0x00FF0000);
ddsHeader.setABitMask(0xFF000000);

// If we don't do this we get weird results when the file already exists
Files.deleteIfExists(path);
Files.write(path, ddsHeader.toBytes(), StandardOpenOption.CREATE);
Files.write(path, imageBytes.array(), StandardOpenOption.APPEND);
}

public static BufferedImage getMapwideTexture(NormalMask normalMask, FloatMask waterDepth, FloatMask shadowMask) {
if (shadowMask.getSize() != normalMask.getSize()) {
throw new IllegalArgumentException("Mask sizes do not match: shadow size %d, normal size %d"
.formatted(shadowMask.getSize(), normalMask.getSize()));
}
waterDepth.resample(shadowMask.getSize());
int size = shadowMask.getSize();
int length = size * size * 4;
ByteBuffer imageByteBuffer = ByteBuffer.allocate(length).order(ByteOrder.LITTLE_ENDIAN);
BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
WritableRaster imageRaster = image.getRaster();
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
Vector3 normalValue = normalMask.get(x, y);
int xV = (byte) StrictMath.min(StrictMath.max(128 * normalValue.getX() + 127, 0), 255);
int yV = (byte) StrictMath.min(StrictMath.max(128 * normalValue.getZ() + 127, 0), 255);
int zV = (byte) StrictMath.min(StrictMath.max(waterDepth.get(x, y) * 255, 0), 255);
int wV = (byte) StrictMath.min(StrictMath.max(shadowMask.get(x, y) * 255, 0), 255);
imageByteBuffer.put((byte) xV);
imageByteBuffer.put((byte) yV);
imageByteBuffer.put((byte) zV);
imageByteBuffer.put((byte) wV);
imageRaster.setPixel(x, y, new int[]{xV, yV, zV, wV});
}
}
return getRawDDSImageBytes(size, imageByteBuffer);
return image;
}

public static BufferedImage normalToARGB(NormalMask mask) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void generateEnvTexture() throws Exception {
.clampMin(0f)
.clampMax(1f);

map.setRawMapTexture(ImageUtil.getMapwideTextureBytes(normals, scaledWaterDepth, shadows));
map.setMapwideTexture(ImageUtil.getMapwideTexture(normals, scaledWaterDepth, shadows));
SCMapExporter.exportMapwideTexture(requiredMapPathMixin.getMapPath(), map);
}
}
Loading