The great particle refactoring part 2: Splitting and Streamlining!

- Splits all particle textures into individual files intead of sprite sheets
- Replaces arc and lightning pulse entities with particles
- Pulls particle code up to the general ParticleWizardry so behaviours such as spin can be applied to all particle types
- Renames a few ParticleBuilder methods for conciseness.
- Adds a few new particle effects to various spells and entities, with a slight tweak to EntityMagicArrow#onBlockHit to facilitate impact effects
This commit is contained in:
Electroblob77
2019-01-01 21:40:04 +00:00
parent ad363c64af
commit b374f71533
201 changed files with 1682 additions and 1551 deletions
@@ -13,25 +13,9 @@ import electroblob.wizardry.Wizardry;
import electroblob.wizardry.client.gui.GuiSpellDisplay;
import electroblob.wizardry.client.gui.GuiWizardHandbook;
import electroblob.wizardry.client.model.ModelWizardArmour;
import electroblob.wizardry.client.particle.ParticleBlizzard;
import electroblob.wizardry.client.particle.ParticleBuff;
import electroblob.wizardry.client.particle.ParticleDarkMagic;
import electroblob.wizardry.client.particle.ParticleDust;
import electroblob.wizardry.client.particle.ParticleFlash;
import electroblob.wizardry.client.particle.ParticleIce;
import electroblob.wizardry.client.particle.ParticleLeaf;
import electroblob.wizardry.client.particle.ParticleMagicBubble;
import electroblob.wizardry.client.particle.ParticleMagicFlame;
import electroblob.wizardry.client.particle.ParticlePath;
import electroblob.wizardry.client.particle.ParticleRotatingSparkle;
import electroblob.wizardry.client.particle.ParticleSnow;
import electroblob.wizardry.client.particle.ParticleSpark;
import electroblob.wizardry.client.particle.ParticleSparkle;
import electroblob.wizardry.client.particle.ParticleTornado;
import electroblob.wizardry.client.particle.ParticleWizardry;
import electroblob.wizardry.client.particle.*;
import electroblob.wizardry.client.particle.ParticleWizardry.IWizardryParticleFactory;
import electroblob.wizardry.client.renderer.LayerStone;
import electroblob.wizardry.client.renderer.RenderArc;
import electroblob.wizardry.client.renderer.RenderArcaneWorkbench;
import electroblob.wizardry.client.renderer.RenderBlackHole;
import electroblob.wizardry.client.renderer.RenderBlank;
@@ -45,7 +29,6 @@ import electroblob.wizardry.client.renderer.RenderHammer;
import electroblob.wizardry.client.renderer.RenderIceGiant;
import electroblob.wizardry.client.renderer.RenderIceSpike;
import electroblob.wizardry.client.renderer.RenderLightningDisc;
import electroblob.wizardry.client.renderer.RenderLightningPulse;
import electroblob.wizardry.client.renderer.RenderMagicArrow;
import electroblob.wizardry.client.renderer.RenderMagicLight;
import electroblob.wizardry.client.renderer.RenderPhoenix;
@@ -55,7 +38,6 @@ import electroblob.wizardry.client.renderer.RenderSpiritHorse;
import electroblob.wizardry.client.renderer.RenderSpiritWolf;
import electroblob.wizardry.client.renderer.RenderStatue;
import electroblob.wizardry.client.renderer.RenderWizard;
import electroblob.wizardry.entity.EntityArc;
import electroblob.wizardry.entity.EntityShield;
import electroblob.wizardry.entity.construct.EntityArrowRain;
import electroblob.wizardry.entity.construct.EntityBlackHole;
@@ -71,7 +53,6 @@ import electroblob.wizardry.entity.construct.EntityHailstorm;
import electroblob.wizardry.entity.construct.EntityHammer;
import electroblob.wizardry.entity.construct.EntityHealAura;
import electroblob.wizardry.entity.construct.EntityIceSpike;
import electroblob.wizardry.entity.construct.EntityLightningPulse;
import electroblob.wizardry.entity.construct.EntityLightningSigil;
import electroblob.wizardry.entity.construct.EntityTornado;
import electroblob.wizardry.entity.living.EntityDecoy;
@@ -291,27 +272,30 @@ public class ClientProxy extends CommonProxy {
public void initParticleFactories(){
factories = new HashMap<>();
factories.put(Type.BLIZZARD, (world, x, y, z) -> new ParticleBlizzard(world, x, y, z));
factories.put(Type.DARK_MAGIC, (world, x, y, z) -> new ParticleDarkMagic(world, x, y, z));
factories.put(Type.DUST, (world, x, y, z) -> new ParticleDust(world, x, y, z));
factories.put(Type.FLASH, (world, x, y, z) -> new ParticleFlash(world, x, y, z));
factories.put(Type.ICE, (world, x, y, z) -> new ParticleIce(world, x, y, z));
factories.put(Type.LEAF, (world, x, y, z) -> new ParticleLeaf(world, x, y, z));
factories.put(Type.MAGIC_BUBBLE, (world, x, y, z) -> new ParticleMagicBubble(world, x, y, z));
factories.put(Type.MAGIC_FIRE, (world, x, y, z) -> new ParticleMagicFlame(world, x, y, z));
factories.put(Type.PATH, (world, x, y, z) -> new ParticlePath(world, x, y, z));
factories.put(Type.SNOW, (world, x, y, z) -> new ParticleSnow(world, x, y, z));
factories.put(Type.SPARK, (world, x, y, z) -> new ParticleSpark(world, x, y, z));
factories.put(Type.SPARKLE, (world, x, y, z) -> new ParticleSparkle(world, x, y, z));
factories.put(Type.SPARKLE_ROTATING, (world, x, y, z) -> new ParticleRotatingSparkle(world, x, y, z));
factories.put(Type.BEAM, ParticleBeam::new);
factories.put(Type.BUFF, ParticleBuff::new);
factories.put(Type.DARK_MAGIC, ParticleDarkMagic::new);
factories.put(Type.DUST, ParticleDust::new);
factories.put(Type.FLASH, ParticleFlash::new);
factories.put(Type.ICE, ParticleIce::new);
factories.put(Type.LEAF, ParticleLeaf::new);
factories.put(Type.LIGHTNING, ParticleLightning::new);
factories.put(Type.LIGHTNING_PULSE, ParticleLightningPulse::new);
factories.put(Type.MAGIC_BUBBLE, ParticleMagicBubble::new);
factories.put(Type.MAGIC_FIRE, ParticleMagicFlame::new);
factories.put(Type.PATH, ParticlePath::new);
factories.put(Type.SCORCH, ParticleScorch::new);
factories.put(Type.SNOW, ParticleSnow::new);
factories.put(Type.SPARK, ParticleSpark::new);
factories.put(Type.SPARKLE, ParticleSparkle::new);
}
@Override
public ParticleWizardry createParticle(Type type, World world, double x, double y, double z){
IWizardryParticleFactory factory = factories.get(type);
if(factory == null){
Wizardry.logger.warn("Unrecognised particle type %s! Ensure the particle is properly registered.", type);
Wizardry.logger.warn("Unrecognised particle type %s ! Ensure the particle is properly registered.", type);
return null;
}
return factory.createParticle(world, x, y, z);
@@ -431,8 +415,8 @@ public class ClientProxy extends CommonProxy {
double x = caster.posX + radius * Math.cos(angle);
double y = caster.getEntityBoundingBox().minY + world.rand.nextDouble() * 2;
double z = caster.posZ + radius * Math.sin(angle);
ParticleBuilder.create(Type.SPARKLE).pos(x, y, z).vel(0, 0.02, 0).colour(0.6f, 1, 0.6f)
.lifetime(80 + world.rand.nextInt(10)).spawn(world);
ParticleBuilder.create(Type.SPARKLE).pos(x, y, z).vel(0, 0.02, 0).clr(0.6f, 1, 0.6f)
.time(80 + world.rand.nextInt(10)).spawn(world);
}
for(int i = 0; i < 20; i++){
double radius = 1;
@@ -578,7 +562,6 @@ public class ClientProxy extends CommonProxy {
manager -> new RenderProjectile(manager, 0.6f, new ResourceLocation(Wizardry.MODID, "textures/items/smoke_bomb.png"), false));
// Effects and constructs
RenderingRegistry.registerEntityRenderingHandler(EntityArc.class, RenderArc::new);
RenderingRegistry.registerEntityRenderingHandler(EntityBlackHole.class, RenderBlackHole::new);
RenderingRegistry.registerEntityRenderingHandler(EntityShield.class, RenderBlank::new);
RenderingRegistry.registerEntityRenderingHandler(EntityBubble.class, RenderBubble::new);
@@ -608,7 +591,6 @@ public class ClientProxy extends CommonProxy {
RenderingRegistry.registerEntityRenderingHandler(EntityFireRing.class,
manager -> new RenderFireRing(manager, new ResourceLocation(Wizardry.MODID, "textures/entity/ring_of_fire.png"), 5.0f));
RenderingRegistry.registerEntityRenderingHandler(EntityDecay.class, RenderDecay::new);
RenderingRegistry.registerEntityRenderingHandler(EntityLightningPulse.class, manager -> new RenderLightningPulse(manager, 8.0f));
// TESRs
ClientRegistry.bindTileEntitySpecialRenderer(TileEntityArcaneWorkbench.class, new RenderArcaneWorkbench());
@@ -0,0 +1,95 @@
package electroblob.wizardry.client.particle;
import org.lwjgl.opengl.GL11;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.world.World;
public class ParticleBeam extends ParticleTargeted {
/** Half the width of the outermost layer. */
private static final float THICKNESS = 0.1f;
public ParticleBeam(World world, double x, double y, double z){
super(world, x, y, z); // Does not have a texture!
this.setRBGColorF(1, 1, 1);
this.setMaxAge(1);
this.particleScale = 1;
}
@Override
public int getFXLayer(){
return 3;
}
@Override
protected void draw(Tessellator tessellator, double length, float partialTicks){
float scale = this.particleScale;
if(this.particleMaxAge > 1){
float ageFraction = (particleAge + partialTicks - 1)/particleMaxAge;
// Squaring this makes it look smoother than a linear shrinking effect
scale = this.particleScale * (1 - ageFraction*ageFraction);
}
GlStateManager.disableLighting();
GlStateManager.enableBlend();
GlStateManager.disableTexture2D();
GlStateManager.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE);
OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, 240f, 240f);
for(int layer=0; layer<3; layer++){
drawSegment(tessellator, layer, 0, 0, 0, 0, 0, length, THICKNESS * scale);
}
GlStateManager.enableTexture2D();
GlStateManager.enableLighting();
GlStateManager.disableBlend();
}
/** Draws the given layer of a segment of the arc, from the point (x1, y1, z1) to the point (x2, y2, z2), with the given thickness. */
private void drawSegment(Tessellator tessellator, int layer, double x1, double y1, double z1, double x2, double y2, double z2, float thickness){
BufferBuilder buffer = tessellator.getBuffer();
buffer.begin(GL11.GL_TRIANGLE_STRIP, DefaultVertexFormats.POSITION_COLOR);
switch(layer){
case 0:
drawShearedBox(buffer, x1, y1, z1, x2, y2, z2, 0.25f*thickness, 1, 1, 1, 1);
break;
case 1:
drawShearedBox(buffer, x1, y1, z1, x2, y2, z2, 0.6f*thickness, (particleRed + 1)/2, (particleGreen + 1)/2,
(particleBlue + 1)/2, 0.65f);
break;
case 2:
drawShearedBox(buffer, x1, y1, z1, x2, y2, z2, thickness, particleRed, particleGreen, particleBlue, 0.3f);
break;
}
tessellator.draw();
}
/** Draws a single box for one segment of the arc, from the point (x1, y1, z1) to the point (x2, y2, z2), with given width and colour. */
private void drawShearedBox(BufferBuilder buffer, double x1, double y1, double z1, double x2, double y2, double z2, float width, float r, float g, float b, float a){
buffer.pos(x1-width, y1-width, z1).color(r, g, b, a).endVertex();
buffer.pos(x2-width, y2-width, z2).color(r, g, b, a).endVertex();
buffer.pos(x1-width, y1+width, z1).color(r, g, b, a).endVertex();
buffer.pos(x2-width, y2+width, z2).color(r, g, b, a).endVertex();
buffer.pos(x1+width, y1+width, z1).color(r, g, b, a).endVertex();
buffer.pos(x2+width, y2+width, z2).color(r, g, b, a).endVertex();
buffer.pos(x1+width, y1-width, z1).color(r, g, b, a).endVertex();
buffer.pos(x2+width, y2-width, z2).color(r, g, b, a).endVertex();
buffer.pos(x1-width, y1-width, z1).color(r, g, b, a).endVertex();
buffer.pos(x2-width, y2-width, z2).color(r, g, b, a).endVertex();
}
}
@@ -1,67 +0,0 @@
package electroblob.wizardry.client.particle;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class ParticleBlizzard extends ParticleSnow {
private double angle;
private double radius;
private double speed;
public ParticleBlizzard(World world, double ox, double y, double oz){
super(world, ox, y, oz);
this.angle = this.rand.nextDouble() * Math.PI * 2;
double x = ox - Math.cos(angle) * radius;
double z = oz + radius * Math.sin(angle);
this.setPosition(x, y, z);
this.prevPosX = x;
this.prevPosY = y;
this.prevPosZ = z;
if(rand.nextBoolean()){
speed = rand.nextDouble() * 2 + 1;
}else{
speed = rand.nextDouble() * -2 - 1;
}
this.multipleParticleScaleBy(1.5f);
}
// @Override
// public void renderParticle(VertexBuffer buffer, Entity entity, float partialTicks, float rotationX, float
// rotationZ, float rotationYZ, float rotationXY, float rotationXZ){
// if(this.particleAge < this.particleMaxAge / 3 || (this.particleAge + this.particleMaxAge) / 3 % 2 == 0){
// super.renderParticle(buffer, entity, partialTicks, rotationX, rotationZ, rotationYZ, rotationXY, rotationXZ);
// }
// }
@Override
public void onUpdate(){
this.prevPosX = this.posX;
this.prevPosY = this.posY;
this.prevPosZ = this.posZ;
if(this.particleAge++ >= this.particleMaxAge){
this.setExpired();
}
// This is in radians per tick...
double omega = Math.signum(speed) * ((Math.PI * 2) / 20 - speed / (20 * radius));
// v = r times omega; therefore the normalised velocity vector needs to be r times the angle increment / 2 pi.
this.angle += omega;
this.motionY -= 0.04D * (double)this.particleGravity;
this.motionZ = radius * omega * Math.cos(angle);
this.motionX = radius * omega * Math.sin(angle);
this.move(motionX, motionY, motionZ);
if(this.particleAge > this.particleMaxAge / 2){
this.setAlphaF(
1.0F - ((float)this.particleAge - (float)(this.particleMaxAge / 2)) / (float)this.particleMaxAge);
}
}
}
@@ -21,36 +21,41 @@ import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class ParticleBuff extends ParticleEntityLinked {
public class ParticleBuff extends ParticleWizardry {
private static final ResourceLocation TEXTURE = new ResourceLocation(Wizardry.MODID, "textures/particle/buff.png");
private final boolean mirror;
public ParticleBuff(World world, Entity entity, float r, float g, float b, boolean mirror){
super(world, entity);
this.setRBGColorF(r, g, b);
this.mirror = mirror;
}
public ParticleBuff(World world, Entity entity, int maxAge, float r, float g, float b, boolean mirror){
super(world, entity, maxAge);
this.setRBGColorF(r, g, b);
this.mirror = mirror;
}
@Override
public void init(){
this.particleGravity = 0;
this.posY += 1;
public ParticleBuff(World world, double x, double y, double z){
super(world, x, y, z);
this.setVelocity(0, 0.27, 0); // Approximately what it was before
this.mirror = world.rand.nextBoolean();
this.setMaxAge(15);
this.setGravity(false);
this.canCollide = false;
}
@Override
public void onUpdate(){
super.onUpdate();
this.setPosition(this.entity.posX, this.entity.posY + 1 + 4f * this.particleAge/this.particleMaxAge, this.entity.posZ);
if(this.particleAge > this.particleMaxAge/2) this.particleAlpha = 2f - 2f*(float)this.particleAge/(float)this.particleMaxAge;
}
/* There are 4 layers of particles, specified as 0-3 by the method below. - Layer 0 causes the normal particles.png
* to be bound to the render engine for normal particles. - Layer 1 causes the block textures to be bound to the
* render engine for digging fx and falling fx. - Layer 2 causes the item textures to be bound to the render engine
* for tool breaking fx, snowballpoofs, slime particles, etc. - Layer 3 is not used in vanilla minecraft and was
* presumably added by forge for exactly this reason. This means no texture is bound by vanilla minecraft, meaning
* you are free to do as you wish without possibly overwriting vanilla particles. Mod particles won't be overwritten
* anyway since they bind their own textures. It is of course important to bind the texture every time you render a
* custom particle, but I don't see how you could do it any other way, since you don't have access to
* EffectRenderer. */
@Override
public int getFXLayer(){
// This can only be 0-3 or it will cause an ArrayIndexOutOfBoundsException in EffectRenderer.
return 3;
}
@Override
public void renderParticle(BufferBuilder buffer, Entity viewer, float partialTicks, float rotationX, float rotationZ,
float rotationYZ, float rotationXY, float rotationXZ){
@@ -1,130 +0,0 @@
package electroblob.wizardry.client.particle;
import org.lwjgl.opengl.GL11;
import net.minecraft.client.Minecraft;
import net.minecraft.client.particle.Particle;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.entity.Entity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
@Deprecated
public abstract class ParticleCustomTexture extends ParticleWizardry {
public ParticleCustomTexture(World world, double x, double y, double z){
super(world, x, y, z);
}
/**
* Returns a ResourceLocation for the particle's texture sheet. Do not create a new ResourceLocation in this method,
* only return a constant.
*/
public abstract ResourceLocation getTexture();
/** Returns how many 'frames' there are in the x direction on the texture. */
protected abstract int getXFrames();
/** Returns how many 'frames' there are in the y direction on the texture. */
protected abstract int getYFrames();
/* There are 4 layers of particles, specified as 0-3 by the method below. - Layer 0 causes the normal particles.png
* to be bound to the render engine for normal particles. - Layer 1 causes the block textures to be bound to the
* render engine for digging fx and falling fx. - Layer 2 causes the item textures to be bound to the render engine
* for tool breaking fx, snowballpoofs, slime particles, etc. - Layer 3 is not used in vanilla minecraft and was
* presumably added by forge for exactly this reason. This means no texture is bound by vanilla minecraft, meaning
* you are free to do as you wish without possibly overwriting vanilla particles. Mod particles won't be overwritten
* anyway since they bind their own textures. It is of course important to bind the texture every time you render a
* custom particle, but I don't see how you could do it any other way, since you don't have access to
* EffectRenderer. */
@Override
public int getFXLayer(){
// This can only be 0-3 or it will cause an ArrayIndexOutOfBoundsException in EffectRenderer.
return 3;
}
@Override
public void setParticleTextureIndex(int index){
this.particleTextureIndexX = index % getXFrames();
this.particleTextureIndexY = index / getYFrames();
}
// Overridden to bind the new texture. I think this can be done with TextureAtlasSprite, but this works as it is
// so I'm not changing it for the time being.
@Override
public void renderParticle(BufferBuilder buffer, Entity viewer, float partialTicks, float rotationX, float rotationZ,
float rotationYZ, float rotationXY, float rotationXZ){
GlStateManager.pushMatrix();
this.applyGLStateChanges();
// This stuff does the shading. It vanilla does this later on for each point, but this also seems to work.
int brightness = this.getBrightnessForRender(partialTicks);
int lightmapX = brightness % 65536;
int lightmapY = brightness / 65536;
OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, (float)lightmapX / 1.0F,
(float)lightmapY / 1.0F);
RenderHelper.disableStandardItemLighting();
Minecraft.getMinecraft().getTextureManager().bindTexture(getTexture());
buffer.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX_COLOR);
float u1 = (float)this.particleTextureIndexX / (float)getXFrames();
float u2 = u1 + 1.0f / getXFrames();
float v1 = (float)this.particleTextureIndexY / (float)getYFrames();
float v2 = v1 + 1.0f / getYFrames();
float scale = 0.1F * this.particleScale;
// I'm pretty sure these were always static.
Particle.interpPosX = viewer.lastTickPosX + (viewer.posX - viewer.lastTickPosX) * (double)partialTicks;
Particle.interpPosY = viewer.lastTickPosY + (viewer.posY - viewer.lastTickPosY) * (double)partialTicks;
Particle.interpPosZ = viewer.lastTickPosZ + (viewer.posZ - viewer.lastTickPosZ) * (double)partialTicks;
float x = (float)(this.prevPosX + (this.posX - this.prevPosX) * (double)partialTicks - interpPosX);
float y = (float)(this.prevPosY + (this.posY - this.prevPosY) * (double)partialTicks - interpPosY);
float z = (float)(this.prevPosZ + (this.posZ - this.prevPosZ) * (double)partialTicks - interpPosZ);
buffer.pos((double)(x - rotationX * scale - rotationXY * scale), (double)(y - rotationZ * scale),
(double)(z - rotationYZ * scale - rotationXZ * scale)).tex(u2, v2)
.color(particleRed, particleGreen, particleBlue, particleAlpha).endVertex();
buffer.pos((double)(x - rotationX * scale + rotationXY * scale), (double)(y + rotationZ * scale),
(double)(z - rotationYZ * scale + rotationXZ * scale)).tex(u2, v1)
.color(particleRed, particleGreen, particleBlue, particleAlpha).endVertex();
buffer.pos((double)(x + rotationX * scale + rotationXY * scale), (double)(y + rotationZ * scale),
(double)(z + rotationYZ * scale + rotationXZ * scale)).tex(u1, v1)
.color(particleRed, particleGreen, particleBlue, particleAlpha).endVertex();
buffer.pos((double)(x + rotationX * scale - rotationXY * scale), (double)(y - rotationZ * scale),
(double)(z + rotationYZ * scale - rotationXZ * scale)).tex(u1, v2)
.color(particleRed, particleGreen, particleBlue, particleAlpha).endVertex();
;
Tessellator.getInstance().draw();
this.undoGLStateChanges();
GlStateManager.popMatrix();
}
/**
* Override to add any GL state changes, like blending. Does nothing by default. <b>State changes should be done
* using GLStateManager, not using GL11 directly</b> (as is the case with all rendering code now).
*/
public void applyGLStateChanges(){
}
/**
* Override to undo any GL state changes, like blending. Does nothing by default. <b>State changes should be done
* using GLStateManager, not using GL11 directly</b> (as is the case with all rendering code now).
*/
public void undoGLStateChanges(){
}
}
@@ -1,61 +0,0 @@
package electroblob.wizardry.client.particle;
import net.minecraft.client.particle.Particle;
import net.minecraft.entity.Entity;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
/**
* Abstract superclass for particles that are linked to entities, i.e. move with a given entity. This is generally used
* for visual effects in spell casting.
*
* @author Electroblob
* @since Wizardry 4.2
*/
@SideOnly(Side.CLIENT)
public abstract class ParticleEntityLinked extends Particle {
/** True if the particle always renders at full brightness. Defaults to false. */
protected boolean fullBrightness = false;
/** The entity this particle is linked to. The particle will move with this entity. */
protected Entity entity;
public ParticleEntityLinked(World world, Entity entity){
super(world, entity.posX, entity.posY, entity.posZ, entity.motionX, entity.motionY, entity.motionZ);
this.entity = entity;
this.init();
}
public ParticleEntityLinked(World world, Entity entity, int maxAge){
this(world, entity);
this.particleMaxAge = maxAge;
}
/**
* Called from both constructors to set constants, avoiding duplicate code. Common fields to set here include:
* particleScale, particleGravity, canCollide, fullBrightness and setting the texture index.
*/
public abstract void init();
/* There are 4 layers of particles, specified as 0-3 by the method below. - Layer 0 causes the normal particles.png
* to be bound to the render engine for normal particles. - Layer 1 causes the block textures to be bound to the
* render engine for digging fx and falling fx. - Layer 2 causes the item textures to be bound to the render engine
* for tool breaking fx, snowballpoofs, slime particles, etc. - Layer 3 is not used in vanilla minecraft and was
* presumably added by forge for exactly this reason. This means no texture is bound by vanilla minecraft, meaning
* you are free to do as you wish without possibly overwriting vanilla particles. Mod particles won't be overwritten
* anyway since they bind their own textures. It is of course important to bind the texture every time you render a
* custom particle, but I don't see how you could do it any other way, since you don't have access to
* EffectRenderer. */
@Override
public int getFXLayer(){
// This can only be 0-3 or it will cause an ArrayIndexOutOfBoundsException in EffectRenderer.
return 3;
}
@Override
public int getBrightnessForRender(float partialTick){
return fullBrightness ? 15728880 : super.getBrightnessForRender(partialTick);
}
}
@@ -17,13 +17,13 @@ public class ParticleFlash extends ParticleWizardry {
super(world, x, y, z);
this.setRBGColorF(1, 1, 1);
this.particleScale = 0.6f; // 7.1f is the value used in fireworks
this.particleMaxAge = 4;
this.particleMaxAge = 6;
}
@Override
public void renderParticle(BufferBuilder buffer, Entity entityIn, float partialTicks, float rotationX, float rotationZ, float rotationYZ, float rotationXY, float rotationXZ){
float f4 = particleScale * MathHelper.sin(((float)this.particleAge + partialTicks - 1.0F) * 0.25F * (float)Math.PI);
this.setAlphaF(0.6F - ((float)this.particleAge + partialTicks - 1.0F) * 0.25F * 0.5F);
float f4 = particleScale * MathHelper.sin(((float)this.particleAge + partialTicks - 1.0F)/particleMaxAge * (float)Math.PI);
this.setAlphaF(0.6F - ((float)this.particleAge + partialTicks - 1.0F)/particleMaxAge * 0.5F);
float f5 = (float)(this.prevPosX + (this.posX - this.prevPosX) * (double)partialTicks - interpPosX);
float f6 = (float)(this.prevPosY + (this.posY - this.prevPosY) * (double)partialTicks - interpPosY);
float f7 = (float)(this.prevPosZ + (this.posZ - this.prevPosZ) * (double)partialTicks - interpPosZ);
@@ -1,40 +1,36 @@
package electroblob.wizardry.client.particle;
import electroblob.wizardry.Wizardry;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.client.event.TextureStitchEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class ParticleIce extends ParticleCustomTexture {
private static final ResourceLocation TEXTURE = new ResourceLocation(Wizardry.MODID, "textures/particle/ice_particles.png");
@Mod.EventBusSubscriber(Side.CLIENT)
public class ParticleIce extends ParticleWizardry {
private static final ResourceLocation[] TEXTURES = generateTextures("ice", 8);
public ParticleIce(World world, double x, double y, double z){
super(world, x, y, z);
this.setParticleTextureIndex(rand.nextInt(8));
super(world, x, y, z, TEXTURES[world.rand.nextInt(TEXTURES.length)]);
this.canCollide = true;
// Defaults
this.setRBGColorF(1, 1, 1);
this.particleScale *= 0.75f;
this.setGravity(true);
this.shaded = false;
}
@Override
public ResourceLocation getTexture(){
return TEXTURE;
}
@Override
protected int getXFrames(){
return 4;
}
@Override
protected int getYFrames(){
return 4;
@SubscribeEvent
public static void onTextureStitchEvent(TextureStitchEvent.Pre event){
for(ResourceLocation texture : TEXTURES){
event.getMap().registerSprite(texture);
}
}
}
@@ -1,41 +1,47 @@
package electroblob.wizardry.client.particle;
import electroblob.wizardry.Wizardry;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.client.event.TextureStitchEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class ParticleLeaf extends ParticleCustomTexture {
@Mod.EventBusSubscriber(Side.CLIENT)
public class ParticleLeaf extends ParticleWizardry {
private static final ResourceLocation TEXTURE = new ResourceLocation(Wizardry.MODID,
"textures/particle/leaf_particles.png");
private static final ResourceLocation[] TEXTURES = generateTextures("leaf", 16);
public ParticleLeaf(World world, double x, double y, double z){
super(world, x, y, z);
super(world, x, y, z, TEXTURES[world.rand.nextInt(TEXTURES.length)]);
this.setVelocity(0, -0.03, 0);
this.setLifetime(10 + rand.nextInt(5));
this.setParticleTextureIndex(rand.nextInt(16));
this.setMaxAge(10 + rand.nextInt(5));
this.particleScale *= 1.4f;
this.particleGravity = 0;
this.canCollide = true;
// Produces a variety of browns and greens
this.setRBGColorF(0.1f + 0.3f * world.rand.nextFloat(), 0.5f + 0.3f * world.rand.nextFloat(), 0.1f);
}
@Override
public ResourceLocation getTexture(){
return TEXTURE;
public void onUpdate(){
super.onUpdate();
// Fading
if(this.particleAge > this.particleMaxAge / 2){
this.setAlphaF(1 - ((float)this.particleAge - (float)(this.particleMaxAge / 2)) / (float)this.particleMaxAge);
}
}
@Override
protected int getXFrames(){
return 4;
}
@Override
protected int getYFrames(){
return 4;
@SubscribeEvent
public static void onTextureStitchEvent(TextureStitchEvent.Pre event){
for(ResourceLocation texture : TEXTURES){
event.getMap().registerSprite(texture);
}
}
}
@@ -0,0 +1,167 @@
package electroblob.wizardry.client.particle;
import java.util.Random;
import org.lwjgl.opengl.GL11;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.world.World;
public class ParticleLightning extends ParticleTargeted {
/** Half the width of the outermost layer. */
private static final float THICKNESS = 0.04f;
/** Maximum length of a segment. */
private static final double MAX_SEGMENT_LENGTH = 0.6;
/** Minimum length of a segment. */
private static final double MIN_SEGMENT_LENGTH = 0.2;
/** Maximum deviation (in x or y, as drawn before transformations) from the centreline. */
private static final double VERTEX_JITTER = 0.15;
/** Maximum number of segments a fork can have before ending. */
private static final int MAX_FORK_SEGMENTS = 3;
/** Probability (as a fraction) that a vertex will have a fork. */
private static final float FORK_CHANCE = 0.3f;
/** Number of ticks to wait before the arc changes shape again. */
private static final int UPDATE_PERIOD = 1;
/** A random long value used by the renderer as a seed to generate its vertices from, ensuring they remain the same
* across multiple frames. Not synced. */
public final long seed;
public ParticleLightning(World world, double x, double y, double z){
super(world, x, y, z); // Does not have a texture!
seed = this.rand.nextLong();
this.setRBGColorF(0.2f, 0.6f, 1); // Default blue colour
this.setMaxAge(3);
this.particleScale = 1;
}
@Override
public int getFXLayer(){
return 3;
}
@Override
protected void draw(Tessellator tessellator, double length, float partialTicks){
GlStateManager.disableLighting();
GlStateManager.enableBlend();
GlStateManager.disableTexture2D();
GlStateManager.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE);
OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, 240f, 240f);
// The direction of the arc drawn by the tessellator is always along the z axis and is rotated to the
// correct orientation, that way there isn't a ton of trigonometry and the code is way neater.
boolean freeEnd = this.target == null;
int numberOfSegments = (int)Math.round(length/MAX_SEGMENT_LENGTH); // Number of segments
for(int layer=0; layer<3; layer++){
double px=0, py=0, pz=0;
// Creates a random from the arc's seed field + the number of ticks it has existed/the update period.
// By using a seed, we can ensure the vertex positions and forks are identical a) for each layer, even
// though they are rendered sequentially, and b) across many frames (and ticks, if updateTime > 1).
Random random = new Random(this.seed + this.particleAge/UPDATE_PERIOD);
// numberOfSegments-1 because the last segment is handled separately.
for(int i=0; i<numberOfSegments-1; i++){
double px2 = (random.nextDouble()*2-1)*VERTEX_JITTER*particleScale;
double py2 = (random.nextDouble()*2-1)*VERTEX_JITTER*particleScale;
double pz2 = pz + length/(double)numberOfSegments; // For now they are all the same length
drawSegment(tessellator, layer, px, py, pz, px2, py2, pz2, THICKNESS*particleScale);
// Forks
if(random.nextFloat() < FORK_CHANCE){
double px3=px, py3=py, pz3=pz;
for(int j=0; j<random.nextInt(MAX_FORK_SEGMENTS-1)+1; j++){
// Forks set their centreline to the x/y coordinates of the vertex they originate from
double px4 = px3 + (random.nextDouble()*2-1)*VERTEX_JITTER*particleScale;
double py4 = py3 + (random.nextDouble()*2-1)*VERTEX_JITTER*particleScale;
double pz4 = pz3 + MIN_SEGMENT_LENGTH + random.nextDouble()*(MAX_SEGMENT_LENGTH - MIN_SEGMENT_LENGTH);
drawSegment(tessellator, layer, px3, py3, pz3, px4, py4, pz4, THICKNESS*0.8f*particleScale);
// Forks of forks
if(random.nextFloat() < FORK_CHANCE){
double px5 = px3 + (random.nextDouble()*2-1)*VERTEX_JITTER*particleScale;
double py5 = py3 + (random.nextDouble()*2-1)*VERTEX_JITTER*particleScale;
double pz5 = pz3 + MIN_SEGMENT_LENGTH + random.nextDouble()*(MAX_SEGMENT_LENGTH - MIN_SEGMENT_LENGTH);
drawSegment(tessellator, layer, px3, py3, pz3, px5, py5, pz5, THICKNESS*0.6f*particleScale);
}
px3 = px4;
py3 = py4;
pz3 = pz4;
}
}
px = px2;
py = py2;
pz = pz2;
}
// Last segment has a specific end position and cannot fork.
double px2 = freeEnd ? (random.nextDouble()*2-1)*VERTEX_JITTER*particleScale : 0;
double py2 = freeEnd ? (random.nextDouble()*2-1)*VERTEX_JITTER*particleScale : 0;
drawSegment(tessellator, layer, px, py, pz, px2, py2, length, THICKNESS*particleScale);
}
GlStateManager.enableTexture2D();
GlStateManager.enableLighting();
GlStateManager.disableBlend();
}
/** Draws the given layer of a segment of the arc, from the point (x1, y1, z1) to the point (x2, y2, z2), with the given thickness. */
private void drawSegment(Tessellator tessellator, int layer, double x1, double y1, double z1, double x2, double y2, double z2, float thickness){
BufferBuilder buffer = tessellator.getBuffer();
buffer.begin(GL11.GL_TRIANGLE_STRIP, DefaultVertexFormats.POSITION_COLOR);
switch(layer){
case 0:
drawShearedBox(buffer, x1, y1, z1, x2, y2, z2, 0.25f*thickness, 1, 1, 1, 1);
break;
case 1:
drawShearedBox(buffer, x1, y1, z1, x2, y2, z2, 0.6f*thickness, (particleRed + 1)/2, (particleGreen + 1)/2,
(particleBlue + 1)/2, 0.65f);
break;
case 2:
drawShearedBox(buffer, x1, y1, z1, x2, y2, z2, thickness, particleRed, particleGreen, particleBlue, 0.3f);
break;
}
tessellator.draw();
}
/** Draws a single box for one segment of the arc, from the point (x1, y1, z1) to the point (x2, y2, z2), with given width and colour. */
private void drawShearedBox(BufferBuilder buffer, double x1, double y1, double z1, double x2, double y2, double z2, float width, float r, float g, float b, float a){
buffer.pos(x1-width, y1-width, z1).color(r, g, b, a).endVertex();
buffer.pos(x2-width, y2-width, z2).color(r, g, b, a).endVertex();
buffer.pos(x1-width, y1+width, z1).color(r, g, b, a).endVertex();
buffer.pos(x2-width, y2+width, z2).color(r, g, b, a).endVertex();
buffer.pos(x1+width, y1+width, z1).color(r, g, b, a).endVertex();
buffer.pos(x2+width, y2+width, z2).color(r, g, b, a).endVertex();
buffer.pos(x1+width, y1-width, z1).color(r, g, b, a).endVertex();
buffer.pos(x2+width, y2-width, z2).color(r, g, b, a).endVertex();
buffer.pos(x1-width, y1-width, z1).color(r, g, b, a).endVertex();
buffer.pos(x2-width, y2-width, z2).color(r, g, b, a).endVertex();
}
}
@@ -0,0 +1,38 @@
package electroblob.wizardry.client.particle;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.client.event.TextureStitchEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
@Mod.EventBusSubscriber(Side.CLIENT)
public class ParticleLightningPulse extends ParticleWizardry {
private static final ResourceLocation[] TEXTURES = generateTextures("lightning_pulse", 8);
public ParticleLightningPulse(World world, double x, double y, double z){
super(world, x, y, z, TEXTURES);
this.particleScale = 32f;
this.setRBGColorF(1, 1, 1);
this.shaded = false;
this.canCollide = false;
this.setMaxAge(7); // Lifetime defaults to 7 (and is very unlikely to be changed)
// Faces up by default
this.pitch = 90;
this.yaw = 0;
}
@SubscribeEvent
public static void onTextureStitchEvent(TextureStitchEvent.Pre event){
for(ResourceLocation texture : TEXTURES){
event.getMap().registerSprite(texture);
}
}
}
@@ -9,9 +9,7 @@ public class ParticleMagicBubble extends ParticleWizardry {
public ParticleMagicBubble(World world, double x, double y, double z){
super(world, x, y, z);
this.particleRed = 1.0F;
this.particleGreen = 1.0F;
this.particleBlue = 1.0F;
this.setRBGColorF(1, 1, 1);
this.setParticleTextureIndex(32);
this.setSize(0.02F, 0.02F);
this.particleScale *= this.rand.nextFloat() * 0.6F + 0.2F;
@@ -15,14 +15,11 @@ public class ParticleMagicFlame extends ParticleWizardry {
public ParticleMagicFlame(World world, double x, double y, double z){
super(world, x, y, z);
// Not sure what these did but they sure as heck won't do anything now...
// TESTME: If it looks the same as before, delete these.
// this.motionX = this.motionX * 0.009999999776482582D + par8;
// this.motionY = this.motionY * 0.009999999776482582D + par10;
// this.motionZ = this.motionZ * 0.009999999776482582D + par12;
this.flameScale = 1 + world.rand.nextFloat();
this.setRBGColorF(1, 1, 1);
this.particleAlpha = 1;
this.particleMaxAge = (int)(2.0D / (Math.random() * 0.8D + 0.2D));
this.shaded = false;
// IDEA: Make the particles for ray spells collide properly and not spawn on the other side of stuff.
this.canCollide = false;
this.setParticleTextureIndex(48);
@@ -1,32 +1,31 @@
package electroblob.wizardry.client.particle;
import org.lwjgl.opengl.GL11;
import electroblob.wizardry.Wizardry;
import electroblob.wizardry.spell.Clairvoyance;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.client.event.TextureStitchEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class ParticlePath extends ParticleCustomTexture {
@Mod.EventBusSubscriber(Side.CLIENT)
public class ParticlePath extends ParticleWizardry {
private static final ResourceLocation TEXTURE = new ResourceLocation(Wizardry.MODID,
"textures/particle/path_particles.png");
private static final ResourceLocation TEXTURE = new ResourceLocation(Wizardry.MODID, "particle/path");
private final double originX, originY, originZ;
public ParticlePath(World world, double x, double y, double z){
super(world, x, y, z);
super(world, x, y, z, TEXTURE); // This particle only has 1 texture
this.originX = x;
this.originY = y;
this.originZ = z;
this.setParticleTextureIndex(0);
// Set to a constant to remove the randomness from Particle.
this.particleScale = 1.25f;
this.particleGravity = 0;
@@ -35,23 +34,10 @@ public class ParticlePath extends ParticleCustomTexture {
this.setRBGColorF(1, 1, 1);
}
@Override
public ResourceLocation getTexture(){
return TEXTURE;
}
@Override
protected int getXFrames(){
return 1;
}
@Override
protected int getYFrames(){
return 1;
}
@Override
public void onUpdate(){
super.onUpdate();
this.prevPosX = this.posX;
this.prevPosY = this.posY;
@@ -77,19 +63,10 @@ public class ParticlePath extends ParticleCustomTexture {
}
}
@Override
public void applyGLStateChanges(){
GlStateManager.enableBlend();
GlStateManager.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
// TESTME: Are these two actually necessary?
GlStateManager.disableLighting();
OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, 240, 240);
}
@Override
public void undoGLStateChanges(){
GlStateManager.disableBlend();
GlStateManager.enableLighting();
@SubscribeEvent
public static void onTextureStitchEvent(TextureStitchEvent.Pre event){
event.getMap().registerSprite(TEXTURE);
}
}
@@ -1,60 +0,0 @@
package electroblob.wizardry.client.particle;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class ParticleRotatingSparkle extends ParticleSparkle {
private double angle;
private double radius;
private double speed;
public ParticleRotatingSparkle(World world, double ox, double y, double oz){
super(world, ox, y, oz);
this.angle = this.rand.nextDouble() * Math.PI * 2;
double x = ox - Math.cos(angle) * radius;
double z = oz + radius * Math.sin(angle);
this.radius = 1; // TODO: Find a suitable default value
this.setPosition(x, y, z);
this.prevPosX = x;
this.prevPosY = y;
this.prevPosZ = z;
if(rand.nextBoolean()){
speed = rand.nextDouble() * 2 + 1;
}else{
speed = rand.nextDouble() * -2 - 1;
}
this.multipleParticleScaleBy(1.5f);
}
@Override
public void onUpdate(){
this.prevPosX = this.posX;
this.prevPosY = this.posY;
this.prevPosZ = this.posZ;
if(this.particleAge++ >= this.particleMaxAge){
this.setExpired();
}
// This is in radians per tick...
double omega = Math.signum(speed) * ((Math.PI * 2) / 20 - speed / (20 * radius));
// v = r times omega; therefore the normalised velocity vector needs to be r times the angle increment / 2 pi.
this.angle += omega;
this.motionY -= 0.04D * (double)this.particleGravity;
this.motionZ = radius * omega * Math.cos(angle);
this.motionX = radius * omega * Math.sin(angle);
this.move(motionX, motionY, motionZ);
if(this.particleAge > this.particleMaxAge / 2){
this.setAlphaF(
1.0F - ((float)this.particleAge - (float)(this.particleMaxAge / 2)) / (float)this.particleMaxAge);
}
}
}
@@ -0,0 +1,70 @@
package electroblob.wizardry.client.particle;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.client.event.TextureStitchEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
@Mod.EventBusSubscriber(Side.CLIENT)
public class ParticleScorch extends ParticleWizardry {
private static final ResourceLocation[] TEXTURES = generateTextures("scorch", 8);
public ParticleScorch(World world, double x, double y, double z){
super(world, x, y, z, TEXTURES[world.rand.nextInt(TEXTURES.length)]);
this.particleGravity = 0;
this.setMaxAge(100 + rand.nextInt(40));
this.particleScale *= 2;
// Defaults to black (which looks like a 'normal' scorch mark)
this.setRBGColorF(0, 0, 0);
this.shaded = false;
}
@Override
public void setRBGColorF(float r, float g, float b){
super.setRBGColorF(r, g, b);
this.setFadeColour(0, 0, 0); // Scorch particles fade to black by default
}
@Override
public void onUpdate(){
super.onUpdate();
// Colour fading (scorch particles do this slightly differently)
float ageFraction = Math.min((float)this.particleAge / ((float)this.particleMaxAge * 0.5f), 1);
// No longer uses setRBGColorF because that method now also sets the initial values
this.particleRed = this.initialRed + (this.fadeRed - this.initialRed) * ageFraction;
this.particleGreen = this.initialGreen + (this.fadeGreen - this.initialGreen) * ageFraction;
this.particleBlue = this.initialBlue + (this.fadeBlue - this.initialBlue) * ageFraction;
// Fading
if(this.particleAge > this.particleMaxAge/2){
this.setAlphaF(1 - ((float)this.particleAge - (float)(this.particleMaxAge/2)) / (float)this.particleMaxAge);
}
EnumFacing facing = EnumFacing.fromAngle(yaw);
if(pitch == 90) facing = EnumFacing.UP;
if(pitch == -90) facing = EnumFacing.DOWN;
// Disappears if there is no block behind it (this is the same check used to spawn it)
if(!world.getBlockState(new BlockPos(posX, posY, posZ).offset(facing.getOpposite())).getMaterial().isSolid()){
this.setExpired();
}
}
@SubscribeEvent
public static void onTextureStitchEvent(TextureStitchEvent.Pre event){
for(ResourceLocation texture : TEXTURES){
event.getMap().registerSprite(texture);
}
}
}
@@ -1,39 +1,36 @@
package electroblob.wizardry.client.particle;
import electroblob.wizardry.Wizardry;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.client.event.TextureStitchEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class ParticleSnow extends ParticleCustomTexture {
@Mod.EventBusSubscriber(Side.CLIENT)
public class ParticleSnow extends ParticleWizardry {
private static final ResourceLocation TEXTURE = new ResourceLocation(Wizardry.MODID,
"textures/particle/snow_particles.png");
private static final ResourceLocation[] TEXTURES = generateTextures("snow", 4);
public ParticleSnow(World world, double x, double y, double z){
super(world, x, y, z);
this.setParticleTextureIndex(rand.nextInt(8));
super(world, x, y, z, TEXTURES[world.rand.nextInt(TEXTURES.length)]);
this.setVelocity(0, -0.02, 0);
this.particleScale *= 0.6f;
this.particleGravity = 0;
this.canCollide = true;
this.setLifetime(40 + rand.nextInt(10));
this.setMaxAge(40 + rand.nextInt(10));
// Produces a variety of light blues and whites
this.setRBGColorF(0.9f + 0.1f * world.rand.nextFloat(), 0.95f + 0.05f * world.rand.nextFloat(), 1);
}
@Override
public ResourceLocation getTexture(){
return TEXTURE;
}
@Override
protected int getXFrames(){
return 4;
}
@Override
protected int getYFrames(){
return 4;
@SubscribeEvent
public static void onTextureStitchEvent(TextureStitchEvent.Pre event){
for(ResourceLocation texture : TEXTURES){
event.getMap().registerSprite(texture);
}
}
}
@@ -1,66 +1,55 @@
package electroblob.wizardry.client.particle;
import org.lwjgl.opengl.GL11;
import electroblob.wizardry.Wizardry;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.client.event.TextureStitchEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class ParticleSpark extends ParticleCustomTexture {
@Mod.EventBusSubscriber(Side.CLIENT)
public class ParticleSpark extends ParticleWizardry {
private static final ResourceLocation TEXTURE = new ResourceLocation(Wizardry.MODID,
"textures/particle/lightning_particles.png");
// 8 different animation strips, 4 in each strip
private static final ResourceLocation[][] TEXTURES = generateTextures("lightning", 8, 4);
public ParticleSpark(World world, double x, double y, double z){
super(world, x, y, z);
// Multiplied by 4 because the index works slightly differently for spark particles.
this.setParticleTextureIndex(rand.nextInt(8) * 4);
super(world, x, y, z, TEXTURES[world.rand.nextInt(TEXTURES.length)]);
this.particleScale *= 1.4f;
this.setRBGColorF(1, 1, 1);
this.shaded = false;
this.canCollide = false;
this.setLifetime(3); // Lifetime defaults to 3 (and is very unlikely to be changed)
this.setMaxAge(3); // Lifetime defaults to 3 (and is very unlikely to be changed)
}
// May no longer be necessary, ParticleManager seems to enable blending now
@Override
public void onUpdate(){
super.onUpdate();
// Well this is handy! Looks like vanilla uses the texture index like this too.
this.nextTextureIndexX();
}
@Override
public ResourceLocation getTexture(){
return TEXTURE;
}
@Override
protected int getXFrames(){
return 4;
}
@Override
protected int getYFrames(){
return 8;
}
@Override
public void applyGLStateChanges(){
GlStateManager.enableBlend();
GlStateManager.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
// TESTME: Are these two actually necessary?
GlStateManager.disableLighting();
OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, 240, 240);
}
@Override
public void undoGLStateChanges(){
GlStateManager.disableBlend();
GlStateManager.enableLighting();
// @Override
// public void applyGLStateChanges(){
// GlStateManager.enableBlend();
// GlStateManager.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
// // TESTME: Are these two actually necessary?
// GlStateManager.disableLighting();
// OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, 240, 240);
// }
//
// @Override
// public void undoGLStateChanges(){
// GlStateManager.disableBlend();
// GlStateManager.enableLighting();
// }
@SubscribeEvent
public static void onTextureStitchEvent(TextureStitchEvent.Pre event){
for(ResourceLocation[] array : TEXTURES){
for(ResourceLocation texture : array){
event.getMap().registerSprite(texture);
}
}
}
}
@@ -1,24 +1,23 @@
package electroblob.wizardry.client.particle;
import electroblob.wizardry.Wizardry;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.client.event.TextureStitchEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class ParticleSparkle extends ParticleCustomTexture {
@Mod.EventBusSubscriber(Side.CLIENT)
public class ParticleSparkle extends ParticleWizardry {
private static final ResourceLocation TEXTURE = new ResourceLocation(Wizardry.MODID,
"textures/particle/sparkle_particles.png");
// Implementation of colour fading (perhaps these belong in ParticleWizardry?)
private float initialRed;
private float initialGreen;
private float initialBlue;
private static final ResourceLocation[] TEXTURES = generateTextures("sparkle", 11);
public ParticleSparkle(World world, double x, double y, double z){
super(world, x, y, z);
super(world, x, y, z, TEXTURES); // This time the textures are all one long animation
this.setRBGColorF(1, 1, 1);
this.particleMaxAge = 48 + this.rand.nextInt(12);
this.particleScale *= 0.75f;
@@ -27,30 +26,6 @@ public class ParticleSparkle extends ParticleCustomTexture {
this.shaded = false;
}
// Overridden to set the initial colour values
@Override
public void setRBGColorF(float r, float g, float b){
super.setRBGColorF(r, g, b);
initialRed = r;
initialGreen = g;
initialBlue = b;
}
@Override
public ResourceLocation getTexture(){
return TEXTURE;
}
@Override
protected int getXFrames(){
return 4;
}
@Override
protected int getYFrames(){
return 4;
}
@Override
public void onUpdate(){
@@ -58,24 +33,14 @@ public class ParticleSparkle extends ParticleCustomTexture {
// Fading
if(this.particleAge > this.particleMaxAge / 2){
this.setAlphaF(
1.0F - ((float)this.particleAge - (float)(this.particleMaxAge / 2)) / (float)this.particleMaxAge);
this.setAlphaF(1 - ((float)this.particleAge - (float)(this.particleMaxAge / 2)) / (float)this.particleMaxAge);
}
}
@SubscribeEvent
public static void onTextureStitchEvent(TextureStitchEvent.Pre event){
for(ResourceLocation texture : TEXTURES){
event.getMap().registerSprite(texture);
}
// Colour fading
float ageFraction = (float)this.particleAge / (float)this.particleMaxAge;
// No longer uses setRBGColorF because that method now also sets the initial values
this.particleRed = this.initialRed + (this.fadeRed - this.initialRed)*ageFraction;
this.particleGreen = this.initialGreen + (this.fadeGreen - this.initialGreen)*ageFraction;
this.particleBlue = this.initialBlue + (this.fadeBlue - this.initialBlue)*ageFraction;
this.setParticleTextureIndex((this.particleAge * 11)/this.particleMaxAge);
}
/* As a side note, I see a lot of magic mods with fancy-looking particle effects that really seem to 'glow'. It's
* actually not that hard - you simply create a reasonably high-res texture with translucency and then set the
* OpenGL blend function to something like SRC_ALPHA, SRC_ALPHA or ONE, ONE. The thing is... they're not very
* Minecraft-y. I still maintain that part of wizardry's appeal is that it stays true to the game's pixelated charm,
* rather than trying to make it something it's not. Still, the newer textures are much better than the defaults I
* used to use. */
}
@@ -0,0 +1,105 @@
package electroblob.wizardry.client.particle;
import javax.annotation.Nullable;
import org.lwjgl.opengl.GL11;
import electroblob.wizardry.Wizardry;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.entity.Entity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
/** Superclass for particles with a second target entity or target position. */
public abstract class ParticleTargeted extends ParticleWizardry {
protected double targetX;
protected double targetY;
protected double targetZ;
/** The target this particle is linked to. The particle will stretch to touch this entity. */
@Nullable
protected Entity target = null;
public ParticleTargeted(World world, double x, double y, double z, ResourceLocation... textures){
super(world, x, y, z, textures);
}
@Override
public void setTargetPosition(double x, double y, double z){
this.targetX = x;
this.targetY = y;
this.targetZ = z;
}
@Override
public void setTargetEntity(Entity target){
this.target = target;
}
@Override
public void renderParticle(BufferBuilder buffer, Entity viewer, float partialTicks, float rotationX, float rotationZ, float rotationYZ,
float rotationXY, float rotationXZ){
if(this.target != null){
this.targetX = this.target.posX;
this.targetY = this.target.getEntityBoundingBox().minY + target.height/2;
this.targetZ = this.target.posZ;
}
if(Double.isNaN(targetX) || Double.isNaN(targetY) || Double.isNaN(targetZ)){
Wizardry.logger.warn("Attempted to render a targeted particle, but neither its target entity nor target"
+ "position was set!");
return;
}
// I'm pretty sure these were always static.
interpPosX = viewer.lastTickPosX + (viewer.posX - viewer.lastTickPosX) * (double)partialTicks;
interpPosY = viewer.lastTickPosY + (viewer.posY - viewer.lastTickPosY) * (double)partialTicks;
interpPosZ = viewer.lastTickPosZ + (viewer.posZ - viewer.lastTickPosZ) * (double)partialTicks;
float x = (float)(this.prevPosX + (this.posX - this.prevPosX) * (double)partialTicks - interpPosX);
float y = (float)(this.prevPosY + (this.posY - this.prevPosY) * (double)partialTicks - interpPosY);
float z = (float)(this.prevPosZ + (this.posZ - this.prevPosZ) * (double)partialTicks - interpPosZ);
GlStateManager.pushMatrix();
GlStateManager.translate(x, y, z);
double dx = this.targetX - this.posX;
double dy = this.targetY - this.posY;
double dz = this.targetZ - this.posZ;
// The distance from origin to endpoint
double length = Math.sqrt(dx*dx+dy*dy+dz*dz);
// Math.atan2 computes within -180 to +180, rather than -90 to +90.
float yaw = (float)(180d/Math.PI * Math.atan2(dx, dz));
float pitch = (float)(180f/(float)Math.PI * Math.atan(-dy/Math.sqrt(dz*dz+dx*dx)));
GL11.glRotatef(yaw, 0, 1, 0);
GL11.glRotatef(pitch, 1, 0, 0);
Tessellator tessellator = Tessellator.getInstance();
this.draw(tessellator, length, partialTicks);
GlStateManager.popMatrix();
}
/** Called from {@link ParticleTargeted#renderParticle(BufferBuilder, Entity, float, float, float, float, float, float)},
* once the appropriate calculations and transformations have been applied, to actually render the particle. Subclasses
* override this <i>instead</i> of overriding {@code renderParticle} directly, and inside render the particle <b>along
* the z-axis, starting at (0, 0, 0)</b> - it will be translated and rotated automatically.
* <p>
* <i>N.B. Other than transformations, no GL state changes are applied; these should be done within this method.</i>
*
* @param tessellator A reference to the tessellator, for convenience.
* @param length The distance from the origin to the endpoint for the particle being rendered; the particle should
* therefore be rendered between (0, 0, 0) and (0, 0, length) within this method.
* @param partialTicks The partial tick time.
*/
protected abstract void draw(Tessellator tessellator, double length, float partialTicks);
}
@@ -1,7 +1,20 @@
package electroblob.wizardry.client.particle;
import java.util.Arrays;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import electroblob.wizardry.Wizardry;
import net.minecraft.client.Minecraft;
import net.minecraft.client.particle.Particle;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.entity.Entity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
import net.minecraftforge.client.event.TextureStitchEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@@ -9,7 +22,7 @@ import net.minecraftforge.fml.relauncher.SideOnly;
* Abstract superclass for all of wizardry's particles. This replaces {@code ParticleCustomTexture} (the functionality of
* which is no longer necessary since wizardry now uses {@code TextureAtlasSprite}s to do the rendering), and fits into
* {@code ParticleBuilder} by exposing all the necessary variables through getters, allowing them to be set on the fly
* rather than needing to be passed into the constructor.
* rather than needing to be passed into the constructor.
* <p>
* The new system is as follows:
* <p>
@@ -17,28 +30,94 @@ import net.minecraftforge.fml.relauncher.SideOnly;
* - Each particle class defines any relevant default values in its constructor, including velocity.<br>
* - The particle builder then overwrites any other values that were set during building.
* <p>
* This beauty of this system is that there are never any redundant parameters when spawning particles. For example,
* This beauty of this system is that there are never any redundant parameters when spawning particles, since you can set
* as many or as few parameters as necessary - and in addition, common defaults don't need setting at all. For example,
* snow particles nearly always fall at the same speed, which can now be defined in the particle class and no longer
* needs to be defined when spawning the particle - but importantly, it can still be overridden if desired.
*
* @author Electroblob
* @since Wizardry 4.2.0
* @since Wizardry 4.2.0
* @see electroblob.wizardry.util.ParticleBuilder ParticleBuilder
*/
@SideOnly(Side.CLIENT)
public abstract class ParticleWizardry extends Particle {
/** Implementation of animated particles using the TextureAtlasSprite system. Why vanilla doesn't support this I
* don't know, considering it too has animated particles. */
protected final TextureAtlasSprite[] sprites;
/** True if the particle is shaded, false if the particle always renders at full brightness. Defaults to false. */
protected boolean shaded = false;
protected float fadeRed = 1;
protected float fadeGreen = 1;
protected float fadeBlue = 0;
public ParticleWizardry(World world, double x, double y, double z){
protected float initialRed;
protected float initialGreen;
protected float initialBlue;
protected float fadeRed = 0;
protected float fadeGreen = 0;
protected float fadeBlue = 0;
protected float angle;
protected double radius = 0;
protected double speed = 0;
/** The entity this particle is linked to. The particle will move with this entity. */
@Nullable
protected Entity entity = null;
/** Coordinates of this particle relative to the linked entity. If the linked entity is null, these are not used. */
protected double relativeX, relativeY, relativeZ;
/** Velocity of this particle relative to the linked entity. The relative x and z velocities are also used when
* the particle has spin to move the centre of rotation. If the linked entity is null and the particle is not
* spinning, these are not used and will be {@code NaN}. */
protected double relativeMotionX = Double.NaN, relativeMotionY = Double.NaN, relativeMotionZ = Double.NaN;
// Note that roll (equivalent to rotating the texture) is effectively handled by particleAngle - although that is
// actually the rotation speed and not the angle itself.
/** The yaw angle this particle is facing, or {@code NaN} if this particle always faces the viewer (default behaviour). */
protected float yaw = Float.NaN;
/** The pitch angle this particle is facing, or {@code NaN} if this particle always faces the viewer (default behaviour). */
protected float pitch = Float.NaN;
/**
* Creates a new particle in the given world at the given position. All other parameters are set via the various
* setter methods ({@link electroblob.wizardry.util.ParticleBuilder ParticleBuilder} deals with all of that anyway).
* @param world The world in which to create the particle.
* @param x The x-coordinate at which to create the particle.
* @param y The y-coordinate at which to create the particle.
* @param z The z-coordinate at which to create the particle.
* @param textures One or more {@code ResourceLocation}s representing the texture(s) used by this particle. These
* <b>must</b> be registered as {@link TextureAtlasSprite}s using {@link TextureStitchEvent} or the textures will be
* missing. If more than one {@code ResourceLocation} is specified, the particle will be animated with each texture
* shown in order for an equal proportion of the particle's lifetime. If this argument is omitted (or a zero-length
* array is given), the particle will use the vanilla system instead (based on the X/Y texture indices).
*/
public ParticleWizardry(World world, double x, double y, double z, ResourceLocation... textures){
super(world, x, y, z);
// Sets the relative coordinates in case they are needed
this.relativeX = x;
this.relativeY = y;
this.relativeZ = z;
// Deals with the textures
if(textures.length > 0){
sprites = Arrays.stream(textures).map(t -> Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite(
t.toString())).collect(Collectors.toList()).toArray(new TextureAtlasSprite[0]);
this.setParticleTexture(sprites[0]);
}else{
sprites = new TextureAtlasSprite[0];
}
}
// ============================================== Parameter Setters ==============================================
// Setters for parameters that affect all particles - these are implemented in this class (although they may be
// reimplemented in subclasses)
/** Sets whether the particle should render at full brightness or not. True if the particle is shaded, false if
* the particle always renders at full brightness. Defaults to false.*/
public void setShaded(boolean shaded){
@@ -50,9 +129,9 @@ public abstract class ParticleWizardry extends Particle {
this.particleGravity = gravity ? 1 : 0;
}
/** Sets this particle's lifetime in ticks.*/
public void setLifetime(int lifetime){
this.particleMaxAge = lifetime;
/** Sets this particle's collisions. True to enable block collisions, false to disable. Defaults to false.*/
public void setCollisions(boolean canCollide){
this.canCollide = canCollide;
}
/**
@@ -67,25 +146,252 @@ public abstract class ParticleWizardry extends Particle {
this.motionZ = vz;
}
/**
* Sets the spin parameters of the particle.
* @param radius The spin radius
* @param speed The spin speed in rotations per tick
*/
public void setSpin(double radius, double speed){
this.radius = radius;
this.speed = speed * 2 * Math.PI; // Converts rotations per tick into radians per tick for the trig functions
this.angle = this.rand.nextFloat() * (float)Math.PI * 2; // Random start angle TODO: Perhaps this should be specified?
// Need to set the start position or the circle won't be centred on the correct position
this.relativeX = radius * -MathHelper.cos(angle);
this.relativeZ = radius * MathHelper.sin(angle);
this.setPosition(posX + relativeX, posY, posZ + relativeZ);
this.prevPosX = posX;
this.prevPosZ = posZ;
// Set these to the correct values
this.relativeMotionX = motionX;
this.relativeMotionY = motionY;
this.relativeMotionZ = motionZ;
}
/**
* Links this particle to the given entity. This will cause its position and velocity to be relative to the entity.
* @param entity The entity to link to.
*/
public void setEntity(Entity entity){
this.entity = entity;
// Set these to the correct values
if(entity != null){
this.setPosition(this.entity.posX + relativeX, this.entity.getEntityBoundingBox().minY
+ relativeY, this.entity.posZ + relativeZ);
this.prevPosX = this.posX;
this.prevPosY = this.posY;
this.prevPosZ = this.posZ;
this.relativeMotionX = motionX;
this.relativeMotionY = motionY;
this.relativeMotionZ = motionZ;
}
}
// Overridden to set the initial colour values
/**
* Sets the base colour of the particle. <i>Note that this also sets the fade colour so that particles without a
* fade colour do not change colour at all; as such fade colour must be set <b>after</b> calling this method.</i>
* @param r The red colour component
* @param g The green colour component
* @param b The blue colour component
*/
@Override
public void setRBGColorF(float r, float g, float b){
super.setRBGColorF(r, g, b);
initialRed = r;
initialGreen = g;
initialBlue = b;
// If fade colour is not specified, it defaults to the main colour - this method is always called first
setFadeColour(r, g, b);
}
/**
* Sets the fade colour of the particle.
* @param r The red colour component
* @param g The green colour component
* @param g The blue colour component
* @param b The blue colour component
*/
public void setFadeColour(float r, float g, float b){
this.fadeRed = r;
this.fadeGreen = g;
this.fadeBlue = b;
}
/**
* Sets the direction this particle faces. This will cause the particle to render facing the given direction.
* @param yaw The yaw angle of this particle in degrees, where 0 is [TODO south?].
* @param pitch The pitch angle of this particle in degrees, where 0 is horizontal.
*/
public void setFacing(float yaw, float pitch){
this.yaw = yaw;
this.pitch = pitch;
}
// Setters for parameters that only affect some particles - these are unimplemented in this class because they
// doesn't make sense for most particles
/**
* Sets the target position for this particle. This will cause it to stretch to touch the given position,
* if supported.
* @param x The x-coordinate of the target position.
* @param y The y-coordinate of the target position.
* @param z The z-coordinate of the target position.
*/
public void setTargetPosition(double x, double y, double z){
// Does nothing for normal particles since normal particles always render at a single point
}
/**
* Links this particle to the given target. This will cause it to stretch to touch the target, if supported.
* @param target The target to link to.
*/
public void setTargetEntity(Entity target){
// Does nothing for normal particles since normal particles always render at a single point
}
// ============================================== Method Overrides ==============================================
@Override
public int getFXLayer(){
return sprites.length == 0 ? super.getFXLayer() : 1; // This has to be 1 for the TextureAtlasSprites to work
}
@Override
public int getBrightnessForRender(float partialTick){
return shaded ? super.getBrightnessForRender(partialTick) : 15728880;
}
/**
* Renders the particle. The mapping names given to the parameters in this method are very misleading; see below for
* details of what they actually do. (They're also in a strange order...)
* @param buffer The {@code BufferBuilder} object.
* @param viewer The entity whose viewpoint the particle is being rendered from; this should always be the
* client-side player.
* @param partialTicks The partial tick time.
* @param lookZ Equal to the cosine of {@code viewer.rotationYaw}. Will be -1 when facing north (negative Z), 0 when
* east/west, and +1 when facing south (positive Z). Independent of pitch.
* @param lookY Equal to the cosine of {@code viewer.rotationPitch}. Will be 1 when facing directly up or down, and 0
* when facing directly horizontally.
* @param lookX Equal to the sine of {@code viewer.rotationYaw}. Will be -1 when facing east (positive X), 0 when
* facing north/south, and +1 when facing west (negative X). Independent of pitch.
* @param lookXY Equal to {@code lookX} times the sine of {@code viewer.rotationPitch}. Will be 0 when facing directly horizontal.
* When facing directly up, will be equal to {@code -lookX}. When facing directly down, will be equal to {@code lookX}.
* @param lookYZ Equal to {@code -lookZ} times the sine of {@code viewer.rotationPitch}. Will be 0 when facing directly horizontal.
* When facing directly up, will be equal to {@code -lookZ}. When facing directly down, will be equal to {@code lookZ}.
*/
@Override
public void renderParticle(BufferBuilder buffer, Entity viewer, float partialTicks, float lookZ, float lookY,
float lookX, float lookXY, float lookYZ){
if(Float.isNaN(this.yaw) || Float.isNaN(this.pitch)){
// Normal behaviour (rotates to face the viewer)
super.renderParticle(buffer, viewer, partialTicks, lookZ, lookY, lookX, lookXY, lookYZ);
}else{
// Specific rotation
// Copied from ActiveRenderInfo; converts yaw and pitch into the weird parameters used by renderParticle.
// The 1st/3rd person distinction has been removed since this has nothing to do with the view angle.
float degToRadFactor = 0.017453292f; // Conversion from degrees to radians
float rotationX = MathHelper.cos(yaw * degToRadFactor);
float rotationZ = MathHelper.sin(yaw * degToRadFactor);
float rotationY = MathHelper.cos(pitch * degToRadFactor);
float rotationYZ = -rotationZ * MathHelper.sin(pitch * degToRadFactor);
float rotationXY = rotationX * MathHelper.sin(pitch * degToRadFactor);
super.renderParticle(buffer, viewer, partialTicks, rotationX, rotationY, rotationZ, rotationYZ, rotationXY);
}
}
@Override
public void onUpdate(){
/** Simple particle factory interface which takes a world and a position and returns a particle. Used (via lambda
* expressions) in the client proxy to link particle enum types to actual particle classes. */
super.onUpdate();
// If any of these values is NaN, the particle has neither an entity nor a spin
if(!Double.isNaN(relativeMotionX) && !Double.isNaN(relativeMotionY) && !Double.isNaN(relativeMotionZ)){
// This allows velocity changes from entity linking and spin to stack
double vx = relativeMotionX;
double vy = relativeMotionY;
double vz = relativeMotionZ;
// Entity linking
if(this.entity != null){
if(this.entity.isDead) this.setExpired();
this.setPosition(this.entity.posX + relativeX, this.entity.getEntityBoundingBox().minY + relativeY,
this.entity.posZ + relativeZ);
// Velocity is set so that the renderer will interpolate correctly between ticks
vx += this.entity.motionX;
vy += this.entity.motionY;
vz += this.entity.motionZ;
}
// Spin
if(radius > 0){
angle += speed;
vx += radius * speed * MathHelper.sin(angle);
vz += radius * speed * MathHelper.cos(angle);
}
this.relativeX += vx;
this.relativeY += vy;
this.relativeZ += vz;
}
// Colour fading
float ageFraction = (float)this.particleAge / (float)this.particleMaxAge;
// No longer uses setRBGColorF because that method now also sets the initial values
this.particleRed = this.initialRed + (this.fadeRed - this.initialRed) * ageFraction;
this.particleGreen = this.initialGreen + (this.fadeGreen - this.initialGreen) * ageFraction;
this.particleBlue = this.initialBlue + (this.fadeBlue - this.initialBlue) * ageFraction;
// Animation
if(sprites.length > 1){
// Math.min included for safety so the index cannot possibly exceed the length - 1 an cause an AIOOBE
// (which would probably otherwise happen if particleAge == particleMaxAge)
this.setParticleTexture(sprites[Math.min((int)(ageFraction * sprites.length), sprites.length - 1)]);
}
}
// =============================================== Helper Methods ===============================================
/** Static helper method that generates an array of n ResourceLocations using the particle file naming convention,
* which is the given stem plus an underscore plus the integer index. */
public static ResourceLocation[] generateTextures(String stem, int n){
ResourceLocation[] textures = new ResourceLocation[n];
for(int i=0; i<n; i++){
textures[i] = new ResourceLocation(Wizardry.MODID, "particle/" + stem + "_" + i);
}
return textures;
}
/** Static helper method that generates a 2D m x n array of ResourceLocations using the particle file naming
* convention, which is the given stem plus an underscore plus the first index, plus an underscore plus the second
* index. Useful for animated particles that also pick a random animation strip. */
public static ResourceLocation[][] generateTextures(String stem, int m, int n){
ResourceLocation[][] textures = new ResourceLocation[m][n];
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
textures[i][j] = new ResourceLocation(Wizardry.MODID, "particle/" + stem + "_" + i + "_" + j);
}
}
return textures;
}
/** Simple particle factory interface which takes a world and a position and returns a particle. Used (via method
* references) in the client proxy to link particle enum types to actual particle classes. */
@SideOnly(Side.CLIENT)
@FunctionalInterface
public interface IWizardryParticleFactory {
@@ -1,187 +0,0 @@
package electroblob.wizardry.client.renderer;
import java.util.Random;
import org.lwjgl.opengl.GL11;
import electroblob.wizardry.entity.EntityArc;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.entity.Render;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.util.ResourceLocation;
public class RenderArc extends Render<EntityArc> {
public RenderArc(RenderManager renderManager){
super(renderManager);
}
@Override
public void doRender(EntityArc arc, double x, double y, double z, float viewPitch, float viewYaw) {
GlStateManager.pushMatrix();
GlStateManager.translate(x, y, z);
GlStateManager.disableLighting();
GlStateManager.enableBlend();
GlStateManager.disableTexture2D();
GlStateManager.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE);
OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, 240f, 240f);
Tessellator tessellator = Tessellator.getInstance();
/* Note: A lot of the maths here works on similar triangles and the ratios between them, avoiding too much
* pythagoras and eliminating the need for any trig. Ratios are used for the positioning of the arc endpoints.
* Ratios are usually used swapping x and z because the triangles are rotated through 90 degrees. */
double dx = -x;
double dy = -y;
double dz = -z;
if(arc.x1 != 0){
dx = arc.x1 - arc.posX;
dy = arc.y1 - arc.posY;
dz = arc.z1 - arc.posZ;
// The distance from origin to endpoint
double arcLength = Math.sqrt(dx*dx+dy*dy+dz*dz);
GL11.glTranslated(dx, dy, dz);
// Math.atan2 computes within -180 to +180, rather than -90 to +90.
float yaw = (float)(180d/Math.PI * Math.atan2(-dx, -dz));
float pitch = (float)(180f/(float)Math.PI * Math.atan(dy/Math.sqrt(dz*dz+dx*dx)));
GL11.glRotatef(yaw, 0, 1, 0);
GL11.glRotatef(pitch, 1, 0, 0);
// The direction of the arc drawn by the tessellator is always along the z axis and is rotated to the
// correct orientation, that way there isn't a ton of trigonometry and the code is way neater.
boolean freeEnd = arc.freeEnd;
// == To be extracted as constants later ==
float thickness = 0.04f; // Half the width of the outermost layer
double maxSegmentLength = 0.6; // Max length of a segment, obviously
double minSegmentLength = 0.2; // Min length of a segment
double vertexDither = 0.15; // Max deviation in x or y axis from the centreline
int maxForkSegments = 3; // Max number of segments a fork can have
float forkChance = 0.3f; // Chance (as a fraction) that a vertex will have a fork
int updateTime = 1; // Number of ticks to wait before the arc changes shape again
int numberOfSegments = (int)Math.round(arcLength/maxSegmentLength); // Number of segments
for(int layer=0; layer<3; layer++){
double px=0, py=0, pz=0;
// Creates a random from the arc's seed field + the number of ticks it has existed/the update period.
// By using a seed, we can ensure the vertex positions and forks are identical a) for each layer, even
// though they are rendered sequentially, and b) across many frames (and ticks, if updateTime > 1).
Random random = new Random(arc.seed + arc.ticksExisted/updateTime);
// numberOfSegments-1 because the last segment is handled separately.
for(int i=0; i<numberOfSegments-1; i++){
double px2 = (random.nextDouble()*2-1)*vertexDither; // Maybe use Gaussians?
double py2 = (random.nextDouble()*2-1)*vertexDither;
double pz2 = pz + arcLength/(double)numberOfSegments; // For now they are all the same length
drawSegment(tessellator, layer, px, py, pz, px2, py2, pz2, thickness);
// Forks
if(random.nextFloat() < forkChance){
double px3=px, py3=py, pz3=pz;
for(int j=0; j<random.nextInt(maxForkSegments-1)+1; j++){
// Forks set their centreline to the x/y coordinates of the vertex they originate from
double px4 = px3 + (random.nextDouble()*2-1)*vertexDither;
double py4 = py3 + (random.nextDouble()*2-1)*vertexDither;
double pz4 = pz3 + minSegmentLength + random.nextDouble()*(maxSegmentLength - minSegmentLength);
drawSegment(tessellator, layer, px3, py3, pz3, px4, py4, pz4, thickness*0.8f);
// Forks of forks
if(random.nextFloat() < forkChance){
double px5 = px3 + (random.nextDouble()*2-1)*vertexDither;
double py5 = py3 + (random.nextDouble()*2-1)*vertexDither;
double pz5 = pz3 + minSegmentLength + random.nextDouble()*(maxSegmentLength - minSegmentLength);
drawSegment(tessellator, layer, px3, py3, pz3, px5, py5, pz5, thickness*0.6f);
}
px3 = px4;
py3 = py4;
pz3 = pz4;
}
}
px = px2;
py = py2;
pz = pz2;
}
// Last segment has a specific end position and cannot fork.
double px2 = freeEnd ? (random.nextDouble()*2-1)*vertexDither : 0;
double py2 = freeEnd ? (random.nextDouble()*2-1)*vertexDither : 0;
drawSegment(tessellator, layer, px, py, pz, px2, py2, arcLength, thickness);
}
}
GlStateManager.enableTexture2D();
GlStateManager.enableLighting();
GlStateManager.disableBlend();
GlStateManager.popMatrix();
}
/** Draws the given layer of a segment of the arc, from the point (x1, y1, z1) to the point (x2, y2, z2), with the given thickness. */
private void drawSegment(Tessellator tessellator, int layer, double x1, double y1, double z1, double x2, double y2, double z2, float thickness){
BufferBuilder buffer = tessellator.getBuffer();
buffer.begin(GL11.GL_TRIANGLE_STRIP, DefaultVertexFormats.POSITION_COLOR);
switch(layer){
case 0:
// TODO: Extract these colours as constants
drawShearedBox(buffer, x1, y1, z1, x2, y2, z2, 0.25f*thickness, 255, 255, 255, 255);
break;
case 1:
drawShearedBox(buffer, x1, y1, z1, x2, y2, z2, 0.6f*thickness, 200, 255, 255, 165);
break;
case 2:
drawShearedBox(buffer, x1, y1, z1, x2, y2, z2, thickness, 50, 150, 255, 75);
break;
}
tessellator.draw();
}
/** Draws a single box for one segment of the arc, from the point (x1, y1, z1) to the point (x2, y2, z2), with given width and colour. */
private void drawShearedBox(BufferBuilder buffer, double x1, double y1, double z1, double x2, double y2, double z2, float width, int r, int g, int b, int a){
buffer.pos(x1-width, y1-width, z1).color(r, g, b, a).endVertex();
buffer.pos(x2-width, y2-width, z2).color(r, g, b, a).endVertex();
buffer.pos(x1-width, y1+width, z1).color(r, g, b, a).endVertex();
buffer.pos(x2-width, y2+width, z2).color(r, g, b, a).endVertex();
buffer.pos(x1+width, y1+width, z1).color(r, g, b, a).endVertex();
buffer.pos(x2+width, y2+width, z2).color(r, g, b, a).endVertex();
buffer.pos(x1+width, y1-width, z1).color(r, g, b, a).endVertex();
buffer.pos(x2+width, y2-width, z2).color(r, g, b, a).endVertex();
buffer.pos(x1-width, y1-width, z1).color(r, g, b, a).endVertex();
buffer.pos(x2-width, y2-width, z2).color(r, g, b, a).endVertex();
}
@Override
protected ResourceLocation getEntityTexture(EntityArc entity){
return null;
}
}
@@ -1,72 +0,0 @@
package electroblob.wizardry.client.renderer;
import org.lwjgl.opengl.GL11;
import electroblob.wizardry.Wizardry;
import electroblob.wizardry.entity.construct.EntityLightningPulse;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.entity.Render;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.util.ResourceLocation;
public class RenderLightningPulse extends Render<EntityLightningPulse> {
private final ResourceLocation[] textures = new ResourceLocation[8];
private float scale = 1.0f;
public RenderLightningPulse(RenderManager renderManager, float scale){
super(renderManager);
for(int i = 0; i < textures.length; i++){
textures[i] = new ResourceLocation(Wizardry.MODID, "textures/entity/lightning_pulse_" + i + ".png");
}
this.scale = scale;
}
@Override
public void doRender(EntityLightningPulse entity, double par2, double par4, double par6, float par8, float par9){
GlStateManager.pushMatrix();
GlStateManager.enableBlend();
GlStateManager.disableLighting();
OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, 240, 240);
GlStateManager.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
float yOffset = 0;
GlStateManager.translate((float)par2, (float)par4 + yOffset, (float)par6);
this.bindTexture(textures[entity.ticksExisted]);
float f6 = 1.0F;
float f7 = 0.5F;
float f8 = 0.5F;
GlStateManager.rotate(-90, 1, 0, 0);
GlStateManager.scale(scale, scale, scale);
Tessellator tessellator = Tessellator.getInstance();
BufferBuilder buffer = tessellator.getBuffer();
buffer.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX);
buffer.pos((double)(0.0F - f7), (double)(0.0F - f8), 0.01).tex(0, 1).endVertex();
buffer.pos((double)(f6 - f7), (double)(0.0F - f8), 0.01).tex(1, 1).endVertex();
buffer.pos((double)(f6 - f7), (double)(1.0F - f8), 0.01).tex(1, 0).endVertex();
buffer.pos((double)(0.0F - f7), (double)(1.0F - f8), 0.01).tex(0, 0).endVertex();
tessellator.draw();
GlStateManager.disableBlend();
GlStateManager.enableLighting();
GlStateManager.disableRescaleNormal();
GlStateManager.popMatrix();
}
@Override
protected ResourceLocation getEntityTexture(EntityLightningPulse entity){
return null;
}
}