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

129 lines
5.2 KiB
Java
Raw Normal View History

2018-10-13 20:35:18 +02:00
package de.ellpeck.naturesaura.particles;
2020-01-21 23:02:39 +01:00
import com.mojang.blaze3d.platform.GlStateManager;
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.Tessellator;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
2020-01-24 17:05:41 +01:00
import net.minecraft.client.settings.ParticleStatus;
2020-01-21 23:02:39 +01:00
import net.minecraft.util.math.MathHelper;
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
}
}
public static void renderParticles(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();
float f = MathHelper.cos(info.getYaw() * ((float) Math.PI / 180F));
float f1 = MathHelper.sin(info.getYaw() * ((float) Math.PI / 180F));
float f2 = -f1 * MathHelper.sin(info.getPitch() * ((float) Math.PI / 180F));
float f3 = f * MathHelper.sin(info.getPitch() * ((float) Math.PI / 180F));
float f4 = MathHelper.cos(info.getPitch() * ((float) Math.PI / 180F));
Particle.interpPosX = info.getProjectedView().x;
Particle.interpPosY = info.getProjectedView().y;
Particle.interpPosZ = info.getProjectedView().z;
2018-10-13 20:35:18 +02:00
GlStateManager.pushMatrix();
2020-01-21 23:02:39 +01:00
GlStateManager.enableAlphaTest();
2018-10-13 20:35:18 +02:00
GlStateManager.enableBlend();
GlStateManager.alphaFunc(516, 0.003921569F);
GlStateManager.disableCull();
2019-02-19 17:23:03 +01:00
GlStateManager.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE);
2018-10-13 20:35:18 +02:00
GlStateManager.depthMask(false);
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-21 23:02:39 +01:00
particle.renderParticle(buffer, info, partialTicks, f, f4, f1, f2, f3);
2019-02-19 17:23:03 +01:00
tessy.draw();
2018-10-13 20:35:18 +02:00
2020-01-21 23:02:39 +01:00
GlStateManager.disableDepthTest();
2019-02-19 17:23:03 +01:00
buffer.begin(GL11.GL_QUADS, DefaultVertexFormats.PARTICLE_POSITION_TEX_COLOR_LMAP);
for (Particle particle : PARTICLES_NO_DEPTH)
2020-01-21 23:02:39 +01:00
particle.renderParticle(buffer, info, partialTicks, f, f4, f1, f2, f3);
2018-10-13 20:35:18 +02:00
tessy.draw();
2020-01-21 23:02:39 +01:00
GlStateManager.enableDepthTest();
2018-10-13 20:35:18 +02:00
GlStateManager.enableCull();
GlStateManager.depthMask(true);
GlStateManager.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA);
GlStateManager.disableBlend();
GlStateManager.alphaFunc(516, 0.1F);
GlStateManager.popMatrix();
}
}
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
}
}