Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<p align="center">
<a href="https://github.com/WilyFoxM/TrajectoryProjected/actions/workflows/build.yml"><img src="https://img.shields.io/github/actions/workflow/status/WilyFoxM/TrajectoryProjected/build.yml?branch=main&label=build&labelColor=2a2b2a" alt="Build"></a>
<img src="https://img.shields.io/badge/Minecraft-26.2-ef6351?labelColor=2a2b2a" alt="Minecraft 26.2">
<img src="https://img.shields.io/badge/Minecraft-26.1%20%7C%2026.2%20%7C%2026.3-ef6351?labelColor=2a2b2a" alt="Minecraft 26.1 | 26.2 | 26.3">
<img src="https://img.shields.io/badge/loader-Fabric-ef6351?labelColor=2a2b2a" alt="Fabric">
<img src="https://img.shields.io/badge/Java-25-ef6351?labelColor=2a2b2a" alt="Java 25">
<img src="https://img.shields.io/badge/license-MIT-ef6351?labelColor=2a2b2a" alt="MIT">
Expand All @@ -14,7 +14,7 @@

**See where it lands, and how far the spread can carry it.**

Trajectory Projected is a client-side mod for Minecraft **26.2 / Fabric** that
Trajectory Projected is a client-side mod for Minecraft **26.1–26.3 / Fabric** that
shows where your bow, crossbow, trident or thrown item will land. The flight is
simulated tick by tick in the order vanilla uses, from the same position and
rotation the server will fire with, so the line follows the real arrow rather
Expand Down Expand Up @@ -77,10 +77,17 @@ casts; see [Accuracy](#accuracy).

## Getting started

Install **Fabric Loader 0.19.3+**, **Fabric API 0.161.0+26.2** and **Java 25**,
then place the Trajectory Projected JAR from
Install **Fabric Loader 0.19.3+**, **Fabric API** and **Java 25**, then place the
Trajectory Projected JAR for your Minecraft version from
[Releases](https://github.com/WilyFoxM/TrajectoryProjected/releases) in your
Minecraft instance's `mods` folder.
Minecraft instance's `mods` folder. Each version has its own file:

| File | Minecraft | Fabric API |
| --- | --- | --- |
| `+26.1.x` | 26.1, 26.1.1, 26.1.2 | 0.145.1 or newer |
| `+26.2` | 26.2 | 0.161.0 or newer |
| `+26.3` | 26.3 | 0.166.2 or newer |

Nothing needs to be installed on the server.

Press **F7** in-game to open settings. A key to turn trajectories on and off can be
Expand All @@ -94,14 +101,18 @@ and Russian translations are included.

## Compatibility

Built for Minecraft **26.2** with Fabric. I play with **Sodium** (0.9.2 tested).
Built for Minecraft **26.1.x, 26.2 and 26.3** with Fabric. I play on 26.2 with **Sodium** (0.9.2 tested).

With **Iris** (1.11.4 tested) and a shader pack active, the line, markers and spread
zone are drawn over the pack's finished image, so they look the same as without
shaders and stay sharp; they are not lit or fogged by the pack. Checked with
Complementary Reimagined r5.8.1 and BSL v10.1.8. Without a pack, Iris changes
nothing.

On 26.3 the mod has been checked in a local client run with Sodium 0.9.2. Iris 1.11.7 does not
start on 26.3 even without this mod, so shaders there are untested. On 26.1.x the line over a
Complementary pack has been checked with Iris 1.11.4.

Other rendering mods have not been tested.

On multiplayer I have tested one small server of my own; the results are under
Expand All @@ -115,6 +126,10 @@ each tick, water, entity hitboxes and the random spread. Unit tests compare it w
vanilla's formulas, which says nothing about the game itself, so I also measured it
in game.

The measurements below were made on 26.2. Each version's file follows its own vanilla,
and the unit tests run against each version's code; 26.1.x and 26.3 have not been
measured in game separately.

I fired 334 logged bow shots and compared each prediction with the launch the server
sent and with where the arrow stopped. The first 30 partial draws on the server
exposed a bug in how the mod counted the draw. It is fixed, and the table shows the
Expand Down
34 changes: 34 additions & 0 deletions build.fabric.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,40 @@ val metadata =
tasks.processResources {
inputs.properties(metadata)
filesMatching("fabric.mod.json") { expand(metadata) }

// 26.3 compiles shaders to SPIR-V: `#include` instead of `#moj_import`, and every stage input
// and output needs an explicit location. Sources keep the older form; this rewrites them.
if (sc.current.parsed >= "26.3") {
val locations =
mapOf(
// Vertex attributes, in the order of DefaultVertexFormat.POSITION_TEX_COLOR.
"Position" to 0, "UV0" to 1, "Color" to 2,
// Vertex outputs and fragment inputs.
"discCoord" to 0, "vertexColor" to 1,
// Fragment output.
"fragColor" to 0,
)
val declaration = Regex("""^(in|out) \w+ (\w+);""")
// Closures are not part of the task's up-to-date check; the table is.
inputs.property("shaderLocations", locations.toString())
filesMatching(listOf("assets/*/shaders/**/*.vsh", "assets/*/shaders/**/*.fsh")) {
val file = path
filter { line ->
val variable = declaration.find(line)?.groupValues?.get(2)
when {
line.startsWith("#version") ->
"$line\n#extension GL_ARB_separate_shader_objects : require"
line.startsWith("#moj_import") -> line.replaceFirst("#moj_import", "#include")
variable != null -> {
val location =
locations[variable] ?: error("No shader location for $variable in $file")
"layout(location = $location) $line"
}
else -> line
}
}
}
}
}

tasks.jar {
Expand Down
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ plugins {
stonecutter {
create(rootProject) {
// One node per Minecraft version, all built by build.fabric.gradle.kts.
version("26.1.2", "26.1.2").buildscript("build.fabric.gradle.kts")
version("26.2", "26.2").buildscript("build.fabric.gradle.kts")
version("26.3", "26.3").buildscript("build.fabric.gradle.kts")
vcsVersion = "26.2"
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/dev/java/ru/wilyfox/trajectory/dev/ShotLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.Minecraft;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityTypes;
import net.minecraft.world.entity.projectile.arrow.Arrow;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -62,7 +61,7 @@ static void released(ShotPreview.Prediction prediction) {
}

static void entityLoaded(Entity entity) {
if (pending == null || entity.getType() != EntityTypes.ARROW) {
if (pending == null || !(entity instanceof Arrow)) {
return;
}
Minecraft minecraft = Minecraft.getInstance();
Expand Down
58 changes: 5 additions & 53 deletions src/main/java/ru/wilyfox/trajectory/client/AfterLevel.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
package ru.wilyfox.trajectory.client;

import com.mojang.blaze3d.buffers.GpuBuffer;
import com.mojang.blaze3d.buffers.GpuBufferSlice;
import com.mojang.blaze3d.pipeline.RenderTarget;
import com.mojang.blaze3d.systems.RenderPass;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.ByteBufferBuilder;
import com.mojang.blaze3d.vertex.MeshData;
import com.mojang.blaze3d.vertex.PoseStack;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.OptionalDouble;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.SubmitNodeCollector;
import net.minecraft.client.renderer.rendertype.RenderType;
import org.joml.Matrix4f;
import org.joml.Matrix4fc;
import ru.wilyfox.trajectory.TrajectoryProjected;
import ru.wilyfox.trajectory.compat.Gpu;

/**
* Drawing after the level has been rendered, onto the finished image, depth-tested against the main
Expand Down Expand Up @@ -89,57 +81,17 @@ public static void flush() {
return;
}
try {
RenderTarget target = Minecraft.getInstance().gameRenderer.mainRenderTarget();
GpuBufferSlice transforms =
RenderSystem.getDynamicUniforms().writeTransform(new Matrix4f(viewRotation));
RenderTarget target = Gpu.mainTarget();
GpuBufferSlice transforms = Gpu.writeTransform(new Matrix4f(viewRotation));
for (Recorded r : RECORDED) {
draw(target, transforms, r);
Gpu.drawNow(
target, transforms, r.type(), builder -> r.geometry().render(r.pose(), builder));
}
} finally {
RECORDED.clear();
}
}

private static void draw(RenderTarget target, GpuBufferSlice transforms, Recorded r) {
RenderType type = r.type();
try (ByteBufferBuilder bytes = new ByteBufferBuilder(type.format().getVertexSize() * 256)) {
BufferBuilder builder = new BufferBuilder(bytes, type.primitiveTopology(), type.format());
r.geometry().render(r.pose(), builder);
try (MeshData mesh = builder.build()) {
if (mesh == null) {
return;
}
int indexCount = mesh.drawState().indexCount();
RenderSystem.AutoStorageIndexBuffer indices =
RenderSystem.getSequentialBuffer(type.primitiveTopology());
GpuBuffer indexBuffer = indices.getBuffer(indexCount);
try (GpuBuffer vertices =
RenderSystem.getDevice()
.createBuffer(
() -> "Trajectory Projected after shaders",
GpuBuffer.USAGE_VERTEX,
mesh.vertexBuffer())) {
try (RenderPass pass =
RenderSystem.getDevice()
.createCommandEncoder()
.createRenderPass(
() -> "Trajectory Projected after shaders",
target.getColorTextureView(),
Optional.empty(),
target.useDepth ? target.getDepthTextureView() : null,
OptionalDouble.empty())) {
pass.setPipeline(type.pipeline());
RenderSystem.bindDefaultUniforms(pass);
pass.setUniform("DynamicTransforms", transforms);
pass.setVertexBuffer(0, vertices.slice());
pass.setIndexBuffer(indexBuffer, indices.type());
pass.drawIndexed(indexCount, 1, 0, 0, 0);
}
}
}
}
}

private static MethodHandle resolve(String method) {
if (!IRIS) {
return null;
Expand Down
43 changes: 17 additions & 26 deletions src/main/java/ru/wilyfox/trajectory/client/DotRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.mojang.blaze3d.pipeline.DepthStencilState;
import com.mojang.blaze3d.pipeline.RenderPipeline;
import com.mojang.blaze3d.platform.CompareOp;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import java.util.ArrayList;
Expand All @@ -27,6 +26,7 @@
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.CollisionContext;
import org.joml.Vector3f;
import ru.wilyfox.trajectory.compat.Gpu;
import ru.wilyfox.trajectory.config.TrajectoryConfig;
import ru.wilyfox.trajectory.sim.Aabb;
import ru.wilyfox.trajectory.sim.Face;
Expand All @@ -48,42 +48,38 @@ final class DotRenderer {
private static final String MOD_ID = "trajectory_projected";

private static final RenderPipeline VISIBLE =
RenderPipeline.builder(RenderPipelines.DEBUG_FILLED_SNIPPET)
Gpu.texColorQuads(RenderPipelines.DEBUG_FILLED_SNIPPET)
.withLocation(Identifier.fromNamespaceAndPath(MOD_ID, "pipeline/dots"))
.withVertexShader(Identifier.fromNamespaceAndPath(MOD_ID, "core/dot"))
.withFragmentShader(Identifier.fromNamespaceAndPath(MOD_ID, "core/dot"))
.withVertexBinding(0, DefaultVertexFormat.POSITION_TEX_COLOR)
.withCull(false)
.build();

/** Passes only where terrain is in front of the dot (the depth buffer is reversed). */
/** Passes only where terrain is in front of the dot. */
private static final RenderPipeline HIDDEN =
RenderPipeline.builder(RenderPipelines.DEBUG_FILLED_SNIPPET)
Gpu.texColorQuads(RenderPipelines.DEBUG_FILLED_SNIPPET)
.withLocation(Identifier.fromNamespaceAndPath(MOD_ID, "pipeline/dots_hidden"))
.withVertexShader(Identifier.fromNamespaceAndPath(MOD_ID, "core/dot"))
.withFragmentShader(Identifier.fromNamespaceAndPath(MOD_ID, "core/dot_hidden"))
.withVertexBinding(0, DefaultVertexFormat.POSITION_TEX_COLOR)
.withDepthStencilState(new DepthStencilState(CompareOp.LESS_THAN, false))
.withDepthStencilState(new DepthStencilState(Gpu.BEHIND, false))
.withCull(false)
.build();

/** The hit marker on a living target: an anti-aliased cross instead of a disc. */
private static final RenderPipeline CROSS =
RenderPipeline.builder(RenderPipelines.DEBUG_FILLED_SNIPPET)
Gpu.texColorQuads(RenderPipelines.DEBUG_FILLED_SNIPPET)
.withLocation(Identifier.fromNamespaceAndPath(MOD_ID, "pipeline/cross"))
.withVertexShader(Identifier.fromNamespaceAndPath(MOD_ID, "core/dot"))
.withFragmentShader(Identifier.fromNamespaceAndPath(MOD_ID, "core/cross"))
.withVertexBinding(0, DefaultVertexFormat.POSITION_TEX_COLOR)
.withCull(false)
.build();

private static final RenderPipeline CROSS_HIDDEN =
RenderPipeline.builder(RenderPipelines.DEBUG_FILLED_SNIPPET)
Gpu.texColorQuads(RenderPipelines.DEBUG_FILLED_SNIPPET)
.withLocation(Identifier.fromNamespaceAndPath(MOD_ID, "pipeline/cross_hidden"))
.withVertexShader(Identifier.fromNamespaceAndPath(MOD_ID, "core/dot"))
.withFragmentShader(Identifier.fromNamespaceAndPath(MOD_ID, "core/cross_hidden"))
.withVertexBinding(0, DefaultVertexFormat.POSITION_TEX_COLOR)
.withDepthStencilState(new DepthStencilState(CompareOp.LESS_THAN, false))
.withDepthStencilState(new DepthStencilState(Gpu.BEHIND, false))
.withCull(false)
.build();

Expand All @@ -93,21 +89,19 @@ final class DotRenderer {
* outlined) is chosen per dot from a ray to the real path point it stands for.
*/
private static final RenderPipeline OVERLAY =
RenderPipeline.builder(RenderPipelines.DEBUG_FILLED_SNIPPET)
Gpu.texColorQuads(RenderPipelines.DEBUG_FILLED_SNIPPET)
.withLocation(Identifier.fromNamespaceAndPath(MOD_ID, "pipeline/dots_overlay"))
.withVertexShader(Identifier.fromNamespaceAndPath(MOD_ID, "core/dot"))
.withFragmentShader(Identifier.fromNamespaceAndPath(MOD_ID, "core/dot"))
.withVertexBinding(0, DefaultVertexFormat.POSITION_TEX_COLOR)
.withDepthStencilState(new DepthStencilState(CompareOp.ALWAYS_PASS, false))
.withCull(false)
.build();

private static final RenderPipeline OVERLAY_HIDDEN =
RenderPipeline.builder(RenderPipelines.DEBUG_FILLED_SNIPPET)
Gpu.texColorQuads(RenderPipelines.DEBUG_FILLED_SNIPPET)
.withLocation(Identifier.fromNamespaceAndPath(MOD_ID, "pipeline/dots_overlay_hidden"))
.withVertexShader(Identifier.fromNamespaceAndPath(MOD_ID, "core/dot"))
.withFragmentShader(Identifier.fromNamespaceAndPath(MOD_ID, "core/dot_hidden"))
.withVertexBinding(0, DefaultVertexFormat.POSITION_TEX_COLOR)
.withDepthStencilState(new DepthStencilState(CompareOp.ALWAYS_PASS, false))
.withCull(false)
.build();
Expand All @@ -124,11 +118,10 @@ final class DotRenderer {

/** The spread zone on block faces: a projected decal, depth-tested, never written to depth. */
private static final RenderPipeline ZONE =
RenderPipeline.builder(RenderPipelines.DEBUG_FILLED_SNIPPET)
Gpu.texColorQuads(RenderPipelines.DEBUG_FILLED_SNIPPET)
.withLocation(Identifier.fromNamespaceAndPath(MOD_ID, "pipeline/zone"))
.withVertexShader(Identifier.fromNamespaceAndPath(MOD_ID, "core/dot"))
.withFragmentShader(Identifier.fromNamespaceAndPath(MOD_ID, "core/zone"))
.withVertexBinding(0, DefaultVertexFormat.POSITION_TEX_COLOR)
.withCull(false)
.build();

Expand All @@ -143,13 +136,13 @@ final class DotRenderer {
* depth test frame by frame. A bias following the surface's slope covers that shift.
*/
private static final RenderPipeline ZONE_OVER_SHADERS =
RenderPipeline.builder(RenderPipelines.DEBUG_FILLED_SNIPPET)
Gpu.texColorQuads(RenderPipelines.DEBUG_FILLED_SNIPPET)
.withLocation(Identifier.fromNamespaceAndPath(MOD_ID, "pipeline/zone_over_shaders"))
.withVertexShader(Identifier.fromNamespaceAndPath(MOD_ID, "core/dot"))
.withFragmentShader(Identifier.fromNamespaceAndPath(MOD_ID, "core/zone"))
.withVertexBinding(0, DefaultVertexFormat.POSITION_TEX_COLOR)
.withDepthStencilState(
new DepthStencilState(DepthStencilState.DEFAULT.depthTest(), false, 2.0F, 0.0F))
new DepthStencilState(
DepthStencilState.DEFAULT.depthTest(), false, Gpu.TOWARDS_CAMERA * 2.0F, 0.0F))
.withCull(false)
.build();

Expand All @@ -168,21 +161,19 @@ final class DotRenderer {

/** The hook of a fishing rod where it will land: a ghost of the bobber. */
private static final RenderPipeline BOBBER =
RenderPipeline.builder(RenderPipelines.DEBUG_FILLED_SNIPPET)
Gpu.texColorQuads(RenderPipelines.DEBUG_FILLED_SNIPPET)
.withLocation(Identifier.fromNamespaceAndPath(MOD_ID, "pipeline/bobber"))
.withVertexShader(Identifier.fromNamespaceAndPath(MOD_ID, "core/dot"))
.withFragmentShader(Identifier.fromNamespaceAndPath(MOD_ID, "core/bobber"))
.withVertexBinding(0, DefaultVertexFormat.POSITION_TEX_COLOR)
.withCull(false)
.build();

private static final RenderPipeline BOBBER_HIDDEN =
RenderPipeline.builder(RenderPipelines.DEBUG_FILLED_SNIPPET)
Gpu.texColorQuads(RenderPipelines.DEBUG_FILLED_SNIPPET)
.withLocation(Identifier.fromNamespaceAndPath(MOD_ID, "pipeline/bobber_hidden"))
.withVertexShader(Identifier.fromNamespaceAndPath(MOD_ID, "core/dot"))
.withFragmentShader(Identifier.fromNamespaceAndPath(MOD_ID, "core/bobber_hidden"))
.withVertexBinding(0, DefaultVertexFormat.POSITION_TEX_COLOR)
.withDepthStencilState(new DepthStencilState(CompareOp.LESS_THAN, false))
.withDepthStencilState(new DepthStencilState(Gpu.BEHIND, false))
.withCull(false)
.build();

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/ru/wilyfox/trajectory/client/ShotPreview.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.ItemStackTemplate;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.component.ChargedProjectiles;
import net.minecraft.world.item.component.Fireworks;
Expand Down Expand Up @@ -333,7 +334,7 @@ private static Prediction crossbow(
if (shooter == null) {
return null;
}
List<ItemStack> items = charged.itemCopies();
List<ItemStack> items = charged.items().stream().map(ItemStackTemplate::create).toList();
List<CrossbowShot.Charge> charges = new ArrayList<>(items.size());
for (ItemStack item : items) {
charges.add(
Expand Down
Loading
Loading