mirror of
https://github.com/GTNewHorizons/Botania.git
synced 2026-11-20 20:22:33 +00:00
Separate segment generation from lightning particle itself
This commit is contained in:
1 parent
4d71707a3a
commit
ae8fbd29e6
3 files changed
+182
-159
No files matched your search
@@ -11,6 +11,7 @@ package vazkii.botania.client.fx;
|
||||
import com.mojang.blaze3d.matrix.MatrixStack;
|
||||
import com.mojang.blaze3d.vertex.IVertexBuilder;
|
||||
|
||||
import com.mojang.datafixers.util.Pair;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.particle.IParticleRenderType;
|
||||
@@ -38,40 +39,26 @@ import java.util.*;
|
||||
public class FXLightning extends Particle {
|
||||
|
||||
private static final int fadetime = 20;
|
||||
private final Map<Integer, Integer> splitParents = new HashMap<>();
|
||||
private final double length;
|
||||
private final Random rand;
|
||||
private final int expandTime;
|
||||
private final int colorOuter;
|
||||
private final int colorInner;
|
||||
|
||||
private List<FXLightningSegment> segments = new ArrayList<>();
|
||||
private int segmentCount = 1;
|
||||
private int splitCount;
|
||||
private float speed = 1.5F;
|
||||
private List<FXLightningSegment> segments;
|
||||
private int segmentCount;
|
||||
|
||||
public FXLightning(ClientWorld world, Vector3 sourcevec, Vector3 targetvec, float ticksPerMeter, long seed, int colorOuter, int colorInner) {
|
||||
public FXLightning(ClientWorld world, Vector3 sourcevec, Vector3 targetvec, float speed, long seed, int colorOuter, int colorInner) {
|
||||
super(world, sourcevec.x, sourcevec.y, sourcevec.z);
|
||||
rand = new Random(seed);
|
||||
speed = ticksPerMeter;
|
||||
this.colorOuter = colorOuter;
|
||||
this.colorInner = colorInner;
|
||||
length = targetvec.subtract(sourcevec).mag();
|
||||
maxAge = fadetime + rand.nextInt(fadetime) - fadetime / 2;
|
||||
double length = targetvec.subtract(sourcevec).mag();
|
||||
maxAge = fadetime + world.rand.nextInt(fadetime) - fadetime / 2;
|
||||
expandTime = (int) (length * speed);
|
||||
age = -(int) (length * speed);
|
||||
|
||||
segments.add(new FXLightningSegment(sourcevec, targetvec));
|
||||
|
||||
fractal(2, length / 1.5, 0.7F, 0.7F, 45);
|
||||
fractal(2, length / 4, 0.5F, 0.8F, 50);
|
||||
fractal(2, length / 15, 0.5F, 0.9F, 55);
|
||||
fractal(2, length / 30, 0.5F, 1.0F, 60);
|
||||
fractal(2, length / 60, 0, 0, 0);
|
||||
fractal(2, length / 100, 0, 0, 0);
|
||||
fractal(2, length / 400, 0, 0, 0);
|
||||
|
||||
calculateCollisionAndDiffs();
|
||||
|
||||
segments.sort((o1, o2) -> Float.compare(o2.light, o1.light));
|
||||
LightningSegmentGenerator gen = new LightningSegmentGenerator(seed);
|
||||
Pair<Integer, List<FXLightningSegment>> res = gen.compute(sourcevec, targetvec, length);
|
||||
segmentCount = res.getFirst();
|
||||
segments = res.getSecond();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -97,8 +84,6 @@ public class FXLightning extends Particle {
|
||||
mainAlpha = 1 - boltAge * 0.5F;
|
||||
}
|
||||
|
||||
int expandTime = (int) (length * speed);
|
||||
|
||||
int renderstart = (int) ((expandTime / 2 - maxAge + age) / (float) (expandTime / 2) * segmentCount);
|
||||
int renderend = (int) ((age + expandTime) / (float) expandTime * segmentCount);
|
||||
|
||||
@@ -161,131 +146,6 @@ public class FXLightning extends Particle {
|
||||
}
|
||||
}
|
||||
|
||||
private void fractal(int splits, double amount, double splitChance, double splitLength, double splitAngle) {
|
||||
List<FXLightningSegment> oldSegments = segments;
|
||||
segments = new ArrayList<>();
|
||||
|
||||
FXLightningSegment prev;
|
||||
|
||||
for (FXLightningSegment segment : oldSegments) {
|
||||
prev = segment.prev;
|
||||
|
||||
Vector3 subsegment = segment.diff.multiply(1F / splits);
|
||||
|
||||
FXLightningBoltPoint[] newpoints = new FXLightningBoltPoint[splits + 1];
|
||||
|
||||
Vector3 startpoint = segment.startPoint.point;
|
||||
newpoints[0] = segment.startPoint;
|
||||
newpoints[splits] = segment.endPoint;
|
||||
|
||||
for (int i = 1; i < splits; i++) {
|
||||
Vector3 randoff = segment.diff.perpendicular().normalize().rotate(rand.nextFloat() * 360, segment.diff);
|
||||
randoff = randoff.multiply((rand.nextFloat() - 0.5F) * amount * 2);
|
||||
|
||||
Vector3 basepoint = startpoint.add(subsegment.multiply(i));
|
||||
|
||||
newpoints[i] = new FXLightningBoltPoint(basepoint, randoff);
|
||||
}
|
||||
|
||||
for (int i = 0; i < splits; i++) {
|
||||
FXLightningSegment next = new FXLightningSegment(newpoints[i], newpoints[i + 1], segment.light, segment.segmentNo * splits + i, segment.splitNo);
|
||||
next.prev = prev;
|
||||
if (prev != null) {
|
||||
prev.next = next;
|
||||
}
|
||||
|
||||
if (i != 0 && rand.nextFloat() < splitChance) {
|
||||
Vector3 splitrot = next.diff.xCrossProduct().rotate(rand.nextFloat() * 360, next.diff);
|
||||
Vector3 diff = next.diff.rotate((rand.nextFloat() * 0.66F + 0.33F) * splitAngle, splitrot).multiply(splitLength);
|
||||
|
||||
splitCount++;
|
||||
splitParents.put(splitCount, next.splitNo);
|
||||
|
||||
FXLightningSegment split = new FXLightningSegment(newpoints[i], new FXLightningBoltPoint(newpoints[i + 1].basepoint, newpoints[i + 1].offsetvec.add(diff)), segment.light / 2F, next.segmentNo, splitCount);
|
||||
split.prev = prev;
|
||||
|
||||
segments.add(split);
|
||||
}
|
||||
|
||||
prev = next;
|
||||
segments.add(next);
|
||||
}
|
||||
|
||||
if (segment.next != null) {
|
||||
segment.next.prev = prev;
|
||||
}
|
||||
}
|
||||
|
||||
segmentCount *= splits;
|
||||
}
|
||||
|
||||
private float rayTraceResistance(Vector3 start, Vector3 end, float prevresistance) {
|
||||
RayTraceContext ctx = new RayTraceContext(start.toVector3d(), end.toVector3d(), RayTraceContext.BlockMode.OUTLINE, RayTraceContext.FluidMode.NONE, null);
|
||||
RayTraceResult mop = world.rayTraceBlocks(ctx);
|
||||
|
||||
if (mop.getType() == RayTraceResult.Type.BLOCK) {
|
||||
BlockPos pos = ((BlockRayTraceResult) mop).getPos();
|
||||
Block block = world.getBlockState(pos).getBlock();
|
||||
|
||||
if (world.isAirBlock(pos)) {
|
||||
return prevresistance;
|
||||
}
|
||||
|
||||
return prevresistance + block.getExplosionResistance() + 0.3F;
|
||||
} else {
|
||||
return prevresistance;
|
||||
}
|
||||
}
|
||||
|
||||
private void calculateCollisionAndDiffs() {
|
||||
Map<Integer, Integer> lastactivesegment = new HashMap<>();
|
||||
|
||||
segments.sort((o1, o2) -> {
|
||||
int comp = Integer.compare(o1.splitNo, o2.splitNo);
|
||||
if (comp == 0) {
|
||||
return Integer.compare(o1.segmentNo, o2.segmentNo);
|
||||
} else {
|
||||
return comp;
|
||||
}
|
||||
});
|
||||
|
||||
int lastSplitCalc = 0;
|
||||
int lastActiveSegment = 0;// unterminated
|
||||
float splitResistance = 0;
|
||||
|
||||
for (FXLightningSegment segment : segments) {
|
||||
if (segment.splitNo > lastSplitCalc) {
|
||||
lastactivesegment.put(lastSplitCalc, lastActiveSegment);
|
||||
lastSplitCalc = segment.splitNo;
|
||||
lastActiveSegment = lastactivesegment.get(splitParents.get(segment.splitNo));
|
||||
splitResistance = lastActiveSegment < segment.segmentNo ? 50 : 0;
|
||||
}
|
||||
|
||||
if (splitResistance >= 40 * segment.light) {
|
||||
continue;
|
||||
}
|
||||
|
||||
splitResistance = rayTraceResistance(segment.startPoint.point, segment.endPoint.point, splitResistance);
|
||||
lastActiveSegment = segment.segmentNo;
|
||||
}
|
||||
lastactivesegment.put(lastSplitCalc, lastActiveSegment);
|
||||
|
||||
lastSplitCalc = 0;
|
||||
lastActiveSegment = lastactivesegment.get(0);
|
||||
for (Iterator<FXLightningSegment> iterator = segments.iterator(); iterator.hasNext();) {
|
||||
FXLightningSegment segment = iterator.next();
|
||||
if (lastSplitCalc != segment.splitNo) {
|
||||
lastSplitCalc = segment.splitNo;
|
||||
lastActiveSegment = lastactivesegment.get(segment.splitNo);
|
||||
}
|
||||
|
||||
if (segment.segmentNo > lastActiveSegment) {
|
||||
iterator.remove();
|
||||
}
|
||||
segment.calcEndDiffs();
|
||||
}
|
||||
}
|
||||
|
||||
private static Vector3 getRelativeViewVector(Vector3 pos) {
|
||||
Entity renderEntity = Minecraft.getInstance().getRenderViewEntity();
|
||||
return new Vector3((float) renderEntity.getPosX() - pos.x, (float) renderEntity.getPosY() - pos.y, (float) renderEntity.getPosZ() - pos.z);
|
||||
|
||||
@@ -15,7 +15,7 @@ public class FXLightningSegment {
|
||||
public final FXLightningBoltPoint startPoint;
|
||||
public final FXLightningBoltPoint endPoint;
|
||||
|
||||
public Vector3 diff;
|
||||
public final Vector3 diff;
|
||||
|
||||
public FXLightningSegment prev;
|
||||
public FXLightningSegment next;
|
||||
@@ -36,18 +36,13 @@ public class FXLightningSegment {
|
||||
this.light = light;
|
||||
segmentNo = segmentnumber;
|
||||
splitNo = splitnumber;
|
||||
|
||||
calcDiff();
|
||||
diff = endPoint.point.subtract(startPoint.point);
|
||||
}
|
||||
|
||||
public FXLightningSegment(Vector3 start, Vector3 end) {
|
||||
this(new FXLightningBoltPoint(start, new Vector3(0, 0, 0)), new FXLightningBoltPoint(end, new Vector3(0, 0, 0)), 1, 0, 0);
|
||||
}
|
||||
|
||||
public void calcDiff() {
|
||||
diff = endPoint.point.subtract(startPoint.point);
|
||||
}
|
||||
|
||||
public void calcEndDiffs() {
|
||||
if (prev != null) {
|
||||
Vector3 prevdiffnorm = prev.diff.normalize();
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
package vazkii.botania.client.fx;
|
||||
|
||||
import com.mojang.datafixers.util.Pair;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.BlockRayTraceResult;
|
||||
import net.minecraft.util.math.RayTraceContext;
|
||||
import net.minecraft.util.math.RayTraceResult;
|
||||
import net.minecraft.world.World;
|
||||
import vazkii.botania.common.core.helper.Vector3;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class LightningSegmentGenerator {
|
||||
private static final int BRANCH_FACTOR = 2;
|
||||
private final Random rand;
|
||||
private final Map<Integer, Integer> splitParents = new HashMap<>();
|
||||
private int segmentCount = 1;
|
||||
private int splitCount = 0;
|
||||
|
||||
public LightningSegmentGenerator(long seed) {
|
||||
this.rand = new Random(seed);
|
||||
}
|
||||
|
||||
public Pair<Integer, List<FXLightningSegment>> compute(Vector3 start, Vector3 end, double length) {
|
||||
List<FXLightningSegment> segmentsA = new ArrayList<>();
|
||||
segmentsA.add(new FXLightningSegment(start, end));
|
||||
|
||||
List<FXLightningSegment> segmentsB = new ArrayList<>();
|
||||
|
||||
// Alternate between two lists to save memory allocations
|
||||
fractal(segmentsA, segmentsB, length / 1.5, 0.7F, 0.7F, 45);
|
||||
fractal(segmentsB, segmentsA, length / 4, 0.5F, 0.8F, 50);
|
||||
fractal(segmentsA, segmentsB, length / 15, 0.5F, 0.9F, 55);
|
||||
fractal(segmentsB, segmentsA, length / 30, 0.5F, 1.0F, 60);
|
||||
fractal(segmentsA, segmentsB, length / 60, 0, 0, 0);
|
||||
fractal(segmentsB, segmentsA, length / 100, 0, 0, 0);
|
||||
fractal(segmentsA, segmentsB, length / 400, 0, 0, 0);
|
||||
|
||||
calculateCollisionAndDiffs(segmentsB);
|
||||
segmentsB.sort((o1, o2) -> Float.compare(o2.light, o1.light));
|
||||
return Pair.of(segmentCount, segmentsB);
|
||||
}
|
||||
|
||||
private void fractal(List<FXLightningSegment> oldSegments, List<FXLightningSegment> outputSegments, double amount, double splitChance, double splitLength, double splitAngle) {
|
||||
outputSegments.clear();
|
||||
FXLightningSegment prev;
|
||||
|
||||
for (FXLightningSegment segment : oldSegments) {
|
||||
prev = segment.prev;
|
||||
|
||||
Vector3 subsegment = segment.diff.multiply(1F / BRANCH_FACTOR);
|
||||
|
||||
FXLightningBoltPoint[] newpoints = new FXLightningBoltPoint[BRANCH_FACTOR + 1];
|
||||
|
||||
Vector3 startpoint = segment.startPoint.point;
|
||||
newpoints[0] = segment.startPoint;
|
||||
newpoints[BRANCH_FACTOR] = segment.endPoint;
|
||||
|
||||
for (int i = 1; i < BRANCH_FACTOR; i++) {
|
||||
Vector3 basepoint = startpoint.add(subsegment.multiply(i));
|
||||
Vector3 randoff = segment.diff.perpendicular().normalize().rotate(rand.nextFloat() * 360, segment.diff)
|
||||
.multiply((rand.nextFloat() - 0.5F) * amount * 2);
|
||||
newpoints[i] = new FXLightningBoltPoint(basepoint, randoff);
|
||||
}
|
||||
|
||||
for (int i = 0; i < BRANCH_FACTOR; i++) {
|
||||
FXLightningSegment next = new FXLightningSegment(newpoints[i], newpoints[i + 1], segment.light, segment.segmentNo * BRANCH_FACTOR + i, segment.splitNo);
|
||||
next.prev = prev;
|
||||
if (prev != null) {
|
||||
prev.next = next;
|
||||
}
|
||||
|
||||
if (i != 0 && rand.nextFloat() < splitChance) {
|
||||
Vector3 splitrot = next.diff.xCrossProduct().rotate(rand.nextFloat() * 360, next.diff);
|
||||
Vector3 diff = next.diff.rotate((rand.nextFloat() * 0.66F + 0.33F) * splitAngle, splitrot).multiply(splitLength);
|
||||
|
||||
splitCount++;
|
||||
splitParents.put(splitCount, next.splitNo);
|
||||
|
||||
FXLightningSegment split = new FXLightningSegment(newpoints[i], new FXLightningBoltPoint(newpoints[i + 1].basepoint, newpoints[i + 1].offsetvec.add(diff)), segment.light / 2F, next.segmentNo, splitCount);
|
||||
split.prev = prev;
|
||||
|
||||
outputSegments.add(split);
|
||||
}
|
||||
|
||||
prev = next;
|
||||
outputSegments.add(next);
|
||||
}
|
||||
|
||||
if (segment.next != null) {
|
||||
segment.next.prev = prev;
|
||||
}
|
||||
}
|
||||
|
||||
segmentCount *= BRANCH_FACTOR;
|
||||
}
|
||||
|
||||
private void calculateCollisionAndDiffs(List<FXLightningSegment> segments) {
|
||||
Map<Integer, Integer> lastactivesegment = new HashMap<>();
|
||||
|
||||
segments.sort((o1, o2) -> {
|
||||
int comp = Integer.compare(o1.splitNo, o2.splitNo);
|
||||
if (comp == 0) {
|
||||
return Integer.compare(o1.segmentNo, o2.segmentNo);
|
||||
} else {
|
||||
return comp;
|
||||
}
|
||||
});
|
||||
|
||||
int lastSplitCalc = 0;
|
||||
int lastActiveSegment = 0;// unterminated
|
||||
float splitResistance = 0;
|
||||
|
||||
for (FXLightningSegment segment : segments) {
|
||||
if (segment.splitNo > lastSplitCalc) {
|
||||
lastactivesegment.put(lastSplitCalc, lastActiveSegment);
|
||||
lastSplitCalc = segment.splitNo;
|
||||
lastActiveSegment = lastactivesegment.get(splitParents.get(segment.splitNo));
|
||||
splitResistance = lastActiveSegment < segment.segmentNo ? 50 : 0;
|
||||
}
|
||||
|
||||
if (splitResistance >= 40 * segment.light) {
|
||||
continue;
|
||||
}
|
||||
|
||||
splitResistance = rayTraceResistance(segment.startPoint.point, segment.endPoint.point, splitResistance);
|
||||
lastActiveSegment = segment.segmentNo;
|
||||
}
|
||||
lastactivesegment.put(lastSplitCalc, lastActiveSegment);
|
||||
|
||||
lastSplitCalc = 0;
|
||||
lastActiveSegment = lastactivesegment.get(0);
|
||||
for (Iterator<FXLightningSegment> iterator = segments.iterator(); iterator.hasNext();) {
|
||||
FXLightningSegment segment = iterator.next();
|
||||
if (lastSplitCalc != segment.splitNo) {
|
||||
lastSplitCalc = segment.splitNo;
|
||||
lastActiveSegment = lastactivesegment.get(segment.splitNo);
|
||||
}
|
||||
|
||||
if (segment.segmentNo > lastActiveSegment) {
|
||||
iterator.remove();
|
||||
}
|
||||
segment.calcEndDiffs();
|
||||
}
|
||||
}
|
||||
|
||||
private float rayTraceResistance(Vector3 start, Vector3 end, float prevresistance) {
|
||||
World world = Minecraft.getInstance().world;
|
||||
RayTraceContext ctx = new RayTraceContext(start.toVector3d(), end.toVector3d(), RayTraceContext.BlockMode.OUTLINE, RayTraceContext.FluidMode.NONE, null);
|
||||
BlockRayTraceResult ray = world.rayTraceBlocks(ctx);
|
||||
|
||||
if (ray.getType() == RayTraceResult.Type.BLOCK) {
|
||||
BlockPos pos = ray.getPos();
|
||||
BlockState state = world.getBlockState(pos);
|
||||
|
||||
if (state.isAir()) {
|
||||
return prevresistance;
|
||||
}
|
||||
|
||||
return prevresistance + state.getBlock().getExplosionResistance() + 0.3F;
|
||||
} else {
|
||||
return prevresistance;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user