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

Change shader back to the original #379

Merged
merged 4 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 @@ -47,7 +47,6 @@
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.faforever.neroxis.map.SCMap.MAP_WIDE_ASSETS_SHADER_NAME;
import static com.faforever.neroxis.map.SCMap.PBR_SHADER_NAME;
import static picocli.CommandLine.Command;
import static picocli.CommandLine.Option;
Expand Down Expand Up @@ -451,10 +450,6 @@ private void save() {
long startTime = System.currentTimeMillis();
Path outputPath = outputFolderMixin.getOutputPath();
Visibility visibility = generatorParameters.visibility();
SCMapExporter.exportMapwideTexture(outputPath.resolve(mapName), map);
if (map.getTerrainShaderPath().equals(PBR_SHADER_NAME)) {
SCMapExporter.exportPBR(outputPath.resolve(mapName), map);
}
MapExporter.exportMap(outputPath, map, visibility == null);
System.out.printf("File export done: %d ms\n", System.currentTimeMillis() - startTime);

Expand Down Expand Up @@ -530,9 +525,6 @@ private void generate() {
.getCubeMaps()
.get(0)
.getPath();
}
if (map.getTerrainShaderPath().equals(PBR_SHADER_NAME) ||
map.getTerrainShaderPath().equals(MAP_WIDE_ASSETS_SHADER_NAME)) {
map.getBiome().terrainMaterials().getTexturePaths()[9] = Path.of("/maps", map.getFolderName(), "env",
"texture", "mapwide.dds")
.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public SCMap generate(GeneratorParameters generatorParameters, long seed) {

CompletableFuture<Void> heightMapFuture = CompletableFuture.runAsync(terrainGenerator::setHeightmapImage);
CompletableFuture<Void> textureFuture = CompletableFuture.runAsync(textureGenerator::setTextures);
CompletableFuture<Void> normalFuture = CompletableFuture.runAsync(textureGenerator::setCompressedDecals);
CompletableFuture<Void> previewFuture = CompletableFuture.runAsync(textureGenerator::generatePreview);
CompletableFuture<Void> resourcesFuture = CompletableFuture.runAsync(resourceGenerator::placeResources);
CompletableFuture<Void> decalsFuture = CompletableFuture.runAsync(decalGenerator::placeDecals);
Expand All @@ -105,7 +106,7 @@ public SCMap generate(GeneratorParameters generatorParameters, long seed) {

CompletableFuture<Void> placementFuture = CompletableFuture.allOf(heightMapFuture, textureFuture, previewFuture,
resourcesFuture, decalsFuture, propsFuture,
unitsFuture)
unitsFuture, normalFuture)
.thenAccept(aVoid -> setHeights());

placementFuture.join();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ public void setTextures() {
});
}

public void setCompressedDecals() {
Pipeline.await(normals, shadows);
DebugUtil.timedRun("com.faforever.neroxis.map.generator", "setCompressedDecals", () -> {
map.setCompressedShadows(ImageUtil.compressShadow(shadows.getFinalMask(),
generatorParameters.biome().lightingSettings()));
map.setCompressedNormal(ImageUtil.compressNormal(normals.getFinalMask()));
});
}

public void generatePreview() {
Pipeline.await(texturesLowPreviewMask, texturesHighPreviewMask, reflectance, heightmapPreview);
DebugUtil.timedRun("com.faforever.neroxis.map.generator", "generatePreview", () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,26 @@
import java.nio.file.Files;
import java.nio.file.Path;

import static com.faforever.neroxis.map.SCMap.LEGACY_SHADER_NAME;
import static com.faforever.neroxis.map.SCMap.PBR_SHADER_NAME;

public class MapExporter {
public static void exportMap(Path folderPath, SCMap map, boolean exportPreview) {
try {
Path mapPath = folderPath.resolve(map.getFolderName());
Files.createDirectories(mapPath);

if (map.getTerrainShaderPath().equals(LEGACY_SHADER_NAME)) {
if (map.getCompressedNormal() != null) {
SCMapExporter.exportNormals(mapPath, map);
}
if (map.getCompressedShadows() != null) {
SCMapExporter.exportShadows(mapPath, map);
}
} else if (map.getTerrainShaderPath().equals(PBR_SHADER_NAME)){
SCMapExporter.exportMapwideTexture(folderPath.resolve(map.getFolderName()), map);
SCMapExporter.exportPBR(folderPath.resolve(map.getFolderName()), map);
}
if (exportPreview) {
SCMapExporter.exportPreview(mapPath, map);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package com.faforever.neroxis.exporter;

import com.faforever.neroxis.map.CubeMap;
import com.faforever.neroxis.map.Decal;
import com.faforever.neroxis.map.DecalGroup;
import com.faforever.neroxis.map.Prop;
import com.faforever.neroxis.map.SCMap;
import com.faforever.neroxis.map.SkyBox;
import com.faforever.neroxis.map.WaveGenerator;
import com.faforever.neroxis.map.*;
import com.faforever.neroxis.util.ImageUtil;
import com.faforever.neroxis.util.dds.DDSHeader;
import com.faforever.neroxis.util.jsquish.Squish;
Expand Down Expand Up @@ -227,6 +221,47 @@ public static void exportPreview(Path folderPath, SCMap map) throws IOException
}
}

public static void exportNormals(Path folderPath, SCMap map) throws IOException {
float size = map.getPlayableArea().getW() - map.getPlayableArea().getX();
Vector2 topLeftOffset = new Vector2(map.getPlayableArea().getX(), map.getPlayableArea().getY());
byte[] compressedNormal = map.getCompressedNormal();
final String fileFormat = "dds";
Path decalsPath = Paths.get("env", "decals");
Path decalParent = Paths.get("/maps").resolve(map.getFolderName());
Path decalPath = decalsPath.resolve(String.format("map_normal.%s", fileFormat));
Path writingPath = folderPath.resolve(decalPath);
Files.createDirectories(writingPath.getParent());
map.getDecals()
.add(new Decal(decalParent.resolve(decalPath).toString().replace('\\', '/'), topLeftOffset, new Vector3(),
size, 1000));
try {
Files.write(writingPath, compressedNormal, StandardOpenOption.CREATE);
} catch (IOException e) {
System.out.print("Could not write the normal map image\n" + e);
}
}

public static void exportShadows(Path folderPath, SCMap map) throws IOException {
float size = map.getPlayableArea().getW() - map.getPlayableArea().getX();
Vector2 topLeftOffset = new Vector2(map.getPlayableArea().getX(), map.getPlayableArea().getY());
byte[] compressedShadows = map.getCompressedShadows();
final String fileFormat = "dds";
Path decalsPath = Paths.get("env", "decals");
Path decalParent = Paths.get("/maps").resolve(map.getFolderName());
Path decalPath = decalsPath.resolve(String.format("map_shadows.%s", fileFormat));
Path writingPath = folderPath.resolve(decalPath);
Files.createDirectories(writingPath.getParent());
Decal shadowDecal = new Decal(decalParent.resolve(decalPath).toString().replace('\\', '/'), topLeftOffset,
new Vector3(), size, 1000);
shadowDecal.setType(DecalType.WATER_ALBEDO);
map.getDecals().add(shadowDecal);
try {
Files.write(writingPath, compressedShadows, StandardOpenOption.CREATE);
} catch (IOException e) {
System.out.print("Could not write the shadow map image\n" + e);
}
}

public static void exportMapwideTexture(Path folderPath, SCMap map) throws IOException {
BufferedImage image = map.getMapwideTexture();
Path textureDirectory = Paths.get("env", "texture");
Expand Down
5 changes: 3 additions & 2 deletions shared/src/main/java/com/faforever/neroxis/map/SCMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
@SuppressWarnings("unused")
@Data
public class SCMap {
public static final String MAP_WIDE_ASSETS_SHADER_NAME = "Terrain002";
public static final String PBR_SHADER_NAME = "TerrainPBR";
public static final String LEGACY_SHADER_NAME = "TTerrainXP";
public static final int SIGNATURE = 443572557;
Expand Down Expand Up @@ -62,14 +61,16 @@ public class SCMap {
private final List<AIMarker> largeExpansionAIMarkers;
private final List<AIMarker> navalAreaAIMarkers;
private final List<AIMarker> navalRallyMarkers;
private byte[] compressedNormal;
private byte[] compressedShadows;
private float heightMapScale = 1f / 128f;
private String name = "";
@Setter(AccessLevel.NONE)
private int size; // must be a power of 2 when exported. 512 equals a 10x10km Map
private Vector4 playableArea;
private int minorVersion = 56;
private String description = "";
private String terrainShaderPath = MAP_WIDE_ASSETS_SHADER_NAME;
private String terrainShaderPath = LEGACY_SHADER_NAME;
private String backgroundPath = "/textures/environment/defaultbackground.dds";
private boolean generatePreview;
private boolean isUnexplored;
Expand Down
45 changes: 45 additions & 0 deletions shared/src/main/java/com/faforever/neroxis/util/ImageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.faforever.neroxis.mask.Vector4Mask;
import com.faforever.neroxis.util.dds.DDSHeader;
import com.faforever.neroxis.util.jsquish.Squish;
import com.faforever.neroxis.util.serial.biome.LightingSettings;
import com.faforever.neroxis.util.vector.Vector2;
import com.faforever.neroxis.util.vector.Vector3;
import com.faforever.neroxis.util.vector.Vector4;
Expand Down Expand Up @@ -191,6 +192,50 @@ public static BufferedImage getMapwideTexture(NormalMask normalMask, FloatMask w
return image;
}

public static byte[] compressNormal(NormalMask mask) {
int size = mask.getSize();
int length = size * size * 4;
ByteBuffer imageByteBuffer = ByteBuffer.allocate(length).order(ByteOrder.LITTLE_ENDIAN);
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
Vector3 value = mask.get(x, y);
int xV = (byte) StrictMath.min(StrictMath.max((128 * value.getX() + 128), 0), 255);
int yV = (byte) StrictMath.min(StrictMath.max((255 * (1 - value.getY())), 0), 255);
int zV = (byte) StrictMath.min(StrictMath.max((128 * value.getZ() + 128), 0), 255);
imageByteBuffer.put((byte) yV);
imageByteBuffer.put((byte) zV);
imageByteBuffer.put((byte) 0);
imageByteBuffer.put((byte) xV);
}
}
return getCompressedDDSImageBytes(size, imageByteBuffer);
}

public static byte[] compressShadow(FloatMask mask, LightingSettings lightingSettings) {
int size = mask.getSize();
int length = size * size * 4;
Vector3 shadowFillColor = lightingSettings.getShadowFillColor()
.copy()
.add(lightingSettings.getSunAmbience())
.divide(4);
float opacityScale = lightingSettings.getLightingMultiplier() / 4;
ByteBuffer imageByteBuffer = ByteBuffer.allocate(length).order(ByteOrder.LITTLE_ENDIAN);
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
float value = mask.get(x, y);
int r = (byte) StrictMath.min(StrictMath.max(shadowFillColor.getX() * 128, 0), 255);
int g = (byte) StrictMath.min(StrictMath.max(shadowFillColor.getY() * 128, 0), 255);
int b = (byte) StrictMath.min(StrictMath.max(shadowFillColor.getZ() * 128, 0), 255);
int a = (byte) StrictMath.min(StrictMath.max((1 - value) * opacityScale * 255, 0), 255);
imageByteBuffer.put((byte) r);
imageByteBuffer.put((byte) g);
imageByteBuffer.put((byte) b);
imageByteBuffer.put((byte) a);
}
}
return getCompressedDDSImageBytes(size, imageByteBuffer);
}

public static BufferedImage normalToARGB(NormalMask mask) {
int size = mask.getSize();
BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
Expand Down
Loading