NaturesAura/src/main/java/de/ellpeck/naturesaura/particles/ParticleHandler.java

130 lines
5 KiB
Java
Raw Normal View History

2018-10-13 20:35:18 +02:00
package de.ellpeck.naturesaura.particles;
2020-01-28 18:08:56 +01:00
import com.mojang.blaze3d.matrix.MatrixStack;
2020-01-21 23:02:39 +01:00
import com.mojang.blaze3d.platform.GlStateManager;
2020-01-28 18:08:56 +01:00
import com.mojang.blaze3d.systems.RenderSystem;
2018-10-22 20:18:54 +02:00
import de.ellpeck.naturesaura.ModConfig;
2018-10-13 20:35:18 +02:00
import net.minecraft.client.Minecraft;
2020-01-21 23:02:39 +01:00
import net.minecraft.client.entity.player.ClientPlayerEntity;
2018-10-13 20:35:18 +02:00
import net.minecraft.client.particle.Particle;
import net.minecraft.client.renderer.ActiveRenderInfo;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.LightTexture;
2018-10-13 20:35:18 +02:00
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
2020-01-24 17:05:41 +01:00
import net.minecraft.client.settings.ParticleStatus;
2019-10-20 22:30:49 +02:00
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
2018-10-13 20:35:18 +02:00
import org.lwjgl.opengl.GL11;
import java.util.ArrayList;
import java.util.List;
2018-10-14 17:46:00 +02:00
import java.util.function.Supplier;
2018-10-13 20:35:18 +02:00
2019-10-20 22:30:49 +02:00
@OnlyIn(Dist.CLIENT)
2018-10-13 20:35:18 +02:00
public final class ParticleHandler {
2019-02-19 17:23:03 +01:00
public static boolean depthEnabled = true;
public static int range = 32;
2018-10-13 20:35:18 +02:00
private static final List<Particle> PARTICLES = new ArrayList<>();
2019-02-19 17:23:03 +01:00
private static final List<Particle> PARTICLES_NO_DEPTH = new ArrayList<>();
2018-10-13 20:35:18 +02:00
2019-02-19 17:23:03 +01:00
public static void spawnParticle(Supplier<Particle> particle, double x, double y, double z) {
2019-11-04 19:08:49 +01:00
if (Minecraft.getInstance().player.getDistanceSq(x, y, z) <= range * range) {
Minecraft mc = Minecraft.getInstance();
2020-01-24 17:05:41 +01:00
if (ModConfig.instance.respectVanillaParticleSettings.get()) {
ParticleStatus setting = mc.gameSettings.particles;
if (setting != ParticleStatus.ALL &&
(setting != ParticleStatus.DECREASED || mc.world.rand.nextInt(3) != 0) &&
(setting != ParticleStatus.MINIMAL || mc.world.rand.nextInt(10) != 0))
2018-10-22 20:18:54 +02:00
return;
2018-10-14 17:46:00 +02:00
}
2020-01-24 17:05:41 +01:00
double setting = ModConfig.instance.particleAmount.get();
2019-01-30 15:29:14 +01:00
if (setting < 1 && mc.world.rand.nextDouble() > setting)
2018-10-22 20:18:54 +02:00
return;
2019-02-19 17:23:03 +01:00
if (depthEnabled)
PARTICLES.add(particle.get());
else
PARTICLES_NO_DEPTH.add(particle.get());
2018-10-13 20:35:18 +02:00
}
}
public static void updateParticles() {
2019-02-19 17:23:03 +01:00
updateList(PARTICLES);
updateList(PARTICLES_NO_DEPTH);
depthEnabled = true;
range = 32;
2019-02-19 17:23:03 +01:00
}
private static void updateList(List<Particle> particles) {
for (int i = particles.size() - 1; i >= 0; i--) {
Particle particle = particles.get(i);
2019-11-04 19:08:49 +01:00
particle.tick();
2019-01-30 15:29:14 +01:00
if (!particle.isAlive())
2019-02-19 17:23:03 +01:00
particles.remove(i);
2018-10-13 20:35:18 +02:00
}
}
2020-01-28 18:08:56 +01:00
public static void renderParticles(MatrixStack stack, float partialTicks) {
2019-11-04 19:08:49 +01:00
Minecraft mc = Minecraft.getInstance();
2020-01-21 23:02:39 +01:00
ClientPlayerEntity player = mc.player;
2018-10-13 20:35:18 +02:00
if (player != null) {
2020-01-21 23:02:39 +01:00
ActiveRenderInfo info = mc.gameRenderer.getActiveRenderInfo();
LightTexture lightmap = mc.gameRenderer.getLightTexture();
2018-10-13 20:35:18 +02:00
2020-01-28 18:08:56 +01:00
RenderSystem.pushMatrix();
RenderSystem.multMatrix(stack.getLast().getPositionMatrix());
lightmap.enableLightmap();
2018-10-13 20:35:18 +02:00
2020-01-28 18:08:56 +01:00
RenderSystem.enableAlphaTest();
RenderSystem.enableBlend();
2020-01-28 18:08:56 +01:00
RenderSystem.alphaFunc(516, 0.003921569F);
RenderSystem.disableCull();
RenderSystem.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE);
RenderSystem.enableFog();
RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
2018-10-13 20:35:18 +02:00
RenderSystem.depthMask(false);
2018-10-13 20:35:18 +02:00
2019-01-30 15:29:14 +01:00
mc.getTextureManager().bindTexture(ParticleMagic.TEXTURE);
2018-10-13 20:35:18 +02:00
Tessellator tessy = Tessellator.getInstance();
BufferBuilder buffer = tessy.getBuffer();
buffer.begin(GL11.GL_QUADS, DefaultVertexFormats.PARTICLE_POSITION_TEX_COLOR_LMAP);
2019-01-30 15:29:14 +01:00
for (Particle particle : PARTICLES)
2020-01-28 18:08:56 +01:00
particle.renderParticle(buffer, info, partialTicks);
2019-02-19 17:23:03 +01:00
tessy.draw();
2018-10-13 20:35:18 +02:00
RenderSystem.disableDepthTest();
buffer.begin(GL11.GL_QUADS, DefaultVertexFormats.PARTICLE_POSITION_TEX_COLOR_LMAP);
2019-02-19 17:23:03 +01:00
for (Particle particle : PARTICLES_NO_DEPTH)
2020-01-28 18:08:56 +01:00
particle.renderParticle(buffer, info, partialTicks);
2018-10-13 20:35:18 +02:00
tessy.draw();
RenderSystem.enableDepthTest();
2018-10-13 20:35:18 +02:00
RenderSystem.enableCull();
RenderSystem.depthMask(true);
RenderSystem.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA);
RenderSystem.disableBlend();
2020-01-28 18:08:56 +01:00
RenderSystem.alphaFunc(516, 0.1F);
RenderSystem.disableFog();
2018-10-13 20:35:18 +02:00
lightmap.disableLightmap();
2020-01-28 18:08:56 +01:00
RenderSystem.popMatrix();
2018-10-13 20:35:18 +02:00
}
}
2019-02-19 17:23:03 +01:00
public static int getParticleAmount(boolean depth) {
return depth ? PARTICLES.size() : PARTICLES_NO_DEPTH.size();
2018-10-13 20:35:18 +02:00
}
public static void clearParticles() {
2019-02-19 17:23:03 +01:00
if (!PARTICLES.isEmpty())
2018-10-13 20:35:18 +02:00
PARTICLES.clear();
2019-02-19 17:23:03 +01:00
if (!PARTICLES_NO_DEPTH.isEmpty())
PARTICLES_NO_DEPTH.clear();
2018-10-13 20:35:18 +02:00
}
}