Skip to content

Commit 9057b18

Browse files
Update ThickBranchBlockBakedModel.java
7x7 Trunk Support
1 parent bc484dc commit 9057b18

1 file changed

Lines changed: 68 additions & 34 deletions

File tree

src/main/java/com/ferreusveritas/dynamictrees/models/baked/ThickBranchBlockBakedModel.java

Lines changed: 68 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@
4545
@OnlyIn(Dist.CLIENT)
4646
public class ThickBranchBlockBakedModel extends BasicBranchBlockBakedModel {
4747

48-
private final BakedModel[] trunksBark = new BakedModel[16]; // The trunk will always feature bark on its sides.
49-
private final BakedModel[] trunksTopBark = new BakedModel[16]; // The trunk will feature bark on its top when there's a branch on top of it.
50-
private final BakedModel[] trunksTopRings = new BakedModel[16]; // The trunk will feature rings on its top when there's no branches on top of it.
51-
private final BakedModel[] trunksBotRings = new BakedModel[16]; // The trunk will always feature rings on its bottom surface if nothing is below it.
48+
private final BakedModel[] trunksBark = new BakedModel[ThickBranchBlock.MAX_RADIUS_THICK - BranchBlock.MAX_RADIUS];
49+
private final BakedModel[] trunksTopBark = new BakedModel[ThickBranchBlock.MAX_RADIUS_THICK - BranchBlock.MAX_RADIUS];
50+
private final BakedModel[] trunksTopRings = new BakedModel[ThickBranchBlock.MAX_RADIUS_THICK - BranchBlock.MAX_RADIUS];
51+
private final BakedModel[] trunksBotRings = new BakedModel[ThickBranchBlock.MAX_RADIUS_THICK - BranchBlock.MAX_RADIUS];
5252

5353
public ThickBranchBlockBakedModel(IGeometryBakingContext customData, ResourceLocation modelLocation, ResourceLocation barkTextureLocation, ResourceLocation ringsTextureLocation,
5454
ResourceLocation thickRingsTextureLocation, Function<Material, TextureAtlasSprite> spriteGetter) {
@@ -74,27 +74,56 @@ private boolean isTextureNull(@Nullable TextureAtlasSprite sprite) {
7474
return sprite == null || sprite.equals(ModelUtils.getTexture(new ResourceLocation("")));
7575
}
7676

77-
public BakedModel bakeTrunkBark(int radius, TextureAtlasSprite bark, boolean side) {
77+
/**
78+
* Determines grid size based on radius.
79+
* @return 3 for radius 9-24, 5 for radius 25-40, 7 for radius 41-56
80+
*/
81+
private int getGridSize(int radius) {
82+
if (radius > ThickBranchBlock.RADIUS_TO_OUTERMOST_SHELL) {
83+
return 7;
84+
} else if (radius > ThickBranchBlock.RADIUS_TO_OUTER_SHELL) {
85+
return 5;
86+
} else {
87+
return 3;
88+
}
89+
}
90+
91+
/**
92+
* Generates grid offsets based on grid size.
93+
*/
94+
private ArrayList<Vec3i> getGridOffsets(int gridSize) {
95+
ArrayList<Vec3i> offsets = new ArrayList<>();
96+
int halfGrid = gridSize / 2;
97+
for (int x = -halfGrid; x <= halfGrid; x++) {
98+
for (int z = -halfGrid; z <= halfGrid; z++) {
99+
offsets.add(new Vec3i(x, 0, z));
100+
}
101+
}
102+
return offsets;
103+
}
78104

105+
public BakedModel bakeTrunkBark(int radius, TextureAtlasSprite bark, boolean side) {
79106
IModelBuilder<?> builder = ModelUtils.getModelBuilder(this.blockModel.customData, bark);
80107
AABB wholeVolume = new AABB(8 - radius, 0, 8 - radius, 8 + radius, 16, 8 + radius);
81108

82109
final Direction[] run = side ? CoordUtils.HORIZONTALS : new Direction[]{Direction.UP, Direction.DOWN};
83-
ArrayList<Vec3i> offsets = new ArrayList<>();
84110

85-
for (Surround dir : Surround.values()) {
86-
offsets.add(dir.getOffset()); // 8 surrounding component pieces
87-
}
88-
offsets.add(new Vec3i(0, 0, 0));//Center
111+
int gridSize = getGridSize(radius);
112+
ArrayList<Vec3i> offsets = getGridOffsets(gridSize);
89113

90114
for (Direction face : run) {
91115
final Vec3i dirVector = face.getNormal();
92116

93117
for (Vec3i offset : offsets) {
94-
if (face.getAxis() == Axis.Y || new Vec3(dirVector.getX(), dirVector.getY(), dirVector.getZ()).add(new Vec3(offset.getX(), offset.getY(), offset.getZ())).lengthSqr() > 2.25) { //This means that the dir and face share a common direction
95-
Vec3 scaledOffset = new Vec3(offset.getX() * 16, offset.getY() * 16, offset.getZ() * 16);//Scale the dimensions to match standard minecraft texels
118+
if (face.getAxis() == Axis.Y || new Vec3(dirVector.getX(), dirVector.getY(), dirVector.getZ()).add(new Vec3(offset.getX(), offset.getY(), offset.getZ())).lengthSqr() > 2.25) {
119+
Vec3 scaledOffset = new Vec3(offset.getX() * 16, offset.getY() * 16, offset.getZ() * 16);
96120
AABB partBoundary = new AABB(0, 0, 0, 16, 16, 16).move(scaledOffset).intersect(wholeVolume);
97121

122+
// Skip if intersection is empty
123+
if (partBoundary.getXsize() <= 0 || partBoundary.getYsize() <= 0 || partBoundary.getZsize() <= 0) {
124+
continue;
125+
}
126+
98127
Vector3f[] limits = ModelUtils.AABBLimits(partBoundary);
99128

100129
Map<Direction, BlockElementFace> mapFacesIn = Maps.newEnumMap(Direction.class);
@@ -105,7 +134,6 @@ public BakedModel bakeTrunkBark(int radius, TextureAtlasSprite bark, boolean sid
105134
BlockElement part = new BlockElement(limits[0], limits[1], mapFacesIn, null, true);
106135
builder.addCulledFace(face, ModelUtils.makeBakedQuad(part, part.faces.get(face), bark, face, BlockModelRotation.X0_Y0, this.modelLocation));
107136
}
108-
109137
}
110138
}
111139

@@ -115,34 +143,38 @@ public BakedModel bakeTrunkBark(int radius, TextureAtlasSprite bark, boolean sid
115143
public BakedModel bakeTrunkRings(int radius, TextureAtlasSprite ring, Direction face) {
116144
IModelBuilder<?> builder = ModelUtils.getModelBuilder(this.blockModel.customData, ring);
117145
AABB wholeVolume = new AABB(8 - radius, 0, 8 - radius, 8 + radius, 16, 8 + radius);
118-
int wholeVolumeWidth = 48;
119146

120-
ArrayList<Vec3i> offsets = new ArrayList<>();
147+
int gridSize = getGridSize(radius);
148+
// Texture width: 48 for 3×3, 80 for 5×5, 112 for 7×7
149+
int wholeVolumeWidth = gridSize * 16;
121150

122-
for (Surround dir : Surround.values()) {
123-
offsets.add(dir.getOffset()); // 8 surrounding component pieces
124-
}
125-
offsets.add(new Vec3i(0, 0, 0)); // Center
151+
ArrayList<Vec3i> offsets = getGridOffsets(gridSize);
152+
153+
// Texture offset based on grid size
154+
float textureOffset = -(gridSize / 2) * 16f;
126155

127156
for (Vec3i offset : offsets) {
128-
Vec3 scaledOffset = new Vec3(offset.getX() * 16, offset.getY() * 16, offset.getZ() * 16); // Scale the dimensions to match standard minecraft texels
157+
Vec3 scaledOffset = new Vec3(offset.getX() * 16, offset.getY() * 16, offset.getZ() * 16);
129158
AABB partBoundary = new AABB(0, 0, 0, 16, 16, 16).move(scaledOffset).intersect(wholeVolume);
130159

160+
// Skip if intersection is empty
161+
if (partBoundary.getXsize() <= 0 || partBoundary.getYsize() <= 0 || partBoundary.getZsize() <= 0) {
162+
continue;
163+
}
164+
131165
Vector3f posFrom = new Vector3f((float) partBoundary.minX, (float) partBoundary.minY, (float) partBoundary.minZ);
132166
Vector3f posTo = new Vector3f((float) partBoundary.maxX, (float) partBoundary.maxY, (float) partBoundary.maxZ);
133167

134168
Map<Direction, BlockElementFace> mapFacesIn = Maps.newEnumMap(Direction.class);
135-
float textureOffsetX = -16f;
136-
float textureOffsetZ = -16f;
137169

138-
float minX = ((float) ((partBoundary.minX - textureOffsetX) / wholeVolumeWidth)) * 16f;
139-
float maxX = ((float) ((partBoundary.maxX - textureOffsetX) / wholeVolumeWidth)) * 16f;
140-
float minZ = ((float) ((partBoundary.minZ - textureOffsetZ) / wholeVolumeWidth)) * 16f;
141-
float maxZ = ((float) ((partBoundary.maxZ - textureOffsetZ) / wholeVolumeWidth)) * 16f;
170+
float minX = ((float) ((partBoundary.minX - textureOffset) / wholeVolumeWidth)) * 16f;
171+
float maxX = ((float) ((partBoundary.maxX - textureOffset) / wholeVolumeWidth)) * 16f;
172+
float minZ = ((float) ((partBoundary.minZ - textureOffset) / wholeVolumeWidth)) * 16f;
173+
float maxZ = ((float) ((partBoundary.maxZ - textureOffset) / wholeVolumeWidth)) * 16f;
142174

143175
if (face == Direction.DOWN) {
144-
minZ = ((float) ((partBoundary.maxZ - textureOffsetZ) / wholeVolumeWidth)) * 16f;
145-
maxZ = ((float) ((partBoundary.minZ - textureOffsetZ) / wholeVolumeWidth)) * 16f;
176+
minZ = ((float) ((partBoundary.maxZ - textureOffset) / wholeVolumeWidth)) * 16f;
177+
maxZ = ((float) ((partBoundary.minZ - textureOffset) / wholeVolumeWidth)) * 16f;
146178
}
147179

148180
float[] uvs = new float[]{minX, minZ, maxX, maxZ};
@@ -170,7 +202,7 @@ public List<BakedQuad> getQuads(@Nullable final BlockState state, @Nullable fina
170202
return super.getQuads(state, null, rand, extraData, renderType);
171203
}
172204

173-
coreRadius = Mth.clamp(coreRadius, 9, 24);
205+
coreRadius = Mth.clamp(coreRadius, 9, ThickBranchBlock.MAX_RADIUS_THICK);
174206

175207
List<BakedQuad> quads = new ArrayList<>(30);
176208

@@ -198,24 +230,26 @@ public List<BakedQuad> getQuads(@Nullable final BlockState state, @Nullable fina
198230
return quads;
199231
}
200232

233+
int arrayIndex = coreRadius - 9;
234+
201235
if (forceRingDir != null) {
202236
connections[forceRingDir.get3DDataValue()] = 0;
203-
quads.addAll(this.trunksBotRings[coreRadius - 9].getQuads(state, forceRingDir, rand, extraData, renderType));
237+
quads.addAll(this.trunksBotRings[arrayIndex].getQuads(state, forceRingDir, rand, extraData, renderType));
204238
}
205239

206240
boolean branchesAround = connections[2] + connections[3] + connections[4] + connections[5] != 0;
207241
for (Direction face : Direction.values()) {
208-
quads.addAll(this.trunksBark[coreRadius - 9].getQuads(state, face, rand, extraData, renderType));
242+
quads.addAll(this.trunksBark[arrayIndex].getQuads(state, face, rand, extraData, renderType));
209243
if (face == Direction.UP || face == Direction.DOWN) {
210244
if (connections[face.get3DDataValue()] < twigRadius && !branchesAround) {
211-
quads.addAll(this.trunksTopRings[coreRadius - 9].getQuads(state, face, rand, extraData, renderType));
245+
quads.addAll(this.trunksTopRings[arrayIndex].getQuads(state, face, rand, extraData, renderType));
212246
} else if (connections[face.get3DDataValue()] < coreRadius) {
213-
quads.addAll(this.trunksTopBark[coreRadius - 9].getQuads(state, face, rand, extraData, renderType));
247+
quads.addAll(this.trunksTopBark[arrayIndex].getQuads(state, face, rand, extraData, renderType));
214248
}
215249
}
216250
}
217251

218252
return quads;
219253
}
220254

221-
}
255+
}

0 commit comments

Comments
 (0)