Skip to content

Commit

Permalink
apply item model transformations in property bundle to fix ordering a…
Browse files Browse the repository at this point in the history
…nd switch to GUI transform mode
  • Loading branch information
gliscowo committed Nov 29, 2023
1 parent 2718260 commit 16ddc8e
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.glisco.isometricrenders.screen.IsometricUI;
import com.glisco.isometricrenders.util.ExportPathSpec;
import com.glisco.isometricrenders.util.ImageIO;
import com.glisco.isometricrenders.util.ParticleRestriction;
import com.glisco.isometricrenders.util.Translate;
import io.wispforest.owo.ui.component.ButtonComponent;
import io.wispforest.owo.ui.component.Components;
Expand All @@ -20,7 +21,6 @@

public class BatchRenderable<R extends Renderable<?>> implements Renderable<BatchRenderable.BatchPropertyBundle> {

private final BatchPropertyBundle properties;
private final List<R> delegates;
private final String contentType;

Expand All @@ -38,8 +38,6 @@ private BatchRenderable(String source, List<R> delegates) {

this.contentType = ExportPathSpec.exportRoot().resolve("batches/")
.relativize(ImageIO.next(ExportPathSpec.exportRoot().resolve("batches/" + source + "/"))).toString();

this.properties = new BatchPropertyBundle(this.currentDelegate.properties());
this.renderDelay = Math.max((int) Math.pow(GlobalProperties.exportResolution / 1024f, 2) * 100L, 75);
}

Expand All @@ -51,6 +49,11 @@ public static <R extends Renderable<?>> BatchRenderable<?> of(String source, Lis
}
}

@Override
public void prepare() {
this.currentDelegate.prepare();
}

@Override
public void emitVertices(MatrixStack matrices, VertexConsumerProvider vertexConsumers, float tickDelta) {
this.currentDelegate.emitVertices(matrices, vertexConsumers, tickDelta);
Expand All @@ -70,6 +73,21 @@ public void draw(Matrix4f modelViewMatrix) {
this.currentDelegate.draw(modelViewMatrix);
}

@Override
public void cleanUp() {
this.currentDelegate.cleanUp();
}

@Override
public void dispose() {
this.delegates.forEach(Renderable::dispose);
}

@Override
public ParticleRestriction<?> particleRestriction() {
return this.currentDelegate.particleRestriction();
}

private void start() {
this.batchActive = true;
this.currentIndex = 0;
Expand All @@ -86,7 +104,7 @@ private void reset() {

@Override
public BatchPropertyBundle properties() {
return this.properties;
return new BatchPropertyBundle(this.currentDelegate.properties());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,39 @@

import com.glisco.isometricrenders.property.DefaultPropertyBundle;
import com.glisco.isometricrenders.util.ExportPathSpec;
import net.fabricmc.fabric.api.renderer.v1.model.ForwardingBakedModel;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.LightmapTextureManager;
import net.minecraft.client.render.OverlayTexture;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.client.render.model.json.ModelTransformation;
import net.minecraft.client.render.model.json.ModelTransformationMode;
import net.minecraft.client.util.ModelIdentifier;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.registry.Registries;
import net.minecraft.util.math.RotationAxis;

public class ItemRenderable extends DefaultRenderable<DefaultPropertyBundle> {

private static final DefaultPropertyBundle PROPERTIES = new DefaultPropertyBundle();
private static BakedModel currentModel = null;
private static final DefaultPropertyBundle PROPERTIES = new DefaultPropertyBundle() {
@Override
public void applyToViewMatrix(MatrixStack modelViewStack) {
final float scale = (this.scale.get() / 100f) * (currentModel != null && currentModel.hasDepth() ? 2f : 1.75f);
modelViewStack.scale(scale, scale, scale);

modelViewStack.translate(this.xOffset.get() / 26000d, this.yOffset.get() / -26000d, 0);

modelViewStack.multiply(RotationAxis.POSITIVE_X.rotationDegrees(this.slant.get()));
if (currentModel != null) currentModel.getTransformation().getTransformation(ModelTransformationMode.GUI).apply(false, modelViewStack);
modelViewStack.multiply(RotationAxis.POSITIVE_Y.rotationDegrees(this.rotation.get()));

this.updateAndApplyRotationOffset(modelViewStack);
}
};

static {
PROPERTIES.slant.setDefaultValue(0).setToDefault();
Expand All @@ -28,27 +48,37 @@ public ItemRenderable(ItemStack stack) {
}

@Override
public void emitVertices(MatrixStack matrices, VertexConsumerProvider vertexConsumers, float tickDelta) {
final var itemRenderer = MinecraftClient.getInstance().getItemRenderer();
final var hasDepth = itemRenderer.getModel(this.stack, null, null, 0).hasDepth();
final float scale = hasDepth ? 2f : 1.75f;

matrices.push();
matrices.scale(scale, scale, scale);

// This funny matrix manipulation here is done in order to
// avoid funny axis rotation on models with depth
if (hasDepth) {
matrices.multiply(RotationAxis.POSITIVE_Y.rotationDegrees(-this.properties().rotation.get()));
matrices.multiply(RotationAxis.POSITIVE_X.rotationDegrees(30));
matrices.multiply(RotationAxis.POSITIVE_Y.rotationDegrees(this.properties().rotation.get() + 135));
public void prepare() {
var itemRenderer = MinecraftClient.getInstance().getItemRenderer();
if (this.stack.isOf(Items.TRIDENT)) {
currentModel = itemRenderer.getModels().getModelManager().getModel(ModelIdentifier.ofVanilla("trident", "inventory"));
} else if (this.stack.isOf(Items.SPYGLASS)) {
currentModel = itemRenderer.getModels().getModelManager().getModel(ModelIdentifier.ofVanilla("spyglass", "inventory"));
} else {
matrices.multiply(RotationAxis.POSITIVE_Y.rotationDegrees(180));
currentModel = itemRenderer.getModel(this.stack, MinecraftClient.getInstance().world, null, 0);
}
}

@Override
public void emitVertices(MatrixStack matrices, VertexConsumerProvider vertexConsumers, float tickDelta) {
final var itemRenderer = MinecraftClient.getInstance().getItemRenderer();
final var model = itemRenderer.getModel(this.stack, null, null, 0);

itemRenderer.renderItem(this.stack, ModelTransformationMode.FIXED, LightmapTextureManager.MAX_LIGHT_COORDINATE,
OverlayTexture.DEFAULT_UV, matrices, vertexConsumers, MinecraftClient.getInstance().world, 0);
matrices.pop();
itemRenderer.renderItem(
this.stack,
ModelTransformationMode.GUI,
false,
matrices,
vertexConsumers,
LightmapTextureManager.MAX_LIGHT_COORDINATE,
OverlayTexture.DEFAULT_UV,
new TransformlessBakedModel(model)
);
}

@Override
public void cleanUp() {
currentModel = null;
}

@Override
Expand All @@ -63,4 +93,15 @@ public ExportPathSpec exportPath() {
"item"
);
}

private static class TransformlessBakedModel extends ForwardingBakedModel {
public TransformlessBakedModel(BakedModel inner) {
this.wrapped = inner;
}

@Override
public ModelTransformation getTransformation() {
return ModelTransformation.NONE;
}
}
}

0 comments on commit 16ddc8e

Please sign in to comment.