-
Notifications
You must be signed in to change notification settings - Fork 22
/
ForgeParticleEngineMixin.java
159 lines (144 loc) · 8.55 KB
/
ForgeParticleEngineMixin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.lowdragmc.shimmer.forge.core.mixins;
import com.google.common.collect.Maps;
import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.llamalad7.mixinextras.injector.ModifyReceiver;
import com.lowdragmc.shimmer.TracedGLState;
import com.lowdragmc.shimmer.client.postprocessing.IPostParticleType;
import com.lowdragmc.shimmer.client.postprocessing.PostProcessing;
import com.lowdragmc.shimmer.core.IParticleDescription;
import com.lowdragmc.shimmer.core.IParticleEngine;
import com.lowdragmc.shimmer.core.mixins.LightTextureAccessor;
import com.lowdragmc.shimmer.core.mixins.ShimmerMixinPlugin;
import com.lowdragmc.shimmer.platform.Services;
import com.mojang.blaze3d.pipeline.RenderTarget;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.Camera;
import net.minecraft.client.Minecraft;
import net.minecraft.client.particle.Particle;
import net.minecraft.client.particle.ParticleDescription;
import net.minecraft.client.particle.ParticleEngine;
import net.minecraft.client.particle.ParticleRenderType;
import net.minecraft.client.renderer.LightTexture;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.core.Registry;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.PreparableReloadListener;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.util.profiling.ProfilerFiller;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import javax.annotation.Nullable;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
/**
* @author KilaBash
* @date 2022/05/02
* @implNote ParticleEngineMixin, inject particle postprocessing
*/
@Mixin(ParticleEngine.class)
public abstract class ForgeParticleEngineMixin implements IParticleEngine {
@Shadow
@Nullable
protected abstract <T extends ParticleOptions> Particle makeParticle(T pParticleData, double pX, double pY, double pZ, double pXSpeed, double pYSpeed, double pZSpeed);
@Shadow
public abstract void add(Particle pEffect);
@Shadow
@Final
private Map<ParticleRenderType, Queue<Particle>> particles;
private final Map<ResourceLocation, String> PARTICLE_EFFECT = Maps.newHashMap();
@Nullable
@Override
public Particle createPostParticle(PostProcessing postProcessing, ParticleOptions pParticleData, double pX, double pY, double pZ, double pXSpeed, double pYSpeed, double pZSpeed) {
Particle particle = makeParticle(pParticleData, pX, pY, pZ, pXSpeed, pYSpeed, pZSpeed);
if (particle != null) {
particle = Services.PLATFORM.createPostParticle(particle, postProcessing);
add(particle);
return particle;
} else {
return null;
}
}
@ModifyReceiver(method = "render(Lcom/mojang/blaze3d/vertex/PoseStack;Lnet/minecraft/client/renderer/MultiBufferSource$BufferSource;Lnet/minecraft/client/renderer/LightTexture;Lnet/minecraft/client/Camera;FLnet/minecraft/client/renderer/culling/Frustum;)V",
at = @At(value = "INVOKE",
target = "Lnet/minecraft/client/particle/ParticleRenderType;begin(Lcom/mojang/blaze3d/vertex/BufferBuilder;Lnet/minecraft/client/renderer/texture/TextureManager;)V"))
private ParticleRenderType injectRenderPre(ParticleRenderType particlerendertype, BufferBuilder bufferBuilder, TextureManager textureManager) {
if (particlerendertype instanceof IPostParticleType && this.particles.get(particlerendertype).size() > 0) {
PostProcessing postProcessing = ((IPostParticleType) particlerendertype).getPost();
postProcessing.getPostTarget(false).bindWrite(false);
postProcessing.hasParticle();
shimmer$turnOnLightTextureIfNeeded();
}
return particlerendertype;
}
/**
* {@link ParticleRenderType#CUSTOM} can implementation custom render logical not defined
* for example {@link net.minecraft.client.particle.ItemPickupParticle}
* where {@link net.minecraft.client.renderer.RenderType#entityShadow(ResourceLocation)} is used
* it will use {@link net.minecraft.client.renderer.RenderStateShard.LightmapStateShard}
* which will call {@link LightTexture#turnOffLightLayer()}, so we need turn on it if needed
*/
@Unique
private void shimmer$turnOnLightTextureIfNeeded() {
int currentLightTextureSlotID = RenderSystem.getShaderTexture(2);
LightTexture lightTexture = Minecraft.getInstance().gameRenderer.lightTexture();
int lightTextureID = ((LightTextureAccessor) lightTexture).getLightTexture().getId();
if (currentLightTextureSlotID != lightTextureID) {
lightTexture.turnOnLightLayer();
}
}
@Inject(method = "render(Lcom/mojang/blaze3d/vertex/PoseStack;Lnet/minecraft/client/renderer/MultiBufferSource$BufferSource;Lnet/minecraft/client/renderer/LightTexture;Lnet/minecraft/client/Camera;FLnet/minecraft/client/renderer/culling/Frustum;)V",
at = @At(value = "INVOKE",
target = "Lnet/minecraft/client/particle/ParticleRenderType;end(Lcom/mojang/blaze3d/vertex/Tesselator;)V",
shift = At.Shift.AFTER))
private void injectRenderPost(CallbackInfo ci) {
RenderTarget mainRenderTarget = Minecraft.getInstance().getMainRenderTarget();
if (TracedGLState.bindFrameBuffer != mainRenderTarget.frameBufferId){
mainRenderTarget.bindWrite(false);
}
}
@Inject(method = "render(Lcom/mojang/blaze3d/vertex/PoseStack;Lnet/minecraft/client/renderer/MultiBufferSource$BufferSource;Lnet/minecraft/client/renderer/LightTexture;Lnet/minecraft/client/Camera;FLnet/minecraft/client/renderer/culling/Frustum;)V",
at = @At(value = "RETURN"), remap = false)
private void injectRenderReturn(PoseStack pMatrixStack, MultiBufferSource.BufferSource pBuffer, LightTexture pLightTexture, Camera pActiveRenderInfo, float pPartialTicks, net.minecraft.client.renderer.culling.Frustum clippingHelper, CallbackInfo ci) {
for (PostProcessing postProcessing : PostProcessing.values()) {
postProcessing.renderParticlePost();
}
}
@ModifyExpressionValue(method = "loadParticleDescription",
at= @At(value = "INVOKE", target = "Lnet/minecraft/client/particle/ParticleDescription;fromJson(Lcom/google/gson/JsonObject;)Lnet/minecraft/client/particle/ParticleDescription;"))
private ParticleDescription injectLoad(ParticleDescription particleDescription,ResourceManager manager, ResourceLocation registryName){
if (particleDescription instanceof IParticleDescription description && description.getEffect() != null) {
PARTICLE_EFFECT.put(registryName, description.getEffect());
}
return particleDescription;
}
@Inject(method = "reload", at = @At(value = "HEAD"))
private void injectReload(PreparableReloadListener.PreparationBarrier $$0, ResourceManager $$1, ProfilerFiller $$2, ProfilerFiller $$3, Executor $$4, Executor $$5, CallbackInfoReturnable<CompletableFuture<Void>> cir) {
PARTICLE_EFFECT.clear();
}
@Inject(method = "createParticle", at = @At(value = "HEAD"), cancellable = true)
private void injectCreateParticle(ParticleOptions particleOptions, double x, double y, double z, double sx, double sy, double sz, CallbackInfoReturnable<Particle> cir) {
ResourceLocation name = Registry.PARTICLE_TYPE.getKey(particleOptions.getType());
if (!ShimmerMixinPlugin.IS_OPT_LOAD) {
if (PARTICLE_EFFECT.containsKey(name)) {
PostProcessing postProcessing = PostProcessing.getPost(PARTICLE_EFFECT.get(name));
if (postProcessing != null) {
cir.setReturnValue(createPostParticle(postProcessing, particleOptions, x, y, z, sx, sy, sz));
}
} else if (PostProcessing.BLOOM_PARTICLE.contains(name)) {
cir.setReturnValue(createPostParticle(PostProcessing.getBlockBloom(), particleOptions, x, y, z, sx, sy, sz));
}
}
}
}