-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
986 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
core/src/main/java/mrtjp/projectred/core/client/FixedScalePostChain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
core/src/main/java/mrtjp/projectred/core/client/HaloBloomPostChain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
Oops, something went wrong.