From 14034464162daa0577205d73fa84b28ca8b2586a Mon Sep 17 00:00:00 2001 From: LZX <1820455493@qq.com> Date: Thu, 23 Jul 2026 23:49:39 +0800 Subject: [PATCH] v1.5.1 --- gradle.properties | 2 +- .../terraria/entity/beam/SwordBeam.java | 5 + .../entity/projectile/StaticProjectile.java | 5 + .../terraria/entity/summon/StaticSummon.java | 5 + .../terraria/event/ShaderRegistry.java | 15 +- .../terraria/renderer/OutlineRenderer.java | 195 ++++++++++++++++++ .../lzxnoneterraria/shaders/core/outline.fsh | 28 +++ .../lzxnoneterraria/shaders/core/outline.json | 16 ++ .../lzxnoneterraria/shaders/core/outline.vsh | 11 + 9 files changed, 280 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/lzxnone/terraria/renderer/OutlineRenderer.java create mode 100644 src/main/resources/assets/lzxnoneterraria/shaders/core/outline.fsh create mode 100644 src/main/resources/assets/lzxnoneterraria/shaders/core/outline.json create mode 100644 src/main/resources/assets/lzxnoneterraria/shaders/core/outline.vsh diff --git a/gradle.properties b/gradle.properties index b0dfd01..ee2bf37 100644 --- a/gradle.properties +++ b/gradle.properties @@ -34,7 +34,7 @@ mod_name=Lzxnone Terraria # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. mod_license=All Rights Reserved # The mod version. See https://semver.org/ -mod_version=1.5.0 +mod_version=1.5.1 ## The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # This should match the base package used for the mod sources. # See https://maven.apache.org/guides/mini/guide-naming-conventions.html diff --git a/src/main/java/com/lzxnone/terraria/entity/beam/SwordBeam.java b/src/main/java/com/lzxnone/terraria/entity/beam/SwordBeam.java index 308790d..21ee8bb 100644 --- a/src/main/java/com/lzxnone/terraria/entity/beam/SwordBeam.java +++ b/src/main/java/com/lzxnone/terraria/entity/beam/SwordBeam.java @@ -42,6 +42,8 @@ public class SwordBeam extends Entity { SynchedEntityData.defineId(SwordBeam.class, EntityDataSerializers.BOOLEAN); public static final EntityDataAccessor INFLATE = SynchedEntityData.defineId(SwordBeam.class, EntityDataSerializers.FLOAT); + public static final EntityDataAccessor OUTLINE = + SynchedEntityData.defineId(SwordBeam.class, EntityDataSerializers.BOOLEAN); public static final EntityDataAccessor AGE = SynchedEntityData.defineId(SwordBeam.class, EntityDataSerializers.INT); public static final EntityDataAccessor LIFETIME = @@ -194,6 +196,7 @@ public class SwordBeam extends Entity { builder.define(ROTATE, 0); builder.define(RIGHT, false); builder.define(INFLATE, 0.0f); + builder.define(OUTLINE, false); builder.define(COLOR0, new Vector3f(1.0f, 1.0f, 1.0f)); builder.define(COLOR1, new Vector3f(1.0f, 1.0f, 1.0f)); builder.define(COLOR2, new Vector3f(1.0f, 1.0f, 1.0f)); @@ -210,6 +213,7 @@ public class SwordBeam extends Entity { tag.putInt("rotate", this.entityData.get(ROTATE)); tag.putBoolean("right", this.entityData.get(RIGHT)); tag.putFloat("inflate", this.entityData.get(INFLATE)); + tag.putBoolean("outline", this.entityData.get(OUTLINE)); Vector3f c0 = this.entityData.get(COLOR0); tag.putFloat("color0R", c0.x); tag.putFloat("color0G", c0.y); @@ -238,6 +242,7 @@ public class SwordBeam extends Entity { if(tag.contains("rotate")) this.entityData.set(ROTATE, tag.getInt("rotate")); if(tag.contains("right")) this.entityData.set(RIGHT, tag.getBoolean("right")); if(tag.contains("inflate")) this.entityData.set(INFLATE, tag.getFloat("inflate")); + if(tag.contains("outline")) this.entityData.set(OUTLINE, tag.getBoolean("outline")); if(tag.contains("color0R")) { this.entityData.set(COLOR0, new Vector3f( tag.getFloat("color0R"), tag.getFloat("color0G"), tag.getFloat("color0B") diff --git a/src/main/java/com/lzxnone/terraria/entity/projectile/StaticProjectile.java b/src/main/java/com/lzxnone/terraria/entity/projectile/StaticProjectile.java index b66e0be..c04cbbe 100644 --- a/src/main/java/com/lzxnone/terraria/entity/projectile/StaticProjectile.java +++ b/src/main/java/com/lzxnone/terraria/entity/projectile/StaticProjectile.java @@ -66,6 +66,8 @@ public class StaticProjectile extends Projectile { SynchedEntityData.defineId(StaticProjectile.class, EntityDataSerializers.FLOAT); public static final EntityDataAccessor GLOW = SynchedEntityData.defineId(StaticProjectile.class, EntityDataSerializers.BOOLEAN); + public static final EntityDataAccessor OUTLINE = + SynchedEntityData.defineId(StaticProjectile.class, EntityDataSerializers.BOOLEAN); public static final EntityDataAccessor AGE = SynchedEntityData.defineId(StaticProjectile.class, EntityDataSerializers.INT); public static final EntityDataAccessor LIFETIME = @@ -126,6 +128,7 @@ public class StaticProjectile extends Projectile { builder.define(COLOR_B, 1.0f); builder.define(COLOR_A, 1.0f); builder.define(GLOW, false); + builder.define(OUTLINE, false); builder.define(AGE, 0); builder.define(LIFETIME, 60); builder.define(EXPRESSION_X, "0"); @@ -224,6 +227,7 @@ public class StaticProjectile extends Projectile { tag.putFloat("colorB", this.entityData.get(COLOR_B)); tag.putFloat("colorA", this.entityData.get(COLOR_A)); tag.putBoolean("glow", this.entityData.get(GLOW)); + tag.putBoolean("outline", this.entityData.get(OUTLINE)); tag.putInt("age", this.entityData.get(AGE)); tag.putInt("lifetime", this.entityData.get(LIFETIME)); tag.putString("expressionX", this.entityData.get(EXPRESSION_X)); @@ -281,6 +285,7 @@ public class StaticProjectile extends Projectile { if(tag.contains("colorB")) this.entityData.set(COLOR_B, tag.getFloat("colorB")); if(tag.contains("colorA")) this.entityData.set(COLOR_A, tag.getFloat("colorA")); if(tag.contains("glow")) this.entityData.set(GLOW, tag.getBoolean("glow")); + if(tag.contains("outline")) this.entityData.set(OUTLINE, tag.getBoolean("outline")); if(tag.contains("age")) this.entityData.set(AGE, tag.getInt("age")); if(tag.contains("lifetime")) this.entityData.set(LIFETIME, tag.getInt("lifetime")); if(tag.contains("expressionX")) this.entityData.set(EXPRESSION_X, tag.getString("expressionX")); diff --git a/src/main/java/com/lzxnone/terraria/entity/summon/StaticSummon.java b/src/main/java/com/lzxnone/terraria/entity/summon/StaticSummon.java index 0af6c03..e6fa45e 100644 --- a/src/main/java/com/lzxnone/terraria/entity/summon/StaticSummon.java +++ b/src/main/java/com/lzxnone/terraria/entity/summon/StaticSummon.java @@ -57,6 +57,8 @@ public class StaticSummon extends Entity { SynchedEntityData.defineId(StaticSummon.class, EntityDataSerializers.FLOAT); public static final EntityDataAccessor GLOW = SynchedEntityData.defineId(StaticSummon.class, EntityDataSerializers.BOOLEAN); + public static final EntityDataAccessor OUTLINE = + SynchedEntityData.defineId(StaticSummon.class, EntityDataSerializers.BOOLEAN); public static final EntityDataAccessor AGE = SynchedEntityData.defineId(StaticSummon.class, EntityDataSerializers.INT); public static final EntityDataAccessor LIFETIME = @@ -121,6 +123,7 @@ public class StaticSummon extends Entity { builder.define(COLOR_B, 1.0f); builder.define(COLOR_A, 1.0f); builder.define(GLOW, false); + builder.define(OUTLINE, false); builder.define(AGE, 0); builder.define(LIFETIME, 100); builder.define(OWNER, Optional.empty()); @@ -156,6 +159,7 @@ public class StaticSummon extends Entity { tag.putFloat("colorB", this.entityData.get(COLOR_B)); tag.putFloat("colorA", this.entityData.get(COLOR_A)); tag.putBoolean("glow", this.entityData.get(GLOW)); + tag.putBoolean("outline", this.entityData.get(OUTLINE)); tag.putInt("age", this.entityData.get(AGE)); tag.putInt("lifetime", this.entityData.get(LIFETIME)); if(getOwner() != null) tag.putUUID("owner", getOwner().getUUID()); @@ -191,6 +195,7 @@ public class StaticSummon extends Entity { if(tag.contains("colorB")) this.entityData.set(COLOR_B, tag.getFloat("colorB")); if(tag.contains("colorA")) this.entityData.set(COLOR_A, tag.getFloat("colorA")); if(tag.contains("glow")) this.entityData.set(GLOW, tag.getBoolean("glow")); + if(tag.contains("outline")) this.entityData.set(OUTLINE, tag.getBoolean("outline")); if(tag.contains("age")) this.entityData.set(AGE, tag.getInt("age")); if(tag.contains("lifetime")) this.entityData.set(LIFETIME, tag.getInt("lifetime")); if(tag.contains("owner")) { diff --git a/src/main/java/com/lzxnone/terraria/event/ShaderRegistry.java b/src/main/java/com/lzxnone/terraria/event/ShaderRegistry.java index c59e8aa..716f34c 100644 --- a/src/main/java/com/lzxnone/terraria/event/ShaderRegistry.java +++ b/src/main/java/com/lzxnone/terraria/event/ShaderRegistry.java @@ -15,6 +15,7 @@ import java.io.IOException; public class ShaderRegistry { private static ShaderInstance blur; private static ShaderInstance composite; + private static ShaderInstance outline; public static ShaderInstance getBlur() { return blur; @@ -24,6 +25,10 @@ public class ShaderRegistry { return composite; } + public static ShaderInstance getOutline() { + return outline; + } + @SubscribeEvent public static void registerShaders(RegisterShadersEvent event) { try { @@ -43,8 +48,16 @@ public class ShaderRegistry { ), shader -> composite = shader ); + event.registerShader( + new ShaderInstance( + event.getResourceProvider(), + ResourceLocation.fromNamespaceAndPath(LzxnoneTerraria.MODID, "outline"), + DefaultVertexFormat.POSITION_TEX + ), + shader -> outline = shader + ); } catch (IOException e) { throw new RuntimeException("Shader load error", e); } } -} \ No newline at end of file +} diff --git a/src/main/java/com/lzxnone/terraria/renderer/OutlineRenderer.java b/src/main/java/com/lzxnone/terraria/renderer/OutlineRenderer.java new file mode 100644 index 0000000..1556412 --- /dev/null +++ b/src/main/java/com/lzxnone/terraria/renderer/OutlineRenderer.java @@ -0,0 +1,195 @@ +package com.lzxnone.terraria.renderer; + +import com.lzxnone.terraria.LzxnoneTerraria; +import com.lzxnone.terraria.entity.IrisCompat; +import com.lzxnone.terraria.entity.beam.SwordBeam; +import com.lzxnone.terraria.entity.beam.SwordBeamBehaviors; +import com.lzxnone.terraria.entity.projectile.StaticProjectile; +import com.lzxnone.terraria.entity.projectile.StaticProjectileBehaviors; +import com.lzxnone.terraria.entity.summon.StaticSummon; +import com.lzxnone.terraria.entity.summon.StaticSummonBehaviors; +import com.lzxnone.terraria.event.ShaderRegistry; +import com.mojang.blaze3d.pipeline.RenderTarget; +import com.mojang.blaze3d.pipeline.TextureTarget; +import com.mojang.blaze3d.platform.GlStateManager; +import com.mojang.blaze3d.shaders.Uniform; +import com.mojang.blaze3d.systems.RenderSystem; +import com.mojang.blaze3d.vertex.*; +import net.minecraft.client.Camera; +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.LightTexture; +import net.minecraft.client.renderer.MultiBufferSource; +import net.minecraft.client.renderer.ShaderInstance; +import net.minecraft.util.Mth; +import net.minecraft.world.entity.Entity; +import net.neoforged.api.distmarker.Dist; +import net.neoforged.bus.api.SubscribeEvent; +import net.neoforged.fml.common.EventBusSubscriber; +import net.neoforged.neoforge.client.GlStateBackup; +import net.neoforged.neoforge.client.event.RenderLevelStageEvent; + +import java.util.Objects; + +@EventBusSubscriber(modid = LzxnoneTerraria.MODID, value = Dist.CLIENT) +public class OutlineRenderer { + private static final int GL_LINEAR = 9729; + + private static RenderTarget outlineTarget; + private static int lastW = -1; + private static int lastH = -1; + + @SubscribeEvent + public static void onRenderStage(RenderLevelStageEvent event) { + if(IrisCompat.isShaderPackInUse()) return; + if(ShaderRegistry.getOutline() == null) return; + + ensureTarget(); + if(outlineTarget == null) return; + + if(event.getStage() == RenderLevelStageEvent.Stage.AFTER_ENTITIES) { + onAfterEntities(event); + }else if(event.getStage() == RenderLevelStageEvent.Stage.AFTER_LEVEL) { + onAfterLevel(); + } + } + + private static void ensureTarget() { + RenderTarget main = Minecraft.getInstance().getMainRenderTarget(); + int w = Math.max(1, main.viewWidth); + int h = Math.max(1, main.viewHeight); + + if(w == lastW && h == lastH && outlineTarget != null) return; + + if(outlineTarget != null) outlineTarget.destroyBuffers(); + outlineTarget = new TextureTarget(w, h, true, false); + outlineTarget.setClearColor(0f, 0f, 0f, 0f); + outlineTarget.setFilterMode(GL_LINEAR); + lastW = w; + lastH = h; + } + + private static void onAfterEntities(RenderLevelStageEvent event) { + GlStateBackup backup = new GlStateBackup(); + RenderSystem.backupGlState(backup); + ShaderInstance prevShader = RenderSystem.getShader(); + int prevTex0 = RenderSystem.getShaderTexture(0); + + try { + RenderTarget main = Minecraft.getInstance().getMainRenderTarget(); + outlineTarget.clear(false); + outlineTarget.bindWrite(true); + outlineTarget.copyDepthFrom(main); + outlineTarget.bindWrite(true); + + renderOutlineEntities( + event.getPoseStack(), + event.getCamera(), + event.getPartialTick().getGameTimeDeltaPartialTick(true) + ); + }finally { + Minecraft.getInstance().getMainRenderTarget().bindWrite(true); + RenderSystem.setShaderTexture(0, prevTex0); + if(prevShader != null) { + RenderSystem.setShader(() -> prevShader); + } + RenderSystem.restoreGlState(backup); + } + } + + private static void onAfterLevel() { + GlStateBackup backup = new GlStateBackup(); + RenderSystem.backupGlState(backup); + ShaderInstance prevShader = RenderSystem.getShader(); + int prevTex0 = RenderSystem.getShaderTexture(0); + + try { + RenderTarget main = Minecraft.getInstance().getMainRenderTarget(); + main.bindWrite(true); + + ShaderInstance shader = ShaderRegistry.getOutline(); + RenderSystem.setShader(() -> shader); + RenderSystem.setShaderTexture(0, outlineTarget.getColorTextureId()); + + Uniform texelSize = shader.getUniform("TexelSize"); + if(texelSize != null) { + texelSize.set(1.0f / outlineTarget.viewWidth, 1.0f / outlineTarget.viewHeight); + } + Uniform outlineColor = shader.getUniform("OutlineColor"); + if(outlineColor != null) { + outlineColor.set(0.8f, 0.176f, 0.78f, 1.0f); + } + Uniform radius = shader.getUniform("Radius"); + if(radius != null) { + radius.set(2.0f); + } + + RenderSystem.enableBlend(); + RenderSystem.blendFunc( + GlStateManager.SourceFactor.SRC_ALPHA, + GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA + ); + RenderSystem.disableDepthTest(); + RenderSystem.depthMask(false); + drawFullscreen(); + }finally { + RenderSystem.setShaderTexture(0, prevTex0); + if(prevShader != null) { + RenderSystem.setShader(() -> prevShader); + } + RenderSystem.restoreGlState(backup); + } + } + + private static void renderOutlineEntities(PoseStack poseStack, Camera camera, float partialTick) { + Minecraft mc = Minecraft.getInstance(); + if(mc.level == null) return; + + try(ByteBufferBuilder byteBuf = new ByteBufferBuilder(65536)) { + MultiBufferSource.BufferSource bufferSource = MultiBufferSource.immediate(byteBuf); + for(Entity entity : mc.level.entitiesForRendering()) { + double x = Mth.lerp(partialTick, entity.xo, entity.getX()); + double y = Mth.lerp(partialTick, entity.yo, entity.getY()); + double z = Mth.lerp(partialTick, entity.zo, entity.getZ()); + float entityYaw = Mth.lerp(partialTick, entity.yRotO, entity.getYRot()); + + double camX = x - camera.getPosition().x(); + double camY = y - camera.getPosition().y(); + double camZ = z - camera.getPosition().z(); + + if(entity instanceof StaticProjectile projectile + && projectile.getEntityData().get(StaticProjectile.OUTLINE)) { + poseStack.pushPose(); + poseStack.translate(camX, camY, camZ); + StaticProjectileBehaviors.getBehavior(projectile.getEntityData().get(StaticProjectile.BEHAVIOR)) + .render(projectile, entityYaw, partialTick, poseStack, bufferSource, LightTexture.FULL_BRIGHT); + poseStack.popPose(); + }else if(entity instanceof StaticSummon summon + && summon.getEntityData().get(StaticSummon.OUTLINE)) { + poseStack.pushPose(); + poseStack.translate(camX, camY, camZ); + StaticSummonBehaviors.getBehavior(summon.getEntityData().get(StaticSummon.BEHAVIOR)) + .render(summon, entityYaw, partialTick, poseStack, bufferSource, LightTexture.FULL_BRIGHT); + poseStack.popPose(); + }else if(entity instanceof SwordBeam beam + && beam.getEntityData().get(SwordBeam.OUTLINE)) { + poseStack.pushPose(); + poseStack.translate(camX, camY, camZ); + SwordBeamBehaviors.getBehavior(beam.getEntityData().get(SwordBeam.BEHAVIOR)) + .render(beam, entityYaw, partialTick, poseStack, bufferSource, LightTexture.FULL_BRIGHT); + poseStack.popPose(); + } + } + bufferSource.endBatch(); + } + } + + private static void drawFullscreen() { + BufferBuilder bb = RenderSystem.renderThreadTesselator().begin( + VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX); + bb.addVertex(-1f, -1f, 0f).setUv(0f, 0f); + bb.addVertex(1f, -1f, 0f).setUv(1f, 0f); + bb.addVertex(1f, 1f, 0f).setUv(1f, 1f); + bb.addVertex(-1f, 1f, 0f).setUv(0f, 1f); + BufferUploader.drawWithShader(Objects.requireNonNull(bb.build())); + } +} diff --git a/src/main/resources/assets/lzxnoneterraria/shaders/core/outline.fsh b/src/main/resources/assets/lzxnoneterraria/shaders/core/outline.fsh new file mode 100644 index 0000000..141bac3 --- /dev/null +++ b/src/main/resources/assets/lzxnoneterraria/shaders/core/outline.fsh @@ -0,0 +1,28 @@ +#version 150 + +in vec2 texCoord0; + +uniform sampler2D Sampler0; +uniform vec2 TexelSize; +uniform vec4 OutlineColor; +uniform float Radius; + +out vec4 fragColor; + +void main() { + float center = texture(Sampler0, texCoord0).a; + float around = 0.0; + vec2 px = TexelSize * Radius; + + around = max(around, texture(Sampler0, texCoord0 + vec2(px.x, 0.0)).a); + around = max(around, texture(Sampler0, texCoord0 - vec2(px.x, 0.0)).a); + around = max(around, texture(Sampler0, texCoord0 + vec2(0.0, px.y)).a); + around = max(around, texture(Sampler0, texCoord0 - vec2(0.0, px.y)).a); + around = max(around, texture(Sampler0, texCoord0 + vec2(px.x, px.y)).a); + around = max(around, texture(Sampler0, texCoord0 + vec2(px.x, -px.y)).a); + around = max(around, texture(Sampler0, texCoord0 + vec2(-px.x, px.y)).a); + around = max(around, texture(Sampler0, texCoord0 - vec2(px.x, px.y)).a); + + float outline = max(around - center, 0.0); + fragColor = vec4(OutlineColor.rgb, outline * OutlineColor.a); +} diff --git a/src/main/resources/assets/lzxnoneterraria/shaders/core/outline.json b/src/main/resources/assets/lzxnoneterraria/shaders/core/outline.json new file mode 100644 index 0000000..4b2aad1 --- /dev/null +++ b/src/main/resources/assets/lzxnoneterraria/shaders/core/outline.json @@ -0,0 +1,16 @@ +{ + "vertex": "lzxnoneterraria:outline", + "fragment": "lzxnoneterraria:outline", + "samplers": [ + { "name": "Sampler0" } + ], + "attributes": [ + "Position", + "UV0" + ], + "uniforms": [ + { "name": "TexelSize", "type": "float", "count": 2, "values": [ 0.001, 0.001 ] }, + { "name": "OutlineColor", "type": "float", "count": 4, "values": [ 0.8, 0.176, 0.78, 1.0 ] }, + { "name": "Radius", "type": "float", "count": 1, "values": [ 2.0 ] } + ] +} diff --git a/src/main/resources/assets/lzxnoneterraria/shaders/core/outline.vsh b/src/main/resources/assets/lzxnoneterraria/shaders/core/outline.vsh new file mode 100644 index 0000000..007d9a0 --- /dev/null +++ b/src/main/resources/assets/lzxnoneterraria/shaders/core/outline.vsh @@ -0,0 +1,11 @@ +#version 150 + +in vec3 Position; +in vec2 UV0; + +out vec2 texCoord0; + +void main() { + gl_Position = vec4(Position, 1.0); + texCoord0 = UV0; +}