Skip to content

Commit

Permalink
Persist misc data when baking and unbaking quads, pass the default st…
Browse files Browse the repository at this point in the history
…ate into rendering ISTERs, and remove unneeded manual end batch call for rendering balloons
  • Loading branch information
pupnewfster committed Aug 11, 2023
1 parent d7fc135 commit a961bcb
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public void render(@NotNull EntityBalloon balloon, float entityYaw, float partia
}
builder.putBulkData(last, quad, color[0], color[1], color[2], color[3], light, OverlayTexture.NO_OVERLAY, false);
}
((MultiBufferSource.BufferSource) renderer).endBatch(renderType);
matrix.popPose();
super.render(balloon, entityYaw, partialTick, matrix, renderer, light);
}
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/mekanism/client/render/item/MekanismISTER.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.HalfTransparentBlock;
import net.minecraft.world.level.block.StainedGlassPaneBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.client.model.data.ModelData;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -70,8 +71,9 @@ protected void renderBlockItem(@NotNull ItemStack stack, @NotNull ItemDisplayCon
}
Minecraft minecraft = Minecraft.getInstance();
ItemRenderer itemRenderer = minecraft.getItemRenderer();
BlockState defaultState = block.defaultBlockState();
//TODO: See if we can come up with a better way to handle getting the model, maybe even one that supports non block items??
BakedModel baseModel = minecraft.getModelManager().getBlockModelShaper().getBlockModel(block.defaultBlockState());
BakedModel baseModel = minecraft.getModelManager().getBlockModelShaper().getBlockModel(defaultState);
long seed = 42;
RandomSource random = RandomSource.create();
boolean hasEffect = stack.hasFoil();
Expand All @@ -86,10 +88,10 @@ protected void renderBlockItem(@NotNull ItemStack stack, @NotNull ItemDisplayCon
//Note: Manually call the render quads lists rather than using renderModelLists so that we can pass the proper render type and model data
for (Direction direction : EnumUtils.DIRECTIONS) {
random.setSeed(seed);
itemRenderer.renderQuadList(matrix, buffer, model.getQuads(null, direction, random, modelData, renderType), stack, light, overlayLight);
itemRenderer.renderQuadList(matrix, buffer, model.getQuads(defaultState, direction, random, modelData, renderType), stack, light, overlayLight);
}
random.setSeed(seed);
itemRenderer.renderQuadList(matrix, buffer, model.getQuads(null, null, random, modelData, renderType), stack, light, overlayLight);
itemRenderer.renderQuadList(matrix, buffer, model.getQuads(defaultState, null, random, modelData, renderType), stack, light, overlayLight);
}
}
}
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/mekanism/client/render/lib/Quad.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.blaze3d.vertex.VertexFormatElement;
import java.util.Arrays;
import java.util.function.Consumer;
import mekanism.api.annotations.NothingNullByDefault;
import mekanism.common.lib.Color;
import net.minecraft.client.renderer.LightTexture;
import net.minecraft.client.renderer.block.model.BakedQuad;
Expand All @@ -13,7 +16,6 @@
import net.minecraft.core.Vec3i;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.client.model.pipeline.QuadBakingVertexConsumer;
import org.jetbrains.annotations.NotNull;
import org.joml.Vector3f;

public class Quad {
Expand Down Expand Up @@ -122,7 +124,7 @@ public BakedQuad bake() {
public Quad copy() {
Vertex[] newVertices = new Vertex[vertices.length];
for (int i = 0; i < newVertices.length; i++) {
newVertices[i] = vertices[i].copy();
newVertices[i] = vertices[i].copy(true);
}
return new Quad(sprite, side, newVertices, tintIndex, shade, hasAmbientOcclusion);
}
Expand All @@ -135,47 +137,42 @@ public Quad flip() {
return new Quad(sprite, side.getOpposite(), flipped, tintIndex, shade, hasAmbientOcclusion);
}

@NothingNullByDefault
private class BakedQuadUnpacker implements VertexConsumer {

private Vertex vertex = new Vertex();
private int vertexIndex = 0;

@NotNull
@Override
public VertexConsumer vertex(double x, double y, double z) {
vertex.pos(new Vec3(x, y, z));
return this;
}

@NotNull
@Override
public VertexConsumer color(int red, int green, int blue, int alpha) {
vertex.color(red, green, blue, alpha);
return this;
}

@NotNull
@Override
public VertexConsumer uv(float u, float v) {
vertex.texRaw(u, v);
return this;
}

@NotNull
@Override
public VertexConsumer overlayCoords(int u, int v) {
vertex.overlay(u, v);
return this;
}

@NotNull
@Override
public VertexConsumer uv2(int u, int v) {
vertex.lightRaw(u, v);
return this;
}

@NotNull
@Override
public VertexConsumer normal(float x, float y, float z) {
vertex.normal(x, y, z);
Expand All @@ -199,6 +196,12 @@ public void defaultColor(int red, int green, int blue, int alpha) {
public void unsetDefaultColor() {
//We don't support having a default color
}

@Override
public VertexConsumer misc(VertexFormatElement element, int... rawData) {
vertex.misc(element, Arrays.copyOf(rawData, rawData.length));
return this;
}
}

public static class Builder {
Expand Down
39 changes: 36 additions & 3 deletions src/main/java/mekanism/client/render/lib/Vertex.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package mekanism.client.render.lib;

import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.blaze3d.vertex.VertexFormatElement;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import mekanism.common.lib.Color;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
Expand All @@ -9,6 +13,8 @@

public class Vertex {

private final Map<VertexFormatElement, int[]> miscData;

private Vec3 pos;
private Vector3f normal;

Expand All @@ -23,13 +29,19 @@ public class Vertex {
private int lightU, lightV;

public Vertex() {
this.miscData = new HashMap<>();
}

public Vertex(Vec3 pos, Vector3f normal, Color color, float texU, float texV, int overlayU, int overlayV, int lightU, int lightV) {
this(pos, normal, color.r(), color.g(), color.b(), color.a(), texU, texV, overlayU, overlayV, lightU, lightV);
}

public Vertex(Vec3 pos, Vector3f normal, int red, int green, int blue, int alpha, float texU, float texV, int overlayU, int overlayV, int lightU, int lightV) {
this(pos, normal, red, green, blue, alpha, texU, texV, overlayU, overlayV, lightU, lightV, new HashMap<>());
}

public Vertex(Vec3 pos, Vector3f normal, int red, int green, int blue, int alpha, float texU, float texV, int overlayU, int overlayV, int lightU, int lightV,
Map<VertexFormatElement, int[]> miscData) {
this.pos = pos;
this.normal = normal;
this.red = red;
Expand All @@ -42,6 +54,7 @@ public Vertex(Vec3 pos, Vector3f normal, int red, int green, int blue, int alpha
this.overlayV = overlayV;
this.lightU = lightU;
this.lightV = lightV;
this.miscData = miscData;
}

public static Vertex create(Vec3 pos, Vector3f normal, Color color, TextureAtlasSprite sprite, float texU, float texV, int overlayU, int overlayV, int lightU,
Expand Down Expand Up @@ -149,12 +162,29 @@ public Vertex light(int u, int v) {
return lightRaw(u << 4, v << 4);
}

public Vertex misc(VertexFormatElement element, int... data) {
miscData.put(element, data);
return this;
}

public Vertex flip() {
return copy().normal(-normal.x(), -normal.y(), -normal.z());
return flip(true);
}

public Vertex flip(boolean deepCopy) {
return copy(deepCopy).normal(-normal.x(), -normal.y(), -normal.z());
}

public Vertex copy() {
return new Vertex(pos, normal, red, green, blue, alpha, texU, texV, overlayU, overlayV, lightU, lightV);
public Vertex copy(boolean deepCopy) {
if (deepCopy) {
//Deep copy the misc data
Map<VertexFormatElement, int[]> miscCopy = new HashMap<>();
for (Map.Entry<VertexFormatElement, int[]> entry : miscData.entrySet()) {
miscCopy.put(entry.getKey(), Arrays.copyOf(entry.getValue(), entry.getValue().length));
}
return new Vertex(pos, new Vector3f(normal), red, green, blue, alpha, texU, texV, overlayU, overlayV, lightU, lightV, miscCopy);
}
return new Vertex(pos, normal, red, green, blue, alpha, texU, texV, overlayU, overlayV, lightU, lightV, miscData);
}

public void write(VertexConsumer consumer) {
Expand All @@ -164,6 +194,9 @@ public void write(VertexConsumer consumer) {
consumer.overlayCoords(overlayU, overlayV);
consumer.uv2(lightU, lightV);
consumer.normal(normal.x(), normal.y(), normal.z());
for (Map.Entry<VertexFormatElement, int[]> entry : miscData.entrySet()) {
consumer.misc(entry.getKey(), entry.getValue());
}
consumer.endVertex();
}
}

0 comments on commit a961bcb

Please sign in to comment.