| 1 | package net.minecraft.client.particle;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.common.collect.Maps;
|
| 5 | import com.google.common.collect.Queues;
|
| 6 | import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Map;
|
| 9 | import java.util.Optional;
|
| 10 | import java.util.Queue;
|
| 11 | import java.util.Map.Entry;
|
| 12 | import net.fabricmc.api.EnvType;
|
| 13 | import net.fabricmc.api.Environment;
|
| 14 | import net.minecraft.client.Camera;
|
| 15 | import net.minecraft.client.multiplayer.ClientLevel;
|
| 16 | import net.minecraft.client.renderer.culling.Frustum;
|
| 17 | import net.minecraft.client.renderer.state.level.ParticlesRenderState;
|
| 18 | import net.minecraft.core.particles.ParticleLimit;
|
| 19 | import net.minecraft.core.particles.ParticleOptions;
|
| 20 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 21 | import net.minecraft.util.RandomSource;
|
| 22 | import net.minecraft.util.profiling.Profiler;
|
| 23 | import net.minecraft.world.entity.Entity;
|
| 24 | import org.jspecify.annotations.Nullable;
|
| 25 |
|
| 26 | @Environment(EnvType.CLIENT)
|
| 27 | public class ParticleEngine {
|
| 28 | private static final List<ParticleRenderType> RENDER_ORDER = List.of(
|
| 29 | ParticleRenderType.SINGLE_QUADS, ParticleRenderType.ITEM_PICKUP, ParticleRenderType.ELDER_GUARDIANS
|
| 30 | );
|
| 31 | protected ClientLevel level;
|
| 32 | private final Map<ParticleRenderType, ParticleGroup<?>> particles = Maps.newIdentityHashMap();
|
| 33 | private final Queue<TrackingEmitter> trackingEmitters = Queues.newArrayDeque();
|
| 34 | private final Queue<Particle> particlesToAdd = Queues.newArrayDeque();
|
| 35 | private final Object2IntOpenHashMap<ParticleLimit> trackedParticleCounts = new Object2IntOpenHashMap<>();
|
| 36 | private final ParticleResources resourceManager;
|
| 37 | private final RandomSource random = RandomSource.create();
|
| 38 |
|
| 39 | public ParticleEngine(final ClientLevel level, final ParticleResources resourceManager) {
|
| 40 | this.level = level;
|
| 41 | this.resourceManager = resourceManager;
|
| 42 | }
|
| 43 |
|
| 44 | public void createTrackingEmitter(final Entity entity, final ParticleOptions particle) {
|
| 45 | this.trackingEmitters.add(new TrackingEmitter(this.level, entity, particle));
|
| 46 | }
|
| 47 |
|
| 48 | public void createTrackingEmitter(final Entity entity, final ParticleOptions particle, final int lifeTime) {
|
| 49 | this.trackingEmitters.add(new TrackingEmitter(this.level, entity, particle, lifeTime));
|
| 50 | }
|
| 51 |
|
| 52 | public @Nullable Particle createParticle(
|
| 53 | final ParticleOptions options, final double x, final double y, final double z, final double xa, final double ya, final double za
|
| 54 | ) {
|
| 55 | Particle particle = this.makeParticle(options, x, y, z, xa, ya, za);
|
| 56 | if (particle != null) {
|
| 57 | this.add(particle);
|
| 58 | return particle;
|
| 59 | } else {
|
| 60 | return null;
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | private <T extends ParticleOptions> @Nullable Particle makeParticle(
|
| 65 | final T options, final double x, final double y, final double z, final double xa, final double ya, final double za
|
| 66 | ) {
|
| 67 | ParticleProvider<T> provider = (ParticleProvider<T>)this.resourceManager.getProviders().get(BuiltInRegistries.PARTICLE_TYPE.getId(options.getType()));
|
| 68 | return provider == null ? null : provider.createParticle(options, this.level, x, y, z, xa, ya, za, this.random);
|
| 69 | }
|
| 70 |
|
| 71 | public void add(final Particle p) {
|
| 72 | Optional<ParticleLimit> limit = p.getParticleLimit();
|
| 73 | if (limit.isPresent()) {
|
| 74 | if (this.hasSpaceInParticleLimit(limit.get())) {
|
| 75 | this.particlesToAdd.add(p);
|
| 76 | this.updateCount(limit.get(), 1);
|
| 77 | }
|
| 78 | } else {
|
| 79 | this.particlesToAdd.add(p);
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | public void tick() {
|
| 84 | this.particles.forEach((type, group) -> {
|
| 85 | Profiler.get().push(type.name());
|
| 86 | group.tickParticles();
|
| 87 | Profiler.get().pop();
|
| 88 | });
|
| 89 | if (!this.trackingEmitters.isEmpty()) {
|
| 90 | List<TrackingEmitter> removed = Lists.newArrayList();
|
| 91 |
|
| 92 | for (TrackingEmitter emitter : this.trackingEmitters) {
|
| 93 | emitter.tick();
|
| 94 | if (!emitter.isAlive()) {
|
| 95 | removed.add(emitter);
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | this.trackingEmitters.removeAll(removed);
|
| 100 | }
|
| 101 |
|
| 102 | Particle particle;
|
| 103 | if (!this.particlesToAdd.isEmpty()) {
|
| 104 | while ((particle = this.particlesToAdd.poll()) != null) {
|
| 105 | if (!this.particles.computeIfAbsent(particle.getGroup(), this::createParticleGroup).add(particle)) {
|
| 106 | particle.getParticleLimit().ifPresent(options -> this.updateCount(options, -1));
|
| 107 | }
|
| 108 | }
|
| 109 | }
|
| 110 | }
|
| 111 |
|
| 112 | private ParticleGroup<?> createParticleGroup(final ParticleRenderType type) {
|
| 113 | if (type == ParticleRenderType.ITEM_PICKUP) {
|
| 114 | return new ItemPickupParticleGroup(this);
|
| 115 | } else if (type == ParticleRenderType.ELDER_GUARDIANS) {
|
| 116 | return new ElderGuardianParticleGroup(this);
|
| 117 | } else {
|
| 118 | return type == ParticleRenderType.NO_RENDER ? new NoRenderParticleGroup(this) : new QuadParticleGroup(this, type);
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | protected void updateCount(final ParticleLimit limit, final int change) {
|
| 123 | this.trackedParticleCounts.addTo(limit, change);
|
| 124 | }
|
| 125 |
|
| 126 | public void extract(final ParticlesRenderState particlesRenderState, final Frustum frustum, final Camera camera, final float partialTickTime) {
|
| 127 | for (ParticleRenderType particleType : RENDER_ORDER) {
|
| 128 | ParticleGroup<?> particles = this.particles.get(particleType);
|
| 129 | if (particles != null && !particles.isEmpty()) {
|
| 130 | particlesRenderState.add(particles.extractRenderState(frustum, camera, partialTickTime));
|
| 131 | }
|
| 132 | }
|
| 133 | }
|
| 134 |
|
| 135 | public void setLevel(final @Nullable ClientLevel level) {
|
| 136 | this.level = level;
|
| 137 | this.clearParticles();
|
| 138 | this.trackingEmitters.clear();
|
| 139 | }
|
| 140 |
|
| 141 | public String countParticles() {
|
| 142 | StringBuilder builder = new StringBuilder();
|
| 143 | int total = 0;
|
| 144 |
|
| 145 | for (Entry<ParticleRenderType, ParticleGroup<?>> group : this.particles.entrySet()) {
|
| 146 | builder.append(group.getKey().shorthand()).append(" ");
|
| 147 | int size = group.getValue().size();
|
| 148 | builder.append(size).append(" ");
|
| 149 | total += size;
|
| 150 | }
|
| 151 |
|
| 152 | builder.append("T ");
|
| 153 | builder.append(total);
|
| 154 | return builder.toString();
|
| 155 | }
|
| 156 |
|
| 157 | private boolean hasSpaceInParticleLimit(final ParticleLimit limit) {
|
| 158 | return this.trackedParticleCounts.getInt(limit) < limit.limit();
|
| 159 | }
|
| 160 |
|
| 161 | public void clearParticles() {
|
| 162 | this.particles.clear();
|
| 163 | this.particlesToAdd.clear();
|
| 164 | this.trackingEmitters.clear();
|
| 165 | this.trackedParticleCounts.clear();
|
| 166 | }
|
| 167 |
|
| 168 | public RandomSource getRandom() {
|
| 169 | return this.random;
|
| 170 | }
|
| 171 | }
|
| 172 |
|