Skip to content

Commit

Permalink
Make ConnectedTextureRenderPipeline mask into a Predicate (#259)
Browse files Browse the repository at this point in the history
* Make ConnectedTextureRenderPipeline mask into a Predicate

* Rename faceMask to faceFilter

* More renaming
  • Loading branch information
ExE-Boss authored and RX14 committed Feb 24, 2017
1 parent 22a3d8d commit c159963
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@
import java.util.function.Predicate;
import java.util.function.Supplier;

// TODO: create a CTM pipeline builder at some point, to automate construction of CTM pipelines
// (there are 47 different CTM textures for a block which connects omni-directionally, writing that manually would get annoying really fast)
public class ConnectedTextureRenderPipeline extends BlockRenderPipeline {

public final Block block;
public final Texture edgeTexture;

/**
* The mask the represents which sides the block should render its connected texture.
* E.g: 000000 will render all directions
* E.g: 00000000 will render all directions
*/
public Supplier<Integer> connectMask;

/**
* A mask of which sides the connected texture renderer should render.
* Each bit corresponds to a direction.
* E.g: 000011 will render top and bottom
* A filter of which sides the connected texture renderer should render.
*/
public int faceMask = 0xff;
public Predicate<Direction> faceFilter = dir -> true;

public ConnectedTextureRenderPipeline(Block block, Texture edgeTexture) {
super(block);
Expand All @@ -61,7 +61,10 @@ public ConnectedTextureRenderPipeline(Block block, Texture edgeTexture) {
connectMask = () -> {
if (this.block.components.has(BlockTransform.class)) {
return Arrays.stream(Direction.VALID_DIRECTIONS)
.filter(d -> this.block.world().getBlock(this.block.position().add(d.toVector())).get().sameType(this.block))
.filter(d -> {
Optional<Block> b = this.block.world().getBlock(this.block.position().add(d.toVector()));
return b.isPresent() && b.get().sameType(this.block);
})
.map(d -> 1 << d.ordinal())
.reduce(0, (b, a) -> a | b);
}
Expand All @@ -76,7 +79,7 @@ public ConnectedTextureRenderPipeline(Block block, Texture edgeTexture) {

//Render the block edge
for (Direction dir : Direction.VALID_DIRECTIONS)
if ((faceMask & (1 << dir.ordinal())) != 0) {
if (faceFilter.test(dir)) {
renderFace(dir, model);
}
};
Expand All @@ -88,33 +91,46 @@ public ConnectedTextureRenderPipeline withConnectMask(Supplier<Integer> connectM
}

/**
* Set the mask used to determine if this pipeline should handle these sides.
* Set the filter used to determine if this pipeline should handle these sides.
*
* @param faceMask A mask of which sides the connected texture renderer should render.
* @param faceFilter A filter of which sides the connected texture renderer should render.
* @return this
*/
public ConnectedTextureRenderPipeline withFaceMask(Predicate<Direction> faceMask) {
int faceMaskInt = 0;
for (int i = 0; i < 6; i++)
faceMaskInt |= (faceMask.test(Direction.fromOrdinal(i)) ? 1 : 0);
this.faceMask = faceMaskInt;
public ConnectedTextureRenderPipeline withFaceFilter(Predicate<Direction> faceFilter) {
this.faceFilter = faceFilter;
return this;
}

/**
* Set the mask used to determine if this pipeline should handle these sides.
* Set the filter used to determine if this pipeline should handle these sides.
*
* @param faces An array of which sides the connected texture renderer should render.
* @return this
*/
public ConnectedTextureRenderPipeline withFaces(Direction... faces) {
this.faceFilter = dir -> Arrays.stream(faces).anyMatch(d -> d == dir);
return this;
}

/**
* Set the filter used to determine if this pipeline should handle these sides.
*
* @param faceMask A mask of which sides the connected texture renderer should render.
* Each bit corresponds to a direction.
* E.g: 000011 will render top and bottom
* @return this
*/
public ConnectedTextureRenderPipeline withFaceMask(int faceMask) {
this.faceMask = faceMask;
this.faceFilter = dir -> (faceMask & (1 << dir.ordinal())) != 0;
return this;
}

//Apply connected texture on top face
/**
* Apply connected texture on top face.
*
* @param direction the direction.
* @param model the model.
*/
protected void renderFace(Direction direction, Model model) {
for (int r = 0; r < 4; r++) {
Cuboid bound = bounds.get()
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/nova/core/util/Pipeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Calclavia
*/
public class Pipeline<O> {
protected Optional<Pipeline> prev = Optional.empty();
protected Optional<Pipeline<O>> prev = Optional.empty();

protected Consumer<O> consumer = model -> {};

Expand All @@ -32,7 +32,7 @@ public Pipeline(Consumer<O> consumer) {
* @param stream The stream to apply.
* @return The new RenderStream
*/
public <T extends Pipeline<?>> T apply(T stream) {
public <T extends Pipeline<O>> T apply(T stream) {
stream.prev = Optional.of(this);
return stream;
}
Expand Down

0 comments on commit c159963

Please sign in to comment.