Skip to content

Commit

Permalink
Fix previews (#397)
Browse files Browse the repository at this point in the history
* Fix previews

* No star import

* Rename sunIllumnation to irradiance
  • Loading branch information
BlackYps authored May 29, 2024
1 parent 6befbe1 commit 4718216
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public abstract class TextureGenerator implements HasParameterConstraints {
protected Vector4Mask texturesLowPreviewMask;
protected Vector4Mask texturesHighPreviewMask;
protected FloatMask heightmapPreview;
protected FloatMask reflectance;
protected FloatMask irradiance;

protected abstract void setupTexturePipeline();

Expand Down Expand Up @@ -93,10 +93,10 @@ public void setCompressedDecals() {
}

public void generatePreview() {
Pipeline.await(texturesLowPreviewMask, texturesHighPreviewMask, reflectance, heightmapPreview);
Pipeline.await(texturesLowPreviewMask, texturesHighPreviewMask, irradiance, heightmapPreview);
DebugUtil.timedRun("com.faforever.neroxis.map.generator", "generatePreview", () -> {
try {
PreviewGenerator.generatePreview(heightmapPreview.getFinalMask(), reflectance.getFinalMask(), map,
PreviewGenerator.generatePreview(heightmapPreview.getFinalMask(), irradiance.getFinalMask(), map,
texturesLowPreviewMask.getFinalMask(),
texturesHighPreviewMask.getFinalMask());
} catch (IOException e) {
Expand All @@ -109,12 +109,11 @@ private void setupPreviewPipeline() {
texturesLowPreviewMask = texturesLowMask.copy().resample(PreviewGenerator.PREVIEW_SIZE);
texturesHighPreviewMask = texturesHighMask.copy().resample(PreviewGenerator.PREVIEW_SIZE);
heightmapPreview = heightmap.copy().resample(PreviewGenerator.PREVIEW_SIZE);
reflectance = heightmap.copy()
irradiance = heightmap.copy()
.copyAsNormalMask(8f)
.resample(PreviewGenerator.PREVIEW_SIZE)
.copyAsDotProduct(map.getBiome().lightingSettings().sunDirection())
.add(1f)
.divide(2f);
.clampMin(0f);
}

public final void setupPipeline() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void generatePreview(SCMap map, SymmetrySettings symmetrySettings)
generatePreview(heightmap, sunReflectance, map, textureMasksLow, textureMasksHigh);
}

public static void generatePreview(FloatMask heightmap, FloatMask sunReflectance, SCMap map,
public static void generatePreview(FloatMask heightmap, FloatMask irradiance, SCMap map,
Vector4Mask textureMasksLow, Vector4Mask textureMasksHigh) throws IOException {
if (!map.isGeneratePreview()) {
generateBlankPreview(map);
Expand All @@ -61,14 +61,14 @@ public static void generatePreview(FloatMask heightmap, FloatMask sunReflectance
Graphics2D layerGraphics = layer.createGraphics();
layerGraphics.setColor(new Color(Integer.parseInt(materials.previewColors().get(i), 16)));
layerGraphics.fillRect(0, 0, PREVIEW_SIZE, PREVIEW_SIZE);
shadeLayer(layer, map, sunReflectance, scaledTextures.get(i));
shadeLayer(layer, map, irradiance, scaledTextures.get(i));
TexturePaint layerPaint = new TexturePaint(layer,
new Rectangle2D.Float(0, 0, PREVIEW_SIZE, PREVIEW_SIZE));
graphics.setPaint(layerPaint);
graphics.fillRect(0, 0, previewImage.getWidth(), previewImage.getHeight());
}
}
BufferedImage waterLayer = getWaterLayer(map, sunReflectance, heightmap);
BufferedImage waterLayer = getWaterLayer(map, heightmap);
TexturePaint layerPaint = new TexturePaint(waterLayer, new Rectangle2D.Float(0, 0, PREVIEW_SIZE, PREVIEW_SIZE));
graphics.setPaint(layerPaint);
graphics.fillRect(0, 0, previewImage.getWidth(), previewImage.getHeight());
Expand All @@ -79,11 +79,11 @@ public static void generateBlankPreview(SCMap map) throws IOException {
map.getPreview().setData(blindPreview.getData());
}

private static BufferedImage shadeLayer(BufferedImage image, SCMap map, FloatMask reflectance,
private static BufferedImage shadeLayer(BufferedImage image, SCMap map, FloatMask irradiance,
FloatMask textureLayer) {
LightingSettings lightingSettings = map.getBiome().lightingSettings();

float ambientCoefficient = .25f;
float ambientCoefficient = .5f;

int[] origRGBA = new int[4];
int[] newRGBA = new int[4];
Expand All @@ -92,7 +92,7 @@ private static BufferedImage shadeLayer(BufferedImage image, SCMap map, FloatMas
for (int x = 0; x < PREVIEW_SIZE; x++) {
image.getRaster().getPixel(x, y, origRGBA);

float coefficient = reflectance.getPrimitive(x, y) + ambientCoefficient;
float coefficient = irradiance.getPrimitive(x, y) + ambientCoefficient;

newRGBA[0] = (int) (origRGBA[0] * ((lightingSettings.sunColor().getX() * coefficient)
+ lightingSettings.sunAmbience().getX())
Expand All @@ -115,10 +115,9 @@ private static BufferedImage shadeLayer(BufferedImage image, SCMap map, FloatMas
return image;
}

private static BufferedImage getWaterLayer(SCMap map, FloatMask reflectance, FloatMask heightmap) {
private static BufferedImage getWaterLayer(SCMap map, FloatMask heightmap) {
Color shallowColor = new Color(53, 85, 117);
Color abyssColor = new Color(60, 67, 137);
LightingSettings lightingSettings = map.getBiome().lightingSettings();
WaterSettings waterSettings = map.getBiome().waterSettings();

BufferedImage waterLayer = new BufferedImage(PREVIEW_SIZE, PREVIEW_SIZE, BufferedImage.TYPE_INT_ARGB);
Expand All @@ -127,7 +126,6 @@ private static BufferedImage getWaterLayer(SCMap map, FloatMask reflectance, Flo
float waterheight = waterSettings.elevation();
float abyssheight = waterSettings.elevationAbyss();

float ambientCoefficient = .5f;
int[] newRGBA = new int[4];
BufferedImage layer = new BufferedImage(PREVIEW_SIZE, PREVIEW_SIZE, BufferedImage.TYPE_INT_ARGB);

Expand All @@ -140,21 +138,13 @@ private static BufferedImage getWaterLayer(SCMap map, FloatMask reflectance, Flo
StrictMath.max((waterheight - heightmap.getPrimitive(x, y)) / (waterheight - abyssheight), 0),
1);

float coefficient = reflectance.getPrimitive(x, y) + ambientCoefficient;

newRGBA[0] = (int) (shallowColor.getRed() * (1 - weight) + abyssColor.getRed() * weight);
newRGBA[1] = (int) (shallowColor.getGreen() * (1 - weight) + abyssColor.getGreen() * weight);
newRGBA[2] = (int) (shallowColor.getBlue() * (1 - weight) + abyssColor.getBlue() * weight);
newRGBA[0] *= (int) ((lightingSettings.sunColor().getX() + waterSettings.surfaceColor().getX())
* coefficient);
newRGBA[1] *= (int) ((lightingSettings.sunColor().getY() + waterSettings.surfaceColor().getY())
* coefficient);
newRGBA[2] *= (int) ((lightingSettings.sunColor().getZ() + waterSettings.surfaceColor().getZ())
* coefficient);
newRGBA[0] = StrictMath.min(255, newRGBA[0]);
newRGBA[1] = StrictMath.min(255, newRGBA[1]);
newRGBA[2] = StrictMath.min(255, newRGBA[2]);
newRGBA[3] = waterheight > heightmap.getPrimitive(x, y) ? (int) (191 * weight + 32) : 0;
newRGBA[3] = waterheight > heightmap.getPrimitive(x, y) ? (int) (64 * weight + 92) : 0;

layer.getRaster().setPixel(x, y, newRGBA);
}
Expand Down

0 comments on commit 4718216

Please sign in to comment.