Fixed leaves cascade update, basic structure classes separation

This commit is contained in:
paulevsGitch 2020-11-07 12:57:07 +03:00
parent d87b98fcd7
commit d0135e83e9
16 changed files with 136 additions and 53 deletions

View file

@ -0,0 +1,243 @@
package ru.betterend.world.features.trees;
import java.util.List;
import java.util.Random;
import java.util.function.Function;
import com.google.common.collect.Lists;
import net.minecraft.block.BlockState;
import net.minecraft.block.LeavesBlock;
import net.minecraft.block.Material;
import net.minecraft.client.util.math.Vector3f;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockPos.Mutable;
import net.minecraft.util.math.Direction;
import net.minecraft.world.StructureWorldAccess;
import net.minecraft.world.gen.chunk.ChunkGenerator;
import net.minecraft.world.gen.feature.DefaultFeatureConfig;
import ru.betterend.noise.OpenSimplexNoise;
import ru.betterend.registry.EndBlocks;
import ru.betterend.registry.EndTags;
import ru.betterend.util.BlocksHelper;
import ru.betterend.util.MHelper;
import ru.betterend.util.SplineHelper;
import ru.betterend.util.sdf.PosInfo;
import ru.betterend.util.sdf.SDF;
import ru.betterend.util.sdf.operator.SDFDisplacement;
import ru.betterend.util.sdf.operator.SDFScale;
import ru.betterend.util.sdf.operator.SDFScale3D;
import ru.betterend.util.sdf.operator.SDFSubtraction;
import ru.betterend.util.sdf.operator.SDFTranslate;
import ru.betterend.util.sdf.primitive.SDFSphere;
import ru.betterend.world.features.DefaultFeature;
public class DragonTreeFeature extends DefaultFeature {
private static final Function<BlockState, Boolean> REPLACE;
private static final Function<BlockState, Boolean> IGNORE;
private static final Function<PosInfo, BlockState> POST;
private static final List<Vector3f> BRANCH;
private static final List<Vector3f> SIDE1;
private static final List<Vector3f> SIDE2;
private static final List<Vector3f> ROOT;
@Override
public boolean generate(StructureWorldAccess world, ChunkGenerator chunkGenerator, Random random, BlockPos pos, DefaultFeatureConfig config) {
if (!world.getBlockState(pos.down()).getBlock().isIn(EndTags.END_GROUND)) return false;
float size = MHelper.randRange(10, 25, random);
List<Vector3f> spline = SplineHelper.makeSpline(0, 0, 0, 0, size, 0, 6);
SplineHelper.offsetParts(spline, random, 1F, 0, 1F);
if (!SplineHelper.canGenerate(spline, pos, world, REPLACE)) {
return false;
}
BlocksHelper.setWithoutUpdate(world, pos, AIR);
Vector3f last = SplineHelper.getPos(spline, 3.5F);
OpenSimplexNoise noise = new OpenSimplexNoise(random.nextLong());
float radius = size * MHelper.randRange(0.5F, 0.7F, random);
makeCap(world, pos.add(last.getX(), last.getY(), last.getZ()), radius, random, noise);
last = spline.get(0);
makeRoots(world, pos.add(last.getX(), last.getY(), last.getZ()), radius, random);
radius = MHelper.randRange(1.2F, 2.3F, random);
SDF function = SplineHelper.buildSDF(spline, radius, 1.2F, (bpos) -> {
return EndBlocks.DRAGON_TREE.bark.getDefaultState();
});
function.setReplaceFunction(REPLACE);
function.setPostProcess(POST);
function.fillRecursiveIgnore(world, pos, IGNORE);
return true;
}
private void makeCap(StructureWorldAccess world, BlockPos pos, float radius, Random random, OpenSimplexNoise noise) {
int count = (int) radius;
int offset = (int) (BRANCH.get(BRANCH.size() - 1).getY() * radius);
for (int i = 0; i < count; i++) {
float angle = (float) i / (float) count * MHelper.PI2;
float scale = radius * MHelper.randRange(0.85F, 1.15F, random);
List<Vector3f> branch = SplineHelper.copySpline(BRANCH);
SplineHelper.rotateSpline(branch, angle);
SplineHelper.scale(branch, scale);
SplineHelper.fillSpline(branch, world, EndBlocks.DRAGON_TREE.bark.getDefaultState(), pos, REPLACE);
branch = SplineHelper.copySpline(SIDE1);
SplineHelper.rotateSpline(branch, angle);
SplineHelper.scale(branch, scale);
SplineHelper.fillSpline(branch, world, EndBlocks.DRAGON_TREE.bark.getDefaultState(), pos, REPLACE);
branch = SplineHelper.copySpline(SIDE2);
SplineHelper.rotateSpline(branch, angle);
SplineHelper.scale(branch, scale);
SplineHelper.fillSpline(branch, world, EndBlocks.DRAGON_TREE.bark.getDefaultState(), pos, REPLACE);
}
leavesBall(world, pos.up(offset), radius * 1.15F + 2, random, noise);
}
private void makeRoots(StructureWorldAccess world, BlockPos pos, float radius, Random random) {
int count = (int) (radius * 1.5F);
for (int i = 0; i < count; i++) {
float angle = (float) i / (float) count * MHelper.PI2;
float scale = radius * MHelper.randRange(0.85F, 1.15F, random);
List<Vector3f> branch = SplineHelper.copySpline(ROOT);
SplineHelper.rotateSpline(branch, angle);
SplineHelper.scale(branch, scale);
SplineHelper.fillSplineRoot(branch, world, EndBlocks.DRAGON_TREE.bark.getDefaultState(), pos, REPLACE);
}
}
private void leavesBall(StructureWorldAccess world, BlockPos pos, float radius, Random random, OpenSimplexNoise noise) {
SDF sphere = new SDFSphere().setRadius(radius).setBlock(EndBlocks.DRAGON_TREE_LEAVES.getDefaultState().with(LeavesBlock.DISTANCE, 6));
SDF sub = new SDFScale().setScale(5).setSource(sphere);
sub = new SDFTranslate().setTranslate(0, -radius * 5, 0).setSource(sub);
sphere = new SDFSubtraction().setSourceA(sphere).setSourceB(sub);
sphere = new SDFScale3D().setScale(1, 0.5F, 1).setSource(sphere);
sphere = new SDFDisplacement().setFunction((vec) -> { return (float) noise.eval(vec.getX() * 0.2, vec.getY() * 0.2, vec.getZ() * 0.2) * 1.5F; }).setSource(sphere);
sphere = new SDFDisplacement().setFunction((vec) -> { return random.nextFloat() * 3F - 1.5F; }).setSource(sphere);
Mutable mut = new Mutable();
sphere.setPostProcess((info) -> {
if (random.nextInt(5) == 0) {
for (Direction dir: Direction.values()) {
BlockState state = info.getState(dir, 2);
if (state.isAir()) {
return info.getState();
}
}
info.setState(EndBlocks.DRAGON_TREE.bark.getDefaultState());
for (int x = -6; x < 7; x++) {
int ax = Math.abs(x);
mut.setX(x + info.getPos().getX());
for (int z = -6; z < 7; z++) {
int az = Math.abs(z);
mut.setZ(z + info.getPos().getZ());
for (int y = -6; y < 7; y++) {
int ay = Math.abs(y);
int d = ax + ay + az;
if (d < 7) {
mut.setY(y + info.getPos().getY());
BlockState state = info.getState(mut);
if (state.getBlock() instanceof LeavesBlock) {
int distance = state.get(LeavesBlock.DISTANCE);
if (d < distance) {
info.setState(mut, state.with(LeavesBlock.DISTANCE, d));
}
}
}
}
}
}
}
return info.getState();
});
sphere.fillRecursiveIgnore(world, pos, IGNORE);
/*Collection<PosInfo> list = sphere.fillRecursiveIgnore(world, pos, IGNORE);
list.forEach((info) -> {
if (info.getState().getBlock() instanceof LeavesBlock && info.getState().get(LeavesBlock.DISTANCE) == 6) {
BlockState state = BlocksHelper.getLeavesState(info.getState(), world, info.getPos());
if (!state.equals(info.getState())) {
BlocksHelper.setWithoutUpdate(world, info.getPos(), state);
}
}
});*/
if (radius > 5) {
int count = (int) (radius * 2.5F);
for (int i = 0; i < count; i++) {
BlockPos p = pos.add(random.nextGaussian() * 1, random.nextGaussian() * 1, random.nextGaussian() * 1);
boolean place = true;
for (Direction d: Direction.values()) {
BlockState state = world.getBlockState(p.offset(d));
if (!EndBlocks.DRAGON_TREE.isTreeLog(state) && !state.isOf(EndBlocks.DRAGON_TREE_LEAVES)) {
place = false;
break;
}
}
if (place) {
BlocksHelper.setWithoutUpdate(world, p, EndBlocks.DRAGON_TREE.bark);
}
}
}
BlocksHelper.setWithoutUpdate(world, pos, EndBlocks.DRAGON_TREE.bark);
}
static {
REPLACE = (state) -> {
if (state.isIn(EndTags.END_GROUND)) {
return true;
}
if (state.getBlock() == EndBlocks.DRAGON_TREE_LEAVES) {
return true;
}
if (state.getMaterial().equals(Material.PLANT)) {
return true;
}
return state.getMaterial().isReplaceable();
};
IGNORE = (state) -> {
return EndBlocks.DRAGON_TREE.isTreeLog(state);
};
POST = (info) -> {
if (EndBlocks.DRAGON_TREE.isTreeLog(info.getStateUp()) && EndBlocks.DRAGON_TREE.isTreeLog(info.getStateDown())) {
return EndBlocks.DRAGON_TREE.log.getDefaultState();
}
return info.getState();
};
BRANCH = Lists.newArrayList(new Vector3f(0, 0, 0),
new Vector3f(0.1F, 0.3F, 0),
new Vector3f(0.4F, 0.6F, 0),
new Vector3f(0.8F, 0.8F, 0),
new Vector3f(1, 1, 0));
SIDE1 = Lists.newArrayList(new Vector3f(0.4F, 0.6F, 0),
new Vector3f(0.8F, 0.8F, 0),
new Vector3f(1, 1, 0));
SIDE2 = SplineHelper.copySpline(SIDE1);
Vector3f offset1 = new Vector3f(-0.4F, -0.6F, 0);
Vector3f offset2 = new Vector3f(0.4F, 0.6F, 0);
SplineHelper.offset(SIDE1, offset1);
SplineHelper.offset(SIDE2, offset1);
SplineHelper.rotateSpline(SIDE1, 0.5F);
SplineHelper.rotateSpline(SIDE2, -0.5F);
SplineHelper.offset(SIDE1, offset2);
SplineHelper.offset(SIDE2, offset2);
ROOT = Lists.newArrayList(new Vector3f(0F, 1F, 0),
new Vector3f(0.1F, 0.7F, 0),
new Vector3f(0.3F, 0.3F, 0),
new Vector3f(0.7F, 0.05F, 0),
new Vector3f(0.8F, -0.2F, 0));
SplineHelper.offset(ROOT, new Vector3f(0, -0.45F, 0));
}
}

View file

@ -0,0 +1,198 @@
package ru.betterend.world.features.trees;
import java.util.List;
import java.util.Random;
import java.util.function.Function;
import net.minecraft.block.BlockState;
import net.minecraft.block.LeavesBlock;
import net.minecraft.block.Material;
import net.minecraft.client.util.math.Vector3f;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockPos.Mutable;
import net.minecraft.util.math.Direction;
import net.minecraft.world.StructureWorldAccess;
import net.minecraft.world.gen.chunk.ChunkGenerator;
import net.minecraft.world.gen.feature.DefaultFeatureConfig;
import ru.betterend.noise.OpenSimplexNoise;
import ru.betterend.registry.EndBlocks;
import ru.betterend.registry.EndTags;
import ru.betterend.util.BlocksHelper;
import ru.betterend.util.MHelper;
import ru.betterend.util.SplineHelper;
import ru.betterend.util.sdf.PosInfo;
import ru.betterend.util.sdf.SDF;
import ru.betterend.util.sdf.operator.SDFDisplacement;
import ru.betterend.util.sdf.operator.SDFSubtraction;
import ru.betterend.util.sdf.operator.SDFTranslate;
import ru.betterend.util.sdf.primitive.SDFSphere;
import ru.betterend.world.features.DefaultFeature;
public class LacugroveFeature extends DefaultFeature {
private static final Function<BlockState, Boolean> REPLACE;
private static final Function<BlockState, Boolean> IGNORE;
private static final Function<PosInfo, BlockState> POST;
@Override
public boolean generate(StructureWorldAccess world, ChunkGenerator chunkGenerator, Random random, BlockPos pos, DefaultFeatureConfig config) {
if (!world.getBlockState(pos.down()).isIn(EndTags.END_GROUND)) return false;
float size = MHelper.randRange(15, 25, random);
List<Vector3f> spline = SplineHelper.makeSpline(0, 0, 0, 0, size, 0, 6);
SplineHelper.offsetParts(spline, random, 1F, 0, 1F);
if (!SplineHelper.canGenerate(spline, pos, world, REPLACE)) {
return false;
}
OpenSimplexNoise noise = new OpenSimplexNoise(random.nextLong());
float radius = MHelper.randRange(6F, 8F, random);
radius *= (size - 15F) / 20F + 1F;
Vector3f center = spline.get(4);
leavesBall(world, pos.add(center.getX(), center.getY(), center.getZ()), radius, random, noise);
radius = MHelper.randRange(1.2F, 1.8F, random);
SDF function = SplineHelper.buildSDF(spline, radius, 0.7F, (bpos) -> {
return EndBlocks.LACUGROVE.bark.getDefaultState();
});
function.setReplaceFunction(REPLACE);
function.setPostProcess(POST);
function.fillRecursive(world, pos);
spline = spline.subList(4, 6);
SplineHelper.fillSpline(spline, world, EndBlocks.LACUGROVE.bark.getDefaultState(), pos, REPLACE);
Mutable mut = new Mutable();
int offset = random.nextInt(2);
for (int i = 0; i < 100; i++) {
double px = pos.getX() + MHelper.randRange(-5, 5, random);
double pz = pos.getZ() + MHelper.randRange(-5, 5, random);
mut.setX(MHelper.floor(px + 0.5));
mut.setZ(MHelper.floor(pz + 0.5));
if (((mut.getX() + mut.getZ() + offset) & 1) == 0) {
double distance = 3.5 - MHelper.length(px - pos.getX(), pz - pos.getZ()) * 0.5;
if (distance > 0) {
int minY = MHelper.floor(pos.getY() - distance * 0.5);
int maxY = MHelper.floor(pos.getY() + distance + random.nextDouble());
boolean generate = false;
for (int y = minY; y < maxY; y++) {
mut.setY(y);
if (world.getBlockState(mut).isIn(EndTags.END_GROUND)) {
generate = true;
break;
}
}
if (generate) {
int top = maxY - 1;
for (int y = top; y >= minY; y--) {
mut.setY(y);
BlockState state = world.getBlockState(mut);
if (state.getMaterial().isReplaceable() || state.getMaterial().equals(Material.PLANT) || state.isIn(EndTags.END_GROUND)) {
BlocksHelper.setWithoutUpdate(world, mut, y == top ? EndBlocks.LACUGROVE.bark : EndBlocks.LACUGROVE.log);
}
else {
break;
}
}
}
}
}
}
return true;
}
private void leavesBall(StructureWorldAccess world, BlockPos pos, float radius, Random random, OpenSimplexNoise noise) {
SDF sphere = new SDFSphere().setRadius(radius).setBlock(EndBlocks.LACUGROVE_LEAVES.getDefaultState().with(LeavesBlock.DISTANCE, 1));
sphere = new SDFDisplacement().setFunction((vec) -> { return (float) noise.eval(vec.getX() * 0.2, vec.getY() * 0.2, vec.getZ() * 0.2) * 3; }).setSource(sphere);
sphere = new SDFDisplacement().setFunction((vec) -> { return random.nextFloat() * 3F - 1.5F; }).setSource(sphere);
sphere = new SDFSubtraction().setSourceA(sphere).setSourceB(new SDFTranslate().setTranslate(0, -radius - 2, 0).setSource(sphere));
Mutable mut = new Mutable();
sphere.setPostProcess((info) -> {
if (random.nextInt(5) == 0) {
for (Direction dir: Direction.values()) {
BlockState state = info.getState(dir, 2);
if (state.isAir()) {
return info.getState();
}
}
info.setState(EndBlocks.LACUGROVE.bark.getDefaultState());
for (int x = -6; x < 7; x++) {
int ax = Math.abs(x);
mut.setX(x + info.getPos().getX());
for (int z = -6; z < 7; z++) {
int az = Math.abs(z);
mut.setZ(z + info.getPos().getZ());
for (int y = -6; y < 7; y++) {
int ay = Math.abs(y);
int d = ax + ay + az;
if (d < 7) {
mut.setY(y + info.getPos().getY());
BlockState state = info.getState(mut);
if (state.getBlock() instanceof LeavesBlock) {
int distance = state.get(LeavesBlock.DISTANCE);
if (d < distance) {
info.setState(mut, state.with(LeavesBlock.DISTANCE, d));
}
}
}
}
}
}
}
return info.getState();
});
sphere.fillRecursiveIgnore(world, pos, IGNORE);
if (radius > 5) {
int count = (int) (radius * 2.5F);
for (int i = 0; i < count; i++) {
BlockPos p = pos.add(random.nextGaussian() * 1, random.nextGaussian() * 1, random.nextGaussian() * 1);
boolean place = true;
for (Direction d: Direction.values()) {
BlockState state = world.getBlockState(p.offset(d));
if (!EndBlocks.LACUGROVE.isTreeLog(state) && !state.isOf(EndBlocks.LACUGROVE_LEAVES)) {
place = false;
break;
}
}
if (place) {
BlocksHelper.setWithoutUpdate(world, p, EndBlocks.LACUGROVE.bark);
}
}
}
BlocksHelper.setWithoutUpdate(world, pos, EndBlocks.LACUGROVE.bark);
}
static {
REPLACE = (state) -> {
if (state.isIn(EndTags.END_GROUND)) {
return true;
}
if (EndBlocks.LACUGROVE.isTreeLog(state)) {
return true;
}
if (state.getBlock() == EndBlocks.LACUGROVE_LEAVES) {
return true;
}
if (state.getMaterial().equals(Material.PLANT)) {
return true;
}
return state.getMaterial().isReplaceable();
};
IGNORE = (state) -> {
return EndBlocks.LACUGROVE.isTreeLog(state);
};
POST = (info) -> {
if (EndBlocks.LACUGROVE.isTreeLog(info.getStateUp()) && EndBlocks.LACUGROVE.isTreeLog(info.getStateDown())) {
return EndBlocks.LACUGROVE.log.getDefaultState();
}
return info.getState();
};
}
}

View file

@ -0,0 +1,170 @@
package ru.betterend.world.features.trees;
import java.util.List;
import java.util.Random;
import java.util.function.Function;
import net.minecraft.block.BlockState;
import net.minecraft.block.Material;
import net.minecraft.client.util.math.Vector3f;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.StructureWorldAccess;
import net.minecraft.world.gen.chunk.ChunkGenerator;
import net.minecraft.world.gen.feature.DefaultFeatureConfig;
import ru.betterend.blocks.BlockMossyGlowshroomCap;
import ru.betterend.blocks.basis.BlockGlowingFur;
import ru.betterend.noise.OpenSimplexNoise;
import ru.betterend.registry.EndBlocks;
import ru.betterend.registry.EndTags;
import ru.betterend.util.BlocksHelper;
import ru.betterend.util.MHelper;
import ru.betterend.util.SplineHelper;
import ru.betterend.util.sdf.SDF;
import ru.betterend.util.sdf.operator.SDFBinary;
import ru.betterend.util.sdf.operator.SDFCoordModify;
import ru.betterend.util.sdf.operator.SDFFlatWave;
import ru.betterend.util.sdf.operator.SDFScale;
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.operator.SDFUnion;
import ru.betterend.util.sdf.primitive.SDFCapedCone;
import ru.betterend.util.sdf.primitive.SDFPrimitive;
import ru.betterend.util.sdf.primitive.SDFSphere;
import ru.betterend.world.features.DefaultFeature;
public class MossyGlowshroomFeature extends DefaultFeature {
private static final Function<BlockState, Boolean> REPLACE;
private static final Vector3f CENTER = new Vector3f();
private static final SDFBinary FUNCTION;
private static final SDFTranslate HEAD_POS;
private static final SDFFlatWave ROOTS_ROT;
private static final SDFPrimitive CONE1;
private static final SDFPrimitive CONE2;
private static final SDFPrimitive CONE_GLOW;
private static final SDFPrimitive ROOTS;
@Override
public boolean generate(StructureWorldAccess world, ChunkGenerator chunkGenerator, Random random, BlockPos blockPos, DefaultFeatureConfig featureConfig) {
BlockState down = world.getBlockState(blockPos.down());
if (!down.isOf(EndBlocks.END_MYCELIUM) && !down.isOf(EndBlocks.END_MOSS)) return false;
CONE1.setBlock(EndBlocks.MOSSY_GLOWSHROOM_CAP);
CONE2.setBlock(EndBlocks.MOSSY_GLOWSHROOM_CAP);
CONE_GLOW.setBlock(EndBlocks.MOSSY_GLOWSHROOM_HYMENOPHORE);
ROOTS.setBlock(EndBlocks.MOSSY_GLOWSHROOM.bark);
float height = MHelper.randRange(10F, 25F, random);
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);
SDF sdf = SplineHelper.buildSDF(spline, 2.1F, 1.5F, (pos) -> {
return EndBlocks.MOSSY_GLOWSHROOM.log.getDefaultState();
});
Vector3f pos = spline.get(spline.size() - 1);
float scale = MHelper.randRange(0.75F, 1.1F, random);
if (!SplineHelper.canGenerate(spline, scale, blockPos, world, REPLACE)) {
return false;
}
BlocksHelper.setWithoutUpdate(world, blockPos, AIR);
CENTER.set(blockPos.getX(), 0, blockPos.getZ());
HEAD_POS.setTranslate(pos.getX(), pos.getY(), pos.getZ());
ROOTS_ROT.setAngle(random.nextFloat() * MHelper.PI2);
FUNCTION.setSourceA(sdf);
new SDFScale().setScale(scale)
.setSource(FUNCTION)
.setReplaceFunction(REPLACE)
.setPostProcess((info) -> {
if (EndBlocks.MOSSY_GLOWSHROOM.isTreeLog(info.getState())) {
if (random.nextBoolean() && info.getStateUp().getBlock() == EndBlocks.MOSSY_GLOWSHROOM_CAP) {
info.setState(EndBlocks.MOSSY_GLOWSHROOM_CAP.getDefaultState().with(BlockMossyGlowshroomCap.TRANSITION, true));
return info.getState();
}
else if (!EndBlocks.MOSSY_GLOWSHROOM.isTreeLog(info.getStateUp()) || !EndBlocks.MOSSY_GLOWSHROOM.isTreeLog(info.getStateDown())) {
info.setState(EndBlocks.MOSSY_GLOWSHROOM.bark.getDefaultState());
return info.getState();
}
}
else if (info.getState().getBlock() == EndBlocks.MOSSY_GLOWSHROOM_CAP) {
if (EndBlocks.MOSSY_GLOWSHROOM.isTreeLog(info.getStateDown().getBlock())) {
info.setState(EndBlocks.MOSSY_GLOWSHROOM_CAP.getDefaultState().with(BlockMossyGlowshroomCap.TRANSITION, true));
return info.getState();
}
info.setState(EndBlocks.MOSSY_GLOWSHROOM_CAP.getDefaultState());
return info.getState();
}
else if (info.getState().getBlock() == EndBlocks.MOSSY_GLOWSHROOM_HYMENOPHORE) {
for (Direction dir: BlocksHelper.HORIZONTAL) {
if (info.getState(dir) == AIR) {
info.setBlockPos(info.getPos().offset(dir), EndBlocks.MOSSY_GLOWSHROOM_FUR.getDefaultState().with(BlockGlowingFur.FACING, dir));
}
}
if (info.getStateDown().getBlock() != EndBlocks.MOSSY_GLOWSHROOM_HYMENOPHORE) {
info.setBlockPos(info.getPos().down(), EndBlocks.MOSSY_GLOWSHROOM_FUR.getDefaultState().with(BlockGlowingFur.FACING, Direction.DOWN));
}
}
return info.getState();
})
.fillRecursive(world, blockPos);
return true;
}
static {
SDFCapedCone cone1 = new SDFCapedCone().setHeight(2.5F).setRadius1(1.5F).setRadius2(2.5F);
SDFCapedCone cone2 = new SDFCapedCone().setHeight(3F).setRadius1(2.5F).setRadius2(13F);
SDF posedCone2 = new SDFTranslate().setTranslate(0, 5, 0).setSource(cone2);
SDF posedCone3 = new SDFTranslate().setTranslate(0, 12F, 0).setSource(new SDFScale().setScale(2).setSource(cone2));
SDF upCone = new SDFSubtraction().setSourceA(posedCone2).setSourceB(posedCone3);
SDF wave = new SDFFlatWave().setRaysCount(12).setIntensity(1.3F).setSource(upCone);
SDF cones = new SDFSmoothUnion().setRadius(3).setSourceA(cone1).setSourceB(wave);
CONE1 = cone1;
CONE2 = cone2;
SDF innerCone = new SDFTranslate().setTranslate(0, 1.25F, 0).setSource(upCone);
innerCone = new SDFScale3D().setScale(1.2F, 1F, 1.2F).setSource(innerCone);
cones = new SDFUnion().setSourceA(cones).setSourceB(innerCone);
SDF glowCone = new SDFCapedCone().setHeight(3F).setRadius1(2F).setRadius2(12.5F);
CONE_GLOW = (SDFPrimitive) glowCone;
glowCone = new SDFTranslate().setTranslate(0, 4.25F, 0).setSource(glowCone);
glowCone = new SDFSubtraction().setSourceA(glowCone).setSourceB(posedCone3);
cones = new SDFUnion().setSourceA(cones).setSourceB(glowCone);
OpenSimplexNoise noise = new OpenSimplexNoise(1234);
cones = new SDFCoordModify().setFunction((pos) -> {
float dist = MHelper.length(pos.getX(), pos.getZ());
float y = pos.getY() + (float) noise.eval(pos.getX() * 0.1 + CENTER.getX(), pos.getZ() * 0.1 + CENTER.getZ()) * dist * 0.3F - dist * 0.15F;
pos.set(pos.getX(), y, pos.getZ());
}).setSource(cones);
HEAD_POS = (SDFTranslate) new SDFTranslate().setSource(new SDFTranslate().setTranslate(0, 2.5F, 0).setSource(cones));
SDF roots = new SDFSphere().setRadius(4F);
ROOTS = (SDFPrimitive) roots;
roots = new SDFScale3D().setScale(1, 0.7F, 1).setSource(roots);
ROOTS_ROT = (SDFFlatWave) new SDFFlatWave().setRaysCount(5).setIntensity(1.5F).setSource(roots);
FUNCTION = new SDFSmoothUnion().setRadius(4).setSourceB(new SDFUnion().setSourceA(HEAD_POS).setSourceB(ROOTS_ROT));
REPLACE = (state) -> {
if (state.isIn(EndTags.END_GROUND)) {
return true;
}
if (state.getMaterial().equals(Material.PLANT)) {
return true;
}
return state.getMaterial().isReplaceable();
};
}
}

View file

@ -0,0 +1,180 @@
package ru.betterend.world.features.trees;
import java.util.List;
import java.util.Random;
import java.util.function.Function;
import net.minecraft.block.BlockState;
import net.minecraft.block.LeavesBlock;
import net.minecraft.block.Material;
import net.minecraft.client.util.math.Vector3f;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockPos.Mutable;
import net.minecraft.util.math.Direction;
import net.minecraft.world.StructureWorldAccess;
import net.minecraft.world.gen.chunk.ChunkGenerator;
import net.minecraft.world.gen.feature.DefaultFeatureConfig;
import ru.betterend.noise.OpenSimplexNoise;
import ru.betterend.registry.EndBlocks;
import ru.betterend.registry.EndTags;
import ru.betterend.util.BlocksHelper;
import ru.betterend.util.MHelper;
import ru.betterend.util.SplineHelper;
import ru.betterend.util.sdf.PosInfo;
import ru.betterend.util.sdf.SDF;
import ru.betterend.util.sdf.operator.SDFDisplacement;
import ru.betterend.util.sdf.operator.SDFScale3D;
import ru.betterend.util.sdf.operator.SDFSubtraction;
import ru.betterend.util.sdf.operator.SDFTranslate;
import ru.betterend.util.sdf.primitive.SDFSphere;
import ru.betterend.world.features.DefaultFeature;
public class PythadendronTreeFeature extends DefaultFeature {
private static final Function<BlockState, Boolean> REPLACE;
private static final Function<BlockState, Boolean> IGNORE;
private static final Function<PosInfo, BlockState> POST;
@Override
public boolean generate(StructureWorldAccess world, ChunkGenerator chunkGenerator, Random random, BlockPos pos, DefaultFeatureConfig config) {
if (world.getBlockState(pos.down()).getBlock() != EndBlocks.CHORUS_NYLIUM) return false;
BlocksHelper.setWithoutUpdate(world, pos, AIR);
float size = MHelper.randRange(10, 20, random);
List<Vector3f> spline = SplineHelper.makeSpline(0, 0, 0, 0, size, 0, 4);
SplineHelper.offsetParts(spline, random, 0.7F, 0, 0.7F);
Vector3f last = spline.get(spline.size() - 1);
int depth = MHelper.floor((size - 10F) * 3F / 10F + 1F);
float bsize = (10F - (size - 10F)) / 10F + 1.5F;
branch(last.getX(), last.getY(), last.getZ(), size * bsize, MHelper.randRange(0, MHelper.PI2, random), random, depth, world, pos);
SDF function = SplineHelper.buildSDF(spline, 1.7F, 1.1F, (bpos) -> {
return EndBlocks.PYTHADENDRON.bark.getDefaultState();
});
function.setReplaceFunction(REPLACE);
function.setPostProcess(POST);
function.fillRecursive(world, pos);
return true;
}
private void branch(float x, float y, float z, float size, float angle, Random random, int depth, StructureWorldAccess world, BlockPos pos) {
if (depth == 0) return;
float dx = (float) Math.cos(angle) * size * 0.15F;
float dz = (float) Math.sin(angle) * size * 0.15F;
float x1 = x + dx;
float z1 = z + dz;
float x2 = x - dx;
float z2 = z - dz;
List<Vector3f> spline = SplineHelper.makeSpline(x, y, z, x1, y, z1, 5);
SplineHelper.powerOffset(spline, size * MHelper.randRange(1.0F, 2.0F, random), 4);
SplineHelper.offsetParts(spline, random, 0.3F, 0, 0.3F);
Vector3f pos1 = spline.get(spline.size() - 1);
boolean s1 = SplineHelper.fillSpline(spline, world, EndBlocks.PYTHADENDRON.bark.getDefaultState(), pos, REPLACE);
spline = SplineHelper.makeSpline(x, y, z, x2, y, z2, 5);
SplineHelper.powerOffset(spline, size * MHelper.randRange(1.0F, 2.0F, random), 4);
SplineHelper.offsetParts(spline, random, 0.3F, 0, 0.3F);
Vector3f pos2 = spline.get(spline.size() - 1);
boolean s2 = SplineHelper.fillSpline(spline, world, EndBlocks.PYTHADENDRON.bark.getDefaultState(), pos, REPLACE);
OpenSimplexNoise noise = new OpenSimplexNoise(random.nextInt());
if (depth < 3) {
if (s1) {
leavesBall(world, pos.add(pos1.getX(), pos1.getY(), pos1.getZ()), random, noise);
}
if (s2) {
leavesBall(world, pos.add(pos2.getX(), pos2.getY(), pos2.getZ()), random, noise);
}
}
float size1 = size * MHelper.randRange(0.75F, 0.95F, random);
float size2 = size * MHelper.randRange(0.75F, 0.95F, random);
float angle1 = angle + (float) Math.PI * 0.5F + MHelper.randRange(-0.1F, 0.1F, random);
float angle2 = angle + (float) Math.PI * 0.5F + MHelper.randRange(-0.1F, 0.1F, random);
if (s1) {
branch(pos1.getX(), pos1.getY(), pos1.getZ(), size1, angle1, random, depth - 1, world, pos);
}
if (s2) {
branch(pos2.getX(), pos2.getY(), pos2.getZ(), size2, angle2, random, depth - 1, world, pos);
}
}
private void leavesBall(StructureWorldAccess world, BlockPos pos, Random random, OpenSimplexNoise noise) {
float radius = MHelper.randRange(4.5F, 6.5F, random);
SDF sphere = new SDFSphere().setRadius(radius).setBlock(EndBlocks.PYTHADENDRON_LEAVES.getDefaultState().with(LeavesBlock.DISTANCE, 1));
sphere = new SDFScale3D().setScale(1, 0.6F, 1).setSource(sphere);
sphere = new SDFDisplacement().setFunction((vec) -> { return (float) noise.eval(vec.getX() * 0.2, vec.getY() * 0.2, vec.getZ() * 0.2) * 3; }).setSource(sphere);
sphere = new SDFDisplacement().setFunction((vec) -> { return random.nextFloat() * 3F - 1.5F; }).setSource(sphere);
sphere = new SDFSubtraction().setSourceA(sphere).setSourceB(new SDFTranslate().setTranslate(0, -radius, 0).setSource(sphere));
Mutable mut = new Mutable();
sphere.setPostProcess((info) -> {
if (random.nextInt(5) == 0) {
for (Direction dir: Direction.values()) {
BlockState state = info.getState(dir, 2);
if (state.isAir()) {
return info.getState();
}
}
info.setState(EndBlocks.PYTHADENDRON.bark.getDefaultState());
for (int x = -6; x < 7; x++) {
int ax = Math.abs(x);
mut.setX(x + info.getPos().getX());
for (int z = -6; z < 7; z++) {
int az = Math.abs(z);
mut.setZ(z + info.getPos().getZ());
for (int y = -6; y < 7; y++) {
int ay = Math.abs(y);
int d = ax + ay + az;
if (d < 7) {
mut.setY(y + info.getPos().getY());
BlockState state = info.getState(mut);
if (state.getBlock() instanceof LeavesBlock) {
int distance = state.get(LeavesBlock.DISTANCE);
if (d < distance) {
info.setState(mut, state.with(LeavesBlock.DISTANCE, d));
}
}
}
}
}
}
}
return info.getState();
});
sphere.fillRecursiveIgnore(world, pos, IGNORE);
}
static {
REPLACE = (state) -> {
if (state.isIn(EndTags.END_GROUND)) {
return true;
}
if (state.getBlock() == EndBlocks.PYTHADENDRON_LEAVES) {
return true;
}
if (state.getMaterial().equals(Material.PLANT)) {
return true;
}
return state.getMaterial().isReplaceable();
};
IGNORE = (state) -> {
return EndBlocks.PYTHADENDRON.isTreeLog(state);
};
POST = (info) -> {
if (EndBlocks.PYTHADENDRON.isTreeLog(info.getStateUp()) && EndBlocks.PYTHADENDRON.isTreeLog(info.getStateDown())) {
return EndBlocks.PYTHADENDRON.log.getDefaultState();
}
return info.getState();
};
}
}