SDF post process
This commit is contained in:
parent
64614bc487
commit
8dab7c1179
8 changed files with 141 additions and 50 deletions
|
@ -3,6 +3,7 @@ package ru.betterend.blocks.complex;
|
|||
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
|
||||
import net.fabricmc.fabric.api.tool.attribute.v1.FabricToolTags;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.block.Material;
|
||||
import net.minecraft.block.MaterialColor;
|
||||
import net.minecraft.item.Items;
|
||||
|
@ -29,8 +30,7 @@ import ru.betterend.recipe.RecipeBuilder;
|
|||
import ru.betterend.registry.BlockRegistry;
|
||||
import ru.betterend.util.TagHelper;
|
||||
|
||||
public class WoodenMaterial
|
||||
{
|
||||
public class WoodenMaterial {
|
||||
public final Block log;
|
||||
public final Block bark;
|
||||
|
||||
|
@ -55,8 +55,7 @@ public class WoodenMaterial
|
|||
public final Block chest;
|
||||
public final Block barrel;
|
||||
|
||||
public WoodenMaterial(String name, MaterialColor woodColor, MaterialColor planksColor)
|
||||
{
|
||||
public WoodenMaterial(String name, MaterialColor woodColor, MaterialColor planksColor) {
|
||||
FabricBlockSettings materialPlanks = FabricBlockSettings.of(Material.WOOD).sounds(BlockSoundGroup.WOOD).breakByTool(FabricToolTags.AXES).materialColor(planksColor);
|
||||
|
||||
log_striped = BlockRegistry.registerBlock(name + "_striped_log", new BlockPillar(materialPlanks));
|
||||
|
@ -125,8 +124,11 @@ public class WoodenMaterial
|
|||
TagHelper.addTags(trapdoor, BlockTags.WOODEN_TRAPDOORS, BlockTags.TRAPDOORS);
|
||||
}
|
||||
|
||||
public boolean isTreeLog(Block block)
|
||||
{
|
||||
public boolean isTreeLog(Block block) {
|
||||
return block == log || block == bark;
|
||||
}
|
||||
|
||||
public boolean isTreeLog(BlockState state) {
|
||||
return isTreeLog(state.getBlock());
|
||||
}
|
||||
}
|
|
@ -10,7 +10,7 @@ import net.minecraft.block.BlockState;
|
|||
import net.minecraft.client.util.math.Vector3f;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.MathHelper;
|
||||
import ru.betterend.util.sdf.ISDF;
|
||||
import ru.betterend.util.sdf.SDF;
|
||||
import ru.betterend.util.sdf.operator.SDFUnion;
|
||||
import ru.betterend.util.sdf.primitive.SDFLine;
|
||||
|
||||
|
@ -41,10 +41,10 @@ public class SplineHelper {
|
|||
}
|
||||
}
|
||||
|
||||
public static ISDF buildSDF(List<Vector3f> spline, float radius1, float radius2, Function<BlockPos, BlockState> placerFunction) {
|
||||
public static SDF buildSDF(List<Vector3f> spline, float radius1, float radius2, Function<BlockPos, BlockState> placerFunction) {
|
||||
int count = spline.size();
|
||||
float max = count - 2;
|
||||
ISDF result = null;
|
||||
SDF result = null;
|
||||
Vector3f start = spline.get(0);
|
||||
for (int i = 1; i < count; i++) {
|
||||
Vector3f pos = spline.get(i);
|
||||
|
|
59
src/main/java/ru/betterend/util/sdf/PosInfo.java
Normal file
59
src/main/java/ru/betterend/util/sdf/PosInfo.java
Normal file
|
@ -0,0 +1,59 @@
|
|||
package ru.betterend.util.sdf;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.block.Blocks;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
|
||||
public class PosInfo {
|
||||
private static final BlockState AIR = Blocks.AIR.getDefaultState();
|
||||
private final Map<BlockPos, PosInfo> blocks;
|
||||
private final BlockPos pos;
|
||||
private BlockState state;
|
||||
|
||||
public static PosInfo create(Map<BlockPos, PosInfo> blocks, BlockPos pos) {
|
||||
return new PosInfo(blocks, pos);
|
||||
}
|
||||
|
||||
private PosInfo(Map<BlockPos, PosInfo> blocks, BlockPos pos) {
|
||||
this.blocks = blocks;
|
||||
this.pos = pos;
|
||||
blocks.put(pos, this);
|
||||
}
|
||||
|
||||
public BlockState getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(BlockState state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public BlockState getStateUp() {
|
||||
PosInfo info = blocks.get(pos.up());
|
||||
if (info == null) {
|
||||
return AIR;
|
||||
}
|
||||
return info.getState();
|
||||
}
|
||||
|
||||
public BlockState getStateDown() {
|
||||
PosInfo info = blocks.get(pos.down());
|
||||
if (info == null) {
|
||||
return AIR;
|
||||
}
|
||||
return info.getState();
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return pos.hashCode();
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
if (!(obj instanceof PosInfo)) {
|
||||
return false;
|
||||
}
|
||||
return pos.equals(((PosInfo) obj).pos);
|
||||
}
|
||||
}
|
|
@ -1,25 +1,39 @@
|
|||
package ru.betterend.util.sdf;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.Direction;
|
||||
import net.minecraft.world.ServerWorldAccess;
|
||||
import ru.betterend.util.BlocksHelper;
|
||||
|
||||
public interface ISDF {
|
||||
public float getDistance(float x, float y, float z);
|
||||
public abstract class SDF {
|
||||
private Function<PosInfo, BlockState> postProcess = (info) -> {
|
||||
return info.getState();
|
||||
};
|
||||
|
||||
public void setBlock(ServerWorldAccess world, BlockPos pos);
|
||||
public abstract float getDistance(float x, float y, float z);
|
||||
|
||||
default void fillRecursive(ServerWorldAccess world, BlockPos start, Function<BlockState, Boolean> canReplace, int dx, int dy, int dz) {
|
||||
public abstract BlockState getBlockState(BlockPos pos);
|
||||
|
||||
public SDF setPostProcess(Function<PosInfo, BlockState> postProcess) {
|
||||
this.postProcess = postProcess;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void fillRecursive(ServerWorldAccess world, BlockPos start, Function<BlockState, Boolean> canReplace, int dx, int dy, int dz) {
|
||||
Map<BlockPos, PosInfo> mapWorld = Maps.newHashMap();
|
||||
Set<BlockPos> blocks = Sets.newHashSet();
|
||||
Set<BlockPos> ends = Sets.newHashSet();
|
||||
Set<BlockPos> add = Sets.newHashSet();
|
||||
ends.add(new BlockPos(0, 0, 0));
|
||||
boolean process = postProcess != null;
|
||||
boolean run = true;
|
||||
|
||||
while (run) {
|
||||
|
@ -34,7 +48,8 @@ public interface ISDF {
|
|||
|
||||
if (!blocks.contains(pos) && canReplace.apply(world.getBlockState(wpos))) {
|
||||
if (this.getDistance(pos.getX(), pos.getY(), pos.getZ()) < 0) {
|
||||
setBlock(world, wpos);
|
||||
BlockState state = getBlockState(wpos);
|
||||
PosInfo.create(mapWorld, wpos).setState(state);
|
||||
if (Math.abs(pos.getX()) < dx && Math.abs(pos.getY()) < dy && Math.abs(pos.getZ()) < dz) {
|
||||
add.add(pos);
|
||||
}
|
||||
|
@ -50,9 +65,15 @@ public interface ISDF {
|
|||
|
||||
run &= !ends.isEmpty();
|
||||
}
|
||||
|
||||
mapWorld.forEach((pos, info) -> {
|
||||
BlockState state = postProcess.apply(info);
|
||||
BlocksHelper.setWithoutUpdate(world, pos, state);
|
||||
});
|
||||
}
|
||||
|
||||
default void fillRecursive(ServerWorldAccess world, BlockPos start, Function<BlockState, Boolean> canReplace) {
|
||||
public void fillRecursive(ServerWorldAccess world, BlockPos start, Function<BlockState, Boolean> canReplace) {
|
||||
Map<BlockPos, PosInfo> mapWorld = Maps.newHashMap();
|
||||
Set<BlockPos> blocks = Sets.newHashSet();
|
||||
Set<BlockPos> ends = Sets.newHashSet();
|
||||
Set<BlockPos> add = Sets.newHashSet();
|
||||
|
@ -67,7 +88,8 @@ public interface ISDF {
|
|||
|
||||
if (!blocks.contains(pos) && canReplace.apply(world.getBlockState(wpos))) {
|
||||
if (this.getDistance(pos.getX(), pos.getY(), pos.getZ()) <= 0) {
|
||||
setBlock(world, wpos);
|
||||
BlockState state = getBlockState(wpos);
|
||||
PosInfo.create(mapWorld, wpos).setState(state);
|
||||
add.add(pos);
|
||||
}
|
||||
}
|
||||
|
@ -81,5 +103,10 @@ public interface ISDF {
|
|||
|
||||
run &= !ends.isEmpty();
|
||||
}
|
||||
|
||||
mapWorld.forEach((pos, info) -> {
|
||||
BlockState state = postProcess.apply(info);
|
||||
BlocksHelper.setWithoutUpdate(world, pos, state);
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,20 +1,20 @@
|
|||
package ru.betterend.util.sdf.operator;
|
||||
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.ServerWorldAccess;
|
||||
import ru.betterend.util.sdf.ISDF;
|
||||
import ru.betterend.util.sdf.SDF;
|
||||
|
||||
public abstract class SDFBinary implements ISDF {
|
||||
protected ISDF sourceA;
|
||||
protected ISDF sourceB;
|
||||
public abstract class SDFBinary extends SDF {
|
||||
protected SDF sourceA;
|
||||
protected SDF sourceB;
|
||||
protected boolean firstValue;
|
||||
|
||||
public SDFBinary setSourceA(ISDF sourceA) {
|
||||
public SDFBinary setSourceA(SDF sourceA) {
|
||||
this.sourceA = sourceA;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SDFBinary setSourceB(ISDF sourceB) {
|
||||
public SDFBinary setSourceB(SDF sourceB) {
|
||||
this.sourceB = sourceB;
|
||||
return this;
|
||||
}
|
||||
|
@ -24,11 +24,11 @@ public abstract class SDFBinary implements ISDF {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void setBlock(ServerWorldAccess world, BlockPos pos) {
|
||||
public BlockState getBlockState(BlockPos pos) {
|
||||
if (firstValue) {
|
||||
sourceA.setBlock(world, pos);
|
||||
return sourceA.getBlockState(pos);
|
||||
} else {
|
||||
sourceB.setBlock(world, pos);
|
||||
return sourceB.getBlockState(pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,19 +1,19 @@
|
|||
package ru.betterend.util.sdf.operator;
|
||||
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.ServerWorldAccess;
|
||||
import ru.betterend.util.sdf.ISDF;
|
||||
import ru.betterend.util.sdf.SDF;
|
||||
|
||||
public abstract class SDFUnary implements ISDF {
|
||||
protected ISDF source;
|
||||
public abstract class SDFUnary extends SDF {
|
||||
protected SDF source;
|
||||
|
||||
public SDFUnary setSource(ISDF source) {
|
||||
public SDFUnary setSource(SDF source) {
|
||||
this.source = source;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBlock(ServerWorldAccess world, BlockPos pos) {
|
||||
source.setBlock(world, pos);
|
||||
public BlockState getBlockState(BlockPos pos) {
|
||||
return source.getBlockState(pos);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,11 +4,9 @@ import java.util.function.Function;
|
|||
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.ServerWorldAccess;
|
||||
import ru.betterend.util.BlocksHelper;
|
||||
import ru.betterend.util.sdf.ISDF;
|
||||
import ru.betterend.util.sdf.SDF;
|
||||
|
||||
public abstract class SDFPrimitive implements ISDF {
|
||||
public abstract class SDFPrimitive extends SDF {
|
||||
protected final Function<BlockPos, BlockState> placerFunction;
|
||||
|
||||
public SDFPrimitive(Function<BlockPos, BlockState> placerFunction) {
|
||||
|
@ -21,7 +19,7 @@ public abstract class SDFPrimitive implements ISDF {
|
|||
};
|
||||
}
|
||||
|
||||
public void setBlock(ServerWorldAccess world, BlockPos pos) {
|
||||
BlocksHelper.setWithoutUpdate(world, pos, placerFunction.apply(pos));
|
||||
public BlockState getBlockState(BlockPos pos) {
|
||||
return placerFunction.apply(pos);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,22 +11,16 @@ import net.minecraft.util.math.BlockPos;
|
|||
import net.minecraft.world.StructureWorldAccess;
|
||||
import net.minecraft.world.gen.chunk.ChunkGenerator;
|
||||
import net.minecraft.world.gen.feature.DefaultFeatureConfig;
|
||||
import ru.betterend.registry.BlockRegistry;
|
||||
import ru.betterend.registry.BlockTagRegistry;
|
||||
import ru.betterend.util.MHelper;
|
||||
import ru.betterend.util.SplineHelper;
|
||||
import ru.betterend.util.sdf.ISDF;
|
||||
import ru.betterend.util.sdf.operator.SDFFlatWave;
|
||||
import ru.betterend.util.sdf.operator.SDFScale3D;
|
||||
import ru.betterend.util.sdf.operator.SDFSmoothUnion;
|
||||
import ru.betterend.util.sdf.operator.SDFSubtraction;
|
||||
import ru.betterend.util.sdf.operator.SDFTranslate;
|
||||
import ru.betterend.util.sdf.SDF;
|
||||
import ru.betterend.util.sdf.primitive.SDFCapedCone;
|
||||
import ru.betterend.util.sdf.primitive.SDFLine;
|
||||
import ru.betterend.util.sdf.primitive.SDFSphere;
|
||||
|
||||
public class MossyGlowshroomFeature extends DefaultFeature {
|
||||
private static final Function<BlockState, Boolean> REPLACE;
|
||||
private static final ISDF FUNCTION;
|
||||
private static final SDF FUNCTION;
|
||||
|
||||
@Override
|
||||
public boolean generate(StructureWorldAccess world, ChunkGenerator chunkGenerator, Random random, BlockPos blockPos, DefaultFeatureConfig featureConfig) {
|
||||
|
@ -43,8 +37,19 @@ public class MossyGlowshroomFeature extends DefaultFeature {
|
|||
int count = MHelper.floor(height / 4);
|
||||
List<Vector3f> spline = SplineHelper.makeSpline(0, 0, 0, 0, height, 0, count);
|
||||
SplineHelper.offsetParts(spline, random, 1F, 0, 1F);
|
||||
ISDF sdf = SplineHelper.buildSDF(spline, 2.1F, 1.5F, (pos) -> {
|
||||
return Blocks.DIAMOND_BLOCK.getDefaultState();
|
||||
SDF sdf = SplineHelper.buildSDF(spline, 2.1F, 1.5F, (pos) -> {
|
||||
return BlockRegistry.MOSSY_GLOWSHROOM.log.getDefaultState();
|
||||
});
|
||||
sdf.setPostProcess((info) -> {
|
||||
if (!BlockRegistry.MOSSY_GLOWSHROOM.isTreeLog(info.getStateUp())) {
|
||||
return BlockRegistry.MOSSY_GLOWSHROOM.bark.getDefaultState();
|
||||
}
|
||||
else if (!BlockRegistry.MOSSY_GLOWSHROOM.isTreeLog(info.getStateDown())) {
|
||||
return BlockRegistry.MOSSY_GLOWSHROOM.bark.getDefaultState();
|
||||
}
|
||||
else {
|
||||
return info.getState();
|
||||
}
|
||||
});
|
||||
sdf.fillRecursive(world, blockPos, REPLACE);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue