From 3c5792091686adc323967063edd2450c40172214 Mon Sep 17 00:00:00 2001 From: Maik Marschner Date: Fri, 6 Oct 2023 21:24:42 +0200 Subject: [PATCH] Use reflection to make the plugin compatible with 2.4.x and recent 2.5.0 snapshots. (#41) --- .../chunky/denoiser/AlbedoRenderer.java | 9 ++- .../chunky/denoiser/ChunkyCompatHelper.java | 67 +++++++++++++++++++ .../chunky/denoiser/DenoiserTabImpl.java | 5 -- .../chunky/denoiser/NormalRenderer.java | 3 +- src/main/resources/plugin.json | 2 +- 5 files changed, 73 insertions(+), 13 deletions(-) create mode 100644 src/main/java/de/lemaik/chunky/denoiser/ChunkyCompatHelper.java diff --git a/src/main/java/de/lemaik/chunky/denoiser/AlbedoRenderer.java b/src/main/java/de/lemaik/chunky/denoiser/AlbedoRenderer.java index b40bb1c..056b9e1 100644 --- a/src/main/java/de/lemaik/chunky/denoiser/AlbedoRenderer.java +++ b/src/main/java/de/lemaik/chunky/denoiser/AlbedoRenderer.java @@ -1,12 +1,11 @@ package de.lemaik.chunky.denoiser; -import se.llbit.chunky.block.minecraft.Air; -import se.llbit.chunky.block.minecraft.Water; import se.llbit.chunky.renderer.PathTracingRenderer; import se.llbit.chunky.renderer.WorkerState; import se.llbit.chunky.renderer.scene.PreviewRayTracer; import se.llbit.chunky.renderer.scene.RayTracer; import se.llbit.chunky.renderer.scene.Scene; +import se.llbit.chunky.world.Material; import se.llbit.math.Ray; public class AlbedoRenderer extends PathTracingRenderer { @@ -24,9 +23,9 @@ private static class AlbedoTracer implements RayTracer { public void trace(Scene scene, WorkerState state) { Ray ray = state.ray; if (scene.isInWater(ray)) { - ray.setCurrentMaterial(Water.INSTANCE, 0); + ray.setCurrentMaterial(ChunkyCompatHelper.Water.INSTANCE, 0); } else { - ray.setCurrentMaterial(Air.INSTANCE, 0); + ray.setCurrentMaterial(ChunkyCompatHelper.Air.INSTANCE, 0); } while (true) { @@ -44,7 +43,7 @@ public void trace(Scene scene, WorkerState state) { break; } - if (ray.getCurrentMaterial() != Air.INSTANCE && ray.color.w > 0.0D) { + if (ray.getCurrentMaterial() != ChunkyCompatHelper.Air.INSTANCE && ray.color.w > 0.0D) { break; } diff --git a/src/main/java/de/lemaik/chunky/denoiser/ChunkyCompatHelper.java b/src/main/java/de/lemaik/chunky/denoiser/ChunkyCompatHelper.java new file mode 100644 index 0000000..dd49836 --- /dev/null +++ b/src/main/java/de/lemaik/chunky/denoiser/ChunkyCompatHelper.java @@ -0,0 +1,67 @@ +package de.lemaik.chunky.denoiser; + +import se.llbit.chunky.world.Material; +import se.llbit.math.Ray; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class ChunkyCompatHelper { + + private static Class getClass(String... classNameCandidates) { + for (String name : classNameCandidates) { + try { + return Class.forName(name); + } catch (ClassNotFoundException ignored) { + } + } + throw new RuntimeException("No class found for " + String.join(" or ", classNameCandidates)); + } + + private static Method getMethod(Class className, String methodName, Class... parameterTypes) { + try { + return className.getDeclaredMethod(methodName, parameterTypes); + } catch (NoSuchMethodException e) { + throw new RuntimeException("Could not invoke " + methodName + " on class " + className.getName(), e); + } + } + + public static class Air { + public static final Material INSTANCE; + + static { + try { + INSTANCE = (Material) ChunkyCompatHelper.getClass("se.llbit.chunky.block.Air", "se.llbit.chunky.block.minecraft.Air").getDeclaredField("INSTANCE").get(null); + } catch (IllegalAccessException | NoSuchFieldException e) { + throw new RuntimeException("Could not get Air.INSTANCE", e); + } + } + } + + public static class Water { + public static final Material INSTANCE; + + static { + try { + INSTANCE = (Material) ChunkyCompatHelper.getClass("se.llbit.chunky.block.Water", "se.llbit.chunky.block.minecraft.Water").getDeclaredField("INSTANCE").get(null); + } catch (IllegalAccessException | NoSuchFieldException e) { + throw new RuntimeException("Could not get Water.INSTANCE", e); + } + } + + private static Class water = ChunkyCompatHelper.getClass( + "se.llbit.chunky.model.WaterModel", + "se.llbit.chunky.model.minecraft.WaterModel" + ); + + private static Method doWaterDisplacementImpl = getMethod(water, "doWaterDisplacement", Ray.class); + + public static void doWaterDisplacement(Ray ray) { + try { + doWaterDisplacementImpl.invoke(null, ray); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException("Could not invoke doWaterDisplacement", e); + } + } + } +} diff --git a/src/main/java/de/lemaik/chunky/denoiser/DenoiserTabImpl.java b/src/main/java/de/lemaik/chunky/denoiser/DenoiserTabImpl.java index 6aa8f12..365b606 100644 --- a/src/main/java/de/lemaik/chunky/denoiser/DenoiserTabImpl.java +++ b/src/main/java/de/lemaik/chunky/denoiser/DenoiserTabImpl.java @@ -3,7 +3,6 @@ import javafx.fxml.FXMLLoader; import javafx.scene.Node; import se.llbit.chunky.renderer.scene.Scene; -import se.llbit.chunky.ui.controller.RenderControlsFxController; import se.llbit.chunky.ui.render.RenderControlsTab; import java.io.IOException; @@ -13,10 +12,6 @@ public class DenoiserTabImpl implements RenderControlsTab { public void update(Scene scene) { } - @Override - public void setController(RenderControlsFxController controller) { - } - @Override public String getTabTitle() { return "Denoiser"; diff --git a/src/main/java/de/lemaik/chunky/denoiser/NormalRenderer.java b/src/main/java/de/lemaik/chunky/denoiser/NormalRenderer.java index 5adc0ea..4d7b05b 100644 --- a/src/main/java/de/lemaik/chunky/denoiser/NormalRenderer.java +++ b/src/main/java/de/lemaik/chunky/denoiser/NormalRenderer.java @@ -1,6 +1,5 @@ package de.lemaik.chunky.denoiser; -import se.llbit.chunky.model.minecraft.WaterModel; import se.llbit.chunky.renderer.PathTracingRenderer; import se.llbit.chunky.renderer.WorkerState; import se.llbit.chunky.renderer.scene.PreviewRayTracer; @@ -32,7 +31,7 @@ public void trace(Scene scene, WorkerState state) { if (PreviewRayTracer.nextIntersection(scene, ray)) { if (BetterRenderManager.NORMAL_WATER_DISPLACEMENT && !scene.stillWaterEnabled() && ray.getCurrentMaterial().isWater()) { - WaterModel.doWaterDisplacement(ray); + ChunkyCompatHelper.Water.doWaterDisplacement(ray); } if (MAP_POSITIVE) { diff --git a/src/main/resources/plugin.json b/src/main/resources/plugin.json index 1f6eee8..6733c73 100644 --- a/src/main/resources/plugin.json +++ b/src/main/resources/plugin.json @@ -3,6 +3,6 @@ "author": "leMaik", "main": "de.lemaik.chunky.denoiser.DenoiserPlugin", "version": "0.4.1", - "targetVersion": "2.5.0", + "targetVersion": "2.4.0", "description": "Renders normal and albedo maps to pfm files for use with denoisers." }