From 913b798b6031d7cdc327807225987d66d3f3dcc9 Mon Sep 17 00:00:00 2001 From: Peregrine05 <92183530+Peregrine05@users.noreply.github.com> Date: Sat, 14 Oct 2023 11:09:31 +0200 Subject: [PATCH] Add volumetric cloud JSON --- .../se/llbit/chunky/renderer/scene/Sky.java | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/chunky/src/java/se/llbit/chunky/renderer/scene/Sky.java b/chunky/src/java/se/llbit/chunky/renderer/scene/Sky.java index 49987d9593..1d32fe9cec 100644 --- a/chunky/src/java/se/llbit/chunky/renderer/scene/Sky.java +++ b/chunky/src/java/se/llbit/chunky/renderer/scene/Sky.java @@ -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)); @@ -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 theGradient = gradientFromJson(json.get("gradient").array()); @@ -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"))); @@ -1109,7 +1126,7 @@ private Pair 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); @@ -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)); @@ -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);