Added a global Voxelshape cache for cable bus voxel shapes, since combining voxel shapes turns out to be an extremely expensive process (i.e. 1.7s for 10000x5 shapes).
This commit is contained in:
@@ -41,7 +41,6 @@ import net.minecraft.util.Hand;
|
|||||||
import net.minecraft.util.math.AxisAlignedBB;
|
import net.minecraft.util.math.AxisAlignedBB;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
import net.minecraft.util.math.shapes.VoxelShape;
|
import net.minecraft.util.math.shapes.VoxelShape;
|
||||||
import net.minecraft.util.math.shapes.VoxelShapes;
|
|
||||||
import net.minecraft.util.math.vector.Vector3d;
|
import net.minecraft.util.math.vector.Vector3d;
|
||||||
import net.minecraft.world.IBlockReader;
|
import net.minecraft.world.IBlockReader;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
@@ -82,7 +81,6 @@ public class CableBusContainer extends CableBusStorage implements AEMultiTile, I
|
|||||||
private final EnumSet<LayerFlags> myLayerFlags = EnumSet.noneOf(LayerFlags.class);
|
private final EnumSet<LayerFlags> myLayerFlags = EnumSet.noneOf(LayerFlags.class);
|
||||||
private YesNo hasRedstone = YesNo.UNDECIDED;
|
private YesNo hasRedstone = YesNo.UNDECIDED;
|
||||||
private IPartHost tcb;
|
private IPartHost tcb;
|
||||||
// TODO 1.10.2-R - does somebody seriously want to make parts TESR??? Hope not.
|
|
||||||
private boolean requiresDynamicRender = false;
|
private boolean requiresDynamicRender = false;
|
||||||
private boolean inWorld = false;
|
private boolean inWorld = false;
|
||||||
// Cached collision shape for living entities
|
// Cached collision shape for living entities
|
||||||
@@ -1096,11 +1094,7 @@ public class CableBusContainer extends CableBusStorage implements AEMultiTile, I
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
VoxelShape shape = VoxelShapes.empty();
|
return VoxelShapeCache.get(boxes);
|
||||||
for (final AxisAlignedBB bx : boxes) {
|
|
||||||
shape = VoxelShapes.or(shape, VoxelShapes.create(bx));
|
|
||||||
}
|
|
||||||
return shape;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void invalidateShapes() {
|
private void invalidateShapes() {
|
||||||
|
|||||||
@@ -0,0 +1,81 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of Applied Energistics 2.
|
||||||
|
* Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
|
||||||
|
*
|
||||||
|
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package appeng.parts;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.google.common.cache.CacheBuilder;
|
||||||
|
import com.google.common.cache.CacheLoader;
|
||||||
|
import com.google.common.cache.LoadingCache;
|
||||||
|
|
||||||
|
import net.minecraft.util.math.AxisAlignedBB;
|
||||||
|
import net.minecraft.util.math.shapes.IBooleanFunction;
|
||||||
|
import net.minecraft.util.math.shapes.VoxelShape;
|
||||||
|
import net.minecraft.util.math.shapes.VoxelShapes;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* While creation of a {@link VoxelShape} with
|
||||||
|
* {@link VoxelShapes#create(AxisAlignedBB)} is fast enough, combining voxel
|
||||||
|
* shapes with {@link VoxelShapes#or(VoxelShape, VoxelShape)} or any other
|
||||||
|
* combination method, as well as {@link VoxelShape#simplify()} are <b>extremely
|
||||||
|
* slow</b>. For example: Creating a VoxelShape for a list of 5 bounding boxes
|
||||||
|
* 10,000 times takes about 1.7 seconds.
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* To reduce the impact of this on cables, we introduce a global voxel shape
|
||||||
|
* cache so that cables can share their combined voxel shapes better.
|
||||||
|
*/
|
||||||
|
final class VoxelShapeCache {
|
||||||
|
|
||||||
|
// Why using a List here should not make much of a difference vs. using a Set:
|
||||||
|
// The part's bounding box depends on the side it is attached to, and the sides
|
||||||
|
// are iterated over in a fixed order, meaning the order of bounding boxes
|
||||||
|
// should
|
||||||
|
// be the same for a same set of parts.
|
||||||
|
private static final LoadingCache<List<AxisAlignedBB>, VoxelShape> CACHE = CacheBuilder.newBuilder()//
|
||||||
|
.maximumSize(10000L)//
|
||||||
|
.build(new CacheLoader<List<AxisAlignedBB>, VoxelShape>() {
|
||||||
|
@Override
|
||||||
|
public VoxelShape load(List<AxisAlignedBB> key) {
|
||||||
|
return create(key);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
private VoxelShapeCache() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static VoxelShape get(List<AxisAlignedBB> boxes) {
|
||||||
|
return CACHE.getUnchecked(boxes);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static VoxelShape create(List<AxisAlignedBB> boxes) {
|
||||||
|
if (boxes.isEmpty()) {
|
||||||
|
return VoxelShapes.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
VoxelShape shape = VoxelShapes.create(boxes.get(i));
|
||||||
|
for (; i < boxes.size(); i++) {
|
||||||
|
AxisAlignedBB box = boxes.get(i);
|
||||||
|
shape = VoxelShapes.combine(shape, VoxelShapes.create(box), IBooleanFunction.OR);
|
||||||
|
}
|
||||||
|
return shape.simplify();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user