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

Add a GUI for layered fog #1618

Merged
merged 4 commits into from
Sep 24, 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
54 changes: 45 additions & 9 deletions chunky/src/java/se/llbit/chunky/renderer/scene/Fog.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package se.llbit.chunky.renderer.scene;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Random;
import java.util.stream.Collectors;

import se.llbit.chunky.PersistentSettings;
import se.llbit.json.JsonArray;
Expand All @@ -11,19 +12,25 @@
import se.llbit.math.Ray;
import se.llbit.math.Vector3;
import se.llbit.math.Vector4;
import se.llbit.util.JsonSerializable;

public final class Fog {
public final class Fog implements JsonSerializable {
private final Scene scene;
protected FogMode mode = FogMode.NONE;
protected boolean fastFog = true;
protected double uniformDensity = Scene.DEFAULT_FOG_DENSITY;
protected double skyFogDensity = 1;
protected FogLayer[] layers = new FogLayer[0];
protected ArrayList<FogLayer> layers = new ArrayList<>(0);
protected Vector3 fogColor = new Vector3(PersistentSettings.getFogColorRed(), PersistentSettings.getFogColorGreen(), PersistentSettings.getFogColorBlue());

private static final double EXTINCTION_FACTOR = 0.04;
public static final double FOG_LIMIT = 30000;
public static final Vector4 SKY_SCATTER = new Vector4(1, 1, 1, 1);

public Fog(Scene scene) {
this.scene = scene;
}

public boolean fogEnabled() {
return mode != FogMode.NONE;
}
Expand All @@ -48,6 +55,35 @@ public boolean fastFog() {
return fastFog;
}

public ArrayList<FogLayer> getFogLayers() {
return layers;
}

public void addLayer() {
layers.add(new FogLayer(scene));
scene.refresh();
}

public void removeLayer(int index) {
layers.remove(index);
scene.refresh();
}

public void setY(int index, double value) {
layers.get(index).setY(value);
scene.refresh();
}

public void setBreadth(int index, double value) {
layers.get(index).setBreadth(value);
scene.refresh();
}

public void setDensity(int index, double value) {
layers.get(index).setDensity(value);
scene.refresh();
}

private static double clampDy(double dy) {
// This method prevents numerical errors or division by 0 when dy is close to 0.
final double epsilon = 0.00001;
Expand Down Expand Up @@ -149,11 +185,11 @@ public double sampleSkyScatterOffset(Scene scene, Ray ray, Random random) {
}

private double sampleLayeredScatterOffset(Random random, double y1, double y2, double dy) {
if (layers.length == 0) {
if (layers.size() == 0) {
return Ray.EPSILON;
}
// This works only for one fog layer yet.
FogLayer layer = layers[0];
FogLayer layer = layers.get(0);
// Logistic distribution CDF.
double y1v = 1 / (1 + Math.exp((layer.yWithOrigin - y1) * layer.breadthInv));
double y2v = 1 / (1 + Math.exp((layer.yWithOrigin - y2) * layer.breadthInv));
Expand All @@ -163,7 +199,7 @@ private double sampleLayeredScatterOffset(Random random, double y1, double y2, d
return (offsetY - y1) / clampDy(dy);
}

public JsonObject toJson() {
@Override public JsonObject toJson() {
JsonObject fogObj = new JsonObject();
fogObj.add("mode", mode.name());
fogObj.add("uniformDensity", uniformDensity);
Expand Down Expand Up @@ -194,7 +230,7 @@ public void importFromJson(JsonObject json, Scene scene) {
o.get("y").doubleValue(0),
o.get("breadth").doubleValue(0),
o.get("density").doubleValue(0),
scene)).toArray(FogLayer[]::new);
scene)).collect(Collectors.toCollection(ArrayList<FogLayer>::new));
JsonObject colorObj = json.get("color").object();
fogColor.x = colorObj.get("red").doubleValue(fogColor.x);
fogColor.y = colorObj.get("green").doubleValue(fogColor.y);
Expand All @@ -206,7 +242,7 @@ public void importFromLegacy(JsonObject json) {
mode = json.get("fogEnabled").boolValue(mode != FogMode.NONE) ? FogMode.NONE : FogMode.UNIFORM;
uniformDensity = json.get("fogDensity").doubleValue(uniformDensity);
skyFogDensity = json.get("skyFogDensity").doubleValue(skyFogDensity);
layers = new FogLayer[0];
layers = new ArrayList<>(0);
JsonObject colorObj = json.get("fogColor").object();
fogColor.x = colorObj.get("red").doubleValue(fogColor.x);
fogColor.y = colorObj.get("green").doubleValue(fogColor.y);
Expand All @@ -218,7 +254,7 @@ public void set(Fog other) {
mode = other.mode;
uniformDensity = other.uniformDensity;
skyFogDensity = other.skyFogDensity;
layers = Arrays.stream(other.layers).map(FogLayer::clone).toArray(FogLayer[]::new);
layers = new ArrayList<>(other.layers);
fogColor.set(other.fogColor);
}
}
7 changes: 7 additions & 0 deletions chunky/src/java/se/llbit/chunky/renderer/scene/FogLayer.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package se.llbit.chunky.renderer.scene;

public final class FogLayer {
public static final double DEFAULT_Y = 62;
public static final double DEFAULT_BREADTH = 5;
public static final double DEFAULT_DENSITY = 1;
public double yMin, y, yWithOrigin, breadth, breadthInv, density;

public FogLayer(double y, double breadth, double density, Scene scene) {
this(y, breadth, density, scene.yMin);
}

public FogLayer(Scene scene) {
this(DEFAULT_Y, DEFAULT_BREADTH, DEFAULT_DENSITY, scene.yMin);
}

private FogLayer(double y, double breadth, double density, double yMin) {
this.yMin = yMin;
setY(y);
Expand Down
2 changes: 1 addition & 1 deletion chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public class Scene implements JsonSerializable, Refreshable {
protected boolean waterPlaneChunkClip = true;
protected WaterShader waterShading = new LegacyWaterShader();

public final Fog fog = new Fog();
public final Fog fog = new Fog(this);

protected boolean biomeColors = true;
protected boolean biomeBlending = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/* 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.ui.render.settings;

import javafx.beans.value.ChangeListener;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import se.llbit.chunky.renderer.RenderController;
import se.llbit.chunky.renderer.scene.Scene;
import se.llbit.chunky.ui.DoubleAdjuster;
import se.llbit.fx.LuxColorPicker;
import se.llbit.math.ColorUtil;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class LayeredFogSettings extends VBox implements Initializable {
private Scene scene;

@FXML private ComboBox<String> layers;
@FXML private Button addLayer;
@FXML private Button removeLayer;
@FXML private DoubleAdjuster layerDensity;
@FXML private DoubleAdjuster layerY;
@FXML private DoubleAdjuster layerBreadth;
@FXML private LuxColorPicker fogColor;

private ChangeListener<? super Color> fogColorListener =
(observable, oldValue, newValue) -> scene.setFogColor(ColorUtil.fromFx(newValue));

public LayeredFogSettings() throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("LayeredFogSettings.fxml"));
loader.setRoot(this);
loader.setController(this);
loader.load();
}

public void enableControls() {
layerY.setDisable(false);
layerBreadth.setDisable(false);
layerDensity.setDisable(false);
}

public void disableControls() {
layerY.setDisable(true);
layerBreadth.setDisable(true);
layerDensity.setDisable(true);
}

@Override public void initialize(URL location, ResourceBundle resources) {
layers.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) ->
updateControls()
);

addLayer.setOnAction(e -> {
scene.fog.addLayer();
update(scene);
layers.getSelectionModel().selectLast();
});

removeLayer.setOnAction(e -> {
if (scene.fog.getFogLayers().size() > 0) {
scene.fog.removeLayer(layers.getSelectionModel().getSelectedIndex());
update(scene);
}
});

if (!(layers.getItems().size() > 0)) {
disableControls();
}

layerY.setTooltip("Y-coordinate of the selected fog layer");
layerY.setRange(-64, 320);
layerY.onValueChange(value -> scene.fog.setY(layers.getSelectionModel().getSelectedIndex(), value));
layerY.setName("Layer altitude");

layerBreadth.setTooltip("Vertical spread of the selected fog layer");
layerBreadth.setRange(0.1, 100);
layerBreadth.makeLogarithmic();
layerBreadth.clampMin();
layerBreadth.onValueChange(value -> scene.fog.setBreadth(layers.getSelectionModel().getSelectedIndex(), value));
layerBreadth.setName("Layer thickness");

layerDensity.setTooltip("Density of the selected fog layer");
layerDensity.setRange(0, 1);
layerDensity.setMaximumFractionDigits(6);
layerDensity.makeLogarithmic();
layerDensity.clampMin();
layerDensity.onValueChange(value -> scene.fog.setDensity(layers.getSelectionModel().getSelectedIndex(), value));
layerDensity.setName("Fog density");

fogColor.colorProperty().addListener(fogColorListener);
}

public void setRenderController(RenderController controller) {
scene = controller.getSceneManager().getScene();
}

private boolean updateLayersList() {
layers.getSelectionModel().clearSelection();
layers.getItems().clear();
int numLayers = scene.fog.getFogLayers().size();
boolean emptyLayers = !(numLayers > 0);
if (!emptyLayers) {
for (int i = 0; i < numLayers; i++) {
layers.getItems().add(String.format("Layer %d", i + 1));
}
}
return emptyLayers;
}

private void updateControls() {
if (!layers.getSelectionModel().isEmpty()) {
int index = layers.getSelectionModel().getSelectedIndex();
layerY.set(scene.fog.getFogLayers().get(index).y);
layerBreadth.set(scene.fog.getFogLayers().get(index).breadth);
layerDensity.set(scene.fog.getFogLayers().get(index).density);
enableControls();
}
}

public void update(Scene scene) {
disableControls();
if (updateLayersList()) {
updateControls();
}
fogColor.colorProperty().removeListener(fogColorListener);
fogColor.setColor(ColorUtil.toFx(scene.fog.getFogColor()));
fogColor.colorProperty().addListener(fogColorListener);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2016 Jesper Öqvist <[email protected]>
/* Copyright (c) 2023 Chunky Contributors
*
* This file is part of Chunky.
*
Expand Down
6 changes: 5 additions & 1 deletion chunky/src/java/se/llbit/chunky/ui/render/tabs/SkyTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import se.llbit.chunky.ui.elements.GradientEditor;
import se.llbit.chunky.ui.controller.RenderControlsFxController;
import se.llbit.chunky.ui.render.RenderControlsTab;
import se.llbit.chunky.ui.render.settings.LayeredFogSettings;
import se.llbit.chunky.ui.render.settings.UniformFogSettings;
import se.llbit.chunky.ui.render.settings.SkyboxSettings;
import se.llbit.chunky.ui.render.settings.SkymapSettings;
Expand Down Expand Up @@ -79,6 +80,7 @@ public class SkyTab extends ScrollPane implements RenderControlsTab, Initializab
private final SkyboxSettings skyboxSettings = new SkyboxSettings();
private final SkymapSettings skymapSettings = new SkymapSettings();
private final UniformFogSettings uniformFogSettings = new UniformFogSettings();
private final LayeredFogSettings layeredFogSettings = new LayeredFogSettings();

private ChangeListener<? super javafx.scene.paint.Color> skyColorListener =
(observable, oldValue, newValue) -> scene.sky().setColor(ColorUtil.fromFx(newValue));
Expand All @@ -99,6 +101,7 @@ public SkyTab() throws IOException {
skyboxSettings.setRenderController(controller.getRenderController());
skymapSettings.setRenderController(controller.getRenderController());
uniformFogSettings.setRenderController(controller.getRenderController());
layeredFogSettings.setRenderController(controller.getRenderController());
}

@Override public void initialize(URL location, ResourceBundle resources) {
Expand Down Expand Up @@ -164,7 +167,7 @@ public SimulatedSky fromString(String string) {
break;
}
case LAYERED: {
fogDetailsBox.getChildren().setAll(new Label("Selected mode does not yet have a GUI."));
fogDetailsBox.getChildren().setAll(layeredFogSettings);
break;
}
}
Expand Down Expand Up @@ -239,6 +242,7 @@ public SimulatedSky fromString(String string) {
skyboxSettings.update(scene);
skymapSettings.update(scene);
uniformFogSettings.update(scene);
layeredFogSettings.update(scene);
}

@Override public String getTabTitle() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>
<?import se.llbit.chunky.ui.DoubleAdjuster?>
<?import se.llbit.fx.LuxColorPicker?>

<?import javafx.scene.control.ComboBox?>
<fx:root spacing="10.0" type="javafx.scene.layout.VBox" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">
<HBox alignment="CENTER_LEFT" spacing="10.0">
<ComboBox fx:id="layers" prefWidth="150.0" />
<Button fx:id="addLayer" mnemonicParsing="false" text="Add layer"/>
<Button fx:id="removeLayer" mnemonicParsing="false" text="Remove layer"/>
</HBox>
<DoubleAdjuster fx:id="layerY" />
<DoubleAdjuster fx:id="layerBreadth" />
<DoubleAdjuster fx:id="layerDensity" />
<HBox alignment="CENTER_LEFT" spacing="10.0">
<Label text="Fog color:" />
<LuxColorPicker fx:id="fogColor" />
</HBox>
<Text strokeType="OUTSIDE" strokeWidth="0.0" wrappingWidth="350" text="Hint: Set fog density &gt; 0.1 for thick fog, and &lt; 0.1 for haze effect." />
<Text strokeType="OUTSIDE" strokeWidth="0.0" wrappingWidth="350" text="Note: The 'Fog Color' control affects the color of all fog layers." />
</fx:root>