diff --git a/src/main/java/com/lzxnone/terraria/LzxnoneTerrariaClient.java b/src/main/java/com/lzxnone/terraria/LzxnoneTerrariaClient.java index 7e21533..4d7f885 100644 --- a/src/main/java/com/lzxnone/terraria/LzxnoneTerrariaClient.java +++ b/src/main/java/com/lzxnone/terraria/LzxnoneTerrariaClient.java @@ -38,6 +38,9 @@ public class LzxnoneTerrariaClient { modEventBus.addListener(RegisterParticleProvidersEvent.class, event -> { event.registerSpriteSet(ModParticles.DUST_PARTICLE.get(), DustParticle.Provider::new); }); + modEventBus.addListener(RegisterParticleProvidersEvent.class, event -> { + event.registerSpriteSet(ModParticles.ZENITH_TRAIL_PARTICLE.get(), ZenithTrailParticle.Provider::new); + }); modEventBus.addListener(RegisterParticleProvidersEvent.class, event -> { event.registerSpriteSet(ModParticles.STAR_PARTICLE.get(), StarParticle.Provider::new); }); diff --git a/src/main/java/com/lzxnone/terraria/item/weapon/melee/Zenith.java b/src/main/java/com/lzxnone/terraria/item/weapon/melee/Zenith.java index bfccec7..9276990 100644 --- a/src/main/java/com/lzxnone/terraria/item/weapon/melee/Zenith.java +++ b/src/main/java/com/lzxnone/terraria/item/weapon/melee/Zenith.java @@ -4,12 +4,11 @@ import com.lzxnone.terraria.LzxnoneTerraria; import com.lzxnone.terraria.ModSounds; import com.lzxnone.terraria.entity.ModEntities; import com.lzxnone.terraria.entity.TintedVertexConsumer; -import com.lzxnone.terraria.entity.beam.SwordBeam; import com.lzxnone.terraria.entity.projectile.IStaticProjectileBehavior; import com.lzxnone.terraria.entity.projectile.StaticProjectile; import com.lzxnone.terraria.entity.projectile.StaticProjectileBehaviors; import com.lzxnone.terraria.item.ModItems; -import com.lzxnone.terraria.particle.ModParticles; +import com.lzxnone.terraria.particle.ZenithTrailParticleOptions; import com.lzxnone.terraria.utils.*; import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.VertexConsumer; @@ -22,7 +21,6 @@ import net.minecraft.client.renderer.texture.OverlayTexture; import net.minecraft.core.component.DataComponents; import net.minecraft.nbt.CompoundTag; import net.minecraft.resources.ResourceLocation; -import net.minecraft.util.Mth; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResultHolder; import net.minecraft.world.entity.Entity; @@ -30,17 +28,14 @@ import net.minecraft.world.entity.EquipmentSlotGroup; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.ai.attributes.AttributeModifier; import net.minecraft.world.entity.ai.attributes.Attributes; -import net.minecraft.world.entity.monster.Monster; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.*; import net.minecraft.world.item.component.CustomData; import net.minecraft.world.item.component.ItemAttributeModifiers; import net.minecraft.world.level.Level; import net.minecraft.world.phys.AABB; -import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.EntityHitResult; import net.minecraft.world.phys.Vec3; -import org.joml.Matrix3f; import org.joml.Matrix4f; import org.joml.Quaternionf; import org.joml.Vector3f; @@ -198,7 +193,7 @@ public class Zenith extends SwordItem { //尾迹 int quadCount = proj.trailPositions.size() / 2 - 1; for(int j = 0;j < 2;j++) { - for(int i = 0; i < quadCount; i++) { + for(int i = 1; i < quadCount; i++) { Vec3 currentPoint1 = proj.trailPositions.get(i * 2); Vec3 currentPoint2 = proj.trailPositions.get(i * 2 + 1); Vec3 nextPoint1 = proj.trailPositions.get(i * 2 + 3); @@ -292,26 +287,60 @@ public class Zenith extends SwordItem { @Override public void onMoving(StaticProjectile projectile) { if(projectile.getOwner() == null) return; - addTrailPoints(projectile); - LzxnoneTerraria.LOGGER.info("{}", projectile.tickCount); + CompoundTag customData = projectile.getEntityData().get(StaticProjectile.CUSTOM_DATA); + if(!customData.contains("w") || !customData.contains("angle")) return; + + float lifeRadio = (float) projectile.getEntityData().get(StaticProjectile.AGE) / (float) projectile.getEntityData().get(StaticProjectile.LIFETIME); + + Vector3f originalRight = projectile.getEntityData().get(StaticProjectile.RIGHT).normalize(); + Vector3f tempRight = new Vector3f(originalRight); + Vector3f originalDir = projectile.getEntityData().get(StaticProjectile.DIRECTION).normalize(); + Vector3f tempDir = new Vector3f(originalDir); + Vector3f up = projectile.getEntityData().get(StaticProjectile.UP).normalize(); + double rad = customData.getDouble("w") * projectile.getEntityData().get(StaticProjectile.AGE); + Quaternionf rotation = new Quaternionf() + .fromAxisAngleRad(up, (float) (Math.PI / 2 - rad)); + + Vec3 right = MathUtil.toVec3(tempRight.rotate(rotation)); //剑尖方向 + Vec3 dir = MathUtil.toVec3(tempDir.rotate(rotation)); //剑飞行方向 + if(customData.getDouble("w") < 0) dir = dir.scale(-1); + + projectile.trailPositions.addFirst(projectile.position().add(right.scale(-1.2))); + projectile.trailPositions.addFirst(projectile.position().add(right.scale(1.5))); + while(projectile.trailPositions.size() > MAX_LENGTH) projectile.trailPositions.removeLast(); projectile.setBoundingBox(new AABB( projectile.getX() - BOUNDING_BOX, projectile.getY() - BOUNDING_BOX, projectile.getZ() - BOUNDING_BOX, projectile.getX() + BOUNDING_BOX, projectile.getY() + BOUNDING_BOX, projectile.getZ() + BOUNDING_BOX )); + if(!projectile.level().isClientSide()) { List targets = projectile.level().getEntitiesOfClass( LivingEntity.class, projectile.getBoundingBox(), FilterUtil.createLivingTargetFilter(projectile, projectile.getOwner()) ); - for(LivingEntity target : targets) this.onHitEntity(projectile, new EntityHitResult(target, target.position())); + }else { + if(lifeRadio > 0.1f && lifeRadio < 0.9f && projectile.getRandom().nextInt(2) == 0) { + Vector3f color = new Vector3f( + projectile.getEntityData().get(StaticProjectile.COLOR_R), + projectile.getEntityData().get(StaticProjectile.COLOR_G), + projectile.getEntityData().get(StaticProjectile.COLOR_B) + ); + ZenithTrailParticleOptions options = new ZenithTrailParticleOptions(0.05f, 40, true, color, up, right.toVector3f(), customData.getInt("angle")); + Vec3 pos = right.scale(Math.random()).add(projectile.position()); + Vec3 speed = dir.scale(Math.max(0.05, Math.random() * 0.2)); + ParticleUtil.addParticle( + projectile.level(), options, + pos, 0, + speed, 0 + ); + } } - Vector3f dir = projectile.getEntityData().get(StaticProjectile.DIRECTION).normalize(); - Vec3 currentPos = (projectile.getOwner().getBoundingBox().getCenter()).add(MathUtil.toVec3(dir).scale(-2)); + Vec3 currentPos = (projectile.getOwner().getBoundingBox().getCenter()).add(MathUtil.toVec3(originalDir).scale(-2)); projectile.getEntityData().set(StaticProjectile.ORIGIN, currentPos.toVector3f()); } @@ -328,24 +357,6 @@ public class Zenith extends SwordItem { } }; - public static void addTrailPoints(StaticProjectile projectile) { - CompoundTag customData = projectile.getEntityData().get(StaticProjectile.CUSTOM_DATA); - if(!customData.contains("w")) return; - - Vector3f originalRight = projectile.getEntityData().get(StaticProjectile.RIGHT).normalize(); - Vector3f tempRight = new Vector3f(originalRight); - Vector3f up = projectile.getEntityData().get(StaticProjectile.UP).normalize(); - double rad = customData.getDouble("w") * projectile.getEntityData().get(StaticProjectile.AGE); - Quaternionf rotation = new Quaternionf() - .fromAxisAngleRad(up, (float) (Math.PI / 2 - rad - 0.3)); - - Vec3 right = MathUtil.toVec3(tempRight.rotate(rotation)); - - projectile.trailPositions.addFirst(projectile.position().add(right.scale(-1.2))); - projectile.trailPositions.addFirst(projectile.position().add(right.scale(1.5))); - while(projectile.trailPositions.size() > MAX_LENGTH) projectile.trailPositions.removeLast(); - } - public static void summon(Player player, boolean isFirst) { Vector3f[] dirs = MathUtil.computeCoordinateSystem(player); int randomAngle = (int) ((Math.random() * 2 - 1) * 80); diff --git a/src/main/java/com/lzxnone/terraria/particle/InfluxWaverDropParticle.java b/src/main/java/com/lzxnone/terraria/particle/InfluxWaverDropParticle.java index e9cb0e2..f41b346 100644 --- a/src/main/java/com/lzxnone/terraria/particle/InfluxWaverDropParticle.java +++ b/src/main/java/com/lzxnone/terraria/particle/InfluxWaverDropParticle.java @@ -64,7 +64,7 @@ public class InfluxWaverDropParticle extends TextureSheetParticle { @Override public void render(VertexConsumer buffer, Camera camera, float partialTick) { - if (this.alpha <= 0.01F) return; + if(this.alpha <= 0.01F) return; float lifeRatio = (this.age + partialTick) / (float) this.lifetime; double progress = (lifeRatio - DROP_RADIO) / (1.0 - DROP_RADIO); diff --git a/src/main/java/com/lzxnone/terraria/particle/ModParticles.java b/src/main/java/com/lzxnone/terraria/particle/ModParticles.java index 315b7e4..6b98390 100644 --- a/src/main/java/com/lzxnone/terraria/particle/ModParticles.java +++ b/src/main/java/com/lzxnone/terraria/particle/ModParticles.java @@ -63,6 +63,17 @@ public class ModParticles { return DustParticleOptions.STREAM_CODEC; } }); + public static final Supplier> ZENITH_TRAIL_PARTICLE = + PARTICLE_TYPES.register("zenith_trail_particle", () -> new ParticleType<>(false) { + @Override + public MapCodec codec() { + return ZenithTrailParticleOptions.CODEC; + } + @Override + public StreamCodec streamCodec() { + return ZenithTrailParticleOptions.STREAM_CODEC; + } + }); public static final Supplier STAR_PARTICLE = PARTICLE_TYPES.register("star_particle", () -> new SimpleParticleType(true)); public static final Supplier EXPLODE_PARTICLE = diff --git a/src/main/java/com/lzxnone/terraria/particle/ZenithTrailParticle.java b/src/main/java/com/lzxnone/terraria/particle/ZenithTrailParticle.java new file mode 100644 index 0000000..a58545c --- /dev/null +++ b/src/main/java/com/lzxnone/terraria/particle/ZenithTrailParticle.java @@ -0,0 +1,161 @@ +package com.lzxnone.terraria.particle; + +import com.lzxnone.terraria.utils.MathUtil; +import com.mojang.blaze3d.vertex.VertexConsumer; +import net.minecraft.client.Camera; +import net.minecraft.client.multiplayer.ClientLevel; +import net.minecraft.client.particle.*; +import net.minecraft.client.renderer.LightTexture; +import net.minecraft.util.Mth; +import net.minecraft.world.phys.Vec3; +import org.joml.Quaternionf; +import org.joml.Vector3f; + + +public class ZenithTrailParticle extends TextureSheetParticle { + private final float baseSize; + + public float initSize; + public int initLifetime; + public boolean glow; + + public Vec3 dir; + public Vec3 up; + public Vec3 right; + public int angle; + + private static final float MAX_DIST = 0.05f; + private static final int COUNT = 30; + + protected ZenithTrailParticle(ClientLevel level, double x, double y, double z, + double xSpeed, double ySpeed, double zSpeed, + ZenithTrailParticleOptions options, SpriteSet spriteSet) { + super(level, x, y, z, xSpeed, ySpeed, zSpeed); + this.hasPhysics = false; + + this.friction = 0.9F; + this.gravity = 0.0F; + this.xd = xSpeed; + this.yd = ySpeed; + this.zd = zSpeed; + + this.initSize = options.initSize; + this.initLifetime = options.initLifetime; + this.glow = options.glow; + this.dir = new Vec3(xSpeed, ySpeed, zSpeed).normalize(); + this.up = new Vec3(options.up).normalize(); + this.right = new Vec3(options.right).normalize(); + + this.rCol = options.color.x; + this.gCol = options.color.y; + this.bCol = options.color.z; + + this.baseSize = initSize + (this.random.nextFloat() * initSize); + this.quadSize = baseSize; + + this.roll = this.random.nextFloat() * ((float)Math.PI * 2F); + this.oRoll = this.roll; + + this.lifetime = initLifetime + this.random.nextInt(initLifetime / 2); + + this.pickSprite(spriteSet); + } + + @Override + public void tick() { + super.tick(); + + this.oRoll = this.roll; + + float lifeRatio = (float) this.age / (float) this.lifetime; + + this.quadSize = this.baseSize * (1.0F - lifeRatio); + this.alpha = 1.0F - lifeRatio; + } + + @Override + public void render(VertexConsumer buffer, Camera camera, float partialTick) { + if(this.alpha <= 0.01F) return; + float lifeRatio = (this.age + partialTick) / (float) this.lifetime; + + Vec3 headPos = new Vec3( + (Mth.lerp(partialTick, this.xo, this.x) - camera.getPosition().x), + (Mth.lerp(partialTick, this.yo, this.y) - camera.getPosition().y), + (Mth.lerp(partialTick, this.zo, this.z) - camera.getPosition().z) + ); + + float u0 = this.getU0(); + float u1 = this.getU1(); + float v0 = this.getV0(); + float v1 = this.getV1(); + + float[][] uvs = new float[][]{ + new float[]{u1, v1}, + new float[]{u1, v0}, + new float[]{u0, v0}, + new float[]{u0, v1} + }; + + float[] xyRot = MathUtil.computeXYRot(dir.toVector3f(), up.toVector3f()); + Quaternionf rotation = new Quaternionf() + .fromAxisAngleRad(dir.toVector3f(), (float) Math.toRadians(this.angle)) + .rotateY((float) Math.toRadians(-xyRot[1])) + .rotateX((float) Math.toRadians(-90 + xyRot[0]) + ); + + Vector3f[] baseVertices = new Vector3f[]{ + new Vector3f(1.0F, -1.0F, 0.0F).rotate(rotation), + new Vector3f(1.0F, 1.0F, 0.0F).rotate(rotation), + new Vector3f( -1.0F, 1.0F, 0.0F).rotate(rotation), + new Vector3f( -1.0F, -1.0F, 0.0F).rotate(rotation) + }; + + + float dist = (1.0f - lifeRatio) * MAX_DIST; + + for(int i = 0; i < COUNT; i++) { + float radio = 1.0F - ((float) i / (COUNT - 1));; + float currentQuadSize = this.quadSize * radio; + float currentAlpha = this.alpha * radio; + + if(currentQuadSize < 0.001f || alpha < 0.001f) continue; + + Vec3 currentPos = headPos.add(dir.scale(-i * dist)); + + for(int j = 0; j < 4; j++) { + Vector3f vertex = new Vector3f(baseVertices[j]); + vertex.mul(currentQuadSize).add((float) currentPos.x, (float) currentPos.y, (float) currentPos.z); + + buffer.addVertex(vertex.x(), vertex.y(), vertex.z()) + .setUv(uvs[j][0], uvs[j][1]) + .setColor(this.rCol, this.gCol, this.bCol, currentAlpha) + .setLight(LightTexture.FULL_BRIGHT); + } + } + } + + @Override + public int getLightColor(float partialTick) { + return glow ? LightTexture.FULL_BRIGHT : super.getLightColor(partialTick); + } + + @Override + public ParticleRenderType getRenderType() { + return ModParticleRenderTypes.TRANSLUCENT_EMISSIVE; + } + + public static class Provider implements ParticleProvider { + private final SpriteSet spriteSet; + + public Provider(SpriteSet spriteSet) { + this.spriteSet = spriteSet; + } + + @Override + public Particle createParticle(ZenithTrailParticleOptions type, ClientLevel level, + double x, double y, double z, + double xSpeed, double ySpeed, double zSpeed) { + return new ZenithTrailParticle(level, x, y, z, xSpeed, ySpeed, zSpeed, type, this.spriteSet); + } + } +} diff --git a/src/main/java/com/lzxnone/terraria/particle/ZenithTrailParticleOptions.java b/src/main/java/com/lzxnone/terraria/particle/ZenithTrailParticleOptions.java new file mode 100644 index 0000000..62dbf52 --- /dev/null +++ b/src/main/java/com/lzxnone/terraria/particle/ZenithTrailParticleOptions.java @@ -0,0 +1,89 @@ +package com.lzxnone.terraria.particle; + +import com.mojang.serialization.Codec; +import com.mojang.serialization.MapCodec; +import com.mojang.serialization.codecs.RecordCodecBuilder; +import net.minecraft.core.particles.ParticleOptions; +import net.minecraft.core.particles.ParticleType; +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.codec.StreamCodec; +import org.joml.Vector3f; + +public class ZenithTrailParticleOptions implements ParticleOptions { + public final float initSize; + public final int initLifetime; + public final boolean glow; + public final Vector3f color; + public final Vector3f up; + public final Vector3f right; + public final int angle; + + public ZenithTrailParticleOptions(float initSize, int initLifetime, boolean glow, Vector3f color, Vector3f up, Vector3f right, int angle) { + this.initSize = initSize; + this.initLifetime = initLifetime; + this.glow = glow; + this.color = color; + this.up = up; + this.right = right; + this.angle = angle; + } + + public ZenithTrailParticleOptions() { + this(0.05f, 40, true, new Vector3f(1.0F, 1.0F, 1.0F), new Vector3f(1.0F, 0.0F, 0.0F), new Vector3f(0.0F, 1.0F, 0.0F), 0); + } + + @Override + public ParticleType getType() { + return ModParticles.ZENITH_TRAIL_PARTICLE.get(); + } + + private static final Codec COLOR_CODEC = + Codec.FLOAT.listOf().xmap( + l -> new Vector3f(l.get(0), l.get(1), l.get(2)), + c -> java.util.List.of(c.x, c.y, c.z) + ); + + public static final MapCodec CODEC = RecordCodecBuilder.mapCodec(instance -> + instance.group( + Codec.FLOAT.optionalFieldOf("initSize", 0.05f).forGetter(o -> o.initSize), + Codec.INT.optionalFieldOf("initLifetime", 40).forGetter(o -> o.initLifetime), + Codec.BOOL.optionalFieldOf("glow", true).forGetter(o -> o.glow), + COLOR_CODEC.optionalFieldOf("color", new Vector3f(1.0F, 1.0F, 1.0F)).forGetter(o -> o.color), + COLOR_CODEC.optionalFieldOf("up", new Vector3f(0.0F, 1.0F, 0.0F)).forGetter(o -> o.up), + COLOR_CODEC.optionalFieldOf("right", new Vector3f(1.0F, 0.0F, 0.0F)).forGetter(o -> o.right), + Codec.INT.optionalFieldOf("angle", 0).forGetter(o -> o.angle) + ).apply(instance, ZenithTrailParticleOptions::new) + ); + + public static final StreamCodec STREAM_CODEC = + new StreamCodec<>() { + @Override + public ZenithTrailParticleOptions decode(RegistryFriendlyByteBuf buf) { + float size = buf.readFloat(); + int lifetime = buf.readInt(); + boolean glowFlag = buf.readBoolean(); + Vector3f col = new Vector3f(buf.readFloat(), buf.readFloat(), buf.readFloat()); + Vector3f up = new Vector3f(buf.readFloat(), buf.readFloat(), buf.readFloat()); + Vector3f right = new Vector3f(buf.readFloat(), buf.readFloat(), buf.readFloat()); + int angle = buf.readInt(); + return new ZenithTrailParticleOptions(size, lifetime, glowFlag, col, up, right, angle); + } + + @Override + public void encode(RegistryFriendlyByteBuf buf, ZenithTrailParticleOptions opts) { + buf.writeFloat(opts.initSize); + buf.writeInt(opts.initLifetime); + buf.writeBoolean(opts.glow); + buf.writeFloat(opts.color.x); + buf.writeFloat(opts.color.y); + buf.writeFloat(opts.color.z); + buf.writeFloat(opts.up.x); + buf.writeFloat(opts.up.y); + buf.writeFloat(opts.up.z); + buf.writeFloat(opts.right.x); + buf.writeFloat(opts.right.y); + buf.writeFloat(opts.right.z); + buf.writeInt(opts.angle); + } + }; +} diff --git a/src/main/resources/assets/lzxnoneterraria/particles/zenith_trail_particle.json b/src/main/resources/assets/lzxnoneterraria/particles/zenith_trail_particle.json new file mode 100644 index 0000000..365dc70 --- /dev/null +++ b/src/main/resources/assets/lzxnoneterraria/particles/zenith_trail_particle.json @@ -0,0 +1,5 @@ +{ + "textures": [ + "lzxnoneterraria:zenith_trail_particle" + ] +} \ No newline at end of file diff --git a/src/main/resources/assets/lzxnoneterraria/textures/particle/zenith_trail_particle.png b/src/main/resources/assets/lzxnoneterraria/textures/particle/zenith_trail_particle.png new file mode 100644 index 0000000..de94daf Binary files /dev/null and b/src/main/resources/assets/lzxnoneterraria/textures/particle/zenith_trail_particle.png differ