Skip to content

Commit

Permalink
Use reflection to make the plugin compatible with 2.4.x and recent 2.…
Browse files Browse the repository at this point in the history
…5.0 snapshots. (#41)
  • Loading branch information
leMaik committed Oct 7, 2023
1 parent 6c461ff commit 3c57920
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 13 deletions.
9 changes: 4 additions & 5 deletions src/main/java/de/lemaik/chunky/denoiser/AlbedoRenderer.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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) {
Expand All @@ -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;
}

Expand Down
67 changes: 67 additions & 0 deletions src/main/java/de/lemaik/chunky/denoiser/ChunkyCompatHelper.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
5 changes: 0 additions & 5 deletions src/main/java/de/lemaik/chunky/denoiser/DenoiserTabImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/de/lemaik/chunky/denoiser/NormalRenderer.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}

0 comments on commit 3c57920

Please sign in to comment.