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

128 lines
5 KiB
Java
Raw Normal View History

2018-10-13 20:35:18 +02:00
package de.ellpeck.naturesaura.particles;
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;
import net.minecraft.client.particle.Particle;
import net.minecraft.client.renderer.ActiveRenderInfo;
import net.minecraft.client.renderer.BufferBuilder;
2019-10-20 22:30:49 +02:00
import com.mojang.blaze3d.platform.GlStateManager;
2018-10-13 20:35:18 +02:00
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
2019-10-20 22:30:49 +02:00
import net.minecraft.entity.player.PlayerEntity;
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();
2018-10-22 20:18:54 +02:00
if (ModConfig.client.respectVanillaParticleSettings) {
2019-11-04 19:08:49 +01:00
int setting = mc.gameSettings.particles.func_216832_b();
2018-10-22 20:18:54 +02:00
if (setting != 0 &&
(setting != 1 || mc.world.rand.nextInt(3) != 0) &&
2019-01-30 15:29:14 +01:00
(setting != 2 || mc.world.rand.nextInt(10) != 0))
2018-10-22 20:18:54 +02:00
return;
2018-10-14 17:46:00 +02:00
}
2018-10-22 20:18:54 +02:00
double setting = ModConfig.client.particleAmount;
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();
2019-10-20 22:30:49 +02:00
PlayerEntity player = mc.player;
2018-10-13 20:35:18 +02:00
if (player != null) {
float x = ActiveRenderInfo.getRotationX();
float z = ActiveRenderInfo.getRotationZ();
float yz = ActiveRenderInfo.getRotationYZ();
float xy = ActiveRenderInfo.getRotationXY();
float xz = ActiveRenderInfo.getRotationXZ();
Particle.interpPosX = player.lastTickPosX + (player.posX - player.lastTickPosX) * partialTicks;
Particle.interpPosY = player.lastTickPosY + (player.posY - player.lastTickPosY) * partialTicks;
Particle.interpPosZ = player.lastTickPosZ + (player.posZ - player.lastTickPosZ) * partialTicks;
2019-11-04 19:08:49 +01:00
Particle.dir = player.getLook(partialTicks);
2018-10-13 20:35:18 +02:00
GlStateManager.pushMatrix();
2018-10-13 20:45:32 +02:00
GlStateManager.enableAlpha();
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)
2018-10-13 20:45:32 +02:00
particle.renderParticle(buffer, player, partialTicks, x, xz, z, yz, xy);
2019-02-19 17:23:03 +01:00
tessy.draw();
2018-10-13 20:35:18 +02:00
2019-02-19 17:23:03 +01:00
GlStateManager.disableDepth();
buffer.begin(GL11.GL_QUADS, DefaultVertexFormats.PARTICLE_POSITION_TEX_COLOR_LMAP);
for (Particle particle : PARTICLES_NO_DEPTH)
particle.renderParticle(buffer, player, partialTicks, x, xz, z, yz, xy);
2018-10-13 20:35:18 +02:00
tessy.draw();
2019-02-19 17:23:03 +01:00
GlStateManager.enableDepth();
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
}
}