Skip to content

Commit

Permalink
Uniforms: support shadowLightPosition
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbot16 committed Oct 24, 2020
1 parent 6aa7442 commit a77d6b4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/main/java/net/coderbot/iris/mixin/MixinWorldRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Slice;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.client.gl.GlProgramManager;
Expand All @@ -22,7 +23,10 @@
@Environment(EnvType.CLIENT)
public class MixinWorldRenderer {
private static final String RENDER = "render(Lnet/minecraft/client/util/math/MatrixStack;FJZLnet/minecraft/client/render/Camera;Lnet/minecraft/client/render/GameRenderer;Lnet/minecraft/client/render/LightmapTextureManager;Lnet/minecraft/util/math/Matrix4f;)V";
private static final String RENDER_SKY = "renderSky(Lnet/minecraft/client/util/math/MatrixStack;F)V";
private static final String PROFILER_SWAP = "Lnet/minecraft/util/profiler/Profiler;swap(Ljava/lang/String;)V";
private static final String POSITIVE_Y = "Lnet/minecraft/client/util/math/Vector3f;POSITIVE_Y:Lnet/minecraft/client/util/math/Vector3f;";
private static final String PEEK = "Lnet/minecraft/client/util/math/MatrixStack;peek()Lnet/minecraft/client/util/math/MatrixStack$Entry;";

@Inject(method = RENDER, at = @At(value = "INVOKE_STRING", target = PROFILER_SWAP, args = "ldc=terrain"))
private void setupTerrainShaders(MatrixStack matrices, float tickDelta, long limitTime, boolean renderBlockOutline, Camera camera, GameRenderer gameRenderer, LightmapTextureManager lightmapTextureManager, Matrix4f matrix4f, CallbackInfo callback) {
Expand All @@ -34,4 +38,11 @@ private void setupTerrainShaders(MatrixStack matrices, float tickDelta, long lim
private void stopUsingTerrainShaders(MatrixStack matrices, float tickDelta, long limitTime, boolean renderBlockOutline, Camera camera, GameRenderer gameRenderer, LightmapTextureManager lightmapTextureManager, Matrix4f matrix4f, CallbackInfo callback) {
GlProgramManager.useProgram(0);
}

@Inject(method = RENDER_SKY,
slice = @Slice(from = @At(value = "FIELD", target = POSITIVE_Y)),
at = @At(value = "INVOKE:FIRST", target = PEEK))
private void iris$renderSky$postCelestialRotate(MatrixStack matrices, float tickDelta, CallbackInfo callback) {
CapturedRenderingState.INSTANCE.setCelestialModelView(matrices.peek().getModel().copy());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ public class CapturedRenderingState {
private Matrix4f gbufferModelView;
private Matrix4f gbufferProjection;

/**
* The state of the modelview matrix right after the sky angle rotation has been applied
*/
private Matrix4f celestialModelView;

private CapturedRenderingState() {
}

Expand All @@ -26,4 +31,12 @@ public Matrix4f getGbufferProjection() {
public void setGbufferProjection(Matrix4f gbufferProjection) {
this.gbufferProjection = gbufferProjection;
}

public Matrix4f getCelestialModelView() {
return celestialModelView;
}

public void setCelestialModelView(Matrix4f celestialModelView) {
this.celestialModelView = celestialModelView;
}
}
32 changes: 30 additions & 2 deletions src/main/java/net/coderbot/iris/uniforms/Uniforms.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gl.GlProgram;
import net.minecraft.client.util.math.Vector3f;
import net.minecraft.client.util.math.Vector4f;
import net.minecraft.util.math.Matrix4f;
import net.minecraft.util.math.Vec3d;

Expand All @@ -22,6 +24,8 @@ public class Uniforms {

private int cameraPosition;

private int shadowLightPosition;

public Uniforms(GlProgram program) {
int programId = program.getProgramRef();

Expand All @@ -34,6 +38,8 @@ public Uniforms(GlProgram program) {
gbufferProjectionInverse = GL21.glGetUniformLocation(programId, "gbufferProjectionInverse");

cameraPosition = GL21.glGetUniformLocation(programId, "cameraPosition");

shadowLightPosition = GL21.glGetUniformLocation(programId, "shadowLightPosition");
}

public void update() {
Expand All @@ -46,8 +52,22 @@ public void update() {
updateMatrix(gbufferProjection, CapturedRenderingState.INSTANCE.getGbufferProjection());
updateMatrix(gbufferProjectionInverse, invertedCopy(CapturedRenderingState.INSTANCE.getGbufferProjection()));

Vec3d cameraPos = MinecraftClient.getInstance().gameRenderer.getCamera().getPos();
GL21.glUniform3f(cameraPosition, (float) cameraPos.x, (float) cameraPos.y, (float) cameraPos.z);
updateVector(cameraPosition, MinecraftClient.getInstance().gameRenderer.getCamera().getPos());

// TODO: Simplify this
Vector4f shadowLightPositionVector;

if (MinecraftClient.getInstance().world.isDay()) {
// Sun position
shadowLightPositionVector = new Vector4f(0.0F, 100.0F, 0.0F, 0.0F);
} else {
// Moon position
shadowLightPositionVector = new Vector4f(0.0F, -100.0F, 0.0F, 0.0F);
}

shadowLightPositionVector.transform(CapturedRenderingState.INSTANCE.getCelestialModelView());

updateVector(shadowLightPosition, new Vector3f(0.0F, 100.0F, 0.0F));
}

private void updateMatrix(int location, Matrix4f instance) {
Expand All @@ -59,6 +79,14 @@ private void updateMatrix(int location, Matrix4f instance) {
GL21.glUniformMatrix4fv(location, false, buffer);
}

private void updateVector(int location, Vec3d instance) {
GL21.glUniform3f(location, (float) instance.x, (float) instance.y, (float) instance.z);
}

private void updateVector(int location, Vector3f instance) {
GL21.glUniform3f(location, instance.getX(), instance.getY(), instance.getZ());
}

private Matrix4f invertedCopy(Matrix4f matrix) {
// PERF: Don't copy this matrix every time
Matrix4f copy = matrix.copy();
Expand Down

0 comments on commit a77d6b4

Please sign in to comment.