Skip to content

Commit

Permalink
feat: fabulous lighting
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTJP committed Jun 24, 2023
1 parent 1b0726f commit 8e2f808
Show file tree
Hide file tree
Showing 30 changed files with 986 additions and 68 deletions.
4 changes: 3 additions & 1 deletion core/src/main/java/mrtjp/projectred/core/Configurator.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class Configurator {
public static boolean staticWires = true;
public static boolean staticGates = true;
public static int lightHaloMax = -1;
public static boolean fabulousLights = true;

/* World Gen */
public static boolean gen_MarbleCave = true;
Expand Down Expand Up @@ -67,7 +68,8 @@ private static void loadValues(ConfigCategory config) {
logicwires3D = rendering.getValue("gate_3d_wires").setDefaultBoolean(logicwires3D).setComment("If set to false, flat wire textures will be used for logic gates. Significant performance improvement").getBoolean();
staticWires = rendering.getValue("static_wire_renderer").setDefaultBoolean(staticWires).setComment("If set to false, wires will be rendered in the TESR rather than the WorldRenderer").getBoolean();
staticGates = rendering.getValue("static_gate_renderer").setDefaultBoolean(staticGates).setComment("If set to false, gates will be rendered in the TESR rather than the WorldRenderer").getBoolean();
lightHaloMax = rendering.getValue("max_light_halo_count").setDefaultInt(lightHaloMax).setComment("Number of lights to render, -1 for unlimited").getInt();
lightHaloMax = rendering.getValue("max_lights").setDefaultInt(lightHaloMax).setComment("Max lights on screen at a time, -1 for unlimited").getInt();
fabulousLights = rendering.getValue("fabulous_lights").setDefaultBoolean(fabulousLights).setComment("Use fabulous shader pipeline for lights when on Fabulous Graphics mode").getBoolean();

ConfigCategory gen = config.getCategory("world_gen").setComment("World gen settings");
gen_Ruby = gen.getValue("ruby_ore").setDefaultBoolean(gen_Ruby).setComment("Enable Ruby Ore generation").getBoolean();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package mrtjp.projectred.core.client;

import com.google.gson.JsonSyntaxException;
import com.mojang.blaze3d.pipeline.RenderTarget;
import com.mojang.math.Matrix4f;
import mrtjp.projectred.core.ProjectRedCore;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.PostChain;
import net.minecraft.client.renderer.PostPass;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
* Implementation of PostChain that allows intermediary targets to be different sizes vs the main target, and maintains
* their scale relative to main target. This allows use up-scaling/downscaling shaders:
* <p>
* 1. All passes will receive an ortho matrix scaled to its input target rather than always the main target.
* 2. Targets that need to maintain a fixed scale relative to main target can be registered via {@link FixedScalePostChain#addFixedTargetScale(String, double)}.
*/
public class FixedScalePostChain extends PostChain {

private final Map<String, Double> fixedRenderTargetScales = new HashMap<>();

public FixedScalePostChain(TextureManager textureManager, ResourceManager resourceManager, RenderTarget renderTarget, ResourceLocation location) throws IOException, JsonSyntaxException {
super(textureManager, resourceManager, renderTarget, location);
}

public FixedScalePostChain(ResourceLocation location) throws IOException {
this(Minecraft.getInstance().getTextureManager(),
Minecraft.getInstance().getResourceManager(),
Minecraft.getInstance().getMainRenderTarget(),
location);
}

public FixedScalePostChain(String modId, String filename) throws IOException {
this(new ResourceLocation(modId, "shaders/post/" + filename + ".json"));
}

/**
* Attach a fixed scale to a render target. It will be applied on resize
* @param name Name of render target defined in JSON
* @param scale Scale relative to main target
*/
public void addFixedTargetScale(String name, double scale) {
fixedRenderTargetScales.put(name, scale);
}

/**
* Utility to check and resize targets if screen is resized. Useful since as far as I know, there is no Forge "onScreenResized" event.
* Call this from the render thread before drawing into inputs
*/
public void resizeIfNeeded() {
if (screenWidth != screenTarget.width || screenHeight != screenTarget.height) {
resize(screenTarget.width, screenTarget.height);
}
}

@Override
public void resize(int newWidth, int newHeight) {
ProjectRedCore.LOGGER.debug("Resizing FixedScalePostChain from {}x{} to {}x{}", screenWidth, screenHeight, newWidth, newHeight);
super.resize(newWidth, newHeight);

// Resize targets with fixed scales
for (var entry : fixedRenderTargetScales.entrySet()) {
String name = entry.getKey();
Double scale = entry.getValue();

RenderTarget target = customRenderTargets.get(name);

int newTargetWidth = (int) Math.ceil(newWidth * scale);
int newTargetHeight = (int) Math.ceil(newHeight * scale);

// Resize the target
ProjectRedCore.LOGGER.debug("Resizing target {} from {}x{} to {}x{}", entry.getKey(), target.width, target.height, newTargetWidth, newTargetHeight);
target.resize(newTargetWidth, newTargetHeight, Minecraft.ON_OSX);
}

// Re-compute projection matrices scaled to each pass's input
for (PostPass pass : this.passes) {
Matrix4f orthoMatrix = Matrix4f.orthographic(0.0F, (float) pass.inTarget.width, (float) pass.inTarget.height, 0.0F, 0.1F, 1000.0F);
pass.setOrthoMatrix(orthoMatrix);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package mrtjp.projectred.core.client;

import com.mojang.blaze3d.pipeline.RenderTarget;

import java.io.IOException;

import static mrtjp.projectred.core.ProjectRedCore.MOD_ID;

public class HaloBloomPostChain extends FixedScalePostChain {

public HaloBloomPostChain() throws IOException {
super(MOD_ID, "halo_bloom");

// Post chain JSON description requires absolute target sizes. We want to use
// sizes relative to the main target. Scale is manually set here. Superclass
// implementation will take care of re-scaling on resize.
addFixedTargetScale("ds1", 0.5); // 1/2
addFixedTargetScale("ds2", 0.25); // 1/4
addFixedTargetScale("ds3", 0.125); // 1/8
addFixedTargetScale("ds4", 0.0625); // 1/16
}

public RenderTarget getInputTarget() {
return getTempTarget("bloom_in");
}
}
Loading

0 comments on commit 8e2f808

Please sign in to comment.