Skip to content

Commit

Permalink
Use a ChoiceBox for water shader (#1634)
Browse files Browse the repository at this point in the history
* Use a ChoiceBox for water shader

* Rename legacy water shading to tiled normal map.

* Improve water shading tooltip.

---------

Co-authored-by: Maik Marschner <[email protected]>
  • Loading branch information
Peregrine05 and leMaik authored Oct 28, 2023
1 parent 355f76d commit c89be36
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import se.llbit.chunky.block.minecraft.Lava;
import se.llbit.chunky.block.minecraft.Water;
import se.llbit.chunky.renderer.scene.Scene;
import se.llbit.chunky.renderer.scene.StillWaterShader;
import se.llbit.chunky.resources.Texture;
import se.llbit.math.Quad;
import se.llbit.math.Ray;
Expand Down Expand Up @@ -413,8 +414,8 @@ public static boolean intersectWithWater(Ray ray, Scene scene, int level) {
// TODO since this water is the same block, refraction is not taken into account – still better than no water
Quad water = waterLevels[level];
if (water != null && water.intersect(ray)) {
if (!scene.stillWaterEnabled()) {
scene.getWaterShading().doWaterShading(ray, scene.getAnimationTime());
if (!(scene.getCurrentWaterShader() instanceof StillWaterShader)) {
scene.getCurrentWaterShader().doWaterShading(ray, scene.getAnimationTime());
} else {
ray.setNormal(water.n);
}
Expand Down
48 changes: 48 additions & 0 deletions chunky/src/java/se/llbit/chunky/renderer/WaterShadingStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* Copyright (c) 2023 Chunky Contributors
*
* This file is part of Chunky.
*
* Chunky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chunky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Chunky. If not, see <http://www.gnu.org/licenses/>.
*/
package se.llbit.chunky.renderer;

import se.llbit.util.Registerable;

public enum WaterShadingStrategy implements Registerable {
SIMPLEX("Simplex", "Uses configurable noise to shade the water, which prevents tiling at great distances."),
TILED_NORMALMAP("Tiled normal map", "Uses a built-in tiled normal map to shade the water"),
STILL("Still", "Renders the water surface as flat.");

private final String displayName;
private final String description;

WaterShadingStrategy(String displayName, String description) {
this.displayName = displayName;
this.description = description;
}

@Override
public String getName() {
return this.displayName;
}

@Override
public String getDescription() {
return this.description;
}

@Override
public String getId() {
return this.name();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public WaterShader clone() {

@Override
public void save(JsonObject json) {
json.add("waterShader", "LEGACY");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ public static boolean pathTrace(Scene scene, Ray ray, WorkerState state, int add
Material currentMat = ray.getCurrentMaterial();
Material prevMat = ray.getPrevMaterial();

if (!scene.stillWater && ray.getNormal().y != 0 &&
if (!(scene.getCurrentWaterShader() instanceof StillWaterShader) && ray.getNormal().y != 0 &&
((currentMat.isWater() && prevMat == Air.INSTANCE)
|| (currentMat == Air.INSTANCE && prevMat.isWater()))) {
scene.getWaterShading().doWaterShading(ray, scene.getAnimationTime());
scene.getCurrentWaterShader().doWaterShading(ray, scene.getAnimationTime());
if (currentMat == Air.INSTANCE) {
ray.invertNormal();
}
Expand Down
100 changes: 56 additions & 44 deletions chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,18 @@ public class Scene implements JsonSerializable, Refreshable {
*/
protected double waterOpacity = PersistentSettings.getWaterOpacity();
protected double waterVisibility = PersistentSettings.getWaterVisibility();
protected boolean stillWater = PersistentSettings.getStillWater();
protected WaterShadingStrategy waterShadingStrategy = WaterShadingStrategy.valueOf(PersistentSettings.getWaterShadingStrategy());
private final StillWaterShader stillWaterShader = new StillWaterShader();
private final LegacyWaterShader legacyWaterShader = new LegacyWaterShader();
private final SimplexWaterShader simplexWaterShader = new SimplexWaterShader();

private WaterShader currentWaterShader = getWaterShader(waterShadingStrategy);
protected boolean useCustomWaterColor = PersistentSettings.getUseCustomWaterColor();

protected boolean waterPlaneEnabled = false;
protected double waterPlaneHeight = World.SEA_LEVEL;
protected boolean waterPlaneOffsetEnabled = true;
protected boolean waterPlaneChunkClip = true;
protected WaterShader waterShading = new SimplexWaterShader();

public final Fog fog = new Fog(this);

Expand Down Expand Up @@ -440,7 +444,8 @@ public synchronized void copyState(Scene other, boolean copyChunks) {

exposure = other.exposure;

stillWater = other.stillWater;
waterShadingStrategy = other.waterShadingStrategy;
currentWaterShader = other.currentWaterShader.clone();
waterOpacity = other.waterOpacity;
waterVisibility = other.waterVisibility;
useCustomWaterColor = other.useCustomWaterColor;
Expand All @@ -466,7 +471,6 @@ public synchronized void copyState(Scene other, boolean copyChunks) {
waterPlaneHeight = other.waterPlaneHeight;
waterPlaneOffsetEnabled = other.waterPlaneOffsetEnabled;
waterPlaneChunkClip = other.waterPlaneChunkClip;
waterShading = other.waterShading.clone();

hideUnknownBlocks = other.hideUnknownBlocks;

Expand Down Expand Up @@ -612,16 +616,6 @@ public double getExposure() {
return exposure;
}

/**
* Set still water mode.
*/
public void setStillWater(boolean value) {
if (value != stillWater) {
stillWater = value;
refresh();
}
}

/**
* Set emitters enable flag.
*/
Expand Down Expand Up @@ -1617,13 +1611,6 @@ public synchronized void haltRender() {
}
}

/**
* @return <code>true</code> if still water is enabled
*/
public boolean stillWaterEnabled() {
return stillWater;
}

/**
* @return <code>true</code> if biome colors are enabled
*/
Expand Down Expand Up @@ -2650,7 +2637,7 @@ public void setUseCustomWaterColor(boolean value) {
json.add("fancierTranslucency", fancierTranslucency);
json.add("transmissivityCap", transmissivityCap);
json.add("sunSamplingStrategy", sunSamplingStrategy.getId());
json.add("stillWater", stillWater);
json.add("waterShadingStrategy", waterShadingStrategy.getId());
json.add("waterOpacity", waterOpacity);
json.add("waterVisibility", waterVisibility);
json.add("useCustomWaterColor", useCustomWaterColor);
Expand All @@ -2661,7 +2648,7 @@ public void setUseCustomWaterColor(boolean value) {
colorObj.add("blue", waterColor.z);
json.add("waterColor", colorObj);
}
waterShading.save(json);
currentWaterShader.save(json);
json.add("fog", fog.toJson());
json.add("biomeColorsEnabled", biomeColors);
json.add("transparentSky", transparentSky);
Expand Down Expand Up @@ -2937,7 +2924,28 @@ public synchronized void importFromJson(JsonObject json) {
sunSamplingStrategy = SunSamplingStrategy.valueOf(json.get("sunSamplingStrategy").asString(SunSamplingStrategy.FAST.getId()));
}

stillWater = json.get("stillWater").boolValue(stillWater);
waterShadingStrategy = WaterShadingStrategy.valueOf(json.get("waterShadingStrategy").asString(WaterShadingStrategy.SIMPLEX.getId()));
if (!json.get("waterShader").isUnknown()) {
String waterShader = json.get("waterShader").stringValue("SIMPLEX");
if(waterShader.equals("LEGACY"))
waterShadingStrategy = WaterShadingStrategy.TILED_NORMALMAP;
else if(waterShader.equals("SIMPLEX"))
waterShadingStrategy = WaterShadingStrategy.SIMPLEX;
else {
Log.infof("Unknown water shader %s, using SIMPLEX", waterShader);
waterShadingStrategy = WaterShadingStrategy.SIMPLEX;
}
} else {
waterShadingStrategy = WaterShadingStrategy.TILED_NORMALMAP;
}
if (!json.get("stillWater").isUnknown()) {
if (json.get("stillWater").boolValue(false)) {
waterShadingStrategy = WaterShadingStrategy.STILL;
}
}
setCurrentWaterShader(waterShadingStrategy);
currentWaterShader.load(json);

waterOpacity = json.get("waterOpacity").doubleValue(waterOpacity);
waterVisibility = json.get("waterVisibility").doubleValue(waterVisibility);
useCustomWaterColor = json.get("useCustomWaterColor").boolValue(useCustomWaterColor);
Expand All @@ -2947,16 +2955,6 @@ public synchronized void importFromJson(JsonObject json) {
waterColor.y = colorObj.get("green").doubleValue(waterColor.y);
waterColor.z = colorObj.get("blue").doubleValue(waterColor.z);
}
String waterShader = json.get("waterShader").stringValue("SIMPLEX");
if(waterShader.equals("LEGACY"))
waterShading = new LegacyWaterShader();
else if(waterShader.equals("SIMPLEX"))
waterShading = new SimplexWaterShader();
else {
Log.infof("Unknown water shader %s, using SIMPLEX", waterShader);
waterShading = new SimplexWaterShader();
}
waterShading.load(json);
biomeColors = json.get("biomeColorsEnabled").boolValue(biomeColors);
transparentSky = json.get("transparentSky").boolValue(transparentSky);
JsonValue fogObj = json.get("fog");
Expand Down Expand Up @@ -3400,18 +3398,32 @@ public World getWorld() {
return loadedWorld;
}

/**
* Get the water shader
*/
public WaterShader getWaterShading() {
return waterShading;
public WaterShadingStrategy getWaterShadingStrategy() {
return waterShadingStrategy;
}

/**
* Set the Water shader
*/
public void setWaterShading(WaterShader waterShading) {
this.waterShading = waterShading;
public void setWaterShadingStrategy(WaterShadingStrategy waterShadingStrategy) {
this.waterShadingStrategy = waterShadingStrategy;
setCurrentWaterShader(waterShadingStrategy);
refresh();
}

public WaterShader getCurrentWaterShader() {
return currentWaterShader;
}
private void setCurrentWaterShader(WaterShadingStrategy waterShadingStrategy) {
currentWaterShader = getWaterShader(waterShadingStrategy);
}

private WaterShader getWaterShader(WaterShadingStrategy waterShadingStrategy) {
switch (waterShadingStrategy) {
case STILL:
return stillWaterShader;
case TILED_NORMALMAP:
return legacyWaterShader;
default:
return simplexWaterShader;
}
}

public boolean getHideUnknownBlocks() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ public WaterShader clone() {

@Override
public void save(JsonObject json) {
json.add("waterShader", "SIMPLEX");
JsonObject params = new JsonObject();
params.add("iterations", iterations);
params.add("frequency", baseFrequency);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* Copyright (c) 2012-2023 Chunky contributors
*
* This file is part of Chunky.
*
* Chunky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chunky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Chunky. If not, see <http://www.gnu.org/licenses/>.
*/
package se.llbit.chunky.renderer.scene;

import se.llbit.chunky.model.minecraft.WaterModel;
import se.llbit.json.JsonObject;
import se.llbit.math.Ray;

public class StillWaterShader implements WaterShader {
@Override
public void doWaterShading(Ray ray, double animationTime) {
}

@Override
public WaterShader clone() {
return new StillWaterShader();
}

@Override
public void save(JsonObject json) {
}

@Override
public void load(JsonObject json) {
}
}
Loading

0 comments on commit c89be36

Please sign in to comment.