Skip to content

Commit

Permalink
Quartz glass: Render unconnected when no modeldata is provided, and s…
Browse files Browse the repository at this point in the history
…implify model
  • Loading branch information
Technici4n committed Jan 27, 2025
1 parent 37238ee commit 38f7811
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 50 deletions.
37 changes: 7 additions & 30 deletions src/main/java/appeng/client/render/model/GlassBakedModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.IntStream;

Expand Down Expand Up @@ -61,8 +61,6 @@ class GlassBakedModel implements IDynamicBakedModel {
// rendered
public static final ModelProperty<GlassState> GLASS_STATE = new ModelProperty<>();

private static final byte[][][] OFFSETS = generateOffsets();

// Alternating textures based on position
static final Material TEXTURE_A = new Material(TextureAtlas.LOCATION_BLOCKS,
ResourceLocation.parse("ae2:block/glass/quartz_glass_a"));
Expand Down Expand Up @@ -114,21 +112,13 @@ public List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction
return Collections.emptyList();
}

final GlassState glassState = extraData.get(GLASS_STATE);

if (glassState == null) {
return Collections.emptyList();
}

// TODO: This could just use the Random instance we're given...
final int cx = Math.abs(glassState.getX() % 10);
final int cy = Math.abs(glassState.getY() % 10);
final int cz = Math.abs(glassState.getZ() % 10);
GlassState glassState = Objects.requireNonNullElse(extraData.get(GLASS_STATE), GlassState.DEFAULT);

var u = (OFFSETS[cx][cy][cz] % 4) / 16f;
var v = (OFFSETS[9 - cx][9 - cy][9 - cz] % 4) / 16f;
int randomOffset = rand.nextInt(4);
var u = randomOffset / 16f;
var v = rand.nextInt(4) / 16f;

int texIdx = Math.abs((OFFSETS[cx][cy][cz] + (glassState.getX() + glassState.getY() + glassState.getZ())) % 4);
int texIdx = (randomOffset + rand.nextInt(4)) % 4;

if (texIdx < 2) {
u /= 2;
Expand Down Expand Up @@ -270,19 +260,6 @@ public TextureAtlasSprite getParticleIcon() {
return this.frameTextures[this.frameTextures.length - 1];
}

private static byte[][][] generateOffsets() {
final Random r = new Random(924);
final byte[][][] offset = new byte[10][10][10];

for (int x = 0; x < 10; x++) {
for (int y = 0; y < 10; y++) {
r.nextBytes(offset[x][y]);
}
}

return offset;
}

private static GlassState getGlassState(BlockAndTintGetter level, BlockState state, BlockPos pos) {
/*
* This needs some explanation: The bit-field contains 4-bits, one for each direction that a frame may be drawn.
Expand All @@ -301,7 +278,7 @@ private static GlassState getGlassState(BlockAndTintGetter level, BlockState sta
adjacentGlassBlocks[facing.get3DDataValue()] = isGlassBlock(level, state, pos, facing.getOpposite(),
facing);
}
return new GlassState(pos.getX(), pos.getY(), pos.getZ(), masks, adjacentGlassBlocks);
return new GlassState(masks, adjacentGlassBlocks);
}

/**
Expand Down
45 changes: 25 additions & 20 deletions src/main/java/appeng/decorative/solid/GlassState.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,51 @@

package appeng.decorative.solid;

import java.util.Arrays;
import java.util.Objects;

import net.minecraft.core.Direction;

/**
* Immutable (and thus thread-safe) class that encapsulates the rendering state required for a connected texture glass
* block.
*/
public final class GlassState {

private final int x;
private final int y;
private final int z;
public static final GlassState DEFAULT;
static {
var masks = new int[6];
Arrays.fill(masks, 0b1111); // Render all 4 borders
var adjacentGlassBlocks = new boolean[6]; // Not adjacent to any glass block
DEFAULT = new GlassState(masks, adjacentGlassBlocks);
}

private final int[] masks;
private final boolean[] adjacentGlassBlocks;

public GlassState(int x, int y, int z, int[] masks, boolean[] adjacentGlassBlocks) {
this.x = x;
this.y = y;
this.z = z;
public GlassState(int[] masks, boolean[] adjacentGlassBlocks) {
this.masks = masks.clone();
this.adjacentGlassBlocks = adjacentGlassBlocks.clone();
}

public int getX() {
return this.x;
}

public int getY() {
return this.y;
}

public int getZ() {
return this.z;
}

public int getMask(Direction side) {
return masks[side.get3DDataValue()];
}

public boolean hasAdjacentGlassBlock(Direction side) {
return adjacentGlassBlocks[side.get3DDataValue()];
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof GlassState that))
return false;
return Arrays.equals(masks, that.masks) && Arrays.equals(adjacentGlassBlocks, that.adjacentGlassBlocks);
}

@Override
public int hashCode() {
return Objects.hash(Arrays.hashCode(masks), Arrays.hashCode(adjacentGlassBlocks));
}
}

0 comments on commit 38f7811

Please sign in to comment.