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 ground plane. #1638

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 @@ -95,6 +95,9 @@ public static boolean nextIntersection(Scene scene, Ray ray) {
if (scene.isWaterPlaneEnabled()) {
hit = waterPlaneIntersection(scene, ray) || hit;
}
if (scene.isGroundPlaneEnabled()) {
hit |= groundPlaneIntersection(scene, ray);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't call the groundPlaneIntersection method if hit is already true, eg. if the water plane was hit before. If this gets fixed, we could have ocean grounds for water world mode. 🤩

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should, |= uses the bitwise or operator so it can't get short circuited.

}
if (scene.intersect(ray)) {
// Octree tracer handles updating distance.
return true;
Expand Down Expand Up @@ -139,6 +142,30 @@ private static boolean waterPlaneIntersection(Scene scene, Ray ray) {
return false;
}

private static boolean groundPlaneIntersection(Scene scene, Ray ray) {
double t = (scene.getGroundPlaneHeight() - ray.o.y - scene.origin.y) / ray.d.y;
if (scene.getGroundPlaneChunkClip()) {
Vector3 pos = new Vector3(ray.o);
pos.scaleAdd(t, ray.d);
if (scene.isChunkLoaded((int)Math.floor(pos.x), (int)Math.floor(pos.y), (int)Math.floor(pos.z)))
return false;
}
if (ray.d.y < 0 && t > 0 && t < ray.t) {
double px = ray.o.x + t * ray.d.x;
double pz = ray.o.z + t * ray.d.z;

ray.u = px - Math.floor(px);
ray.v = pz - Math.floor(pz);

ray.t = t;
scene.getPalette().stone.getColor(ray);
ray.setNormal(0, 1, 0);
ray.setCurrentMaterial(scene.getPalette().stone);
return true;
}
return false;
}

// Chunk pattern config
private static final double chunkPatternLineWidth = 0.5; // in blocks
private static final double chunkPatternLinePosition = 8 - chunkPatternLineWidth / 2;
Expand Down
53 changes: 53 additions & 0 deletions chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ public class Scene implements JsonSerializable, Refreshable {
protected double waterPlaneHeight = World.SEA_LEVEL;
protected boolean waterPlaneOffsetEnabled = true;
protected boolean waterPlaneChunkClip = true;

protected boolean groundPlaneEnabled = false;
protected double groundPlaneHeight = 0;
protected boolean groundPlaneChunkClip = true;

protected WaterShader waterShading = new SimplexWaterShader();

public final Fog fog = new Fog(this);
Expand Down Expand Up @@ -466,6 +471,11 @@ public synchronized void copyState(Scene other, boolean copyChunks) {
waterPlaneHeight = other.waterPlaneHeight;
waterPlaneOffsetEnabled = other.waterPlaneOffsetEnabled;
waterPlaneChunkClip = other.waterPlaneChunkClip;

groundPlaneEnabled = other.groundPlaneEnabled;
groundPlaneHeight = other.groundPlaneHeight;
groundPlaneChunkClip = other.groundPlaneChunkClip;

waterShading = other.waterShading.clone();

hideUnknownBlocks = other.hideUnknownBlocks;
Expand Down Expand Up @@ -1859,6 +1869,39 @@ public boolean getWaterPlaneChunkClip() {
return waterPlaneChunkClip;
}

public void setGroundPlaneEnabled(boolean enabled) {
if (enabled != groundPlaneEnabled) {
groundPlaneEnabled = enabled;
refresh();
}
}

public boolean isGroundPlaneEnabled() {
return groundPlaneEnabled;
}

public void setGroundPlaneHeight(double height) {
if (height != groundPlaneHeight) {
groundPlaneHeight = height;
refresh();
}
}

public double getGroundPlaneHeight() {
return groundPlaneHeight;
}

public void setGroundPlaneChunkClip(boolean enabled) {
if (enabled != groundPlaneChunkClip) {
groundPlaneChunkClip = enabled;
refresh();
}
}

public boolean getGroundPlaneChunkClip() {
return groundPlaneChunkClip;
}

/**
* @return the dumpFrequency
*/
Expand Down Expand Up @@ -2663,10 +2706,16 @@ public void setUseCustomWaterColor(boolean value) {
json.add("fog", fog.toJson());
json.add("biomeColorsEnabled", biomeColors);
json.add("transparentSky", transparentSky);

json.add("waterWorldEnabled", waterPlaneEnabled);
json.add("waterWorldHeight", waterPlaneHeight);
json.add("waterWorldHeightOffsetEnabled", waterPlaneOffsetEnabled);
json.add("waterWorldClipEnabled", waterPlaneChunkClip);

json.add("groundPlaneEnabled", groundPlaneEnabled);
json.add("groundPlaneHeight", groundPlaneHeight);
json.add("groundPlaneClipEnabled", groundPlaneChunkClip);

json.add("hideUnknownBlocks", hideUnknownBlocks);

if (!worldPath.isEmpty()) {
Expand Down Expand Up @@ -2972,6 +3021,10 @@ else if(waterShader.equals("SIMPLEX"))
waterPlaneChunkClip = json.get("waterWorldClipEnabled").boolValue(waterPlaneChunkClip);
}

groundPlaneEnabled = json.get("groundPlaneEnabled").boolValue(groundPlaneEnabled);
groundPlaneHeight = json.get("groundPlaneHeight").doubleValue(groundPlaneHeight);
groundPlaneChunkClip = json.get("groundPlaneClipEnabled").boolValue(groundPlaneChunkClip);

hideUnknownBlocks = json.get("hideUnknownBlocks").boolValue(hideUnknownBlocks);
materials = json.get("materials").object().copy().toMap();

Expand Down
32 changes: 32 additions & 0 deletions chunky/src/java/se/llbit/chunky/ui/render/tabs/WaterTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,18 @@ public class WaterTab extends ScrollPane implements RenderControlsTab, Initializ
@FXML private CheckBox useCustomWaterColor;
@FXML private LuxColorPicker waterColor;
@FXML private Button saveDefaults;

@FXML private CheckBox waterPlaneEnabled;
@FXML private DoubleAdjuster waterPlaneHeight;
@FXML private CheckBox waterPlaneOffsetEnabled;
@FXML private CheckBox waterPlaneClip;
@FXML private TitledPane waterWorldModeDetailsPane;

@FXML private CheckBox groundPlaneEnabled;
@FXML private DoubleAdjuster groundPlaneHeight;
@FXML private CheckBox groundPlaneClip;
@FXML private TitledPane groundPlaneDetailsPane;

@FXML private CheckBox useProceduralWater;
@FXML private IntegerAdjuster proceduralWaterIterations;
@FXML private DoubleAdjuster proceduralWaterFrequency;
Expand Down Expand Up @@ -104,6 +111,11 @@ public void update(Scene scene) {
waterPlaneOffsetEnabled.setSelected(scene.isWaterPlaneOffsetEnabled());
waterPlaneClip.setSelected(scene.getWaterPlaneChunkClip());

groundPlaneEnabled.setSelected(scene.isGroundPlaneEnabled());
groundPlaneHeight.setRange(scene.yClipMin, scene.yClipMax);
groundPlaneHeight.set(scene.getGroundPlaneHeight());
groundPlaneClip.setSelected(scene.getGroundPlaneChunkClip());

if(scene.getWaterShading() instanceof SimplexWaterShader) {
useProceduralWater.setSelected(true);
SimplexWaterShader simplexWaterShader = (SimplexWaterShader) scene.getWaterShading();
Expand Down Expand Up @@ -195,6 +207,26 @@ public void initialize(URL location, ResourceBundle resources) {
scene.setWaterPlaneChunkClip(newValue)
);

groundPlaneDetailsPane.setVisible(groundPlaneEnabled.isSelected());
groundPlaneDetailsPane.setExpanded(groundPlaneEnabled.isSelected());
groundPlaneDetailsPane.setManaged(groundPlaneEnabled.isSelected());

groundPlaneEnabled.setTooltip(new Tooltip("If enabled, an infinite ground fills the scene."));
groundPlaneEnabled.selectedProperty().addListener((observable, oldValue, newValue) -> {
scene.setGroundPlaneEnabled(newValue);
groundPlaneDetailsPane.setVisible(newValue);
groundPlaneDetailsPane.setExpanded(newValue);
groundPlaneDetailsPane.setManaged(newValue);
});

groundPlaneHeight.setName("Ground height");
groundPlaneHeight.setTooltip("The default ground height is at y=0.");
groundPlaneHeight.onValueChange(value -> scene.setGroundPlaneHeight(value));

groundPlaneClip.selectedProperty().addListener((observable, oldValue, newValue) ->
scene.setGroundPlaneChunkClip(newValue)
);

proceduralWaterDetailsPane.setVisible(useProceduralWater.isSelected());
proceduralWaterDetailsPane.setExpanded(useProceduralWater.isSelected());
proceduralWaterDetailsPane.setManaged(useProceduralWater.isSelected());
Expand Down
14 changes: 14 additions & 0 deletions chunky/src/res/se/llbit/chunky/ui/render/tabs/WaterTab.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
</HBox>
<Button fx:id="saveDefaults" mnemonicParsing="false" text="Save as defaults" />
<Separator prefWidth="200.0" />

<CheckBox fx:id="waterPlaneEnabled" mnemonicParsing="false" text="Water world mode" />
<TitledPane fx:id="waterWorldModeDetailsPane" animated="false" text="Water world mode settings">
<VBox spacing="10.0">
Expand All @@ -34,6 +35,19 @@
<CheckBox fx:id="waterPlaneClip" mnemonicParsing="false" text="Hide the water plane in loaded chunks" />
</VBox>
</TitledPane>

<CheckBox fx:id="groundPlaneEnabled" mnemonicParsing="false" text="Ground plane" />
<TitledPane fx:id="groundPlaneDetailsPane" animated="false" text="Ground plane settings">
<VBox spacing="10.0">
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>

<DoubleAdjuster fx:id="groundPlaneHeight" />
<CheckBox fx:id="groundPlaneClip" mnemonicParsing="false" text="Hide the ground plane in loaded chunks" />
</VBox>
</TitledPane>

<CheckBox fx:id="useProceduralWater" mnemonicParsing="false" text="Procedural water" />
<TitledPane fx:id="proceduralWaterDetailsPane" animated="false" text="Procedural water settings">
<VBox spacing="10.0">
Expand Down
Loading