From 37bcdf51fc7b3d1bbb4be0bbb1747c9deb0a339c Mon Sep 17 00:00:00 2001 From: Maik Marschner Date: Sun, 27 Oct 2024 18:48:07 +0100 Subject: [PATCH] Fix transparent sky not working with render regions. (#1790) --- .../chunky/renderer/scene/AlphaBuffer.java | 18 +++++++++++------- .../se/llbit/chunky/renderer/scene/Scene.java | 4 ++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/chunky/src/java/se/llbit/chunky/renderer/scene/AlphaBuffer.java b/chunky/src/java/se/llbit/chunky/renderer/scene/AlphaBuffer.java index 10198a784c..0562b8aab1 100644 --- a/chunky/src/java/se/llbit/chunky/renderer/scene/AlphaBuffer.java +++ b/chunky/src/java/se/llbit/chunky/renderer/scene/AlphaBuffer.java @@ -72,8 +72,12 @@ void computeAlpha(Scene scene, Type type, TaskTracker taskTracker) { try (TaskTracker.Task task = taskTracker.task("Computing alpha channel")) { this.type = type; + int cropX = scene.canvasConfig.getCropX(); + int cropY = scene.canvasConfig.getCropY(); int width = scene.canvasConfig.getWidth(); int height = scene.canvasConfig.getHeight(); + int fullWidth = scene.canvasConfig.getCropWidth(); + int fullHeight = scene.canvasConfig.getCropHeight(); buffer = ByteBuffer.allocate(scene.canvasConfig.getPixelCount() * type.byteSize); AtomicInteger done = new AtomicInteger(0); @@ -83,10 +87,10 @@ void computeAlpha(Scene scene, Type type, TaskTracker taskTracker) { state.ray = new Ray(); for (int y = 0; y < height; y++) { - computeAlpha(scene, x, y, width, height, state); + computeAlpha(scene, x, y, cropX, cropY, width, height, fullWidth, fullHeight, state); } - task.update(width, done.incrementAndGet()); + task.update(height, done.incrementAndGet()); }); }).get(); } catch (InterruptedException | ExecutionException e) { @@ -97,10 +101,10 @@ void computeAlpha(Scene scene, Type type, TaskTracker taskTracker) { /** * Compute the alpha channel based on sky visibility. */ - public void computeAlpha(Scene scene, int x, int y, int width, int height, WorkerState state) { + public void computeAlpha(Scene scene, int x, int y, int cropX, int cropY, int width, int height, int fullWidth, int fullHeight, WorkerState state) { Ray ray = state.ray; - double halfWidth = width / (2.0 * height); - double invHeight = 1.0 / height; + double halfWidth = fullWidth / (2.0 * fullHeight); + double invHeight = 1.0 / fullHeight; // Rotated grid supersampling. double[][] offsets = new double[][]{ @@ -113,8 +117,8 @@ public void computeAlpha(Scene scene, int x, int y, int width, int height, Worke double occlusion = 0.0; for (double[] offset : offsets) { scene.camera.calcViewRay(ray, - -halfWidth + (x + offset[0]) * invHeight, - -0.5 + (y + offset[1]) * invHeight); + -halfWidth + (x + offset[0] + cropX) * invHeight, + -0.5 + (y + offset[1] + cropY) * invHeight); ray.o.x -= scene.origin.x; ray.o.y -= scene.origin.y; ray.o.z -= scene.origin.z; diff --git a/chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java b/chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java index 4afb6c46ef..e2770c1cac 100644 --- a/chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java +++ b/chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java @@ -2060,9 +2060,9 @@ public synchronized void writeFrame(OutputStream out, PictureExportFormat mode, if (transparentSky) { if (mode.getTransparencyType() == AlphaBuffer.Type.UNSUPPORTED) { Log.warn("You selected \"transparent sky\", but the selected picture format \"" + mode.getName() + "\" does not support alpha layers.\nUse a different format like PNG instead."); - } - alphaBuffer.computeAlpha(this, mode.getTransparencyType(), taskTracker); } + alphaBuffer.computeAlpha(this, mode.getTransparencyType(), taskTracker); + } if (mode.wantsPostprocessing() && !finalized) { postProcessFrame(taskTracker); }