Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize shape calls #476

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 25 additions & 34 deletions src/conduits/java/com/enderio/conduits/common/ConduitShape.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,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 @@ -94,8 +94,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 @@ -104,68 +104,56 @@ 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());
return;
}

var type = allTypes.get(i);
@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 All @@ -185,14 +173,17 @@ public static VoxelShape rotateVoxelShape(VoxelShape toRotate, Direction directi
} else {
buffer[0].forAllBoxes((minX, minY, minZ, maxX, maxY, maxZ) -> buffer[1] = Shapes.or(buffer[1], Shapes.box(minX, minZ, minY, maxX, maxZ, maxY)));
}

return buffer[1];
}

for (int i = 0; i < (direction.get2DDataValue()) % 4; i++) {
buffer[0].forAllBoxes(
(minX, minY, minZ, maxX, maxY, maxZ) -> buffer[1] = Shapes.or(buffer[1], Shapes.box(1 - maxZ, minY, minX, 1 - minZ, maxY, maxX)));
buffer[0] = buffer[1];
buffer[1] = Shapes.empty();
}

return buffer[0];
}

Expand Down