| 1 | package net.minecraft.client.renderer.item;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 4 | import it.unimi.dsi.fastutil.ints.IntArrayList;
|
| 5 | import it.unimi.dsi.fastutil.ints.IntList;
|
| 6 | import java.util.Arrays;
|
| 7 | import java.util.function.Consumer;
|
| 8 | import java.util.function.Supplier;
|
| 9 | import net.fabricmc.api.EnvType;
|
| 10 | import net.fabricmc.api.Environment;
|
| 11 | import net.fabricmc.fabric.api.client.renderer.v1.render.FabricLayerRenderState;
|
| 12 | import net.fabricmc.fabric.api.client.rendering.v1.FabricRenderState;
|
| 13 | import net.minecraft.client.renderer.SubmitNodeCollector;
|
| 14 | import net.minecraft.client.renderer.special.SpecialModelRenderer;
|
| 15 | import net.minecraft.client.resources.model.cuboid.ItemTransform;
|
| 16 | import net.minecraft.client.resources.model.geometry.ItemQuads;
|
| 17 | import net.minecraft.client.resources.model.sprite.Material;
|
| 18 | import net.minecraft.util.RandomSource;
|
| 19 | import net.minecraft.world.item.ItemDisplayContext;
|
| 20 | import net.minecraft.world.phys.AABB;
|
| 21 | import net.minecraft.world.phys.Vec3;
|
| 22 | import net.minecraft.world.phys.AABB.Builder;
|
| 23 | import org.joml.Matrix4f;
|
| 24 | import org.joml.Matrix4fc;
|
| 25 | import org.joml.Vector3f;
|
| 26 | import org.joml.Vector3fc;
|
| 27 | import org.jspecify.annotations.Nullable;
|
| 28 |
|
| 29 | @Environment(EnvType.CLIENT)
|
| 30 | public class ItemStackRenderState implements FabricRenderState {
|
| 31 | ItemDisplayContext displayContext = ItemDisplayContext.NONE;
|
| 32 | private int activeLayerCount;
|
| 33 | private boolean animated;
|
| 34 | private boolean oversizedInGui;
|
| 35 | private @Nullable AABB cachedModelBoundingBox;
|
| 36 | private ItemStackRenderState.LayerRenderState[] layers = new ItemStackRenderState.LayerRenderState[]{new ItemStackRenderState.LayerRenderState()};
|
| 37 |
|
| 38 | public void ensureCapacity(final int requestedCount) {
|
| 39 | int currentCapacity = this.layers.length;
|
| 40 | int requiredNewCapacity = this.activeLayerCount + requestedCount;
|
| 41 | if (requiredNewCapacity > currentCapacity) {
|
| 42 | this.layers = Arrays.copyOf(this.layers, requiredNewCapacity);
|
| 43 |
|
| 44 | for (int i = currentCapacity; i < requiredNewCapacity; i++) {
|
| 45 | this.layers[i] = new ItemStackRenderState.LayerRenderState();
|
| 46 | }
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | public ItemStackRenderState.LayerRenderState newLayer() {
|
| 51 | this.ensureCapacity(1);
|
| 52 | return this.layers[this.activeLayerCount++];
|
| 53 | }
|
| 54 |
|
| 55 | public void clear() {
|
| 56 | this.displayContext = ItemDisplayContext.NONE;
|
| 57 |
|
| 58 | for (int i = 0; i < this.activeLayerCount; i++) {
|
| 59 | this.layers[i].clear();
|
| 60 | }
|
| 61 |
|
| 62 | this.activeLayerCount = 0;
|
| 63 | this.animated = false;
|
| 64 | this.oversizedInGui = false;
|
| 65 | this.cachedModelBoundingBox = null;
|
| 66 | }
|
| 67 |
|
| 68 | public void setAnimated() {
|
| 69 | this.animated = true;
|
| 70 | }
|
| 71 |
|
| 72 | public boolean isAnimated() {
|
| 73 | return this.animated;
|
| 74 | }
|
| 75 |
|
| 76 | public void appendModelIdentityElement(final Object element) {
|
| 77 | }
|
| 78 |
|
| 79 | private ItemStackRenderState.LayerRenderState firstLayer() {
|
| 80 | return this.layers[0];
|
| 81 | }
|
| 82 |
|
| 83 | public boolean isEmpty() {
|
| 84 | return this.activeLayerCount == 0;
|
| 85 | }
|
| 86 |
|
| 87 | public boolean usesBlockLight() {
|
| 88 | return this.firstLayer().usesBlockLight;
|
| 89 | }
|
| 90 |
|
| 91 | public Material.@Nullable Baked pickParticleMaterial(final RandomSource randomSource) {
|
| 92 | return this.activeLayerCount == 0 ? null : this.layers[randomSource.nextInt(this.activeLayerCount)].particleMaterial;
|
| 93 | }
|
| 94 |
|
| 95 | public void visitExtents(final Consumer<Vector3fc> output) {
|
| 96 | Vector3f scratch = new Vector3f();
|
| 97 | PoseStack.Pose pose = new PoseStack.Pose();
|
| 98 |
|
| 99 | for (int i = 0; i < this.activeLayerCount; i++) {
|
| 100 | ItemStackRenderState.LayerRenderState layer = this.layers[i];
|
| 101 | layer.applyTransform(pose);
|
| 102 | Matrix4f poseTransform = pose.pose();
|
| 103 | Vector3fc[] layerExtents = layer.extents.get();
|
| 104 |
|
| 105 | for (Vector3fc extent : layerExtents) {
|
| 106 | output.accept(scratch.set(extent).mulPosition(poseTransform));
|
| 107 | }
|
| 108 |
|
| 109 | pose.setIdentity();
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | public void submit(
|
| 114 | final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final int lightCoords, final int overlayCoords, final int outlineColor
|
| 115 | ) {
|
| 116 | for (int i = 0; i < this.activeLayerCount; i++) {
|
| 117 | this.layers[i].submit(poseStack, submitNodeCollector, lightCoords, overlayCoords, outlineColor);
|
| 118 | }
|
| 119 | }
|
| 120 |
|
| 121 | public AABB getModelBoundingBox() {
|
| 122 | if (this.cachedModelBoundingBox != null) {
|
| 123 | return this.cachedModelBoundingBox;
|
| 124 | }
|
| 125 |
|
| 126 | Builder collector = new Builder();
|
| 127 | this.visitExtents(collector::include);
|
| 128 | AABB aabb = collector.isDefined() ? collector.build() : AABB.ofSize(Vec3.ZERO, 0.0, 0.0, 0.0);
|
| 129 | this.cachedModelBoundingBox = aabb;
|
| 130 | return aabb;
|
| 131 | }
|
| 132 |
|
| 133 | public void setOversizedInGui(final boolean oversizedInGui) {
|
| 134 | this.oversizedInGui = oversizedInGui;
|
| 135 | }
|
| 136 |
|
| 137 | public boolean isOversizedInGui() {
|
| 138 | return this.oversizedInGui;
|
| 139 | }
|
| 140 |
|
| 141 | @Environment(EnvType.CLIENT)
|
| 142 | public enum FoilType {
|
| 143 | NONE,
|
| 144 | STANDARD,
|
| 145 | SPECIAL;
|
| 146 | }
|
| 147 |
|
| 148 | @Environment(EnvType.CLIENT)
|
| 149 | public class LayerRenderState implements FabricLayerRenderState, FabricRenderState {
|
| 150 | private static final Vector3fc[] NO_EXTENTS = new Vector3fc[0];
|
| 151 | public static final Supplier<Vector3fc[]> NO_EXTENTS_SUPPLIER = () -> NO_EXTENTS;
|
| 152 | public static final int[] EMPTY_TINTS = new int[0];
|
| 153 | private ItemQuads quads = ItemQuads.EMPTY;
|
| 154 | private boolean usesBlockLight;
|
| 155 | private Material.@Nullable Baked particleMaterial;
|
| 156 | private ItemTransform itemTransform = ItemTransform.NO_TRANSFORM;
|
| 157 | private final Matrix4f localTransform = new Matrix4f();
|
| 158 | private ItemStackRenderState.FoilType foilType = ItemStackRenderState.FoilType.NONE;
|
| 159 | private @Nullable IntList tintLayers;
|
| 160 | private @Nullable SpecialModelRenderer<Object> specialRenderer;
|
| 161 | private @Nullable Object argumentForSpecialRendering;
|
| 162 | private Supplier<Vector3fc[]> extents = NO_EXTENTS_SUPPLIER;
|
| 163 |
|
| 164 | public void clear() {
|
| 165 | this.quads = ItemQuads.EMPTY;
|
| 166 | this.foilType = ItemStackRenderState.FoilType.NONE;
|
| 167 | this.specialRenderer = null;
|
| 168 | this.argumentForSpecialRendering = null;
|
| 169 | if (this.tintLayers != null) {
|
| 170 | this.tintLayers.clear();
|
| 171 | }
|
| 172 |
|
| 173 | this.usesBlockLight = false;
|
| 174 | this.particleMaterial = null;
|
| 175 | this.itemTransform = ItemTransform.NO_TRANSFORM;
|
| 176 | this.localTransform.identity();
|
| 177 | this.extents = NO_EXTENTS_SUPPLIER;
|
| 178 | }
|
| 179 |
|
| 180 | public void setQuads(final ItemQuads quads) {
|
| 181 | this.quads = quads;
|
| 182 | }
|
| 183 |
|
| 184 | public void setUsesBlockLight(final boolean usesBlockLight) {
|
| 185 | this.usesBlockLight = usesBlockLight;
|
| 186 | }
|
| 187 |
|
| 188 | public void setExtents(final Supplier<Vector3fc[]> extents) {
|
| 189 | this.extents = extents;
|
| 190 | }
|
| 191 |
|
| 192 | public void setParticleMaterial(final Material.Baked particleMaterial) {
|
| 193 | this.particleMaterial = particleMaterial;
|
| 194 | }
|
| 195 |
|
| 196 | public void setItemTransform(final ItemTransform transform) {
|
| 197 | this.itemTransform = transform;
|
| 198 | }
|
| 199 |
|
| 200 | public void setLocalTransform(final Matrix4fc transform) {
|
| 201 | this.localTransform.set(transform);
|
| 202 | }
|
| 203 |
|
| 204 | public <T> void setupSpecialModel(final SpecialModelRenderer<T> renderer, final @Nullable T argument) {
|
| 205 | this.specialRenderer = eraseSpecialRenderer(renderer);
|
| 206 | this.argumentForSpecialRendering = argument;
|
| 207 | }
|
| 208 |
|
| 209 | private static SpecialModelRenderer<Object> eraseSpecialRenderer(final SpecialModelRenderer<?> renderer) {
|
| 210 | return (SpecialModelRenderer<Object>)renderer;
|
| 211 | }
|
| 212 |
|
| 213 | public void setFoilType(final ItemStackRenderState.FoilType foilType) {
|
| 214 | this.foilType = foilType;
|
| 215 | }
|
| 216 |
|
| 217 | public IntList tintLayers() {
|
| 218 | if (this.tintLayers == null) {
|
| 219 | this.tintLayers = new IntArrayList();
|
| 220 | }
|
| 221 |
|
| 222 | return this.tintLayers;
|
| 223 | }
|
| 224 |
|
| 225 | private void submit(
|
| 226 | final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final int lightCoords, final int overlayCoords, final int outlineColor
|
| 227 | ) {
|
| 228 | poseStack.pushPose();
|
| 229 | this.applyTransform(poseStack.last());
|
| 230 | if (this.specialRenderer != null) {
|
| 231 | this.specialRenderer
|
| 232 | .submit(
|
| 233 | this.argumentForSpecialRendering,
|
| 234 | poseStack,
|
| 235 | submitNodeCollector,
|
| 236 | lightCoords,
|
| 237 | overlayCoords,
|
| 238 | this.foilType != ItemStackRenderState.FoilType.NONE,
|
| 239 | outlineColor
|
| 240 | );
|
| 241 | } else {
|
| 242 | int[] tints = this.tintLayers != null ? this.tintLayers.toArray(EMPTY_TINTS) : EMPTY_TINTS;
|
| 243 | submitNodeCollector.submitItem(
|
| 244 | poseStack, ItemStackRenderState.this.displayContext, lightCoords, overlayCoords, outlineColor, tints, this.quads, this.foilType
|
| 245 | );
|
| 246 | }
|
| 247 |
|
| 248 | poseStack.popPose();
|
| 249 | }
|
| 250 |
|
| 251 | private void applyTransform(final PoseStack.Pose localPose) {
|
| 252 | this.itemTransform.apply(ItemStackRenderState.this.displayContext.leftHand(), localPose);
|
| 253 | localPose.mulPose(this.localTransform);
|
| 254 | }
|
| 255 | }
|
| 256 | }
|
| 257 |
|