Skip to content

Commit

Permalink
Add volumetric cloud JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Peregrine05 committed Oct 14, 2023
1 parent c3a7721 commit 913b798
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions chunky/src/java/se/llbit/chunky/renderer/scene/Sky.java
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,13 @@ public void setSkyCacheResolution(int resolution) {
sky.add("cloudsEnabled", cloudsEnabled);
sky.add("cloudSize", cloudSize.toJson());
sky.add("cloudOffset", cloudOffset.toJson());
JsonObject cloudColorObj = new JsonObject();
cloudColorObj.add("red", cloudColor.x);
cloudColorObj.add("green", cloudColor.y);
cloudColorObj.add("blue", cloudColor.z);
sky.add("cloudColor", cloudColorObj);
sky.add("volumetricClouds", volumetricClouds);
sky.add("cloudDensity", cloudDensity);

// Always save gradient.
sky.add("gradient", gradientJson(gradient));
Expand Down Expand Up @@ -714,6 +721,16 @@ public void importFromJson(JsonObject json) {
if (json.get("cloudOffset").isObject()) {
cloudOffset.fromJson(json.get("cloudOffset").object());
}
if (json.get("cloudColor").isObject()) {
JsonObject colorObj = json.get("cloudColor").object();
cloudColor.x = colorObj.get("red").doubleValue(cloudColor.x);
cloudColor.y = colorObj.get("green").doubleValue(cloudColor.y);
cloudColor.z = colorObj.get("blue").doubleValue(cloudColor.z);
} else {
cloudColor.set(1, 1, 1);
}
volumetricClouds = json.get("volumetricClouds").boolValue(false);
cloudDensity = json.get("cloudDensity").doubleValue(cloudDensity);

if (json.get("gradient").isArray()) {
List<Vector4> theGradient = gradientFromJson(json.get("gradient").array());
Expand All @@ -724,9 +741,9 @@ public void importFromJson(JsonObject json) {

if (json.get("color").isObject()) {
JsonObject colorObj = json.get("color").object();
color.x = colorObj.get("red").doubleValue(1);
color.y = colorObj.get("green").doubleValue(1);
color.z = colorObj.get("blue").doubleValue(1);
color.x = colorObj.get("red").doubleValue(color.x);
color.y = colorObj.get("green").doubleValue(color.y);
color.z = colorObj.get("blue").doubleValue(color.z);
} else {
// Maintain backwards-compatibility with scenes saved in older Chunky versions
color.set(JsonUtil.vec3FromJsonArray(json.get("color")));
Expand Down Expand Up @@ -1109,7 +1126,7 @@ private Pair<Double, Double> getCloudDistance(Scene scene, Ray ray) {
}

public boolean cloudIntersection(Scene scene, Ray ray, Random random) {
if (random == null) {
if (random == null && volumetricClouds) {
return false;
}
Ray test = new Ray(ray);
Expand All @@ -1134,6 +1151,10 @@ public boolean cloudIntersection(Scene scene, Ray ray, Random random) {
if (testFirstIntersection + fogDistance < testSecondIntersection) {
t = testFirstIntersection + fogDistance;

if (t >= ray.t) {
return false;
}

// Set a random normal
Vector3 a1 = new Vector3();
a1.cross(ray.d, new Vector3(0, 1, 0));
Expand Down Expand Up @@ -1170,6 +1191,9 @@ public boolean cloudIntersection(Scene scene, Ray ray, Random random) {
}
} else {
t = firstIntersection;
if (t >= ray.t) {
return false;
}
ray.setNormal(test.getNormal());
if (secondIntersection == 1) {
ray.setCurrentMaterial(Air.INSTANCE);
Expand Down

0 comments on commit 913b798

Please sign in to comment.