This commit is contained in:
LZX committed 2026-08-24 00:34:17 +08:00
1 parent 94e10c465f
commit 53e12d46df
5 files changed
+377 -352

No files matched your search

@@ -78,7 +78,7 @@ public class ShaderRegistry {
new ShaderInstance( new ShaderInstance(
event.getResourceProvider(), event.getResourceProvider(),
ResourceLocation.fromNamespaceAndPath(LzxnoneTerraria.MODID, "devils_devastation_energy"), ResourceLocation.fromNamespaceAndPath(LzxnoneTerraria.MODID, "devils_devastation_energy"),
DefaultVertexFormat.NEW_ENTITY DefaultVertexFormat.POSITION_COLOR
), ),
shader -> devilsDevastationEnergy = shader shader -> devilsDevastationEnergy = shader
); );
@@ -452,13 +452,17 @@ public class ClientDevilsDevastation {
.setNormal(0.0F, 1.0F, 0.0F); .setNormal(0.0F, 1.0F, 0.0F);
} }
//采样空间(光线步进长方体):x 厚度 ±0.25、y 长 0→32、z 宽 ±1 //三棱柱采样空间:沿 X 轴挤出,YZ 平面是从剑根线性收窄到剑尖的等腰三角形。
private static final float BLADE_MIN_X = -0.25f; private static final float BLADE_HALF_THICKNESS = 0.05f;
private static final float BLADE_MAX_X = 0.25f; private static final float BLADE_LENGTH = 32.0f;
private static final float BLADE_MIN_Y = 0.0f; private static final float BLADE_ROOT_HALF_WIDTH = 1.0f;
private static final float BLADE_MAX_Y = 32.0f; private static final float BLADE_MAX_OUTLINE_PROTRUSION = BLADE_ROOT_HALF_WIDTH * 2.0f * 0.15f;
private static final float BLADE_MIN_Z = -1.0f; private static final float BLADE_COLOR_TO_CORE_THICKNESS_RATIO = 1.0f / 3.0f;
private static final float BLADE_MAX_Z = 1.0f; private static final float BLADE_AURA_WIDTH = 0.022f;
private static final float BLADE_EXTINCTION = 4.0f;
private static final Vector3f BLADE_EDGE_COLOR_0 = new Vector3f(0.725f, 0.345f, 1.0f);
private static final Vector3f BLADE_EDGE_COLOR_1 = new Vector3f(0.847f, 0.247f, 0.745f);
private static final Vector3f BLADE_EDGE_COLOR_2 = new Vector3f(0.882f, 0.345f, 0.247f);
public static void renderEnergyWave(MultiBufferSource buffer, PoseStack poseStack, Entity entity) { public static void renderEnergyWave(MultiBufferSource buffer, PoseStack poseStack, Entity entity) {
ShaderInstance shader = ShaderRegistry.getDevilsDevastationEnergy(); ShaderInstance shader = ShaderRegistry.getDevilsDevastationEnergy();
@@ -481,14 +485,47 @@ public class ClientDevilsDevastation {
//注入 uniform //注入 uniform
Uniform modelMat = shader.getUniform("ModelMat"); Uniform modelMat = shader.getUniform("ModelMat");
if (modelMat != null) modelMat.set(modelMatrix); if (modelMat != null) modelMat.set(modelMatrix);
Uniform gameTime = shader.getUniform("EffectTime"); Uniform cameraLocalUniform = shader.getUniform("CameraLocal");
if (gameTime != null) gameTime.set((float) entity.tickCount); if (cameraLocalUniform != null) {
Uniform colorA = shader.getUniform("ColorA"); Vector3f cameraLocal = new Matrix4f(RenderSystem.getModelViewMatrix())
if (colorA != null) colorA.set(0.729f, 0.396f, 0.345f, 1.0f); .mul(modelMatrix)
Uniform colorB = shader.getUniform("ColorB"); .invert()
if (colorB != null) colorB.set(0.8f, 0.176f, 0.78f, 1.0f); .transformPosition(new Vector3f());
cameraLocalUniform.set(cameraLocal.x, cameraLocal.y, cameraLocal.z);
}
Uniform bladeDimensionsUniform = shader.getUniform("BladeDimensions");
if (bladeDimensionsUniform != null) {
bladeDimensionsUniform.set(BLADE_HALF_THICKNESS, BLADE_LENGTH, BLADE_ROOT_HALF_WIDTH);
}
Uniform volumeParametersUniform = shader.getUniform("VolumeParameters");
if (volumeParametersUniform != null) {
volumeParametersUniform.set(
BLADE_MAX_OUTLINE_PROTRUSION,
BLADE_COLOR_TO_CORE_THICKNESS_RATIO,
BLADE_AURA_WIDTH,
BLADE_EXTINCTION
);
}
Uniform flowTimeUniform = shader.getUniform("FlowTime");
if (flowTimeUniform != null) {
float flowTime = entity.tickCount
+ Minecraft.getInstance().getTimer().getGameTimeDeltaPartialTick(false);
flowTimeUniform.set(flowTime);
}
Uniform edgeColor0Uniform = shader.getUniform("EdgeColor0");
if (edgeColor0Uniform != null) {
edgeColor0Uniform.set(BLADE_EDGE_COLOR_0.x, BLADE_EDGE_COLOR_0.y, BLADE_EDGE_COLOR_0.z);
}
Uniform edgeColor1Uniform = shader.getUniform("EdgeColor1");
if (edgeColor1Uniform != null) {
edgeColor1Uniform.set(BLADE_EDGE_COLOR_1.x, BLADE_EDGE_COLOR_1.y, BLADE_EDGE_COLOR_1.z);
}
Uniform edgeColor2Uniform = shader.getUniform("EdgeColor2");
if (edgeColor2Uniform != null) {
edgeColor2Uniform.set(BLADE_EDGE_COLOR_2.x, BLADE_EDGE_COLOR_2.y, BLADE_EDGE_COLOR_2.z);
}
//GL 状态:加法混合 + 深度测试 + 背面剔除(代理盒只需朝向相机的面) //GL 状态:加法混合 + 深度测试 + 背面剔除(代理三棱柱只需朝向相机的面)
RenderSystem.setShader(() -> shader); RenderSystem.setShader(() -> shader);
RenderSystem.enableBlend(); RenderSystem.enableBlend();
RenderSystem.blendFunc( RenderSystem.blendFunc(
@@ -499,19 +536,60 @@ public class ClientDevilsDevastation {
RenderSystem.depthMask(true); RenderSystem.depthMask(true);
RenderSystem.enableCull(); RenderSystem.enableCull();
//加法混合下重复绘制 3 次 = 亮度叠加,补偿 LDR 无 bloom 时的亮度损失 BufferBuilder bb = RenderSystem.renderThreadTesselator().begin(
for (int pass = 0; pass < 3; pass++) { VertexFormat.Mode.TRIANGLES,
BufferBuilder bb = RenderSystem.renderThreadTesselator().begin( DefaultVertexFormat.POSITION_COLOR
VertexFormat.Mode.QUADS, );
DefaultVertexFormat.NEW_ENTITY
); float auraPadding = BLADE_AURA_WIDTH * 3.0f;
addBox( float faceAmplitude = BLADE_HALF_THICKNESS * 0.5f;
bb, float deformedRootHalfWidth = BLADE_ROOT_HALF_WIDTH + BLADE_MAX_OUTLINE_PROTRUSION;
BLADE_MIN_X, BLADE_MIN_Y, BLADE_MIN_Z, float proxyHalfThickness = BLADE_HALF_THICKNESS + faceAmplitude + auraPadding;
BLADE_MAX_X, BLADE_MAX_Y, BLADE_MAX_Z float proxyRootHalfWidth = deformedRootHalfWidth + auraPadding;
); float proxyLength = BLADE_LENGTH * proxyRootHalfWidth / deformedRootHalfWidth;
BufferUploader.drawWithShader(Objects.requireNonNull(bb.build()));
} float x0 = -proxyHalfThickness;
float x1 = proxyHalfThickness;
float y0 = 0.0f;
float y1 = proxyLength;
float z0 = -proxyRootHalfWidth;
float z1 = proxyRootHalfWidth;
// -X 三角面
addEnergyVertex(bb, x0, y0, z0);
addEnergyVertex(bb, x0, y0, z1);
addEnergyVertex(bb, x0, y1, 0.0f);
// +X 三角面
addEnergyVertex(bb, x1, y0, z0);
addEnergyVertex(bb, x1, y1, 0.0f);
addEnergyVertex(bb, x1, y0, z1);
// 剑根矩形面(-Y),拆成两个三角形
addEnergyVertex(bb, x0, y0, z0);
addEnergyVertex(bb, x1, y0, z0);
addEnergyVertex(bb, x1, y0, z1);
addEnergyVertex(bb, x0, y0, z0);
addEnergyVertex(bb, x1, y0, z1);
addEnergyVertex(bb, x0, y0, z1);
// -Z 斜面,拆成两个三角形
addEnergyVertex(bb, x0, y0, z0);
addEnergyVertex(bb, x0, y1, 0.0f);
addEnergyVertex(bb, x1, y1, 0.0f);
addEnergyVertex(bb, x0, y0, z0);
addEnergyVertex(bb, x1, y1, 0.0f);
addEnergyVertex(bb, x1, y0, z0);
// +Z 斜面,拆成两个三角形
addEnergyVertex(bb, x0, y0, z1);
addEnergyVertex(bb, x1, y0, z1);
addEnergyVertex(bb, x1, y1, 0.0f);
addEnergyVertex(bb, x0, y0, z1);
addEnergyVertex(bb, x1, y1, 0.0f);
addEnergyVertex(bb, x0, y1, 0.0f);
BufferUploader.drawWithShader(Objects.requireNonNull(bb.build()));
} finally { } finally {
poseStack.popPose(); poseStack.popPose();
RenderSystem.depthMask(true); RenderSystem.depthMask(true);
@@ -525,90 +603,11 @@ public class ClientDevilsDevastation {
} }
} }
private static void addBox( private static void addEnergyVertex(
BufferBuilder bb, BufferBuilder bb,
float minX, float minY, float minZ, float x, float y, float z
float maxX, float maxY, float maxZ
) {
// +X
addQuad(bb,
maxX, minY, minZ,
maxX, maxY, minZ,
maxX, maxY, maxZ,
maxX, minY, maxZ,
1, 0, 0
);
// -X
addQuad(bb,
minX, minY, maxZ,
minX, maxY, maxZ,
minX, maxY, minZ,
minX, minY, minZ,
-1, 0, 0
);
// +Y
addQuad(bb,
minX, maxY, maxZ,
maxX, maxY, maxZ,
maxX, maxY, minZ,
minX, maxY, minZ,
0, 1, 0
);
// -Y
addQuad(bb,
minX, minY, minZ,
maxX, minY, minZ,
maxX, minY, maxZ,
minX, minY, maxZ,
0, -1, 0
);
// +Z
addQuad(bb,
minX, minY, maxZ,
maxX, minY, maxZ,
maxX, maxY, maxZ,
minX, maxY, maxZ,
0, 0, 1
);
// -Z
addQuad(bb,
maxX, minY, minZ,
minX, minY, minZ,
minX, maxY, minZ,
maxX, maxY, minZ,
0, 0, -1
);
}
private static void addQuad(
BufferBuilder bb,
float x0, float y0, float z0,
float x1, float y1, float z1,
float x2, float y2, float z2,
float x3, float y3, float z3,
float nx, float ny, float nz
) {
addVertex(bb, x0, y0, z0, nx, ny, nz);
addVertex(bb, x1, y1, z1, nx, ny, nz);
addVertex(bb, x2, y2, z2, nx, ny, nz);
addVertex(bb, x3, y3, z3, nx, ny, nz);
}
private static void addVertex(
BufferBuilder bb,
float x, float y, float z,
float nx, float ny, float nz
) { ) {
bb.addVertex(x, y, z) bb.addVertex(x, y, z)
.setColor(255, 255, 255, 255) .setColor(255, 255, 255, 255);
.setUv(0, 0)
.setUv1(0, 0)
.setUv2(0, 0)
.setNormal(nx, ny, nz);
} }
} }
@@ -1,258 +1,275 @@
#version 150 #version 150
in vec3 vLocalPos; in vec3 vLocalPos;
flat in vec3 vCameraLocal;
in vec4 vColor; in vec4 vColor;
out vec4 fragColor; out vec4 fragColor;
uniform float EffectTime; uniform vec3 CameraLocal;
uniform vec4 ColorA; uniform vec3 BladeDimensions;
uniform vec4 ColorB; // x=剑根最大轮廓外凸,y=单侧彩层厚度/核心完整厚度,z=外焰宽度,w=消光系数
uniform vec4 VolumeParameters;
uniform vec3 EdgeColor0;
uniform vec3 EdgeColor1;
uniform vec3 EdgeColor2;
uniform float FlowTime;
const vec3 BOX_MIN = vec3(-0.25, 0.0, -1.0); const int VOLUME_STEPS = 32;
const vec3 BOX_MAX = vec3( 0.25, 32.0, 1.0); const float EDGE_SOFTNESS = 0.003;
const float AURA_DENSITY = 0.40;
const float CORE_DENSITY_BOOST = 18.0;
const float CORE_BRIGHTNESS = 2.5;
const float CORE_LENGTH_RATIO = 0.90;
const float FACE_AMPLITUDE_RATIO = 0.5;
const float PARALLEL_EPSILON = 0.00001;
vec3 mod289(vec3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; } // 使用半空间 n·p <= d 裁剪射线,逐平面收紧进入和离开距离。
vec4 mod289(vec4 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; } bool clipHalfSpace(
vec4 permute(vec4 x) { return mod289(((x * 34.0) + 1.0) * x); } vec3 rayOrigin,
vec4 taylorInvSqrt(vec4 r) { return 1.79284291400159 - 0.85373472095314 * r; } vec3 rayDirection,
vec3 normal,
float distance,
inout float tEnter,
inout float tExit
) {
float denominator = dot(normal, rayDirection);
float numerator = distance - dot(normal, rayOrigin);
float snoise(vec3 v) { if (abs(denominator) < PARALLEL_EPSILON) {
const vec2 C = vec2(1.0 / 6.0, 1.0 / 3.0); return numerator >= 0.0;
const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);
vec3 i = floor(v + dot(v, C.yyy));
vec3 x0 = v - i + dot(i, C.xxx);
vec3 g = step(x0.yzx, x0.xyz);
vec3 l = 1.0 - g;
vec3 i1 = min(g.xyz, l.zxy);
vec3 i2 = max(g.xyz, l.zxy);
vec3 x1 = x0 - i1 + C.xxx;
vec3 x2 = x0 - i2 + C.yyy;
vec3 x3 = x0 - D.yyy;
i = mod289(i);
vec4 p = permute(permute(permute(
i.z + vec4(0.0, i1.z, i2.z, 1.0))
+ i.y + vec4(0.0, i1.y, i2.y, 1.0))
+ i.x + vec4(0.0, i1.x, i2.x, 1.0));
float n_ = 0.142857142857;
vec3 ns = n_ * D.wyz - D.xzx;
vec4 j = p - 49.0 * floor(p * ns.z * ns.z);
vec4 x_ = floor(j * ns.z);
vec4 y_ = floor(j - 7.0 * x_);
vec4 x = x_ * ns.x + ns.yyyy;
vec4 y = y_ * ns.x + ns.yyyy;
vec4 h = 1.0 - abs(x) - abs(y);
vec4 b0 = vec4(x.xy, y.xy);
vec4 b1 = vec4(x.zw, y.zw);
vec4 s0 = floor(b0) * 2.0 + 1.0;
vec4 s1 = floor(b1) * 2.0 + 1.0;
vec4 sh = -step(h, vec4(0.0));
vec4 a0 = b0.xzyw + s0.xzyw * sh.xxyy;
vec4 a1 = b1.xzyw + s1.xzyw * sh.zzww;
vec3 p0 = vec3(a0.xy, h.x);
vec3 p1 = vec3(a0.zw, h.y);
vec3 p2 = vec3(a1.xy, h.z);
vec3 p3 = vec3(a1.zw, h.w);
vec4 norm = taylorInvSqrt(vec4(dot(p0, p0), dot(p1, p1), dot(p2, p2), dot(p3, p3)));
p0 *= norm.x; p1 *= norm.y; p2 *= norm.z; p3 *= norm.w;
vec4 m = max(0.6 - vec4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0);
m = m * m;
return 42.0 * dot(m * m, vec4(dot(p0, x0), dot(p1, x1), dot(p2, x2), dot(p3, x3)));
}
float fbm3(vec3 p) {
float value = 0.0;
float amplitude = 0.5;
// 每层旋转坐标,打破各八度格点对齐(消除 tile 拼接感)
mat3 rot = mat3(
0.866, -0.5, 0.0,
0.5, 0.866, 0.0,
0.0, 0.0, 1.0
);
for (int i = 0; i < 3; i++) {
value += snoise(p) * amplitude;
p = rot * p * 2.03 + vec3(17.1, 9.2, 13.7);
amplitude *= 0.5;
} }
return value; float t = numerator / denominator;
if (denominator < 0.0) {
tEnter = max(tEnter, t);
} else {
tExit = min(tExit, t);
}
return tEnter <= tExit;
} }
float safeComponent(float x) { vec2 intersectTriangularPrism(
if (abs(x) > 0.00001) return x; vec3 rayOrigin,
return x < 0.0 ? -0.00001 : 0.00001; vec3 rayDirection,
} vec3 dimensions
) {
float tEnter = -1.0e20;
float tExit = 1.0e20;
float halfThickness = dimensions.x;
float bladeLength = dimensions.y;
float rootHalfWidth = dimensions.z;
float bladeSlope = rootHalfWidth / bladeLength;
vec2 intersectBox(vec3 ro, vec3 rd) { if (!clipHalfSpace(rayOrigin, rayDirection, vec3( 1.0, 0.0, 0.0), halfThickness, tEnter, tExit)) return vec2(1.0, -1.0);
vec3 safeRd = vec3( if (!clipHalfSpace(rayOrigin, rayDirection, vec3(-1.0, 0.0, 0.0), halfThickness, tEnter, tExit)) return vec2(1.0, -1.0);
safeComponent(rd.x), if (!clipHalfSpace(rayOrigin, rayDirection, vec3( 0.0, -1.0, 0.0), 0.0, tEnter, tExit)) return vec2(1.0, -1.0);
safeComponent(rd.y), if (!clipHalfSpace(rayOrigin, rayDirection, vec3(0.0, bladeSlope, 1.0), rootHalfWidth, tEnter, tExit)) return vec2(1.0, -1.0);
safeComponent(rd.z) if (!clipHalfSpace(rayOrigin, rayDirection, vec3(0.0, bladeSlope, -1.0), rootHalfWidth, tEnter, tExit)) return vec2(1.0, -1.0);
);
vec3 inv = 1.0 / safeRd;
vec3 t0 = (BOX_MIN - ro) * inv;
vec3 t1 = (BOX_MAX - ro) * inv;
vec3 nearT = min(t0, t1);
vec3 farT = max(t0, t1);
float tEnter = max(max(nearT.x, nearT.y), nearT.z);
float tExit = min(min(farT.x, farT.y), farT.z);
return vec2(tEnter, tExit); return vec2(tEnter, tExit);
} }
float densityField(vec3 p, float time) { // 无三角函数的确定性伪随机 Hash。
// 转换成统一的归一化 Sword Space。 float hash31(vec3 p) {
float nx = p.x / 0.25; p = fract(p * 0.1031);
float ny = p.y / 32.0; p += dot(p, p.yzx + 33.33);
float nz = p.z; return fract((p.x + p.y) * p.z);
// 顶部收窄:z 宽度随 ny 减小到接近 0(剑尖锥形)
float tip = smoothstep(1.0, 0.5, ny);
float zHalf = 0.06 + 0.94 * tip;
nz = p.z / zHalf;
// 横截面中心更亮,靠近 X/Z 边界逐渐减弱。
// 边界阈值随噪声波动 → 边缘不规则撕裂(能量溢出感)
float edgeNoise = fbm3(vec3(ny * 22.0, time * 4.0, 0.0)) * 0.5 + 0.5;
float edgeX = 1.0 - smoothstep(0.55 + edgeNoise * 0.35, 1.05 + edgeNoise * 0.35, abs(nx));
float edgeZ = 1.0 - smoothstep(0.55 + edgeNoise * 0.35, 1.05 + edgeNoise * 0.35, abs(nz));
float volumeMask = edgeX * edgeZ;
// 根部与剑尖稍微渐隐。
volumeMask *= smoothstep(0.0, 0.02, ny);
volumeMask *= 1.0 - smoothstep(0.96, 1.0, ny);
// 三维噪声沿 Y 轴流动(加快速度)。
vec3 flowPos = vec3(
nx * 3.5,
ny * 18.0 - time * 4.0,
nz * 3.5
);
// domain warping:单次 fbm 驱动三个轴向,减少噪声采样开销(原来是 3 次独立 fbm)
float warp = fbm3(flowPos + vec3(0.0, 0.0, time * 0.4)) - 0.5;
flowPos += vec3(warp, warp * 0.7, warp * 0.5) * 0.65;
// snoise 输出范围 -1~1,重映射到 0~1 再参与密度(与旧 value noise 语义一致)
float n1 = fbm3(flowPos) * 0.5 + 0.5;
float n2 = fbm3(vec3(
nx * 6.0 + 7.3,
ny * 11.0 - time * 2.6,
nz * 6.0 - 4.7
)) * 0.5 + 0.5;
// 明显的纵向能量束。
float stream1 = 0.5 + 0.5 * sin(
ny * 95.0 - time * 16.0 + n1 * 10.0 + nz * 5.0
);
float stream2 = 0.5 + 0.5 * sin(
ny * 143.0 - time * 22.0 - n2 * 13.0 + nx * 6.0
);
stream1 = pow(stream1, 7.0);
stream2 = pow(stream2, 10.0);
// 中央能量核。
float radial = length(vec2(nx, nz));
float core = 1.0 - smoothstep(0.05, 0.75, radial);
// 溢出细丝:边界附近的高频刺状丝,断续随机,随时间沿剑身流动
float filament = fbm3(vec3(nx * 8.0, ny * 36.0, nz * 8.0) + vec3(0.0, time * 5.0, 0.0)) * 0.5 + 0.5;
float edgeDist = min(1.0 - abs(nx), 1.0 - abs(nz)); // 距边界(内部)距离,0=边界
float leak = step(0.74, filament) * smoothstep(0.12, 0.0, edgeDist);
float density =
n1 * 0.30 +
stream1 * 0.85 +
stream2 * 0.55 +
core * 0.65;
// 细丝顶破表面(不乘 volumeMask,靠 slab 限制在盒内,边缘处 mask 已衰减由丝接管)
density = max(density, leak * 1.4);
density = smoothstep(0.30, 1.15, density);
// leak 必须绕过 volumeMask(边界处 mask≈0 会把它乘没),靠 slab 限制在盒内
return max(density * volumeMask, leak);
} }
vec3 densityColor(vec3 p, float density) { // 对格点随机值作三线性平滑插值,得到空间连续的 Value Noise。
float alongBlade = clamp(p.y / 32.0, 0.0, 1.0); float valueNoise(vec3 p) {
vec3 cell = floor(p);
vec3 local = fract(p);
vec3 weight = local * local * (3.0 - 2.0 * local);
vec3 base = mix(ColorA.rgb, ColorB.rgb, alongBlade); float n000 = hash31(cell + vec3(0.0, 0.0, 0.0));
float n100 = hash31(cell + vec3(1.0, 0.0, 0.0));
float n010 = hash31(cell + vec3(0.0, 1.0, 0.0));
float n110 = hash31(cell + vec3(1.0, 1.0, 0.0));
float n001 = hash31(cell + vec3(0.0, 0.0, 1.0));
float n101 = hash31(cell + vec3(1.0, 0.0, 1.0));
float n011 = hash31(cell + vec3(0.0, 1.0, 1.0));
float n111 = hash31(cell + vec3(1.0, 1.0, 1.0));
float nx = p.x / 0.25; float z0 = mix(
float nz = p.z; mix(n000, n100, weight.x),
float core = 1.0 - smoothstep(0.0, 0.65, length(vec2(nx, nz))); mix(n010, n110, weight.x),
weight.y
);
float z1 = mix(
mix(n001, n101, weight.x),
mix(n011, n111, weight.x),
weight.y
);
return mix(z0, z1, weight.z);
}
// 核心更白热:平方增强 + 高权重,中间不再是淡色 float surfaceNoise(vec3 p) {
vec3 hot = mix(base, vec3(1.0, 0.85, 1.0), pow(core, 1.5) * 0.85); // 将正反表面作为 YZ 高度场采样,避免噪声沿射线深度变化后被体积积分平均掉。
vec3 noisePosition = vec3(
p.y / max(BladeDimensions.y, 0.0001) * 12.0,
p.z / max(BladeDimensions.z, 0.0001) * 2.6,
0.0
);
float flowPhase = FlowTime * 0.18;
// 整体提亮 + 核心区额外增益(加法混合下超 1 的亮度会喂给 bloom) // 两层噪声主要沿 +Y(剑根到剑尖)流动,并带少量横向漂移。
float brighten = 1.0 + core * 0.9; // 细节层的位移约为粗糙层的 2.03 倍,使两层具有相近的实际流速。
return hot * (1.1 + density * 2.4) * brighten; float coarse = valueNoise(
noisePosition + vec3(-flowPhase * 1.35, flowPhase * 0.11, flowPhase * 0.18)
);
float detail = valueNoise(
noisePosition * 2.03
+ vec3(17.1, 9.2, 13.7)
+ vec3(-flowPhase * 2.74, flowPhase * 0.23, -flowPhase * 0.27)
);
float centered = (coarse * 0.72 + detail * 0.28) * 2.0 - 1.0;
return clamp(centered * 2.2, -1.0, 1.0);
}
float sharedOutlineDisplacement(vec3 p, float y01) {
// 剑根端点保持闭合;剑尖处由局部宽度自然把轮廓幅度收敛到 0。
float rootMask = smoothstep(0.0, 0.025, y01);
float localHalfWidth = BladeDimensions.z * max(1.0 - y01, 0.0);
float localWidthRatio = localHalfWidth / max(BladeDimensions.z, 0.0001);
// 左右侧分别采样边界上的流动噪声。最大外凸随当地剑宽同比缩放:
// 剑根为完整宽度的 1/3,之后一直保持相同的相对比例。
float sideSign = p.z < 0.0 ? -1.0 : 1.0;
vec3 outlineSamplePosition = vec3(0.0, p.y, sideSign * localHalfWidth);
float outlineNoise = surfaceNoise(outlineSamplePosition);
float localMaxOutward = VolumeParameters.x * localWidthRatio;
float localMaxInward = localHalfWidth * 0.20;
float outlineDisplacement = outlineNoise >= 0.0
? pow(max(outlineNoise, 0.0), 0.70) * localMaxOutward
: outlineNoise * localMaxInward;
return outlineDisplacement * rootMask;
}
void evaluateBladeFields(vec3 p, out float outerField, out float coreField) {
float y01 = clamp(p.y / BladeDimensions.y, 0.0, 1.0);
float rootMask = smoothstep(0.0, 0.025, y01);
float baseOuterHalfWidth = BladeDimensions.z * max(1.0 - y01, 0.0);
float outlineDisplacement = sharedOutlineDisplacement(p, y01);
// 把噪声看成坐标扭曲,而不是只修改某一层的边界。
// 外层和核心使用同一 warpedX/warpedZ,因此位移大小与速度严格一致。
float faceDisplacement = surfaceNoise(p)
* BladeDimensions.x * FACE_AMPLITUDE_RATIO * rootMask;
float warpedX = abs(p.x) - faceDisplacement;
float warpedZ = abs(p.z) - outlineDisplacement;
float outerXDistance = warpedX - BladeDimensions.x;
float outerRootDistance = -p.y;
float outerSideDistance = warpedZ - baseOuterHalfWidth;
outerField = max(
outerXDistance,
max(outerRootDistance, outerSideDistance)
);
// 若单侧彩层 T = 核心完整厚度 C * ratio,则:
// 外层完整厚度 = C + 2T = C * (1 + 2ratio)。
float colorToCoreRatio = max(VolumeParameters.y, 0.0);
float coreScale = 1.0 / (1.0 + 2.0 * colorToCoreRatio);
float coreLength = BladeDimensions.y * CORE_LENGTH_RATIO;
float coreY01 = clamp(p.y / max(coreLength, 0.0001), 0.0, 1.0);
float baseCoreHalfWidth = BladeDimensions.z
* coreScale
* max(1.0 - coreY01, 0.0);
float coreXDistance = warpedX - BladeDimensions.x * coreScale;
float coreRootDistance = -p.y;
float coreTipDistance = p.y - coreLength;
float coreSideDistance = warpedZ - baseCoreHalfWidth;
coreField = max(
max(coreXDistance, coreRootDistance),
max(coreTipDistance, coreSideDistance)
);
}
vec3 sectionEdgeColor(float y01) {
if (y01 < 1.0 / 3.0) {
return EdgeColor0;
}
if (y01 < 2.0 / 3.0) {
return EdgeColor1;
}
return EdgeColor2;
} }
void main() { void main() {
float time = EffectTime * 0.2; float maxOutlineProtrusion = VolumeParameters.x;
float auraWidth = VolumeParameters.z;
float extinction = VolumeParameters.w;
vec3 ro = vCameraLocal; // 轮廓变形按剑宽同比缩放,因此代理保持短三角形;只对外焰作平行扩张。
vec3 rd = normalize(vLocalPos - ro); float auraPadding = auraWidth * 3.0;
float faceAmplitude = BladeDimensions.x * FACE_AMPLITUDE_RATIO;
float deformedRootHalfWidth = BladeDimensions.z + maxOutlineProtrusion;
float proxyRootHalfWidth = deformedRootHalfWidth + auraPadding;
vec3 proxyDimensions = vec3(
BladeDimensions.x + faceAmplitude + auraPadding,
BladeDimensions.y * proxyRootHalfWidth / deformedRootHalfWidth,
proxyRootHalfWidth
);
vec2 hit = intersectBox(ro, rd); vec3 rayOrigin = CameraLocal;
vec3 rayDirection = normalize(vLocalPos - rayOrigin);
vec2 hit = intersectTriangularPrism(
rayOrigin,
rayDirection,
proxyDimensions
);
float tStart = max(hit.x, 0.0); float tStart = max(hit.x, 0.0);
float tEnd = hit.y; float tEnd = hit.y;
if (tEnd <= tStart) { if (tEnd <= tStart) {
discard; discard;
} }
const int STEPS = 20; float stepSize = (tEnd - tStart) / float(VOLUME_STEPS);
float stepSize = (tEnd - tStart) / float(STEPS);
float t = tStart + stepSize * 0.5; float t = tStart + stepSize * 0.5;
vec3 accumulatedColor = vec3(0.0); vec3 accumulatedColor = vec3(0.0);
float accumulatedAlpha = 0.0; float accumulatedAlpha = 0.0;
for (int i = 0; i < STEPS; i++) { for (int i = 0; i < VOLUME_STEPS; i++) {
vec3 p = ro + rd * t; vec3 p = rayOrigin + rayDirection * t;
float density = densityField(p, time); float outerField;
float coreField;
evaluateBladeFields(p, outerField, coreField);
if (density > 0.001) { float outerDensity =
float opticalDepth = density * stepSize * 5.0; 1.0 - smoothstep(-EDGE_SOFTNESS, EDGE_SOFTNESS, outerField);
float sampleAlpha = 1.0 - exp(-opticalDepth); float coreDensity =
vec3 sampleColor = densityColor(p, density); 1.0 - smoothstep(-EDGE_SOFTNESS, EDGE_SOFTNESS, coreField);
float colorDensity = max(outerDensity - coreDensity, 0.0);
float outsideBlend =
smoothstep(-EDGE_SOFTNESS, EDGE_SOFTNESS, outerField);
float auraDensity =
outsideBlend
* exp(-max(outerField, 0.0) / max(auraWidth, 0.0001));
float auraWeight = auraDensity * AURA_DENSITY;
float coreWeight = coreDensity * CORE_DENSITY_BOOST;
float sampleDensity = colorDensity + coreWeight + auraWeight;
if (sampleDensity > 0.0001) {
float y01 = clamp(p.y / BladeDimensions.y, 0.0, 1.0);
vec3 edgeColor = sectionEdgeColor(y01);
vec3 sampleColor = (
edgeColor * (colorDensity + auraWeight)
+ vec3(CORE_BRIGHTNESS) * coreWeight
) / sampleDensity;
float sampleAlpha =
1.0 - exp(-sampleDensity * stepSize * extinction);
float remain = 1.0 - accumulatedAlpha; float remain = 1.0 - accumulatedAlpha;
accumulatedColor += remain * sampleColor * sampleAlpha; accumulatedColor +=
remain * sampleColor * sampleAlpha;
accumulatedAlpha += remain * sampleAlpha; accumulatedAlpha += remain * sampleAlpha;
if (accumulatedAlpha > 0.98) { if (accumulatedAlpha > 0.995) {
break; break;
} }
} }
@@ -260,14 +277,11 @@ void main() {
t += stepSize; t += stepSize;
} }
if (accumulatedAlpha < 0.005) { if (accumulatedAlpha < 0.001) {
discard; discard;
} }
float pulse = 0.9 + 0.1 * sin(time * 8.0); vec3 color =
accumulatedColor *= pulse; accumulatedColor / max(accumulatedAlpha, 0.0001);
vec3 color = accumulatedColor / max(accumulatedAlpha, 0.0001);
fragColor = vec4(color, accumulatedAlpha * vColor.a); fragColor = vec4(color, accumulatedAlpha * vColor.a);
} }
@@ -8,11 +8,7 @@
"fragment": "lzxnoneterraria:devils_devastation_energy", "fragment": "lzxnoneterraria:devils_devastation_energy",
"attributes": [ "attributes": [
"Position", "Position",
"Color", "Color"
"UV0",
"UV1",
"UV2",
"Normal"
], ],
"samplers": [], "samplers": [],
"uniforms": [ "uniforms": [
@@ -50,22 +46,46 @@
] ]
}, },
{ {
"name": "EffectTime", "name": "CameraLocal",
"type": "float",
"count": 3,
"values": [0.0, 0.0, 0.0]
},
{
"name": "BladeDimensions",
"type": "float",
"count": 3,
"values": [0.1, 32.0, 1.0]
},
{
"name": "VolumeParameters",
"type": "float",
"count": 4,
"values": [0.3, 0.33333334, 0.022, 4.0]
},
{
"name": "EdgeColor0",
"type": "float",
"count": 3,
"values": [0.725, 0.345, 1.0]
},
{
"name": "EdgeColor1",
"type": "float",
"count": 3,
"values": [0.847, 0.247, 0.745]
},
{
"name": "EdgeColor2",
"type": "float",
"count": 3,
"values": [0.882, 0.345, 0.247]
},
{
"name": "FlowTime",
"type": "float", "type": "float",
"count": 1, "count": 1,
"values": [0.0] "values": [0.0]
},
{
"name": "ColorA",
"type": "float",
"count": 4,
"values": [0.729, 0.396, 0.345, 1.0]
},
{
"name": "ColorB",
"type": "float",
"count": 4,
"values": [0.8, 0.176, 0.78, 1.0]
} }
] ]
} }
@@ -8,20 +8,12 @@ uniform mat4 ModelViewMat;
uniform mat4 ProjMat; uniform mat4 ProjMat;
out vec3 vLocalPos; out vec3 vLocalPos;
flat out vec3 vCameraLocal;
out vec4 vColor; out vec4 vColor;
void main() { void main() {
vec4 posedPos = ModelMat * vec4(Position, 1.0); vec4 viewPosition = ModelViewMat * ModelMat * vec4(Position, 1.0);
vec4 viewPos = ModelViewMat * posedPos; gl_Position = ProjMat * viewPosition;
gl_Position = ProjMat * viewPos;
vLocalPos = Position; vLocalPos = Position;
vColor = Color; vColor = Color;
mat4 localToView = ModelViewMat * ModelMat;
vec4 cameraLocal = inverse(localToView) * vec4(0.0, 0.0, 0.0, 1.0);
vCameraLocal = cameraLocal.xyz / cameraLocal.w;
} }