Skip to content

Commit

Permalink
Optimize shape calls
Browse files Browse the repository at this point in the history
  • Loading branch information
ferriarnus committed Aug 19, 2023
1 parent 727efa4 commit e58cb85
Showing 1 changed file with 19 additions and 34 deletions.
53 changes: 19 additions & 34 deletions src/conduits/java/com/enderio/conduits/common/ConduitShape.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private <T> T getLookUpValue(Map<T, VoxelShape> shapes, BlockPos pos, HitResult

private void updateTotalShape() {
this.totalShape = Shapes.empty();
this.conduitShapes.values().forEach(s -> this.totalShape = Shapes.join(this.totalShape, s, BooleanOp.OR));
this.conduitShapes.values().forEach(s -> this.totalShape = Shapes.joinUnoptimized(this.totalShape, s, BooleanOp.OR));
totalShape.optimize();
}

Expand All @@ -86,8 +86,8 @@ private void updateShapeForConduit(ConduitBundle conduitBundle, IConduitType<?>
VoxelShape directionShape = directionShapes.getOrDefault(direction, Shapes.empty());
if (conduitBundle.getConnection(direction).getConnectionState(conduitType) instanceof DynamicConnectionState) {
VoxelShape connectorShape = rotateVoxelShape(connector, direction);
directionShape = Shapes.join(directionShape, connectorShape, BooleanOp.OR);
conduitShape = Shapes.join(conduitShape, connectorShape, BooleanOp.OR);
directionShape = Shapes.joinUnoptimized(directionShape, connectorShape, BooleanOp.OR);
conduitShape = Shapes.joinUnoptimized(conduitShape, connectorShape, BooleanOp.OR);
}
var connectedTypes = conduitBundle.getConnection(direction).getConnectedTypes();
if (connectedTypes.contains(conduitType)) {
Expand All @@ -96,16 +96,15 @@ private void updateShapeForConduit(ConduitBundle conduitBundle, IConduitType<?>
offsets.computeIfAbsent(conduitType, ignored -> new ArrayList<>()).add(offset);
VoxelShape connectionShape = rotateVoxelShape(connection, direction).move(offset.getX() * 3f / 16f, offset.getY() * 3f / 16f,
offset.getZ() * 3f / 16f);
directionShape = Shapes.join(directionShape, connectionShape, BooleanOp.OR);
conduitShape = Shapes.join(conduitShape, connectionShape, BooleanOp.OR);
directionShape = Shapes.joinUnoptimized(directionShape, connectionShape, BooleanOp.OR);
conduitShape = Shapes.joinUnoptimized(conduitShape, connectionShape, BooleanOp.OR);
}
directionShapes.put(direction, directionShape.optimize());
}

var allTypes = conduitBundle.getTypes();
@Nullable Area box = null;
Map<IConduitType<?>, Integer> notRendered = new HashMap<>();
List<IConduitType<?>> rendered = new ArrayList<>();
@Nullable IConduitType<?> notRendered = null;
int i = allTypes.indexOf(conduitType);
if (i == -1) {
conduitShapes.put(conduitType, Shapes.block());
Expand All @@ -115,45 +114,31 @@ private void updateShapeForConduit(ConduitBundle conduitBundle, IConduitType<?>
@Nullable List<Vec3i> offsetsForType = offsets.get(type);
if (offsetsForType != null) {
//all are pointing to the same xyz reference meaning that we can draw the core
if (offsetsForType.stream().distinct().count() == 1) {
rendered.add(type);
} else {
if (offsetsForType.stream().distinct().count() != 1) {
box = new Area(offsetsForType.toArray(new Vec3i[0]));
}
} else {
notRendered.put(type, i);
notRendered = type;
}

Set<Vec3i> duplicateFinder = new HashSet<>();
//rendered have only one distinct pos, so I can safely assume get(0) is valid
List<Vec3i> duplicatePositions = rendered.stream().map(offsets::get).map(l -> l.get(0)).filter(n -> !duplicateFinder.add(n)).toList();
for (Vec3i duplicatePosition : duplicatePositions) {
if (box == null) {
box = new Area(duplicatePosition);
} else {
box.makeContain(duplicatePosition);
}
}
for (IConduitType<?> toRender : rendered) {
if (box == null || !box.contains(offsetsForType.get(0)))
conduitShape = Shapes.join(conduitShape,
core.move(offsetsForType.get(0).getX() * 3f / 16f, offsetsForType.get(0).getY() * 3f / 16f, offsetsForType.get(0).getZ() * 3f / 16f),
BooleanOp.OR);
}
if (offsetsForType != null && (box == null || !box.contains(offsetsForType.get(0))))
conduitShape = Shapes.joinUnoptimized(conduitShape,
core.move(offsetsForType.get(0).getX() * 3f / 16f, offsetsForType.get(0).getY() * 3f / 16f, offsetsForType.get(0).getZ() * 3f / 16f),
BooleanOp.OR);

if (box != null) {
for (Map.Entry<IConduitType<?>, Integer> notRenderedEntry : notRendered.entrySet()) {
Vec3i offset = OffsetHelper.translationFor(axis, OffsetHelper.offsetConduit(notRenderedEntry.getValue(), allTypes.size()));
if (notRendered != null) {
Vec3i offset = OffsetHelper.translationFor(axis, OffsetHelper.offsetConduit(i, allTypes.size()));
if (!box.contains(offset))
conduitShape = Shapes.join(conduitShape, core.move(offset.getX() * 3f / 16f, offset.getY() * 3f / 16f, offset.getZ() * 3f / 16f),
conduitShape = Shapes.joinUnoptimized(conduitShape, core.move(offset.getX() * 3f / 16f, offset.getY() * 3f / 16f, offset.getZ() * 3f / 16f),
BooleanOp.OR);
}
conduitShape = Shapes.join(conduitShape, core.move(box.getMin().getX() * 3f / 16f, box.getMin().getY() * 3f / 16f, box.getMin().getZ() * 3f / 16f),
conduitShape = Shapes.joinUnoptimized(conduitShape, core.move(box.getMin().getX() * 3f / 16f, box.getMin().getY() * 3f / 16f, box.getMin().getZ() * 3f / 16f),
BooleanOp.OR);
} else {
for (Map.Entry<IConduitType<?>, Integer> notRenderedEntry : notRendered.entrySet()) {
Vec3i offset = OffsetHelper.translationFor(axis, OffsetHelper.offsetConduit(notRenderedEntry.getValue(), allTypes.size()));
conduitShape = Shapes.join(conduitShape, core.move(offset.getX() * 3f / 16f, offset.getY() * 3f / 16f, offset.getZ() * 3f / 16f), BooleanOp.OR);
if (notRendered != null) {
Vec3i offset = OffsetHelper.translationFor(axis, OffsetHelper.offsetConduit(i, allTypes.size()));
conduitShape = Shapes.joinUnoptimized(conduitShape, core.move(offset.getX() * 3f / 16f, offset.getY() * 3f / 16f, offset.getZ() * 3f / 16f), BooleanOp.OR);
}
}
conduitShapes.put(conduitType, conduitShape.optimize());
Expand Down

0 comments on commit e58cb85

Please sign in to comment.