From 52541aa195b549b28fa5652807f000b191d579ac Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Fri, 4 Dec 2020 14:15:34 +0100 Subject: [PATCH] switch to the vanilla particle system Probably closes #165 --- .../naturesaura/events/ClientEvents.java | 12 +- .../particles/ParticleHandler.java | 147 +++++++----------- .../naturesaura/particles/ParticleMagic.java | 7 +- .../naturesaura/proxy/ClientProxy.java | 5 +- 4 files changed, 61 insertions(+), 110 deletions(-) diff --git a/src/main/java/de/ellpeck/naturesaura/events/ClientEvents.java b/src/main/java/de/ellpeck/naturesaura/events/ClientEvents.java index f8aec773..02006b9b 100644 --- a/src/main/java/de/ellpeck/naturesaura/events/ClientEvents.java +++ b/src/main/java/de/ellpeck/naturesaura/events/ClientEvents.java @@ -81,13 +81,8 @@ public class ClientEvents { if (mc.gameSettings.showDebugInfo && ModConfig.instance.debugText.get()) { String prefix = TextFormatting.GREEN + "[" + NaturesAura.MOD_NAME + "]" + TextFormatting.RESET + " "; List left = event.getLeft(); - left.add(""); - - int depth = ParticleHandler.getParticleAmount(true); - int noDepth = ParticleHandler.getParticleAmount(false); - left.add(prefix + "P: " + (depth + noDepth) + " (D: " + depth + " nD: " + noDepth + ")"); - if (mc.player.isCreative()) { + left.add(""); MutableInt amount = new MutableInt(IAuraChunk.DEFAULT_AURA); MutableInt spots = new MutableInt(); IAuraChunk.getSpotsInArea(mc.world, mc.player.getPosition(), 35, (blockPos, drainSpot) -> { @@ -110,7 +105,6 @@ public class ClientEvents { Minecraft mc = Minecraft.getInstance(); if (mc.world == null) { - ParticleHandler.clearParticles(); ItemRangeVisualizer.clear(); PENDING_AURA_CHUNKS.clear(); } else { @@ -161,8 +155,6 @@ public class ClientEvents { inst.setParticleSpawnRange(32); } - ParticleHandler.updateParticles(); - heldCache = Helper.getEquippedItem(s -> s.getItem() instanceof ItemAuraCache, mc.player); heldEye = Helper.getEquippedItem(s -> s.getItem() == ModItems.EYE, mc.player); heldOcular = Helper.getEquippedItem(s -> s.getItem() == ModItems.EYE_IMPROVED, mc.player); @@ -180,8 +172,6 @@ public class ClientEvents { @SubscribeEvent public void onWorldRender(RenderWorldLastEvent event) { Minecraft mc = Minecraft.getInstance(); - MatrixStack stack = event.getMatrixStack(); - ParticleHandler.renderParticles(event.getMatrixStack(), mc.getRenderPartialTicks()); RenderSystem.pushMatrix(); RenderSystem.multMatrix(event.getMatrixStack().getLast().getMatrix()); diff --git a/src/main/java/de/ellpeck/naturesaura/particles/ParticleHandler.java b/src/main/java/de/ellpeck/naturesaura/particles/ParticleHandler.java index c9a39053..2b0ff32a 100644 --- a/src/main/java/de/ellpeck/naturesaura/particles/ParticleHandler.java +++ b/src/main/java/de/ellpeck/naturesaura/particles/ParticleHandler.java @@ -1,39 +1,73 @@ package de.ellpeck.naturesaura.particles; -import com.mojang.blaze3d.matrix.MatrixStack; import com.mojang.blaze3d.platform.GlStateManager; import com.mojang.blaze3d.systems.RenderSystem; import de.ellpeck.naturesaura.ModConfig; +import de.ellpeck.naturesaura.NaturesAura; import net.minecraft.client.Minecraft; -import net.minecraft.client.entity.player.ClientPlayerEntity; +import net.minecraft.client.particle.IParticleRenderType; import net.minecraft.client.particle.Particle; -import net.minecraft.client.renderer.ActiveRenderInfo; import net.minecraft.client.renderer.BufferBuilder; -import net.minecraft.client.renderer.LightTexture; import net.minecraft.client.renderer.Tessellator; +import net.minecraft.client.renderer.texture.TextureManager; import net.minecraft.client.renderer.vertex.DefaultVertexFormats; import net.minecraft.client.settings.ParticleStatus; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; import org.lwjgl.opengl.GL11; -import java.util.ArrayList; -import java.util.List; +import java.util.Locale; import java.util.function.Supplier; @OnlyIn(Dist.CLIENT) public final class ParticleHandler { - private static final List PARTICLES = new ArrayList<>(); - private static final List PARTICLES_NO_DEPTH = new ArrayList<>(); + public static final IParticleRenderType MAGIC = new IParticleRenderType() { + @Override + public void beginRender(BufferBuilder buffer, TextureManager textureManager) { + setupRendering(textureManager); + RenderSystem.enableDepthTest(); + buffer.begin(GL11.GL_QUADS, DefaultVertexFormats.PARTICLE_POSITION_TEX_COLOR_LMAP); + } + + @Override + public void finishRender(Tessellator tessellator) { + tessellator.draw(); + } + + @Override + public String toString() { + return NaturesAura.MOD_ID.toUpperCase(Locale.ROOT) + "_MAGIC"; + } + }; + + public static final IParticleRenderType MAGIC_NO_DEPTH = new IParticleRenderType() { + @Override + public void beginRender(BufferBuilder buffer, TextureManager textureManager) { + setupRendering(textureManager); + RenderSystem.disableDepthTest(); + buffer.begin(GL11.GL_QUADS, DefaultVertexFormats.PARTICLE_POSITION_TEX_COLOR_LMAP); + } + + @Override + public void finishRender(Tessellator tessellator) { + tessellator.draw(); + } + + @Override + public String toString() { + return NaturesAura.MOD_ID.toUpperCase(Locale.ROOT) + "_MAGIC_NO_DEPTH"; + } + }; + public static boolean depthEnabled = true; public static int range = 32; public static boolean culling = true; public static void spawnParticle(Supplier particle, double x, double y, double z) { - if (Minecraft.getInstance().player.getDistanceSq(x, y, z) <= range * range) { + Minecraft mc = Minecraft.getInstance(); + if (mc.player.getDistanceSq(x, y, z) <= range * range) { if (culling) { - Minecraft mc = Minecraft.getInstance(); if (ModConfig.instance.respectVanillaParticleSettings.get()) { ParticleStatus setting = mc.gameSettings.particles; if (setting != ParticleStatus.ALL && @@ -45,90 +79,19 @@ public final class ParticleHandler { if (setting < 1 && mc.world.rand.nextDouble() > setting) return; } - - if (depthEnabled) - PARTICLES.add(particle.get()); - else - PARTICLES_NO_DEPTH.add(particle.get()); + mc.particles.addEffect(particle.get()); } } - public static void updateParticles() { - updateList(PARTICLES); - updateList(PARTICLES_NO_DEPTH); - - depthEnabled = true; - range = 32; - culling = true; - } - - private static void updateList(List particles) { - for (int i = particles.size() - 1; i >= 0; i--) { - Particle particle = particles.get(i); - particle.tick(); - if (!particle.isAlive()) - particles.remove(i); - } - } - - public static void renderParticles(MatrixStack stack, float partialTicks) { - Minecraft mc = Minecraft.getInstance(); - ClientPlayerEntity player = mc.player; - - if (player != null) { - ActiveRenderInfo info = mc.gameRenderer.getActiveRenderInfo(); - LightTexture lightmap = mc.gameRenderer.getLightTexture(); - - RenderSystem.pushMatrix(); - RenderSystem.multMatrix(stack.getLast().getMatrix()); - lightmap.enableLightmap(); - - RenderSystem.enableAlphaTest(); - RenderSystem.enableBlend(); - 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); - - RenderSystem.depthMask(false); - - mc.getTextureManager().bindTexture(ParticleMagic.TEXTURE); - Tessellator tessy = Tessellator.getInstance(); - BufferBuilder buffer = tessy.getBuffer(); - - RenderSystem.enableDepthTest(); - buffer.begin(GL11.GL_QUADS, DefaultVertexFormats.PARTICLE_POSITION_TEX_COLOR_LMAP); - for (Particle particle : PARTICLES) - particle.renderParticle(buffer, info, partialTicks); - tessy.draw(); - RenderSystem.disableDepthTest(); - buffer.begin(GL11.GL_QUADS, DefaultVertexFormats.PARTICLE_POSITION_TEX_COLOR_LMAP); - for (Particle particle : PARTICLES_NO_DEPTH) - particle.renderParticle(buffer, info, partialTicks); - tessy.draw(); - RenderSystem.enableDepthTest(); - - RenderSystem.enableCull(); - RenderSystem.depthMask(true); - RenderSystem.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA); - RenderSystem.disableBlend(); - RenderSystem.alphaFunc(516, 0.1F); - RenderSystem.disableFog(); - - lightmap.disableLightmap(); - RenderSystem.popMatrix(); - } - } - - public static int getParticleAmount(boolean depth) { - return depth ? PARTICLES.size() : PARTICLES_NO_DEPTH.size(); - } - - public static void clearParticles() { - if (!PARTICLES.isEmpty()) - PARTICLES.clear(); - if (!PARTICLES_NO_DEPTH.isEmpty()) - PARTICLES_NO_DEPTH.clear(); + private static void setupRendering(TextureManager textureManager) { + RenderSystem.enableAlphaTest(); + RenderSystem.enableBlend(); + 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); + RenderSystem.depthMask(false); + textureManager.bindTexture(ParticleMagic.TEXTURE); } } \ No newline at end of file diff --git a/src/main/java/de/ellpeck/naturesaura/particles/ParticleMagic.java b/src/main/java/de/ellpeck/naturesaura/particles/ParticleMagic.java index 0a4f33f9..f419bfd3 100644 --- a/src/main/java/de/ellpeck/naturesaura/particles/ParticleMagic.java +++ b/src/main/java/de/ellpeck/naturesaura/particles/ParticleMagic.java @@ -14,7 +14,6 @@ import net.minecraft.util.math.shapes.ISelectionContext; import net.minecraft.util.math.vector.Quaternion; import net.minecraft.util.math.vector.Vector3d; import net.minecraft.util.math.vector.Vector3f; -import net.minecraft.world.World; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; @@ -27,15 +26,17 @@ public class ParticleMagic extends Particle { private final float desiredScale; private final boolean fade; + private final boolean depth; private float particleScale; - public ParticleMagic(ClientWorld world, double posX, double posY, double posZ, double motionX, double motionY, double motionZ, int color, float scale, int maxAge, float gravity, boolean collision, boolean fade) { + public ParticleMagic(ClientWorld world, double posX, double posY, double posZ, double motionX, double motionY, double motionZ, int color, float scale, int maxAge, float gravity, boolean collision, boolean fade, boolean depth) { super(world, posX, posY, posZ); this.desiredScale = scale; this.maxAge = maxAge; this.canCollide = collision; this.particleGravity = gravity; this.fade = fade; + this.depth = depth; this.motionX = motionX; this.motionY = motionY; @@ -119,7 +120,7 @@ public class ParticleMagic extends Particle { @Override public IParticleRenderType getRenderType() { - return IParticleRenderType.CUSTOM; + return this.depth ? ParticleHandler.MAGIC : ParticleHandler.MAGIC_NO_DEPTH; } @Override diff --git a/src/main/java/de/ellpeck/naturesaura/proxy/ClientProxy.java b/src/main/java/de/ellpeck/naturesaura/proxy/ClientProxy.java index 3617a29e..79080919 100644 --- a/src/main/java/de/ellpeck/naturesaura/proxy/ClientProxy.java +++ b/src/main/java/de/ellpeck/naturesaura/proxy/ClientProxy.java @@ -9,7 +9,6 @@ import de.ellpeck.naturesaura.items.ItemColorChanger; import de.ellpeck.naturesaura.items.ModItems; import de.ellpeck.naturesaura.particles.ParticleHandler; import de.ellpeck.naturesaura.particles.ParticleMagic; -import de.ellpeck.naturesaura.recipes.ModRecipes; import de.ellpeck.naturesaura.reg.*; import de.ellpeck.naturesaura.renderers.PlayerLayerTrinkets; import de.ellpeck.naturesaura.renderers.SupporterFancyHandler; @@ -26,8 +25,6 @@ import net.minecraft.entity.Entity; import net.minecraft.entity.EntityType; import net.minecraft.item.Item; import net.minecraft.item.ItemModelsProperties; -import net.minecraft.resources.IReloadableResourceManager; -import net.minecraft.resources.IResourceManager; import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntityType; import net.minecraft.util.ResourceLocation; @@ -103,7 +100,7 @@ public class ClientProxy implements IProxy { ParticleHandler.spawnParticle(() -> new ParticleMagic(Minecraft.getInstance().world, posX, posY, posZ, motionX, motionY, motionZ, - color, scale, maxAge, gravity, collision, fade), posX, posY, posZ); + color, scale, maxAge, gravity, collision, fade, ParticleHandler.depthEnabled), posX, posY, posZ); } @Override