Skip to content

Commit

Permalink
修复光影兼容性
Browse files Browse the repository at this point in the history
  • Loading branch information
ChloePrime committed Feb 28, 2024
1 parent 4bc326a commit 9a2634d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package mod.chloeprime.aaaparticles.client.render;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.logging.LogUtils;
import com.mojang.math.Matrix4f;
import mod.chloeprime.aaaparticles.api.client.effekseer.DeviceType;
import mod.chloeprime.aaaparticles.api.client.effekseer.Effekseer;
import mod.chloeprime.aaaparticles.client.loader.EffekAssetLoader;
import net.minecraft.client.Camera;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.RenderType;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.slf4j.Logger;

import java.nio.FloatBuffer;
import java.util.Optional;
Expand All @@ -26,7 +24,6 @@ public class EffekRenderer {

private static final FloatBuffer CAMERA_TRANSFORM_BUFFER = BufferUtils.createFloatBuffer(16);
private static final FloatBuffer PROJECTION_BUFFER = BufferUtils.createFloatBuffer(16);
private static final Logger LOGGER = LogUtils.getLogger();
private static final AtomicBoolean INIT = new AtomicBoolean();

public static void init() {
Expand All @@ -43,8 +40,8 @@ public static void init() {
}
}

public static void onRenderWorldLast(float partialTick, PoseStack pose, Matrix4f projection) {
draw(partialTick, pose, projection);
public static void onRenderWorldLast(float partialTick, PoseStack pose, Matrix4f projection, Camera camera) {
draw(partialTick, pose, projection, camera);

CAMERA_TRANSFORM_BUFFER.clear();
PROJECTION_BUFFER.clear();
Expand All @@ -54,18 +51,17 @@ public static void onRenderWorldLast(float partialTick, PoseStack pose, Matrix4f
private static final float[] CAMERA_TRANSFORM_DATA = new float[16];
private static final float[] PROJECTION_MATRIX_DATA = new float[16];

private static void draw(float partialTick, PoseStack pose, Matrix4f projection) {
private static void draw(float partialTick, PoseStack pose, Matrix4f projection, Camera camera) {
int w = MINECRAFT.getWindow().getWidth();
int h = MINECRAFT.getWindow().getHeight();
var camera = MINECRAFT.gameRenderer.getMainCamera().getPosition();

projection.store(PROJECTION_BUFFER);
transposeMatrix(PROJECTION_BUFFER);
PROJECTION_BUFFER.get(PROJECTION_MATRIX_DATA);

pose.pushPose();
{
pose.translate(-camera.x(), -camera.y(), -camera.z());
pose.translate(-camera.getPosition().x(), -camera.getPosition().y(), -camera.getPosition().z());

pose.last().pose().store(CAMERA_TRANSFORM_BUFFER);
transposeMatrix(CAMERA_TRANSFORM_BUFFER);
Expand All @@ -83,11 +79,6 @@ private static void draw(float partialTick, PoseStack pose, Matrix4f projection)
RenderType.PARTICLES_TARGET.clearRenderState();
}

public static void fixCloudDepth() {
Optional.ofNullable(MINECRAFT.levelRenderer.getCloudsTarget())
.ifPresent(rt -> MINECRAFT.getMainRenderTarget().copyDepthFrom(rt));
}

private static void transposeMatrix(FloatBuffer m) {
float m00, m01, m02, m03;
float m10, m11, m12, m13;
Expand Down Expand Up @@ -129,33 +120,6 @@ private static void transposeMatrix(FloatBuffer m) {
m.put(0xF , m33);
}

private static final FloatBuffer DEBUG_MATRIX = BufferUtils.createFloatBuffer(16);

private static void printMatrix(int glSource, FloatBuffer buf, String name) {
if (!bPrintMatrix) {
return;
}
GL11.glGetFloatv(glSource, buf);
transposeMatrix(buf);

StringBuilder sb = new StringBuilder(name).append("=\n");

float[] floats = new float[16];
buf.get(floats);

for (int i = 0; i < 4; i++) {
sb.append("|");
for (int j = 0; j < 4; j++) {
sb.append(floats[i * 4 + j]).append(", ");
}
sb.setLength(sb.length() - 2);
sb.append("|\n");
}

LOGGER.debug(sb.toString());
buf.clear();
}

private static float getDeltaTime() {
long last = lastDrawTime;
if (last == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public void onInitializeClient() {
AAAParticlesClient.init();
AAAParticlesClient.setup();
EffekRenderer.init();
WorldRenderEvents.LAST.register(
ctx -> EffekRenderer.onRenderWorldLast(ctx.tickDelta(), ctx.matrixStack(), ctx.projectionMatrix())
WorldRenderEvents.END.register(
ctx -> EffekRenderer.onRenderWorldLast(ctx.tickDelta(), ctx.matrixStack(), ctx.projectionMatrix(), ctx.camera())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public AAAParticlesForge() {
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> {
AAAParticlesClient.init();
MinecraftForge.EVENT_BUS.addListener(AAAParticlesForgeClient::onClientSetup);
MinecraftForge.EVENT_BUS.addListener(AAAParticlesForgeClient::onRenderStage);
});
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
package mod.chloeprime.aaaparticles.forge;

import mod.chloeprime.aaaparticles.client.AAAParticlesClient;
import mod.chloeprime.aaaparticles.client.render.EffekRenderer;
import net.minecraftforge.client.event.RenderLevelStageEvent;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;

public class AAAParticlesForgeClient {
static void onRenderStage(RenderLevelStageEvent e) {
if (e.getStage() == RenderLevelStageEvent.Stage.AFTER_WEATHER) {
EffekRenderer.onRenderWorldLast(e.getPartialTick(), e.getPoseStack(), e.getProjectionMatrix());
}
}

static void onClientSetup(FMLClientSetupEvent e) {
AAAParticlesClient.setup();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package mod.chloeprime.aaaparticles.forge.mixin.client;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Matrix4f;
import mod.chloeprime.aaaparticles.client.render.EffekRenderer;
import net.minecraft.client.Camera;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.renderer.LevelRenderer;
import net.minecraft.client.renderer.LightTexture;
import net.minecraftforge.client.MinecraftForgeClient;
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.callback.CallbackInfo;

@Mixin(LevelRenderer.class)
public class MixinLevelRenderer {
@Inject(method = "renderLevel", at = @At("RETURN"))
private void onRenderLevelLast(PoseStack pose, float g, long m, boolean bl, Camera arg2, GameRenderer arg3, LightTexture arg4, Matrix4f arg5, CallbackInfo ci) {
EffekRenderer.onRenderWorldLast(MinecraftForgeClient.getPartialTick(), pose, arg5, arg2);
}
}
1 change: 1 addition & 0 deletions forge/src/main/resources/aaa_particles.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"mixins": [
],
"client": [
"client.MixinLevelRenderer"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit 9a2634d

Please sign in to comment.