| 1 | package net.minecraft.client.renderer.state.level;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.VertexConsumer;
|
| 4 | import java.util.Arrays;
|
| 5 | import java.util.HashMap;
|
| 6 | import java.util.Map;
|
| 7 | import java.util.Set;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.client.particle.SingleQuadParticle;
|
| 11 | import net.minecraft.client.renderer.SubmitNodeCollector;
|
| 12 | import org.joml.Quaternionf;
|
| 13 | import org.joml.Vector3f;
|
| 14 |
|
| 15 | @Environment(EnvType.CLIENT)
|
| 16 | public class QuadParticleRenderState implements ParticleGroupRenderState {
|
| 17 | private static final int INITIAL_PARTICLE_CAPACITY = 1024;
|
| 18 | private static final int FLOATS_PER_PARTICLE = 12;
|
| 19 | private static final int INTS_PER_PARTICLE = 2;
|
| 20 | private final Map<SingleQuadParticle.Layer, QuadParticleRenderState.Storage> particles = new HashMap<>();
|
| 21 | private int particleCount;
|
| 22 |
|
| 23 | public void add(
|
| 24 | final SingleQuadParticle.Layer layer,
|
| 25 | final float x,
|
| 26 | final float y,
|
| 27 | final float z,
|
| 28 | final float xRot,
|
| 29 | final float yRot,
|
| 30 | final float zRot,
|
| 31 | final float wRot,
|
| 32 | final float scale,
|
| 33 | final float u0,
|
| 34 | final float u1,
|
| 35 | final float v0,
|
| 36 | final float v1,
|
| 37 | final int color,
|
| 38 | final int lightCoords
|
| 39 | ) {
|
| 40 | this.particles
|
| 41 | .computeIfAbsent(layer, ignored -> new QuadParticleRenderState.Storage())
|
| 42 | .add(x, y, z, xRot, yRot, zRot, wRot, scale, u0, u1, v0, v1, color, lightCoords);
|
| 43 | this.particleCount++;
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | public void clear() {
|
| 48 | this.particles.values().forEach(QuadParticleRenderState.Storage::clear);
|
| 49 | this.particleCount = 0;
|
| 50 | }
|
| 51 |
|
| 52 | public boolean isEmpty() {
|
| 53 | return this.particleCount == 0;
|
| 54 | }
|
| 55 |
|
| 56 | public void buildLayer(final SingleQuadParticle.Layer layer, final VertexConsumer bufferBuilder) {
|
| 57 | QuadParticleRenderState.Storage storage = this.particles.get(layer);
|
| 58 | if (storage != null) {
|
| 59 | storage.forEachParticle(
|
| 60 | (x, y, z, xRot, yRot, zRot, wRot, scale, u0, u1, v0, v1, color, lightCoords) -> this.renderRotatedQuad(
|
| 61 | bufferBuilder, x, y, z, xRot, yRot, zRot, wRot, scale, u0, u1, v0, v1, color, lightCoords
|
| 62 | )
|
| 63 | );
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | public Set<SingleQuadParticle.Layer> layers() {
|
| 68 | return this.particles.keySet();
|
| 69 | }
|
| 70 |
|
| 71 | protected void renderRotatedQuad(
|
| 72 | final VertexConsumer builder,
|
| 73 | final float x,
|
| 74 | final float y,
|
| 75 | final float z,
|
| 76 | final float xRot,
|
| 77 | final float yRot,
|
| 78 | final float zRot,
|
| 79 | final float wRot,
|
| 80 | final float scale,
|
| 81 | final float u0,
|
| 82 | final float u1,
|
| 83 | final float v0,
|
| 84 | final float v1,
|
| 85 | final int color,
|
| 86 | final int lightCoords
|
| 87 | ) {
|
| 88 | Quaternionf rotation = new Quaternionf(xRot, yRot, zRot, wRot);
|
| 89 | this.renderVertex(builder, rotation, x, y, z, 1.0F, -1.0F, scale, u1, v1, color, lightCoords);
|
| 90 | this.renderVertex(builder, rotation, x, y, z, 1.0F, 1.0F, scale, u1, v0, color, lightCoords);
|
| 91 | this.renderVertex(builder, rotation, x, y, z, -1.0F, 1.0F, scale, u0, v0, color, lightCoords);
|
| 92 | this.renderVertex(builder, rotation, x, y, z, -1.0F, -1.0F, scale, u0, v1, color, lightCoords);
|
| 93 | }
|
| 94 |
|
| 95 | private void renderVertex(
|
| 96 | final VertexConsumer builder,
|
| 97 | final Quaternionf rotation,
|
| 98 | final float x,
|
| 99 | final float y,
|
| 100 | final float z,
|
| 101 | final float nx,
|
| 102 | final float ny,
|
| 103 | final float scale,
|
| 104 | final float u,
|
| 105 | final float v,
|
| 106 | final int color,
|
| 107 | final int lightCoords
|
| 108 | ) {
|
| 109 | Vector3f scratch = new Vector3f(nx, ny, 0.0F).rotate(rotation).mul(scale).add(x, y, z);
|
| 110 | builder.addVertex(scratch.x(), scratch.y(), scratch.z()).setUv(u, v).setColor(color).setLight(lightCoords);
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | public void submit(final SubmitNodeCollector submitNodeCollector, final CameraRenderState camera) {
|
| 115 | if (this.particleCount > 0) {
|
| 116 | submitNodeCollector.submitQuadParticleGroup(this);
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 | @FunctionalInterface
|
| 121 | @Environment(EnvType.CLIENT)
|
| 122 | public interface ParticleConsumer {
|
| 123 | void consume(
|
| 124 | final float x,
|
| 125 | final float y,
|
| 126 | final float z,
|
| 127 | final float xRot,
|
| 128 | final float yRot,
|
| 129 | final float zRot,
|
| 130 | final float wRot,
|
| 131 | final float scale,
|
| 132 | final float u0,
|
| 133 | final float u1,
|
| 134 | final float v0,
|
| 135 | final float v1,
|
| 136 | final int color,
|
| 137 | final int lightCoords
|
| 138 | );
|
| 139 | }
|
| 140 |
|
| 141 | @Environment(EnvType.CLIENT)
|
| 142 | private static class Storage {
|
| 143 | private int capacity = 1024;
|
| 144 | private float[] floatValues = new float[12288];
|
| 145 | private int[] intValues = new int[2048];
|
| 146 | private int currentParticleIndex;
|
| 147 |
|
| 148 | public void add(
|
| 149 | final float x,
|
| 150 | final float y,
|
| 151 | final float z,
|
| 152 | final float xRot,
|
| 153 | final float yRot,
|
| 154 | final float zRot,
|
| 155 | final float wRot,
|
| 156 | final float scale,
|
| 157 | final float u0,
|
| 158 | final float u1,
|
| 159 | final float v0,
|
| 160 | final float v1,
|
| 161 | final int color,
|
| 162 | final int lightCoords
|
| 163 | ) {
|
| 164 | if (this.currentParticleIndex >= this.capacity) {
|
| 165 | this.grow();
|
| 166 | }
|
| 167 |
|
| 168 | int index = this.currentParticleIndex * 12;
|
| 169 | this.floatValues[index++] = x;
|
| 170 | this.floatValues[index++] = y;
|
| 171 | this.floatValues[index++] = z;
|
| 172 | this.floatValues[index++] = xRot;
|
| 173 | this.floatValues[index++] = yRot;
|
| 174 | this.floatValues[index++] = zRot;
|
| 175 | this.floatValues[index++] = wRot;
|
| 176 | this.floatValues[index++] = scale;
|
| 177 | this.floatValues[index++] = u0;
|
| 178 | this.floatValues[index++] = u1;
|
| 179 | this.floatValues[index++] = v0;
|
| 180 | this.floatValues[index] = v1;
|
| 181 | index = this.currentParticleIndex * 2;
|
| 182 | this.intValues[index++] = color;
|
| 183 | this.intValues[index] = lightCoords;
|
| 184 | this.currentParticleIndex++;
|
| 185 | }
|
| 186 |
|
| 187 | public void forEachParticle(final QuadParticleRenderState.ParticleConsumer consumer) {
|
| 188 | for (int particleIndex = 0; particleIndex < this.currentParticleIndex; particleIndex++) {
|
| 189 | int floatIndex = particleIndex * 12;
|
| 190 | int intIndex = particleIndex * 2;
|
| 191 | consumer.consume(
|
| 192 | this.floatValues[floatIndex++],
|
| 193 | this.floatValues[floatIndex++],
|
| 194 | this.floatValues[floatIndex++],
|
| 195 | this.floatValues[floatIndex++],
|
| 196 | this.floatValues[floatIndex++],
|
| 197 | this.floatValues[floatIndex++],
|
| 198 | this.floatValues[floatIndex++],
|
| 199 | this.floatValues[floatIndex++],
|
| 200 | this.floatValues[floatIndex++],
|
| 201 | this.floatValues[floatIndex++],
|
| 202 | this.floatValues[floatIndex++],
|
| 203 | this.floatValues[floatIndex],
|
| 204 | this.intValues[intIndex++],
|
| 205 | this.intValues[intIndex]
|
| 206 | );
|
| 207 | }
|
| 208 | }
|
| 209 |
|
| 210 | public void clear() {
|
| 211 | this.currentParticleIndex = 0;
|
| 212 | }
|
| 213 |
|
| 214 | private void grow() {
|
| 215 | this.capacity *= 2;
|
| 216 | this.floatValues = Arrays.copyOf(this.floatValues, this.capacity * 12);
|
| 217 | this.intValues = Arrays.copyOf(this.intValues, this.capacity * 2);
|
| 218 | }
|
| 219 |
|
| 220 | public int count() {
|
| 221 | return this.currentParticleIndex;
|
| 222 | }
|
| 223 | }
|
| 224 | }
|
| 225 |
|