Skip to content

Commit

Permalink
Fix regression: handle block colors in multi-state copycats on Forge
Browse files Browse the repository at this point in the history
  • Loading branch information
hlysine committed Jul 17, 2024
1 parent 68a17ab commit 93b0491
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,12 @@ static QuadUVScale uvScale(Direction face, float pivotU, float pivotV, float sca
abstract class Base<Source, Destination> implements CopycatRenderContext {
private final Source source;
private final Destination destination;
private final String key;

public Base(Source source, Destination destination) {
public Base(Source source, Destination destination, String key) {
this.source = source;
this.destination = destination;
this.key = key;
}

public Source source() {
Expand All @@ -379,5 +381,9 @@ public Source source() {
public Destination destination() {
return destination;
}

public String key() {
return key;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
public class CopycatRenderContextFabric extends CopycatRenderContext.Base<List<MutableQuadView>, QuadEmitter> {
public CopycatRenderContextFabric(List<MutableQuadView> source, QuadEmitter destination) {
super(source, destination);
public CopycatRenderContextFabric(List<MutableQuadView> source, QuadEmitter destination, String key) {
super(source, destination, key); // todo: find a way to store the key in quads
}

static SpriteFinder spriteFinder = SpriteFinder.get(Minecraft.getInstance().getModelManager().getAtlas(InventoryMenu.BLOCK_ATLAS));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.copycatsplus.copycats.foundation.copycat.model.assembly.*;
import com.copycatsplus.copycats.foundation.copycat.model.assembly.quad.QuadAutoCull;
import com.copycatsplus.copycats.foundation.copycat.model.assembly.quad.QuadTransform;
import com.copycatsplus.copycats.foundation.copycat.multistate.MultiStateTextureAtlasSprite;
import com.simibubi.create.foundation.model.BakedModelHelper;
import com.simibubi.create.foundation.model.BakedQuadHelper;
import net.minecraft.MethodsReturnNonnullByDefault;
Expand All @@ -21,10 +22,10 @@
@ApiStatus.Internal
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
public class CopycatRenderContextForge extends CopycatRenderContext.Base<List<CopycatRenderContextForge.CullingBakedQuad>, List<CopycatRenderContextForge.CullingBakedQuad>> {
public class CopycatRenderContextForge extends CopycatRenderContext.Base<List<CopycatRenderContextForge.CopycatBakedQuad>, List<CopycatRenderContextForge.CopycatBakedQuad>> {

public CopycatRenderContextForge(List<CullingBakedQuad> source, List<CullingBakedQuad> destination) {
super(source, destination);
public CopycatRenderContextForge(List<CopycatBakedQuad> source, List<CopycatBakedQuad> destination, String property) {
super(source, destination, property);
}

@Override
Expand All @@ -34,11 +35,11 @@ public void assemblePiece(AssemblyTransform assemblyTransform, MutableVec3 offse
assemblyTransform.apply(cull);
AABB aabb = select.toAABB();
Vec3 vec3 = offset.toVec3().subtract(select.minX, select.minY, select.minZ);
for (CullingBakedQuad quad : source()) {
for (CopycatBakedQuad quad : source()) {
if (cull.isCulled(quad.getDirection())) {
continue;
}
assembleQuad(quad, destination(), aabb, vec3, assemblyTransform);
assembleQuad(quad, destination(), key(), aabb, vec3, assemblyTransform);
}
}

Expand All @@ -49,40 +50,40 @@ public void assemblePiece(AssemblyTransform assemblyTransform, MutableVec3 offse
assemblyTransform.apply(cull);
AABB aabb = select.toAABB();
Vec3 vec3 = offset.toVec3().subtract(select.minX, select.minY, select.minZ);
for (CullingBakedQuad quad : source()) {
for (CopycatBakedQuad quad : source()) {
if (cull.isCulled(quad.getDirection())) {
continue;
}
assembleQuad(quad, destination(), aabb, vec3, assemblyTransform, transforms);
assembleQuad(quad, destination(), key(), aabb, vec3, assemblyTransform, transforms);
}
}

@Override
public void assembleAll() {
for (CullingBakedQuad quad : source()) {
for (CopycatBakedQuad quad : source()) {
assembleQuad(quad, destination());
}
}

private static void assembleQuad(CullingBakedQuad src, List<CullingBakedQuad> dest) {
private static void assembleQuad(CopycatBakedQuad src, List<CopycatBakedQuad> dest) {
dest.add(src);
}

@Override
public void assembleRaw(AABB crop, Vec3 move) {
for (CullingBakedQuad quad : source()) {
assembleQuad(quad, destination(), crop, move, AssemblyTransform.IDENTITY);
for (CopycatBakedQuad quad : source()) {
assembleQuad(quad, destination(), key(), crop, move, AssemblyTransform.IDENTITY);
}
}

@Override
public void assembleRaw(AABB crop, Vec3 move, QuadTransform... transforms) {
for (CullingBakedQuad quad : source()) {
assembleQuad(quad, destination(), crop, move, AssemblyTransform.IDENTITY, transforms);
for (CopycatBakedQuad quad : source()) {
assembleQuad(quad, destination(), key(), crop, move, AssemblyTransform.IDENTITY, transforms);
}
}

private static void assembleQuad(CullingBakedQuad src, List<CullingBakedQuad> dest, AABB crop, Vec3 move, AssemblyTransform assemblyTransform, QuadTransform... transforms) {
private static void assembleQuad(CopycatBakedQuad src, List<CopycatBakedQuad> dest, String key, AABB crop, Vec3 move, AssemblyTransform assemblyTransform, QuadTransform... transforms) {
int[] vertices = BakedModelHelper.cropAndMove(src.getVertices(), src.getSprite(), crop, move);
MutableQuad mutableQuad = getMutableQuad(vertices, src.cullFace);
assemblyTransform.apply(mutableQuad);
Expand All @@ -98,7 +99,7 @@ private static void assembleQuad(CullingBakedQuad src, List<CullingBakedQuad> de
BakedQuadHelper.setU(vertices, i, mutableQuad.vertices.get(i).uv.u);
BakedQuadHelper.setV(vertices, i, mutableQuad.vertices.get(i).uv.v);
}
dest.add(new CullingBakedQuad(vertices, src.getTintIndex(), mutableQuad.computeLightFace(), src.getSprite(), src.isShade(), mutableQuad.cullFace));
dest.add(new CopycatBakedQuad(vertices, src.getTintIndex(), mutableQuad.computeLightFace(), src.getSprite(), src.isShade(), mutableQuad.cullFace, key));
}

public static MutableQuad getMutableQuad(int[] vertexData, @Nullable Direction cullFace) {
Expand All @@ -111,17 +112,23 @@ public static MutableQuad getMutableQuad(int[] vertexData, @Nullable Direction c
return new MutableQuad(vertices, cullFace);
}

public static class CullingBakedQuad extends BakedQuad {
public static class CopycatBakedQuad extends BakedQuad {
@Nullable
public final Direction cullFace;
public final String property;

public CullingBakedQuad(int[] vertices, int tintIndex, Direction direction, TextureAtlasSprite sprite, boolean shade, @Nullable Direction cullFace) {
public CopycatBakedQuad(int[] vertices, int tintIndex, Direction direction, TextureAtlasSprite sprite, boolean shade, @Nullable Direction cullFace, String property) {
super(vertices, tintIndex, direction, sprite, shade);
this.cullFace = cullFace;
this.property = property;
}

public CullingBakedQuad(BakedQuad quad, @Nullable Direction cullFace) {
this(quad.getVertices(), quad.getTintIndex(), quad.getDirection(), quad.getSprite(), quad.isShade(), cullFace);
public CopycatBakedQuad(BakedQuad quad, @Nullable Direction cullFace, String property) {
this(quad.getVertices(), quad.getTintIndex(), quad.getDirection(), quad.getSprite(), quad.isShade(), cullFace, property);
}

public BakedQuad toBakedQuad() {
return new BakedQuad(getVertices(), getTintIndex(), getDirection(), new MultiStateTextureAtlasSprite(property, getSprite()), isShade());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.client.ChunkRenderTypeSet;
Expand All @@ -33,7 +32,6 @@
import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.stream.Collectors;

import static com.copycatsplus.copycats.CCBlockStateProperties.BASE_TYPE;
import static com.copycatsplus.copycats.foundation.copycat.CopycatBaseBlock.BASE_TYPE_COUNT;
Expand Down Expand Up @@ -186,11 +184,11 @@ private void gatherOcclusionData(BlockAndTintGetter world, BlockPos pos, BlockSt
}
}

protected @NotNull List<CopycatRenderContextForge.CullingBakedQuad> getQuads(BlockState state, @NotNull RandomSource rand, @NotNull ModelData data, RenderType renderType) {
protected @NotNull List<CopycatRenderContextForge.CopycatBakedQuad> getQuads(BlockState state, @NotNull RandomSource rand, @NotNull ModelData data, RenderType renderType) {

prepareModelCore(state, rand, data);

List<CopycatRenderContextForge.CullingBakedQuad> allQuads = new ArrayList<>();
List<CopycatRenderContextForge.CopycatBakedQuad> allQuads = new ArrayList<>();
Map<String, BlockState> materials = getMaterials(data);
Map<String, OcclusionData> occlusionDataMap = getOcclusion(data);
Map<String, ModelData> wrappedDataMap = getWrappedData(data);
Expand Down Expand Up @@ -221,22 +219,22 @@ private void gatherOcclusionData(BlockAndTintGetter world, BlockPos pos, BlockSt
continue;
}

List<CopycatRenderContextForge.CullingBakedQuad> quads = new ArrayList<>();
List<CopycatRenderContextForge.CopycatBakedQuad> quads = new ArrayList<>();
for (Direction side : Iterate.directions) {
List<BakedQuad> templateQuads = model.getQuads(wrappedState, side, rand, wrappedData, renderType);
for (BakedQuad templateQuad : templateQuads) {
quads.add(new CopycatRenderContextForge.CullingBakedQuad(templateQuad, side));
quads.add(new CopycatRenderContextForge.CopycatBakedQuad(templateQuad, side, entry.key()));
}
}
List<BakedQuad> templateQuads = model.getQuads(wrappedState, null, rand, wrappedData, renderType);
for (BakedQuad templateQuad : templateQuads) {
quads.add(new CopycatRenderContextForge.CullingBakedQuad(templateQuad, null));
quads.add(new CopycatRenderContextForge.CopycatBakedQuad(templateQuad, null, entry.key()));
}

List<CopycatRenderContextForge.CullingBakedQuad> croppedQuads = getCroppedQuads(entry, state, quads, material);
List<CopycatRenderContextForge.CopycatBakedQuad> croppedQuads = getCroppedQuads(entry, state, quads, material);

CopycatModelForge.OcclusionData occlusionData = occlusionDataMap.get(entry.key());
for (CopycatRenderContextForge.CullingBakedQuad croppedQuad : croppedQuads) {
for (CopycatRenderContextForge.CopycatBakedQuad croppedQuad : croppedQuads) {
if (occlusionData != null && occlusionData.isOccluded(croppedQuad.cullFace))
continue;

Expand All @@ -249,21 +247,21 @@ private void gatherOcclusionData(BlockAndTintGetter world, BlockPos pos, BlockSt

@Override
public @NotNull List<BakedQuad> getQuads(BlockState state, Direction side, @NotNull RandomSource rand, @NotNull ModelData data, RenderType renderType) {
List<CopycatRenderContextForge.CullingBakedQuad> templateQuads = renderSession.get().getQuads(state, rand, data, renderType);
List<CopycatRenderContextForge.CopycatBakedQuad> templateQuads = renderSession.get().getQuads(state, rand, data, renderType);
List<BakedQuad> quads = new ArrayList<>();
for (CopycatRenderContextForge.CullingBakedQuad quad : templateQuads) {
for (CopycatRenderContextForge.CopycatBakedQuad quad : templateQuads) {
if (side != quad.cullFace)
continue;
quads.add(new BakedQuad(quad.getVertices(), quad.getTintIndex(), quad.getDirection(), quad.getSprite(), quad.isShade()));
quads.add(quad.toBakedQuad());
}
return quads;
}

private List<CopycatRenderContextForge.CullingBakedQuad> getCroppedQuads(CopycatModelCore.ModelEntry entry, BlockState state, List<CopycatRenderContextForge.CullingBakedQuad> templateQuads, BlockState material) {
private List<CopycatRenderContextForge.CopycatBakedQuad> getCroppedQuads(CopycatModelCore.ModelEntry entry, BlockState state, List<CopycatRenderContextForge.CopycatBakedQuad> templateQuads, BlockState material) {
if (entry.part() == null)
return templateQuads;
List<CopycatRenderContextForge.CullingBakedQuad> quads = new ArrayList<>();
CopycatRenderContextForge context = new CopycatRenderContextForge(templateQuads, quads);
List<CopycatRenderContextForge.CopycatBakedQuad> quads = new ArrayList<>();
CopycatRenderContextForge context = new CopycatRenderContextForge(templateQuads, quads, entry.key());
entry.part().emitCopycatQuads(entry.key(), state, context, material);
return quads;
}
Expand Down Expand Up @@ -333,7 +331,7 @@ public boolean isOccluded(Direction face) {

@FunctionalInterface
public interface Renderer {
List<CopycatRenderContextForge.CullingBakedQuad> getQuads(BlockState state, @NotNull RandomSource rand, @NotNull ModelData data, RenderType renderType);
List<CopycatRenderContextForge.CopycatBakedQuad> getQuads(BlockState state, @NotNull RandomSource rand, @NotNull ModelData data, RenderType renderType);
}

public static class RenderSession implements Renderer {
Expand All @@ -342,14 +340,14 @@ public static class RenderSession implements Renderer {
private RandomSource rand = null;
private ModelData data = null;
private RenderType renderType = null;
private List<CopycatRenderContextForge.CullingBakedQuad> result = null;
private List<CopycatRenderContextForge.CopycatBakedQuad> result = null;

public RenderSession(Renderer renderer) {
this.renderer = renderer;
}

@Override
public List<CopycatRenderContextForge.CullingBakedQuad> getQuads(BlockState state, @NotNull RandomSource rand, @NotNull ModelData data, RenderType renderType) {
public List<CopycatRenderContextForge.CopycatBakedQuad> getQuads(BlockState state, @NotNull RandomSource rand, @NotNull ModelData data, RenderType renderType) {
if (Objects.equals(this.state, state) && this.rand == rand && this.data == data && this.renderType == renderType && this.result != null) {
return result;
}
Expand Down

0 comments on commit 93b0491

Please sign in to comment.