The least performant option

- Fixed slow merging of collision shapes
- Improved async handling of the collision shape merger
- BBs far away from an entity now get skipped in the collision cycle
This commit is contained in:
simibubi 2021-03-31 14:27:27 +02:00
parent 65d21c374b
commit d61e387916
3 changed files with 32 additions and 16 deletions

View file

@ -461,6 +461,8 @@ public abstract class AbstractContraptionEntity extends Entity implements IEntit
if (!ticking) if (!ticking)
contraption.stop(world); contraption.stop(world);
} }
if (contraption != null)
contraption.onEntityRemoved(this);
super.remove(keepData); super.remove(keepData);
} }

View file

@ -137,7 +137,7 @@ public abstract class Contraption {
private Map<BlockPos, Entity> initialPassengers; private Map<BlockPos, Entity> initialPassengers;
private List<BlockFace> pendingSubContraptions; private List<BlockFace> pendingSubContraptions;
private CompletableFuture<List<AxisAlignedBB>> simplifiedEntityColliderProvider; private CompletableFuture<Void> simplifiedEntityColliderProvider;
// Client // Client
public Map<BlockPos, TileEntity> presentTileEntities; public Map<BlockPos, TileEntity> presentTileEntities;
@ -193,6 +193,7 @@ public abstract class Contraption {
String type = nbt.getString("Type"); String type = nbt.getString("Type");
Contraption contraption = ContraptionType.fromType(type); Contraption contraption = ContraptionType.fromType(type);
contraption.readNBT(world, nbt, spawnData); contraption.readNBT(world, nbt, spawnData);
contraption.world = new ContraptionWorld(world, contraption);
contraption.gatherBBsOffThread(); contraption.gatherBBsOffThread();
return contraption; return contraption;
} }
@ -260,6 +261,13 @@ public abstract class Contraption {
gatherBBsOffThread(); gatherBBsOffThread();
} }
public void onEntityRemoved(AbstractContraptionEntity entity) {
if (simplifiedEntityColliderProvider != null) {
simplifiedEntityColliderProvider.cancel(false);
simplifiedEntityColliderProvider = null;
}
}
public void onEntityInitialize(World world, AbstractContraptionEntity contraptionEntity) { public void onEntityInitialize(World world, AbstractContraptionEntity contraptionEntity) {
if (world.isRemote) if (world.isRemote)
return; return;
@ -282,15 +290,6 @@ public abstract class Contraption {
} }
public void onEntityTick(World world) { public void onEntityTick(World world) {
if (simplifiedEntityColliderProvider != null && simplifiedEntityColliderProvider.isDone()) {
try {
simplifiedEntityColliders = Optional.of(simplifiedEntityColliderProvider.join());
} catch (Exception e) {
e.printStackTrace();
}
simplifiedEntityColliderProvider = null;
}
fluidStorage.forEach((pos, mfs) -> mfs.tick(entity, pos, world.isRemote)); fluidStorage.forEach((pos, mfs) -> mfs.tick(entity, pos, world.isRemote));
} }
@ -1182,19 +1181,25 @@ public abstract class Contraption {
} }
private void gatherBBsOffThread() { private void gatherBBsOffThread() {
getContraptionWorld();
simplifiedEntityColliderProvider = CompletableFuture.supplyAsync(() -> { simplifiedEntityColliderProvider = CompletableFuture.supplyAsync(() -> {
VoxelShape combinedShape = VoxelShapes.empty(); VoxelShape combinedShape = VoxelShapes.empty();
for (Entry<BlockPos, BlockInfo> entry : blocks.entrySet()) { for (Entry<BlockPos, BlockInfo> entry : blocks.entrySet()) {
BlockInfo info = entry.getValue(); BlockInfo info = entry.getValue();
BlockPos localPos = entry.getKey(); BlockPos localPos = entry.getKey();
VoxelShape collisionShape = info.state.getCollisionShape(this.world, localPos); VoxelShape collisionShape = info.state.getCollisionShape(world, localPos);
if (collisionShape.isEmpty()) if (collisionShape.isEmpty())
continue; continue;
combinedShape = VoxelShapes.combineAndSimplify(combinedShape, combinedShape = VoxelShapes.combine(combinedShape,
collisionShape.withOffset(localPos.getX(), localPos.getY(), localPos.getZ()), IBooleanFunction.OR); collisionShape.withOffset(localPos.getX(), localPos.getY(), localPos.getZ()), IBooleanFunction.OR);
} }
return combinedShape.toBoundingBoxList(); return combinedShape.simplify()
}); .toBoundingBoxList();
})
.thenAccept(r -> {
simplifiedEntityColliders = Optional.of(r);
simplifiedEntityColliderProvider = null;
});
} }
public static float getRadius(Set<BlockPos> blocks, Direction.Axis axis) { public static float getRadius(Set<BlockPos> blocks, Direction.Axis axis) {

View file

@ -118,7 +118,6 @@ public class ContraptionCollider {
motion = rotationMatrix.transform(motion); motion = rotationMatrix.transform(motion);
// Use simplified bbs when present // Use simplified bbs when present
// TODO: is it worth filtering out far away bbs?
final Vec3d motionCopy = motion; final Vec3d motionCopy = motion;
List<AxisAlignedBB> collidableBBs = contraption.simplifiedEntityColliders.orElseGet(() -> { List<AxisAlignedBB> collidableBBs = contraption.simplifiedEntityColliders.orElseGet(() -> {
@ -147,7 +146,17 @@ public class ContraptionCollider {
for (AxisAlignedBB bb : collidableBBs) { for (AxisAlignedBB bb : collidableBBs) {
Vec3d currentResponse = collisionResponse.getValue(); Vec3d currentResponse = collisionResponse.getValue();
obb.setCenter(obbCenter.add(currentResponse)); Vec3d currentCenter = obbCenter.add(currentResponse);
if (Math.abs(currentCenter.x - bb.getCenter().x) - entityBounds.getXSize() - 1 > bb.getXSize() / 2)
continue;
if (Math.abs((currentCenter.y + motion.y) - bb.getCenter().y) - entityBounds.getYSize()
- 1 > bb.getYSize() / 2)
continue;
if (Math.abs(currentCenter.z - bb.getCenter().z) - entityBounds.getZSize() - 1 > bb.getZSize() / 2)
continue;
obb.setCenter(currentCenter);
ContinuousSeparationManifold intersect = obb.intersect(bb, motion); ContinuousSeparationManifold intersect = obb.intersect(bb, motion);
if (intersect == null) if (intersect == null)