mirror of
https://github.com/GTNewHorizons/Botania.git
synced 2026-11-19 11:49:37 +00:00
Clean up lightning code
This commit is contained in:
7 files changed
+349
-394
No files matched your search
@@ -21,7 +21,6 @@ import net.minecraftforge.fml.common.gameevent.TickEvent.Phase;
|
||||
import net.minecraftforge.fml.common.gameevent.TickEvent.RenderTickEvent;
|
||||
import vazkii.botania.api.mana.IManaCollector;
|
||||
import vazkii.botania.api.mana.TileSignature;
|
||||
import vazkii.botania.client.core.handler.LightningHandler.LightningBolt;
|
||||
import vazkii.botania.client.gui.lexicon.GuiLexicon;
|
||||
import vazkii.botania.common.block.subtile.functional.SubTileVinculotus;
|
||||
import vazkii.botania.common.block.tile.corporea.TileCorporeaIndex;
|
||||
@@ -57,7 +56,6 @@ public class ClientTickHandler {
|
||||
@SubscribeEvent
|
||||
public void clientTickEnd(ClientTickEvent event) {
|
||||
if(event.phase == Phase.END) {
|
||||
LightningBolt.update();
|
||||
RedStringRenderer.tick();
|
||||
ItemsRemainingRenderHandler.tick();
|
||||
|
||||
|
||||
@@ -10,9 +10,6 @@
|
||||
*/
|
||||
package vazkii.botania.client.core.handler;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import gnu.trove.map.hash.TIntIntHashMap;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.GlStateManager;
|
||||
import net.minecraft.client.renderer.Tessellator;
|
||||
@@ -20,37 +17,23 @@ import net.minecraft.client.renderer.texture.TextureManager;
|
||||
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.profiler.Profiler;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.RayTraceResult;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.client.event.RenderWorldLastEvent;
|
||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import vazkii.botania.client.core.handler.LightningHandler.LightningBolt.Segment;
|
||||
import vazkii.botania.client.fx.FXLightning;
|
||||
import vazkii.botania.client.fx.ParticleRenderDispatcher;
|
||||
import vazkii.botania.client.lib.LibResources;
|
||||
import vazkii.botania.common.core.helper.Vector3;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Deque;
|
||||
|
||||
public class LightningHandler {
|
||||
|
||||
private static final int BATCH_THRESHOLD = 200;
|
||||
private static final ResourceLocation outsideResource = new ResourceLocation(LibResources.MISC_WISP_LARGE);
|
||||
private static final ResourceLocation insideResource = new ResourceLocation(LibResources.MISC_WISP_SMALL);
|
||||
|
||||
private static Vector3 getRelativeViewVector(Vector3 pos) {
|
||||
Entity renderEntity = Minecraft.getMinecraft().getRenderViewEntity();
|
||||
return new Vector3((float) renderEntity.posX - pos.x, (float) renderEntity.posY + renderEntity.getEyeHeight() - pos.y, (float) renderEntity.posZ - pos.z);
|
||||
}
|
||||
public static final Deque<FXLightning> queuedLightningBolts = new ArrayDeque<>();
|
||||
|
||||
@SubscribeEvent
|
||||
public void onRenderWorldLast(RenderWorldLastEvent event) {
|
||||
@@ -85,8 +68,8 @@ public class LightningHandler {
|
||||
int counter = 0;
|
||||
|
||||
tessellator.getBuffer().begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX_LMAP_COLOR);
|
||||
for (LightningBolt bolt : LightningBolt.boltlist) {
|
||||
renderBolt(bolt, tessellator, 0, false);
|
||||
for (FXLightning bolt : queuedLightningBolts) {
|
||||
bolt.renderBolt(0, false);
|
||||
if(counter % BATCH_THRESHOLD == BATCH_THRESHOLD - 1) {
|
||||
tessellator.draw();
|
||||
tessellator.getBuffer().begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX_LMAP_COLOR);
|
||||
@@ -99,8 +82,8 @@ public class LightningHandler {
|
||||
counter = 0;
|
||||
|
||||
tessellator.getBuffer().begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX_LMAP_COLOR);
|
||||
for (LightningBolt bolt : LightningBolt.boltlist) {
|
||||
renderBolt(bolt, tessellator, 0, false);
|
||||
for (FXLightning bolt : queuedLightningBolts) {
|
||||
bolt.renderBolt(1, true);
|
||||
if(counter % BATCH_THRESHOLD == BATCH_THRESHOLD - 1) {
|
||||
tessellator.draw();
|
||||
tessellator.getBuffer().begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX_LMAP_COLOR);
|
||||
@@ -109,6 +92,8 @@ public class LightningHandler {
|
||||
}
|
||||
tessellator.draw();
|
||||
|
||||
queuedLightningBolts.clear();
|
||||
|
||||
GlStateManager.disableBlend();
|
||||
GlStateManager.depthMask(true);
|
||||
|
||||
@@ -119,369 +104,4 @@ public class LightningHandler {
|
||||
profiler.endSection();
|
||||
|
||||
}
|
||||
|
||||
public static void spawnLightningBolt(World world, Vector3 vectorStart, Vector3 vectorEnd, float ticksPerMeter, long seed, int colorOuter, int colorInner) {
|
||||
LightningBolt bolt = new LightningBolt(world, vectorStart, vectorEnd, ticksPerMeter, seed, colorOuter, colorInner);
|
||||
bolt.defaultFractal();
|
||||
bolt.finalizeBolt();
|
||||
LightningBolt.boltlist.add(bolt);
|
||||
}
|
||||
|
||||
private void renderBolt(LightningBolt bolt, Tessellator tessellator, int pass, boolean inner) {
|
||||
ParticleRenderDispatcher.lightningCount++;
|
||||
float boltage = bolt.particleAge < 0 ? 0 : (float) bolt.particleAge / (float) bolt.particleMaxAge;
|
||||
float mainalpha;
|
||||
if(pass == 0)
|
||||
mainalpha = (1 - boltage) * 0.4F;
|
||||
else mainalpha = 1 - boltage * 0.5F;
|
||||
|
||||
int expandTime = (int) (bolt.length * bolt.speed);
|
||||
|
||||
int renderstart = (int) ((expandTime / 2 - bolt.particleMaxAge + bolt.particleAge) / (float) (expandTime / 2) * bolt.numsegments0);
|
||||
int renderend = (int) ((bolt.particleAge + expandTime) / (float) expandTime * bolt.numsegments0);
|
||||
|
||||
for(Segment rendersegment : bolt.segments) {
|
||||
if(rendersegment.segmentno < renderstart || rendersegment.segmentno > renderend)
|
||||
continue;
|
||||
|
||||
Vector3 playervec = getRelativeViewVector(rendersegment.startpoint.point).multiply(-1);
|
||||
|
||||
double width = 0.025F * (playervec.mag() / 5 + 1) * (1 + rendersegment.light) * 0.5F;
|
||||
|
||||
Vector3 diff1 = playervec.copy().crossProduct(rendersegment.prevdiff).normalize().multiply(width / rendersegment.sinprev);
|
||||
Vector3 diff2 = playervec.copy().crossProduct(rendersegment.nextdiff).normalize().multiply(width / rendersegment.sinnext);
|
||||
|
||||
Vector3 startvec = rendersegment.startpoint.point;
|
||||
Vector3 endvec = rendersegment.endpoint.point;
|
||||
|
||||
int color = inner ? bolt.colorInner : bolt.colorOuter;
|
||||
int r = (color & 0xFF0000) >> 16;
|
||||
int g = (color & 0xFF00) >> 8;
|
||||
int b = (color & 0xFF);
|
||||
int a = (int) (mainalpha * rendersegment.light * new Color(color).getAlpha());
|
||||
|
||||
tessellator.getBuffer().pos(endvec.x - diff2.x, endvec.y - diff2.y, endvec.z - diff2.z).tex(0.5, 0).lightmap(0xF0, 0xF0).color(r, g, b, a).endVertex();
|
||||
tessellator.getBuffer().pos(startvec.x - diff1.x, startvec.y - diff1.y, startvec.z - diff1.z).tex(0.5, 0).lightmap(0xF0, 0xF0).color(r, g, b, a).endVertex();
|
||||
tessellator.getBuffer().pos(startvec.x + diff1.x, startvec.y + diff1.y, startvec.z + diff1.z).tex(0.5, 1).lightmap(0xF0, 0xF0).color(r, g, b, a).endVertex();
|
||||
tessellator.getBuffer().pos(endvec.x + diff2.x, endvec.y + diff2.y, endvec.z + diff2.z).tex(0.5, 1).lightmap(0xF0, 0xF0).color(r, g, b, a).endVertex();
|
||||
|
||||
if(rendersegment.next == null) {
|
||||
Vector3 roundend = rendersegment.endpoint.point.copy().add(rendersegment.diff.copy().normalize().multiply(width));
|
||||
|
||||
tessellator.getBuffer().pos(roundend.x - diff2.x, roundend.y - diff2.y, roundend.z - diff2.z).tex(0, 0).lightmap(0xF0, 0xF0).color(r, g, b, a).endVertex();
|
||||
tessellator.getBuffer().pos(endvec.x - diff2.x, endvec.y - diff2.y, endvec.z - diff2.z).tex(0.5, 0).lightmap(0xF0, 0xF0).color(r, g, b, a).endVertex();
|
||||
tessellator.getBuffer().pos(endvec.x + diff2.x, endvec.y + diff2.y, endvec.z + diff2.z).tex(0.5, 1).lightmap(0xF0, 0xF0).color(r, g, b, a).endVertex();
|
||||
tessellator.getBuffer().pos(roundend.x + diff2.x, roundend.y + diff2.y, roundend.z + diff2.z).tex(0, 1).lightmap(0xF0, 0xF0).color(r, g, b, a).endVertex();
|
||||
}
|
||||
|
||||
if(rendersegment.prev == null) {
|
||||
Vector3 roundend = rendersegment.startpoint.point.copy().subtract(rendersegment.diff.copy().normalize().multiply(width));
|
||||
|
||||
tessellator.getBuffer().pos(startvec.x - diff1.x, startvec.y - diff1.y, startvec.z - diff1.z).tex(0.5, 0).lightmap(0xF0, 0xF0).color(r, g, b, a).endVertex();
|
||||
tessellator.getBuffer().pos(roundend.x - diff1.x, roundend.y - diff1.y, roundend.z - diff1.z).tex(0, 0).lightmap(0xF0, 0xF0).color(r, g, b, a).endVertex();
|
||||
tessellator.getBuffer().pos(roundend.x + diff1.x, roundend.y + diff1.y, roundend.z + diff1.z).tex(0, 1).lightmap(0xF0, 0xF0).color(r, g, b, a).endVertex();
|
||||
tessellator.getBuffer().pos(startvec.x + diff1.x, startvec.y + diff1.y, startvec.z + diff1.z).tex(0.5, 1).lightmap(0xF0, 0xF0).color(r, g, b, a).endVertex();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class LightningBolt {
|
||||
|
||||
public ArrayList<Segment> segments = new ArrayList<>();
|
||||
public final Vector3 start;
|
||||
public final Vector3 end;
|
||||
final TIntIntHashMap splitparents = new TIntIntHashMap();
|
||||
|
||||
public final double length;
|
||||
public int numsegments0;
|
||||
private int numsplits;
|
||||
private boolean finalized;
|
||||
private final Random rand;
|
||||
public final long seed;
|
||||
|
||||
public int particleAge;
|
||||
public final int particleMaxAge;
|
||||
public boolean isDead;
|
||||
|
||||
private final World world;
|
||||
private Entity source;
|
||||
|
||||
public static final List<LightningBolt> boltlist = new ArrayList<>();
|
||||
|
||||
public float speed = 1.5F;
|
||||
public static final int fadetime = 20;
|
||||
|
||||
public final int colorOuter;
|
||||
public final int colorInner;
|
||||
|
||||
public static class BoltPoint {
|
||||
|
||||
public BoltPoint(Vector3 basepoint, Vector3 offsetvec) {
|
||||
point = basepoint.copy().add(offsetvec);
|
||||
this.basepoint = basepoint;
|
||||
this.offsetvec = offsetvec;
|
||||
}
|
||||
|
||||
public final Vector3 point;
|
||||
final Vector3 basepoint;
|
||||
final Vector3 offsetvec;
|
||||
}
|
||||
|
||||
private static final Comparator<Segment> SEGMENT_SORTER = (o1, o2) -> {
|
||||
int comp = Integer.valueOf(o1.splitno).compareTo(o2.splitno);
|
||||
if(comp == 0)
|
||||
return Integer.valueOf(o1.segmentno).compareTo(o2.segmentno);
|
||||
else return comp;
|
||||
};
|
||||
|
||||
private static final Comparator<Segment> SEGMENT_LIGHT_SORTER = (o1, o2) -> Float.compare(o2.light, o1.light);
|
||||
|
||||
public class Segment {
|
||||
public Segment(BoltPoint start, BoltPoint end, float light, int segmentnumber, int splitnumber) {
|
||||
startpoint = start;
|
||||
endpoint = end;
|
||||
this.light = light;
|
||||
segmentno = segmentnumber;
|
||||
splitno = splitnumber;
|
||||
|
||||
calcDiff();
|
||||
}
|
||||
|
||||
public Segment(Vector3 start, Vector3 end) {
|
||||
this(new BoltPoint(start, new Vector3(0, 0, 0)), new BoltPoint(end, new Vector3(0, 0, 0)), 1, 0, 0);
|
||||
}
|
||||
|
||||
public void calcDiff() {
|
||||
diff = endpoint.point.copy().subtract(startpoint.point);
|
||||
}
|
||||
|
||||
public void calcEndDiffs() {
|
||||
if(prev != null) {
|
||||
Vector3 prevdiffnorm = prev.diff.copy().normalize();
|
||||
Vector3 thisdiffnorm = diff.copy().normalize();
|
||||
|
||||
prevdiff = thisdiffnorm.copy().add(prevdiffnorm).normalize();
|
||||
sinprev = (float) Math.sin(thisdiffnorm.angle(prevdiffnorm.multiply(-1)) / 2);
|
||||
} else {
|
||||
prevdiff = diff.copy().normalize();
|
||||
sinprev = 1;
|
||||
}
|
||||
|
||||
if(next != null) {
|
||||
Vector3 nextdiffnorm = next.diff.copy().normalize();
|
||||
Vector3 thisdiffnorm = diff.copy().normalize();
|
||||
|
||||
nextdiff = thisdiffnorm.add(nextdiffnorm).normalize();
|
||||
sinnext = (float) Math.sin(thisdiffnorm.angle(nextdiffnorm.multiply(-1)) / 2);
|
||||
} else {
|
||||
nextdiff = diff.copy().normalize();
|
||||
sinnext = 1;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return startpoint.point.toString() + " " + endpoint.point.toString();
|
||||
}
|
||||
|
||||
public final BoltPoint startpoint;
|
||||
public final BoltPoint endpoint;
|
||||
|
||||
public Vector3 diff;
|
||||
|
||||
public Segment prev;
|
||||
public Segment next;
|
||||
|
||||
public Vector3 nextdiff;
|
||||
public Vector3 prevdiff;
|
||||
|
||||
public float sinprev;
|
||||
public float sinnext;
|
||||
public final float light;
|
||||
|
||||
public final int segmentno;
|
||||
public final int splitno;
|
||||
}
|
||||
|
||||
public LightningBolt(World world, Vector3 sourcevec, Vector3 targetvec, float ticksPerMeter, long seed, int colorOuter, int colorInner) {
|
||||
this.world = world;
|
||||
this.seed = seed;
|
||||
rand = new Random(seed);
|
||||
|
||||
start = sourcevec;
|
||||
end = targetvec;
|
||||
|
||||
speed = ticksPerMeter;
|
||||
|
||||
this.colorOuter = colorOuter;
|
||||
this.colorInner = colorInner;
|
||||
|
||||
numsegments0 = 1;
|
||||
|
||||
length = end.copy().subtract(start).mag();
|
||||
particleMaxAge = fadetime + rand.nextInt(fadetime) - fadetime / 2;
|
||||
particleAge = -(int) (length * speed);
|
||||
|
||||
segments.add(new Segment(start, end));
|
||||
}
|
||||
|
||||
public void setWrapper(Entity entity) {
|
||||
source = entity;
|
||||
}
|
||||
|
||||
public void fractal(int splits, double amount, double splitchance, double splitlength, double splitangle) {
|
||||
if(finalized)
|
||||
return;
|
||||
|
||||
ArrayList<Segment> oldsegments = segments;
|
||||
segments = new ArrayList<>();
|
||||
|
||||
Segment prev;
|
||||
|
||||
for(Segment segment : oldsegments) {
|
||||
prev = segment.prev;
|
||||
|
||||
Vector3 subsegment = segment.diff.copy().multiply(1F / splits);
|
||||
|
||||
BoltPoint[] newpoints = new BoltPoint[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.copy().perpendicular().normalize().rotate(rand.nextFloat() * 360, segment.diff);
|
||||
randoff.multiply((rand.nextFloat() - 0.5F) * amount * 2);
|
||||
|
||||
Vector3 basepoint = startpoint.copy().add(subsegment.copy().multiply(i));
|
||||
|
||||
newpoints[i] = new BoltPoint(basepoint, randoff);
|
||||
}
|
||||
|
||||
for(int i = 0; i < splits; i++) {
|
||||
Segment next = new Segment(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.copy().xCrossProduct().rotate(rand.nextFloat() * 360, next.diff);
|
||||
Vector3 diff = next.diff.copy().rotate((rand.nextFloat() * 0.66F + 0.33F) * splitangle, splitrot).multiply(splitlength);
|
||||
|
||||
numsplits++;
|
||||
splitparents.put(numsplits, next.splitno);
|
||||
|
||||
Segment split = new Segment(newpoints[i], new BoltPoint(newpoints[i + 1].basepoint, newpoints[i + 1].offsetvec.copy().add(diff)), segment.light / 2F, next.segmentno, numsplits);
|
||||
split.prev = prev;
|
||||
|
||||
segments.add(split);
|
||||
}
|
||||
|
||||
prev = next;
|
||||
segments.add(next);
|
||||
}
|
||||
|
||||
if(segment.next != null)
|
||||
segment.next.prev = prev;
|
||||
}
|
||||
|
||||
numsegments0 *= splits;
|
||||
}
|
||||
|
||||
public void defaultFractal() {
|
||||
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);
|
||||
}
|
||||
|
||||
private float rayTraceResistance(Vector3 start, Vector3 end, float prevresistance) {
|
||||
RayTraceResult mop = world.rayTraceBlocks(start.toVec3D(), end.toVec3D());
|
||||
|
||||
if(mop == null)
|
||||
return prevresistance;
|
||||
|
||||
if(mop.typeOfHit == RayTraceResult.Type.BLOCK) {
|
||||
Block block = world.getBlockState(mop.getBlockPos()).getBlock();
|
||||
|
||||
if(world.isAirBlock(mop.getBlockPos()))
|
||||
return prevresistance;
|
||||
|
||||
return prevresistance + block.getExplosionResistance(source) + 0.3F;
|
||||
} else return prevresistance;
|
||||
}
|
||||
|
||||
private void calculateCollisionAndDiffs() {
|
||||
TIntIntHashMap lastactivesegment = new TIntIntHashMap();
|
||||
|
||||
Collections.sort(segments, SEGMENT_SORTER);
|
||||
|
||||
int lastsplitcalc = 0;
|
||||
int lastactiveseg = 0;// unterminated
|
||||
float splitresistance = 0;
|
||||
|
||||
for(Segment segment : segments) {
|
||||
if(segment.splitno > lastsplitcalc) {
|
||||
lastactivesegment.put(lastsplitcalc, lastactiveseg);
|
||||
lastsplitcalc = segment.splitno;
|
||||
lastactiveseg = lastactivesegment.get(splitparents.get(segment.splitno));
|
||||
splitresistance = lastactiveseg < segment.segmentno ? 50 : 0;
|
||||
}
|
||||
|
||||
if(splitresistance >= 40 * segment.light)
|
||||
continue;
|
||||
|
||||
splitresistance = rayTraceResistance(segment.startpoint.point, segment.endpoint.point, splitresistance);
|
||||
lastactiveseg = segment.segmentno;
|
||||
}
|
||||
lastactivesegment.put(lastsplitcalc, lastactiveseg);
|
||||
|
||||
lastsplitcalc = 0;
|
||||
lastactiveseg = lastactivesegment.get(0);
|
||||
for(Iterator<Segment> iterator = segments.iterator(); iterator.hasNext();) {
|
||||
Segment segment = iterator.next();
|
||||
if(lastsplitcalc != segment.splitno) {
|
||||
lastsplitcalc = segment.splitno;
|
||||
lastactiveseg = lastactivesegment.get(segment.splitno);
|
||||
}
|
||||
|
||||
if(segment.segmentno > lastactiveseg)
|
||||
iterator.remove();
|
||||
segment.calcEndDiffs();
|
||||
}
|
||||
}
|
||||
|
||||
public void finalizeBolt() {
|
||||
if(finalized)
|
||||
return;
|
||||
finalized = true;
|
||||
|
||||
calculateCollisionAndDiffs();
|
||||
|
||||
Collections.sort(segments, SEGMENT_LIGHT_SORTER);
|
||||
|
||||
boltlist.add(this);
|
||||
}
|
||||
|
||||
public void onUpdate() {
|
||||
particleAge++;
|
||||
|
||||
if (particleAge >= particleMaxAge)
|
||||
isDead = true;
|
||||
}
|
||||
|
||||
// Called in ClientTickHandler
|
||||
public static void update() {
|
||||
for(Iterator<LightningBolt> iterator = boltlist.iterator(); iterator.hasNext();) {
|
||||
LightningBolt bolt = iterator.next();
|
||||
|
||||
bolt.onUpdate();
|
||||
if(bolt.isDead)
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -63,6 +63,7 @@ import vazkii.botania.client.core.handler.RenderLexicon;
|
||||
import vazkii.botania.client.core.handler.SubTileRadiusRenderHandler;
|
||||
import vazkii.botania.client.core.handler.TooltipHandler;
|
||||
import vazkii.botania.client.core.helper.ShaderHelper;
|
||||
import vazkii.botania.client.fx.FXLightning;
|
||||
import vazkii.botania.client.fx.FXSparkle;
|
||||
import vazkii.botania.client.fx.FXWisp;
|
||||
import vazkii.botania.client.gui.lexicon.GuiLexicon;
|
||||
@@ -486,7 +487,7 @@ public class ClientProxy extends CommonProxy {
|
||||
|
||||
@Override
|
||||
public void lightningFX(World world, Vector3 vectorStart, Vector3 vectorEnd, float ticksPerMeter, long seed, int colorOuter, int colorInner) {
|
||||
LightningHandler.spawnLightningBolt(world, vectorStart, vectorEnd, ticksPerMeter, seed, colorOuter, colorInner);
|
||||
Minecraft.getMinecraft().effectRenderer.addEffect(new FXLightning(world, vectorStart, vectorEnd, ticksPerMeter, seed, colorOuter, colorInner));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,248 @@
|
||||
package vazkii.botania.client.fx;
|
||||
|
||||
import gnu.trove.map.hash.TIntIntHashMap;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.particle.Particle;
|
||||
import net.minecraft.client.renderer.Tessellator;
|
||||
import net.minecraft.client.renderer.VertexBuffer;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.util.math.RayTraceResult;
|
||||
import net.minecraft.world.World;
|
||||
import vazkii.botania.client.core.handler.LightningHandler;
|
||||
import vazkii.botania.common.core.helper.Vector3;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
// Originally taken with permission from WRCBE - heavily modified
|
||||
public class FXLightning extends Particle {
|
||||
|
||||
private static final int fadetime = 20;
|
||||
private final TIntIntHashMap splitParents = new TIntIntHashMap();
|
||||
private final double length;
|
||||
private final Random rand;
|
||||
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;
|
||||
|
||||
public FXLightning(World world, Vector3 sourcevec, Vector3 targetvec, float ticksPerMeter, long seed, int colorOuter, int colorInner) {
|
||||
super(world, sourcevec.x, sourcevec.y, sourcevec.z);
|
||||
this.rand = new Random(seed);
|
||||
this.speed = ticksPerMeter;
|
||||
this.colorOuter = colorOuter;
|
||||
this.colorInner = colorInner;
|
||||
this.length = targetvec.copy().subtract(sourcevec).mag();
|
||||
this.particleMaxAge = fadetime + rand.nextInt(fadetime) - fadetime / 2;
|
||||
this.particleAge = -(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();
|
||||
|
||||
Collections.sort(segments, (o1, o2) -> Float.compare(o2.light, o1.light));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void renderParticle(VertexBuffer wr, Entity entity, float partialTicks, float rotX, float rotZ, float rotYZ, float rotXY, float rotXZ) {
|
||||
LightningHandler.queuedLightningBolts.offer(this);
|
||||
}
|
||||
|
||||
public void renderBolt(int pass, boolean inner) {
|
||||
ParticleRenderDispatcher.lightningCount++;
|
||||
VertexBuffer wr = Tessellator.getInstance().getBuffer();
|
||||
|
||||
float boltAge = particleAge < 0 ? 0 : (float) particleAge / (float) particleMaxAge;
|
||||
float mainAlpha;
|
||||
if(pass == 0)
|
||||
mainAlpha = (1 - boltAge) * 0.4F;
|
||||
else mainAlpha = 1 - boltAge * 0.5F;
|
||||
|
||||
int expandTime = (int) (length * speed);
|
||||
|
||||
int renderstart = (int) ((expandTime / 2 - particleMaxAge + particleAge) / (float) (expandTime / 2) * segmentCount);
|
||||
int renderend = (int) ((particleAge + expandTime) / (float) expandTime * segmentCount);
|
||||
|
||||
for(FXLightningSegment rendersegment : segments) {
|
||||
if(rendersegment.segmentNo < renderstart || rendersegment.segmentNo > renderend)
|
||||
continue;
|
||||
|
||||
Vector3 playerVec = getRelativeViewVector(rendersegment.startPoint.point).multiply(-1);
|
||||
|
||||
double width = 0.025F * (playerVec.mag() / 5 + 1) * (1 + rendersegment.light) * 0.5F;
|
||||
|
||||
Vector3 diff1 = playerVec.copy().crossProduct(rendersegment.prevDiff).normalize().multiply(width / rendersegment.sinPrev);
|
||||
Vector3 diff2 = playerVec.copy().crossProduct(rendersegment.nextDiff).normalize().multiply(width / rendersegment.sinNext);
|
||||
|
||||
Vector3 startvec = rendersegment.startPoint.point;
|
||||
Vector3 endvec = rendersegment.endPoint.point;
|
||||
|
||||
int color = inner ? colorInner : colorOuter;
|
||||
int r = (color & 0xFF0000) >> 16;
|
||||
int g = (color & 0xFF00) >> 8;
|
||||
int b = (color & 0xFF);
|
||||
int a = (int) (mainAlpha * rendersegment.light * 0xFF);
|
||||
|
||||
wr.pos(endvec.x - diff2.x, endvec.y - diff2.y, endvec.z - diff2.z).tex(0.5, 0).lightmap(0xF0, 0xF0).color(r, g, b, a).endVertex();
|
||||
wr.pos(startvec.x - diff1.x, startvec.y - diff1.y, startvec.z - diff1.z).tex(0.5, 0).lightmap(0xF0, 0xF0).color(r, g, b, a).endVertex();
|
||||
wr.pos(startvec.x + diff1.x, startvec.y + diff1.y, startvec.z + diff1.z).tex(0.5, 1).lightmap(0xF0, 0xF0).color(r, g, b, a).endVertex();
|
||||
wr.pos(endvec.x + diff2.x, endvec.y + diff2.y, endvec.z + diff2.z).tex(0.5, 1).lightmap(0xF0, 0xF0).color(r, g, b, a).endVertex();
|
||||
|
||||
if(rendersegment.next == null) {
|
||||
Vector3 roundend = rendersegment.endPoint.point.copy().add(rendersegment.diff.copy().normalize().multiply(width));
|
||||
|
||||
wr.pos(roundend.x - diff2.x, roundend.y - diff2.y, roundend.z - diff2.z).tex(0, 0).lightmap(0xF0, 0xF0).color(r, g, b, a).endVertex();
|
||||
wr.pos(endvec.x - diff2.x, endvec.y - diff2.y, endvec.z - diff2.z).tex(0.5, 0).lightmap(0xF0, 0xF0).color(r, g, b, a).endVertex();
|
||||
wr.pos(endvec.x + diff2.x, endvec.y + diff2.y, endvec.z + diff2.z).tex(0.5, 1).lightmap(0xF0, 0xF0).color(r, g, b, a).endVertex();
|
||||
wr.pos(roundend.x + diff2.x, roundend.y + diff2.y, roundend.z + diff2.z).tex(0, 1).lightmap(0xF0, 0xF0).color(r, g, b, a).endVertex();
|
||||
}
|
||||
|
||||
if(rendersegment.prev == null) {
|
||||
Vector3 roundend = rendersegment.startPoint.point.copy().subtract(rendersegment.diff.copy().normalize().multiply(width));
|
||||
|
||||
wr.pos(startvec.x - diff1.x, startvec.y - diff1.y, startvec.z - diff1.z).tex(0.5, 0).lightmap(0xF0, 0xF0).color(r, g, b, a).endVertex();
|
||||
wr.pos(roundend.x - diff1.x, roundend.y - diff1.y, roundend.z - diff1.z).tex(0, 0).lightmap(0xF0, 0xF0).color(r, g, b, a).endVertex();
|
||||
wr.pos(roundend.x + diff1.x, roundend.y + diff1.y, roundend.z + diff1.z).tex(0, 1).lightmap(0xF0, 0xF0).color(r, g, b, a).endVertex();
|
||||
wr.pos(startvec.x + diff1.x, startvec.y + diff1.y, startvec.z + diff1.z).tex(0.5, 1).lightmap(0xF0, 0xF0).color(r, g, b, a).endVertex();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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.copy().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.copy().perpendicular().normalize().rotate(rand.nextFloat() * 360, segment.diff);
|
||||
randoff.multiply((rand.nextFloat() - 0.5F) * amount * 2);
|
||||
|
||||
Vector3 basepoint = startpoint.copy().add(subsegment.copy().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.copy().xCrossProduct().rotate(rand.nextFloat() * 360, next.diff);
|
||||
Vector3 diff = next.diff.copy().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.copy().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) {
|
||||
RayTraceResult mop = worldObj.rayTraceBlocks(start.toVec3D(), end.toVec3D());
|
||||
|
||||
if(mop == null)
|
||||
return prevresistance;
|
||||
|
||||
if(mop.typeOfHit == RayTraceResult.Type.BLOCK) {
|
||||
Block block = worldObj.getBlockState(mop.getBlockPos()).getBlock();
|
||||
|
||||
if(worldObj.isAirBlock(mop.getBlockPos()))
|
||||
return prevresistance;
|
||||
|
||||
return prevresistance + block.getExplosionResistance(null) + 0.3F;
|
||||
} else return prevresistance;
|
||||
}
|
||||
|
||||
private void calculateCollisionAndDiffs() {
|
||||
TIntIntHashMap lastactivesegment = new TIntIntHashMap();
|
||||
|
||||
Collections.sort(segments, (o1, o2) -> {
|
||||
int comp = Integer.valueOf(o1.splitNo).compareTo(o2.splitNo);
|
||||
if(comp == 0)
|
||||
return Integer.valueOf(o1.segmentNo).compareTo(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.getMinecraft().getRenderViewEntity();
|
||||
return new Vector3((float) renderEntity.posX - pos.x, (float) renderEntity.posY + renderEntity.getEyeHeight() - pos.y, (float) renderEntity.posZ - pos.z);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package vazkii.botania.client.fx;
|
||||
|
||||
import vazkii.botania.common.core.helper.Vector3;
|
||||
|
||||
public class FXLightningBoltPoint {
|
||||
|
||||
public final Vector3 point;
|
||||
public final Vector3 basepoint;
|
||||
public final Vector3 offsetvec;
|
||||
|
||||
public FXLightningBoltPoint(Vector3 basepoint, Vector3 offsetvec) {
|
||||
point = basepoint.copy().add(offsetvec);
|
||||
this.basepoint = basepoint;
|
||||
this.offsetvec = offsetvec;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package vazkii.botania.client.fx;
|
||||
|
||||
import vazkii.botania.common.core.helper.Vector3;
|
||||
|
||||
public class FXLightningSegment {
|
||||
|
||||
public final FXLightningBoltPoint startPoint;
|
||||
public final FXLightningBoltPoint endPoint;
|
||||
|
||||
public Vector3 diff;
|
||||
|
||||
public FXLightningSegment prev;
|
||||
public FXLightningSegment next;
|
||||
|
||||
public Vector3 nextDiff;
|
||||
public Vector3 prevDiff;
|
||||
|
||||
public float sinPrev;
|
||||
public float sinNext;
|
||||
public final float light;
|
||||
|
||||
public final int segmentNo;
|
||||
public final int splitNo;
|
||||
|
||||
public FXLightningSegment(FXLightningBoltPoint start, FXLightningBoltPoint end, float light, int segmentnumber, int splitnumber) {
|
||||
startPoint = start;
|
||||
endPoint = end;
|
||||
this.light = light;
|
||||
segmentNo = segmentnumber;
|
||||
splitNo = splitnumber;
|
||||
|
||||
calcDiff();
|
||||
}
|
||||
|
||||
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.copy().subtract(startPoint.point);
|
||||
}
|
||||
|
||||
public void calcEndDiffs() {
|
||||
if(prev != null) {
|
||||
Vector3 prevdiffnorm = prev.diff.copy().normalize();
|
||||
Vector3 thisdiffnorm = diff.copy().normalize();
|
||||
|
||||
prevDiff = thisdiffnorm.copy().add(prevdiffnorm).normalize();
|
||||
sinPrev = (float) Math.sin(thisdiffnorm.angle(prevdiffnorm.multiply(-1)) / 2);
|
||||
} else {
|
||||
prevDiff = diff.copy().normalize();
|
||||
sinPrev = 1;
|
||||
}
|
||||
|
||||
if(next != null) {
|
||||
Vector3 nextdiffnorm = next.diff.copy().normalize();
|
||||
Vector3 thisdiffnorm = diff.copy().normalize();
|
||||
|
||||
nextDiff = thisdiffnorm.add(nextdiffnorm).normalize();
|
||||
sinNext = (float) Math.sin(thisdiffnorm.angle(nextdiffnorm.multiply(-1)) / 2);
|
||||
} else {
|
||||
nextDiff = diff.copy().normalize();
|
||||
sinNext = 1;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return startPoint.point.toString() + " " + endPoint.point.toString();
|
||||
}
|
||||
}
|
||||
@@ -92,7 +92,7 @@ public class PacketBotaniaEffect implements IMessage {
|
||||
BlockPos pos = new BlockPos(message.x, message.y, message.z);
|
||||
Vector3 itemVec = Vector3.fromBlockPos(pos).add(0.5, 0.5 + Math.random() * 0.3, 0.5);
|
||||
Vector3 tileVec = Vector3.fromBlockPos(pos).add(0.2 + Math.random() * 0.6, 0, 0.2 + Math.random() * 0.6);
|
||||
LightningHandler.spawnLightningBolt(Minecraft.getMinecraft().theWorld, outputting ? tileVec : itemVec,
|
||||
Botania.proxy.lightningFX(Minecraft.getMinecraft().theWorld, outputting ? tileVec : itemVec,
|
||||
outputting ? itemVec : tileVec, 80, Minecraft.getMinecraft().theWorld.rand.nextLong(), 0x4400799c, 0x4400C6FF);
|
||||
}
|
||||
break;
|
||||
|
||||
Reference in new issue
Block a user