Skip to content

Commit

Permalink
Improve shadow projection code
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Oct 29, 2024
1 parent d846ff8 commit f9c974b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void clearPipeline() {
}

public int getDepthTex() {
if (compatInternalInstance == null) return -1;
if (compatInternalInstance == null) return 0;

try {
return (int) getDepthTex.invoke(compatInternalInstance);
Expand All @@ -161,7 +161,7 @@ public int getDepthTex() {
}

public int getDepthTexNoTranslucent() {
if (compatInternalInstance == null) return -1;
if (compatInternalInstance == null) return 0;

try {
return (int) getDepthTexNoTranslucent.invoke(compatInternalInstance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.irisshaders.iris.gl.texture.InternalTextureFormat;
import net.irisshaders.iris.helpers.OptionalBoolean;
import net.irisshaders.iris.shaderpack.parsing.DirectiveHolder;
import net.irisshaders.iris.shadows.ShadowMatrices;
import org.joml.Vector4f;

import java.util.Optional;
Expand Down Expand Up @@ -60,8 +61,8 @@ public PackShadowDirectives(ShaderProperties properties) {
// shadowRenderDistanceMul to a nonzero value, since having a high shadow render distance will impact
// performance quite heavily on most systems.
this.distance = 160.0f;
this.nearPlane = 0.05f;
this.farPlane = 256.0f;
this.nearPlane = ShadowMatrices.NEAR;
this.farPlane = ShadowMatrices.FAR;
this.voxelDistance = 0.0f;

// By default, shadows are not culled based on distance from the player. However, pack authors may
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,12 @@
import org.joml.Matrix4f;

public class ShadowMatrices {
private static final float NEAR = 0.05f;
private static final float FAR = 256.0f;

// NB: These matrices are in column-major order, not row-major order like what you'd expect!
public static final float NEAR = -100.05f;
public static final float FAR = 156.0f;

public static Matrix4f createOrthoMatrix(float halfPlaneLength, float nearPlane, float farPlane) {
return new Matrix4f(
// column 1
1.0f / halfPlaneLength, 0f, 0f, 0f,
// column 2
0f, 1.0f / halfPlaneLength, 0f, 0f,
// column 3
0f, 0f, 2.0f / (nearPlane - farPlane), 0f,
// column 4
0f, 0f, -(farPlane + nearPlane) / (farPlane - nearPlane), 1f
);
//System.out.println("making a matrix with " + nearPlane + " / " + farPlane + " * " + halfPlaneLength);
return new Matrix4f().setOrthoSymmetric(halfPlaneLength, halfPlaneLength, nearPlane, farPlane);
}

public static Matrix4f createPerspectiveMatrix(float fov) {
Expand All @@ -38,7 +28,7 @@ public static Matrix4f createPerspectiveMatrix(float fov) {
);
}

public static void createBaselineModelViewMatrix(PoseStack target, float shadowAngle, float sunPathRotation) {
public static void createBaselineModelViewMatrix(PoseStack target, float shadowAngle, float sunPathRotation, float nearPlane, float farPlane) {
float skyAngle;

if (shadowAngle < 0.25f) {
Expand All @@ -50,7 +40,6 @@ public static void createBaselineModelViewMatrix(PoseStack target, float shadowA
target.last().normal().identity();
target.last().pose().identity();

target.last().pose().translate(0.0f, 0.0f, -100.0f);
target.mulPose(Axis.XP.rotationDegrees(90.0F));
target.mulPose(Axis.ZP.rotationDegrees(skyAngle * -360.0f));
target.mulPose(Axis.XP.rotationDegrees(sunPathRotation));
Expand Down Expand Up @@ -88,8 +77,8 @@ public static void snapModelViewToGrid(PoseStack target, float shadowIntervalSiz
}

public static void createModelViewMatrix(PoseStack target, float shadowAngle, float shadowIntervalSize,
float sunPathRotation, double cameraX, double cameraY, double cameraZ) {
createBaselineModelViewMatrix(target, shadowAngle, sunPathRotation);
float sunPathRotation, double cameraX, double cameraY, double cameraZ, float nearPlane, float farPlane) {
createBaselineModelViewMatrix(target, shadowAngle, sunPathRotation, nearPlane, farPlane);
snapModelViewToGrid(target, shadowIntervalSize, cameraX, cameraY, cameraZ);
}

Expand Down Expand Up @@ -155,7 +144,7 @@ public static void main(String[] args) {
// When DayTime=0, skyAngle = 282 degrees.
// Thus, sunAngle = shadowAngle = 0.03451777f
createModelViewMatrix(modelView, 0.03451777f, 2.0f,
0.0f, 0.646045982837677f, 82.53274536132812f, -514.0264282226562f);
0.0f, 0.646045982837677f, 82.53274536132812f, -514.0264282226562f, NEAR, FAR);

test("model view at dawn", expectedModelViewAtDawn, modelView.last().pose());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public ShadowRenderer(IrisRenderingPipeline pipeline, ProgramSource shadow, Pack
configureSamplingSettings(shadowDirectives);
}

public static PoseStack createShadowModelView(float sunPathRotation, float intervalSize) {
public static PoseStack createShadowModelView(float sunPathRotation, float intervalSize, float nearPlane, float farPlane) {
// Determine the camera position
Vector3d cameraPos = CameraUniforms.getUnshiftedCameraPosition();

Expand All @@ -164,7 +164,7 @@ public static PoseStack createShadowModelView(float sunPathRotation, float inter

// Set up our modelview matrix stack
PoseStack modelView = new PoseStack();
ShadowMatrices.createModelViewMatrix(modelView, getShadowAngle(), intervalSize, sunPathRotation, cameraX, cameraY, cameraZ);
ShadowMatrices.createModelViewMatrix(modelView, getShadowAngle(), intervalSize, sunPathRotation, cameraX, cameraY, cameraZ, nearPlane, farPlane);

return modelView;
}
Expand Down Expand Up @@ -384,7 +384,7 @@ public void renderShadows(LevelRendererAccessor levelRenderer, Camera playerCame
setupShadowViewport();

// Create our camera
PoseStack modelView = createShadowModelView(this.sunPathRotation, this.intervalSize);
PoseStack modelView = createShadowModelView(this.sunPathRotation, this.intervalSize, nearPlane, farPlane);
MODELVIEW = new Matrix4f(modelView.last().pose());

// Set up our orthographic projection matrix and load it into RenderSystem
Expand All @@ -393,7 +393,7 @@ public void renderShadows(LevelRendererAccessor levelRenderer, Camera playerCame
// If FOV is not null, the pack wants a perspective based projection matrix. (This is to support legacy packs)
shadowProjection = ShadowMatrices.createPerspectiveMatrix(this.fov);
} else {
shadowProjection = ShadowMatrices.createOrthoMatrix(halfPlaneLength, nearPlane < 0 ? -DHCompat.getRenderDistance() : nearPlane, farPlane < 0 ? DHCompat.getRenderDistance() : farPlane);
shadowProjection = ShadowMatrices.createOrthoMatrix(halfPlaneLength, nearPlane < 0 ? -DHCompat.getRenderDistance() * 16 : nearPlane, farPlane < 0 ? DHCompat.getRenderDistance() * 16 : farPlane);
}

IrisRenderSystem.setShadowProjection(shadowProjection);
Expand Down Expand Up @@ -703,6 +703,7 @@ public void addDebugText(List<String> messages) {
messages.add("[" + Iris.MODNAME + "] Shadow Maps: " + debugStringOverall);
messages.add("[" + Iris.MODNAME + "] Shadow Distance Terrain: " + terrainFrustumHolder.getDistanceInfo() + " Entity: " + entityFrustumHolder.getDistanceInfo());
messages.add("[" + Iris.MODNAME + "] Shadow Culling Terrain: " + terrainFrustumHolder.getCullingInfo() + " Entity: " + entityFrustumHolder.getCullingInfo());
messages.add("[" + Iris.MODNAME + "] Shadow Projection: " + getProjectionInfo());
messages.add("[" + Iris.MODNAME + "] Shadow Terrain: " + debugStringTerrain
+ (shouldRenderTerrain ? "" : " (no terrain) ") + (shouldRenderTranslucent ? "" : "(no translucent)"));
messages.add("[" + Iris.MODNAME + "] Shadow Entities: " + getEntitiesDebugString());
Expand All @@ -718,6 +719,10 @@ public void addDebugText(List<String> messages) {
}
}

private String getProjectionInfo() {
return "Near: " + nearPlane + " Far: " + farPlane + " distance " + halfPlaneLength;
}

private String getEntitiesDebugString() {
return (shouldRenderEntities || shouldRenderPlayer) ? (renderedShadowEntities + "/" + Minecraft.getInstance().level.getEntityCount()) : "disabled by pack";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public static void addMatrixUniforms(UniformHolder uniforms, PackDirectives dire
addMatrix(uniforms, "Projection", CapturedRenderingState.INSTANCE::getGbufferProjection);
addDHMatrix(uniforms, "Projection", DHCompat::getProjection);
addShadowMatrix(uniforms, "ModelView", () ->
new Matrix4f(ShadowRenderer.createShadowModelView(directives.getSunPathRotation(), directives.getShadowDirectives().getIntervalSize()).last().pose()));
new Matrix4f(ShadowRenderer.createShadowModelView(directives.getSunPathRotation(), directives.getShadowDirectives().getIntervalSize(), directives.getShadowDirectives().getNearPlane(), directives.getShadowDirectives().getFarPlane()).last().pose()));
addShadowMatrix(uniforms, "Projection", () -> ShadowMatrices.createOrthoMatrix(directives.getShadowDirectives().getDistance(),
directives.getShadowDirectives().getNearPlane() < 0 ? -DHCompat.getRenderDistance() : directives.getShadowDirectives().getNearPlane(),
directives.getShadowDirectives().getFarPlane() < 0 ? DHCompat.getRenderDistance() : directives.getShadowDirectives().getFarPlane()));
directives.getShadowDirectives().getNearPlane() < 0 ? -DHCompat.getRenderDistance() * 16 : directives.getShadowDirectives().getNearPlane(),
directives.getShadowDirectives().getFarPlane() < 0 ? DHCompat.getRenderDistance() * 16 : directives.getShadowDirectives().getFarPlane()));
}

private static void addMatrix(UniformHolder uniforms, String name, Supplier<Matrix4fc> supplier) {
Expand Down

0 comments on commit f9c974b

Please sign in to comment.