Skip to content

Commit

Permalink
chore: improve block mesh generator (#4623)
Browse files Browse the repository at this point in the history
  • Loading branch information
pollend authored May 14, 2021
1 parent 32e444b commit c53b41f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0
package org.terasology.engine.rendering.primitives;

import com.google.common.collect.Maps;
import org.joml.Vector3ic;
import org.terasology.gestalt.assets.ResourceUrn;
import org.terasology.engine.math.Side;
Expand All @@ -14,8 +13,6 @@
import org.terasology.engine.world.block.BlockPart;
import org.terasology.engine.world.block.shapes.BlockMeshPart;

import java.util.Map;

public class BlockMeshGeneratorSingleShape implements BlockMeshGenerator {

private Block block;
Expand All @@ -30,14 +27,14 @@ public void generateChunkMesh(ChunkView view, ChunkMesh chunkMesh, int x, int y,
final Block selfBlock = view.getBlock(x, y, z);

// Gather adjacent blocks
final Map<Side, Block> adjacentBlocks = Maps.newEnumMap(Side.class);
Block[] adjacentBlocks = new Block[Side.values().length];
for (Side side : Side.getAllSides()) {
Vector3ic offset = side.direction();
Block blockToCheck = view.getBlock(x + offset.x(), y + offset.y(), z + offset.z());
adjacentBlocks.put(side, blockToCheck);
adjacentBlocks[side.ordinal()] = blockToCheck;
}
for (final Side side : Side.getAllSides()) {
if (isSideVisibleForBlockTypes(adjacentBlocks.get(side), selfBlock, side)) {
if (isSideVisibleForBlockTypes(adjacentBlocks[side.ordinal()], selfBlock, side)) {
final ChunkMesh.RenderType renderType = getRenderType(selfBlock);
final BlockAppearance blockAppearance = selfBlock.getPrimaryAppearance();
final ChunkVertexFlag vertexFlag = getChunkVertexFlag(view, x, y, z, selfBlock);
Expand All @@ -50,12 +47,12 @@ public void generateChunkMesh(ChunkView view, ChunkMesh chunkMesh, int x, int y,

// If the selfBlock isn't lowered, some more faces may have to be drawn
if (selfBlock.isLiquid()) {
final Block topBlock = adjacentBlocks.get(Side.TOP);
final Block topBlock = adjacentBlocks[Side.TOP.ordinal()];
// Draw horizontal sides if visible from below
if (topBlock.isLiquid() && Side.horizontalSides().contains(side)) {
final Vector3ic offset = side.direction();
final Block adjacentAbove = view.getBlock(x + offset.x(), y + 1, z + offset.z());
final Block adjacent = adjacentBlocks.get(side);
final Block adjacent = adjacentBlocks[side.ordinal()];

if (adjacent.isLiquid() && !adjacentAbove.isLiquid()) {
blockMeshPart = selfBlock.getTopLiquidMesh(side);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ public final class Block {
private boolean stackable = true;

private BlockAppearance primaryAppearance = new BlockAppearance();
private Map<Side, BlockMeshPart> lowLiquidMesh = Maps.newEnumMap(Side.class);
private Map<Side, BlockMeshPart> topLiquidMesh = Maps.newEnumMap(Side.class);
private BlockMeshPart[] lowLiquidMesh = new BlockMeshPart[Side.values().length];
private BlockMeshPart[] topLiquidMesh = new BlockMeshPart[Side.values().length];

/* Collision */
private CollisionShape collisionShape;
Expand Down Expand Up @@ -528,19 +528,19 @@ public Mesh getMesh() {
}

public BlockMeshPart getLowLiquidMesh(Side side) {
return lowLiquidMesh.get(side);
return lowLiquidMesh[side.ordinal()];
}

public void setLowLiquidMesh(Side side, BlockMeshPart meshPart) {
lowLiquidMesh.put(side, meshPart);
lowLiquidMesh[side.ordinal()] = meshPart;
}

public BlockMeshPart getTopLiquidMesh(Side side) {
return topLiquidMesh.get(side);
return topLiquidMesh[side.ordinal()];
}

public void setTopLiquidMesh(Side side, BlockMeshPart meshPart) {
topLiquidMesh.put(side, meshPart);
topLiquidMesh[side.ordinal()] = meshPart;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class BlockUri implements Uri, Comparable<BlockUri> {
private final ResourceUrn blockFamilyDefinition;
private final Optional<ResourceUrn> shape;
private final Name blockName;
private final int hashCode;

public BlockUri(String uri) throws BlockUriParseException {
try {
Expand Down Expand Up @@ -62,6 +63,8 @@ public BlockUri(String uri) throws BlockUriParseException {
} catch (InvalidUrnException e) {
throw new BlockUriParseException("Could not parse block uri: '" + uri + "'", e);
}
this.hashCode = hashCode();

}

public BlockUri(ResourceUrn blockFamilyDefinition) {
Expand All @@ -88,8 +91,11 @@ private BlockUri(ResourceUrn blockFamilyDefinition, Optional<ResourceUrn> shape,
this.blockFamilyDefinition = blockFamilyDefinition;
this.shape = shape;
this.blockName = blockName;

this.hashCode = hashCode();
}


@Override
public Name getModuleName() {
return blockFamilyDefinition.getModuleName();
Expand Down Expand Up @@ -156,7 +162,8 @@ public boolean equals(Object obj) {
}
if (obj instanceof BlockUri) {
BlockUri other = (BlockUri) obj;
return Objects.equal(other.blockFamilyDefinition, blockFamilyDefinition)
return this.hashCode == other.hashCode
&& Objects.equal(other.blockFamilyDefinition, blockFamilyDefinition)
&& Objects.equal(other.blockName, blockName)
&& Objects.equal(other.shape, shape);
}
Expand Down

0 comments on commit c53b41f

Please sign in to comment.