| 1 | /*
|
| 2 | * This file is part of the Combatant Client distribution.
|
| 3 | * Copyright (c) 2026 pivosos2007.
|
| 4 | *
|
| 5 | * Licensed under the GNU General Public License v3.0.
|
| 6 | */
|
| 7 |
|
| 8 | package combatant.client.render.effects.kernels;
|
| 9 |
|
| 10 | import combatant.client.render.effects.CurrentTransientEffectBackend;
|
| 11 | import combatant.client.render.effects.EffectDomain;
|
| 12 | import combatant.client.render.effects.EffectMaterial;
|
| 13 | import combatant.client.render.effects.EffectParameters;
|
| 14 | import combatant.client.render.effects.TransientEffectDescriptor;
|
| 15 | import combatant.client.render.engine.RenderState;
|
| 16 | import combatant.client.render.engine.TextureStorage;
|
| 17 | import combatant.client.render.engine.pipeline.CombatantRenderPipelines;
|
| 18 | import combatant.client.render.engine.renderer.Renderer3D;
|
| 19 | import combatant.client.render.engine.uniform.MeshBuilder;
|
| 20 | import net.minecraft.util.Mth;
|
| 21 | import net.minecraft.world.phys.Vec3;
|
| 22 | import org.joml.Quaternionf;
|
| 23 | import org.joml.Vector3f;
|
| 24 |
|
| 25 | /** Reusable current-renderer kernels for short combat/transient events. */
|
| 26 | public final class TransientAttackKernels {
|
| 27 | public static final String PLASMA_PINCH = "combatant:plasma_pinch";
|
| 28 | public static final String SPARKS = "combatant:attack_sparks";
|
| 29 | public static final String SLASH = "combatant:attack_slash";
|
| 30 |
|
| 31 | private static final double TAU = Math.PI * 2.0;
|
| 32 |
|
| 33 | private TransientAttackKernels() {
|
| 34 | }
|
| 35 |
|
| 36 | public static void register(CurrentTransientEffectBackend backend) {
|
| 37 | if (backend == null) return;
|
| 38 | backend.registerRenderer(PLASMA_PINCH, TransientAttackKernels::renderPlasma);
|
| 39 | backend.registerRenderer(SPARKS, TransientAttackKernels::renderSparks);
|
| 40 | backend.registerRenderer(SLASH, TransientAttackKernels::renderSlash);
|
| 41 | }
|
| 42 |
|
| 43 | public static TransientEffectDescriptor plasma(
|
| 44 | long id, Vec3 position, long nowMs, long lifetimeMs, long seed,
|
| 45 | int sourceEntityId, int primaryColor, int secondaryColor,
|
| 46 | float radius, float intensity, int priority
|
| 47 | ) {
|
| 48 | return new TransientEffectDescriptor(
|
| 49 | id, PLASMA_PINCH, EffectDomain.BILLBOARD, position, nowMs, lifetimeMs,
|
| 50 | sourceEntityId, seed, 0, priority,
|
| 51 | material(primaryColor, secondaryColor, intensity),
|
| 52 | new EffectParameters(Math.max(0.05f, radius), Math.max(0.0f, intensity), 18.0f, 1.0f,
|
| 53 | 0, 0, 0, 0)
|
| 54 | );
|
| 55 | }
|
| 56 |
|
| 57 | public static TransientEffectDescriptor sparks(
|
| 58 | long id, Vec3 position, long nowMs, long lifetimeMs, long seed,
|
| 59 | int sourceEntityId, int primaryColor, int secondaryColor,
|
| 60 | float radius, int rayCount, float intensity, int priority
|
| 61 | ) {
|
| 62 | return new TransientEffectDescriptor(
|
| 63 | id, SPARKS, EffectDomain.BILLBOARD, position, nowMs, lifetimeMs,
|
| 64 | sourceEntityId, seed, 0, priority,
|
| 65 | material(primaryColor, secondaryColor, intensity),
|
| 66 | new EffectParameters(Math.max(0.05f, radius), Math.max(3, rayCount),
|
| 67 | Math.max(0.0f, intensity), 0.12f, 0, 0, 0, 0)
|
| 68 | );
|
| 69 | }
|
| 70 |
|
| 71 | public static TransientEffectDescriptor slash(
|
| 72 | long id, Vec3 position, long nowMs, long lifetimeMs, long seed,
|
| 73 | int sourceEntityId, int primaryColor, int secondaryColor,
|
| 74 | float radius, float arcRadians, float tiltRadians, float intensity, int priority
|
| 75 | ) {
|
| 76 | return new TransientEffectDescriptor(
|
| 77 | id, SLASH, EffectDomain.RIBBON, position, nowMs, lifetimeMs,
|
| 78 | sourceEntityId, seed, 0, priority,
|
| 79 | material(primaryColor, secondaryColor, intensity),
|
| 80 | new EffectParameters(Math.max(0.05f, radius), Math.max(0.15f, arcRadians),
|
| 81 | tiltRadians, Math.max(0.0f, intensity), 22.0f, 0, 0, 0)
|
| 82 | );
|
| 83 | }
|
| 84 |
|
| 85 | private static EffectMaterial material(int primary, int secondary, float intensity) {
|
| 86 | return new EffectMaterial(
|
| 87 | primary,
|
| 88 | secondary,
|
| 89 | 1.0f,
|
| 90 | Math.max(0.0f, intensity),
|
| 91 | EffectMaterial.DepthPolicy.MAIN,
|
| 92 | EffectMaterial.BlendPolicy.ADDITIVE
|
| 93 | );
|
| 94 | }
|
| 95 |
|
| 96 | private static void renderPlasma(Renderer3D renderer,
|
| 97 | TransientEffectDescriptor effect,
|
| 98 | float tickDelta,
|
| 99 | long nowMs) {
|
| 100 | float t = effect.progress(nowMs);
|
| 101 | float fade = 1.0f - smooth(t);
|
| 102 | if (fade <= 0.002f) return;
|
| 103 |
|
| 104 | MeshBuilder mesh = renderer.batchTextured(
|
| 105 | CombatantRenderPipelines.WORLD_TEXTURED_ADDITIVE,
|
| 106 | TextureStorage.FIRE_FLY,
|
| 107 | Renderer3D.DepthMode.MAIN
|
| 108 | );
|
| 109 | if (mesh == null) return;
|
| 110 |
|
| 111 | float radius = effect.parameters().p0();
|
| 112 | float intensity = effect.parameters().p1();
|
| 113 | int count = Mth.clamp(Math.round(effect.parameters().p2()), 8, 48);
|
| 114 | Quaternionf camera = RenderState.cameraRotation;
|
| 115 |
|
| 116 | float coreSize = radius * (0.18f + t * 0.45f);
|
| 117 | addBillboard(mesh, effect.position(), coreSize, camera,
|
| 118 | withAlpha(effect.material().secondaryColor(), Math.round(255.0f * fade)));
|
| 119 |
|
| 120 | double phase = unit(effect.seed(), 0) * TAU + t * 4.2;
|
| 121 | float shellRadius = radius * (0.16f + smooth(t) * 0.95f);
|
| 122 | for (int i = 0; i < count; i++) {
|
| 123 | double u = (i + 0.5) / count;
|
| 124 | double a = TAU * u + phase;
|
| 125 | double wobble = Math.sin(a * 3.0 + t * 8.0 + unit(effect.seed(), i + 1) * TAU) * radius * 0.11;
|
| 126 | Vec3 p = effect.position().add(
|
| 127 | Math.cos(a) * (shellRadius + wobble),
|
| 128 | Math.sin(a * 2.0 + phase) * radius * 0.16,
|
| 129 | Math.sin(a) * (shellRadius + wobble)
|
| 130 | );
|
| 131 | float local = 0.55f + 0.45f * (float) Math.sin(a * 2.0 + 1.7);
|
| 132 | float size = radius * (0.035f + 0.035f * local) * (0.75f + intensity * 0.25f);
|
| 133 | int color = lerpColor(effect.material().primaryColor(), effect.material().secondaryColor(), local);
|
| 134 | addBillboard(mesh, p, size, camera, withAlpha(color, Math.round(210.0f * fade)));
|
| 135 | }
|
| 136 | }
|
| 137 |
|
| 138 | private static void renderSparks(Renderer3D renderer,
|
| 139 | TransientEffectDescriptor effect,
|
| 140 | float tickDelta,
|
| 141 | long nowMs) {
|
| 142 | float t = effect.progress(nowMs);
|
| 143 | float fade = 1.0f - smooth(t);
|
| 144 | if (fade <= 0.002f) return;
|
| 145 |
|
| 146 | MeshBuilder mesh = renderer.batchTextured(
|
| 147 | CombatantRenderPipelines.WORLD_TEXTURED_ADDITIVE,
|
| 148 | TextureStorage.FIRE_FLY,
|
| 149 | Renderer3D.DepthMode.MAIN
|
| 150 | );
|
| 151 | if (mesh == null) return;
|
| 152 |
|
| 153 | float radius = effect.parameters().p0();
|
| 154 | int rays = Mth.clamp(Math.round(effect.parameters().p1()), 3, 64);
|
| 155 | float intensity = effect.parameters().p2();
|
| 156 | float baseSize = Math.max(0.02f, effect.parameters().p3());
|
| 157 | Quaternionf camera = RenderState.cameraRotation;
|
| 158 |
|
| 159 | for (int ray = 0; ray < rays; ray++) {
|
| 160 | double azimuth = TAU * unit(effect.seed(), ray * 3 + 1);
|
| 161 | double elevation = (unit(effect.seed(), ray * 3 + 2) - 0.35) * 1.2;
|
| 162 | double cosEl = Math.cos(elevation);
|
| 163 | Vec3 direction = new Vec3(Math.cos(azimuth) * cosEl, Math.sin(elevation), Math.sin(azimuth) * cosEl);
|
| 164 | float rayScale = 0.55f + 0.65f * (float) unit(effect.seed(), ray * 3 + 3);
|
| 165 | double headDistance = radius * rayScale * smooth(t);
|
| 166 | for (int trail = 0; trail < 4; trail++) {
|
| 167 | float trailT = trail / 3.0f;
|
| 168 | double distance = Math.max(0.0, headDistance - radius * 0.15 * trailT);
|
| 169 | Vec3 p = effect.position().add(direction.scale(distance));
|
| 170 | float alpha = fade * (1.0f - trailT * 0.72f);
|
| 171 | float size = baseSize * (0.85f + intensity * 0.18f) * (1.0f - trailT * 0.45f);
|
| 172 | int color = lerpColor(effect.material().secondaryColor(), effect.material().primaryColor(), trailT);
|
| 173 | addBillboard(mesh, p, size, camera, withAlpha(color, Math.round(alpha * 255.0f)));
|
| 174 | }
|
| 175 | }
|
| 176 | }
|
| 177 |
|
| 178 | private static void renderSlash(Renderer3D renderer,
|
| 179 | TransientEffectDescriptor effect,
|
| 180 | float tickDelta,
|
| 181 | long nowMs) {
|
| 182 | float t = effect.progress(nowMs);
|
| 183 | float fade = 1.0f - smooth(t);
|
| 184 | if (fade <= 0.002f) return;
|
| 185 |
|
| 186 | MeshBuilder mesh = renderer.batchTextured(
|
| 187 | CombatantRenderPipelines.WORLD_TEXTURED_ADDITIVE,
|
| 188 | TextureStorage.FIRE_FLY,
|
| 189 | Renderer3D.DepthMode.MAIN
|
| 190 | );
|
| 191 | if (mesh == null) return;
|
| 192 |
|
| 193 | float radius = effect.parameters().p0();
|
| 194 | float arc = effect.parameters().p1();
|
| 195 | float tilt = effect.parameters().p2();
|
| 196 | float intensity = effect.parameters().p3();
|
| 197 | int count = Mth.clamp(Math.round(effect.parameters().p4()), 8, 48);
|
| 198 | Quaternionf camera = RenderState.cameraRotation;
|
| 199 | double base = unit(effect.seed(), 1) * TAU - arc * 0.5;
|
| 200 | double cosTilt = Math.cos(tilt);
|
| 201 | double sinTilt = Math.sin(tilt);
|
| 202 |
|
| 203 | for (int i = 0; i < count; i++) {
|
| 204 | float u = count <= 1 ? 0.5f : i / (float) (count - 1);
|
| 205 | double a = base + arc * u;
|
| 206 | double x = Math.cos(a) * radius;
|
| 207 | double y = Math.sin(a) * radius * 0.55;
|
| 208 | double z = y * sinTilt;
|
| 209 | y *= cosTilt;
|
| 210 | Vec3 p = effect.position().add(x, y, z);
|
| 211 | float center = 1.0f - Math.abs(u * 2.0f - 1.0f);
|
| 212 | float reveal = Mth.clamp((t * 1.35f - u) * 4.0f, 0.0f, 1.0f);
|
| 213 | float alpha = fade * reveal * (0.35f + center * 0.65f);
|
| 214 | float size = radius * 0.045f * (0.7f + intensity * 0.25f) * (0.7f + center * 0.5f);
|
| 215 | int color = lerpColor(effect.material().primaryColor(), effect.material().secondaryColor(), center);
|
| 216 | addBillboard(mesh, p, size, camera, withAlpha(color, Math.round(alpha * 255.0f)));
|
| 217 | }
|
| 218 | }
|
| 219 |
|
| 220 | private static void addBillboard(MeshBuilder mesh, Vec3 center, float size, Quaternionf camera, int argb) {
|
| 221 | Vector3f right = new Vector3f(1, 0, 0).rotate(camera).mul(size);
|
| 222 | Vector3f up = new Vector3f(0, 1, 0).rotate(camera).mul(size);
|
| 223 | Vec3 p1 = center.add(-right.x - up.x, -right.y - up.y, -right.z - up.z);
|
| 224 | Vec3 p2 = center.add(right.x - up.x, right.y - up.y, right.z - up.z);
|
| 225 | Vec3 p3 = center.add(right.x + up.x, right.y + up.y, right.z + up.z);
|
| 226 | Vec3 p4 = center.add(-right.x + up.x, -right.y + up.y, -right.z + up.z);
|
| 227 | mesh.ensureQuadCapacity();
|
| 228 | int i1 = mesh.vec3(p1.x, p1.y, p1.z).vec2(0, 1).colorArgb(argb).next();
|
| 229 | int i2 = mesh.vec3(p2.x, p2.y, p2.z).vec2(1, 1).colorArgb(argb).next();
|
| 230 | int i3 = mesh.vec3(p3.x, p3.y, p3.z).vec2(1, 0).colorArgb(argb).next();
|
| 231 | int i4 = mesh.vec3(p4.x, p4.y, p4.z).vec2(0, 0).colorArgb(argb).next();
|
| 232 | mesh.quad(i1, i2, i3, i4);
|
| 233 | }
|
| 234 |
|
| 235 | private static float smooth(float value) {
|
| 236 | float t = Mth.clamp(value, 0.0f, 1.0f);
|
| 237 | return t * t * (3.0f - 2.0f * t);
|
| 238 | }
|
| 239 |
|
| 240 | private static int withAlpha(int argb, int alpha) {
|
| 241 | return (Mth.clamp(alpha, 0, 255) << 24) | (argb & 0x00FFFFFF);
|
| 242 | }
|
| 243 |
|
| 244 | private static int lerpColor(int a, int b, float t) {
|
| 245 | t = Mth.clamp(t, 0.0f, 1.0f);
|
| 246 | int ar = (a >>> 16) & 255, ag = (a >>> 8) & 255, ab = a & 255;
|
| 247 | int br = (b >>> 16) & 255, bg = (b >>> 8) & 255, bb = b & 255;
|
| 248 | int r = Math.round(ar + (br - ar) * t);
|
| 249 | int g = Math.round(ag + (bg - ag) * t);
|
| 250 | int bl = Math.round(ab + (bb - ab) * t);
|
| 251 | return 0xFF000000 | (r << 16) | (g << 8) | bl;
|
| 252 | }
|
| 253 |
|
| 254 | private static double unit(long seed, int lane) {
|
| 255 | long z = seed + 0x9E3779B97F4A7C15L * (lane + 1L);
|
| 256 | z = (z ^ (z >>> 30)) * 0xBF58476D1CE4E5B9L;
|
| 257 | z = (z ^ (z >>> 27)) * 0x94D049BB133111EBL;
|
| 258 | z ^= z >>> 31;
|
| 259 | return (z >>> 11) * 0x1.0p-53;
|
| 260 | }
|
| 261 | }
|
| 262 |
|