Merge pull request #1725 from RVZO6/feature/text-display-rendering

Client: render server text displays and lobby labels
This commit is contained in:
Ludovic Marie
2026-10-25 09:07:59 +01:00
committed by GitHub
8 changed files with 480 additions and 1 deletions
@@ -19,6 +19,15 @@ pub fn main() -> Result<(), Box<dyn Error>> {
};
let mut display = TextDisplayData::default();
display.apply(&packet.values);
if let Some(angles) = input["angles"].as_array() {
let a: Vec<_> = angles.iter().map(|v| v.as_f64().unwrap() as f32).collect();
writeln!(
out,
"{}",
serde_json::to_string(&display.matrix([a[0], a[1]], [a[2], a[3]]).to_cols_array())?
)?;
continue;
}
let mut state = serde_json::to_value(&display)?;
state["text"] = display.text.plain().into();
state["alignment"] = format!("{:?}", display.align()).to_uppercase().into();
+6
View File
@@ -154,6 +154,7 @@ mod storage;
mod structures;
mod subtitle_hud;
mod sulfur_cubes;
mod text_displays;
mod titles;
mod tnt;
mod totem;
@@ -1422,6 +1423,8 @@ struct Game<'a> {
dragon_model: Option<horizon_client::mobs::Part>,
/// Map textures waiting for the next HUD batch to upload them.
map_patches: Vec<horizon_client::font::Patch>,
text_displays: std::collections::HashMap<i32, text_displays::RemoteTextDisplay>,
text_display_vertices: usize,
/// Player skin downloads, and which skin each atlas skin slot holds
/// (least recently shown first).
skin_downloads: RefCell<horizon_client::skins::Downloader>,
@@ -6770,6 +6773,7 @@ impl Game<'_> {
let mut see_through = std::mem::take(&mut self.see_through_vertices);
see_through.clear();
self.name_tag_geometry(camera, alpha, &mut out, &mut see_through);
self.text_display_geometry(camera, alpha, &mut out, &mut see_through);
self.see_through_vertices = see_through;
// Entity shadows (EntityRenderer.extractShadow).
if let Some(sprite) = self.gui.atlas.sprites.get("misc/shadow").copied() {
@@ -11882,6 +11886,8 @@ impl<'a> Game<'a> {
breath_clouds: Vec::new(),
dragon_model: prepared.dragon_model,
map_patches: Vec::new(),
text_displays: Default::default(),
text_display_vertices: 0,
skin_downloads: RefCell::new(horizon_client::skins::Downloader::new(
std::env::current_dir()
.unwrap_or_default()
+7
View File
@@ -1511,6 +1511,7 @@ impl Game<'_> {
);
}
self.turn_remote_object(id, rotation.map(|(yaw, _)| yaw), on_ground);
self.move_remote_text_display(id, position, rotation);
self.turn_remote_projectile(id, rotation);
}
fn remove_entity(&mut self, id: i32) {
@@ -1862,6 +1863,7 @@ impl Game<'_> {
p.data,
);
}
self.move_remote_text_display(p.id, p.position, Some((rotation[0], rotation[1])));
}
C::RemoveEntities(p) => {
for id in p.ids {
@@ -3433,6 +3435,9 @@ impl Game<'_> {
/// ClientLevel.tickEntities; a frozen level only ticks the players
/// (`TickRateManager.isEntityFrozen`).
fn tick_remote_entities(&mut self, running: bool) {
if running {
self.tick_text_displays();
}
// EntityFluidInteraction.update for the entities this window only displays: the
// server never sends the fluid contact, and the renderers read it (cod, salmon and
// tropical fish lie on their side without `in_water`, #1591).
@@ -5107,6 +5112,8 @@ impl Game<'_> {
.collect::<Vec<_>>(),
// The player's own model exists (third person and the inventory draw it).
"player_model": self.player_model.is_some(),
"text_display_vertices": self.text_display_vertices,
"text_displays": self.text_displays.len(),
"health": r.health.0,
"food": r.health.1,
"decode_errors": r.decode_errors,
@@ -24,7 +24,12 @@ impl Game<'_> {
let at = Vec3::new(position[0], position[1], position[2]);
let block = position.map(|v| v.floor() as i32);
self.remove_remote_object(id);
if let Some(cart) = super::minecarts::CartKind::from_id(kind) {
if kind == "minecraft:text_display" {
self.text_displays.insert(
id as i32,
super::text_displays::RemoteTextDisplay::new(position, yaw),
);
} else if let Some(cart) = super::minecarts::CartKind::from_id(kind) {
let mut c = super::minecarts::Minecart::new(id, cart, at);
c.yaw = yaw;
c.previous_yaw = yaw;
@@ -93,6 +98,10 @@ impl Game<'_> {
/// `handleSetEntityData` for an object: what changes how it looks.
pub(super) fn remote_object_data(&mut self, id: i32, values: &[(u8, i32, Value)]) {
if let Some(display) = self.text_displays.get_mut(&id) {
display.metadata(values);
return;
}
let id = id as u32;
for (index, _, value) in values {
if self.item_frames.iter().any(|f| f.id == id) {
@@ -273,6 +282,7 @@ impl Game<'_> {
/// Every object a server sent (a new connection, level or dimension).
pub(super) fn clear_remote_objects(&mut self) {
self.text_displays.clear();
self.carts.clear();
self.boats.clear();
self.armor_stands.clear();
@@ -281,6 +291,7 @@ impl Game<'_> {
}
pub(super) fn remove_remote_object(&mut self, id: u32) {
self.text_displays.remove(&(id as i32));
self.carts.retain(|c| c.id != id);
self.boats.retain(|b| b.id != id);
self.armor_stands.retain(|a| a.id != id);
@@ -0,0 +1,302 @@
//! Server text displays: metadata, interpolation, billboard transforms and world text.
use super::Game;
use horizon_client::{
hud::{HudBatch, rgba},
mesher::Vertex,
text::Text,
text_display::{Align, TextDisplayData},
};
use horizon_protocol::entity_data::Value;
pub(super) struct RemoteTextDisplay {
pub data: TextDisplayData,
from: TextDisplayData,
shown: TextDisplayData,
position: glam::DVec3,
previous: glam::DVec3,
target: glam::DVec3,
rotation: [f32; 2],
old_rotation: [f32; 2],
target_rotation: [f32; 2],
steps: i32,
age: i64,
start: i64,
duration: i32,
dirty: bool,
start_dirty: bool,
duration_dirty: bool,
initialized: bool,
/// `Display.lastProgress`: the progress the last frame drew, where a new
/// interpolation starts from.
last_progress: f32,
/// `TextDisplay.clientDisplayCache`: the split lines, until the render
/// state changes.
lines: Option<Lines>,
}
/// `TextDisplay.CachedInfo`: each line and its width, and the widest.
struct Lines {
lines: Vec<(Text, f32)>,
width: f32,
}
/// `Display.RENDER_STATE_IDS` and `TextDisplay.TEXT_RENDER_STATE_IDS`: the
/// data a render state is built from (not the view range, size or glow).
fn renders(id: u8) -> bool {
matches!(id, 11..=16 | 18 | 19 | 23..=27)
}
impl RemoteTextDisplay {
pub fn new(position: [f64; 3], yaw: f32) -> Self {
Self {
data: Default::default(),
from: Default::default(),
shown: Default::default(),
position: position.into(),
previous: position.into(),
target: position.into(),
rotation: [yaw, 0.0],
old_rotation: [yaw, 0.0],
target_rotation: [yaw, 0.0],
steps: 0,
age: 0,
start: 0,
duration: 0,
dirty: true,
start_dirty: false,
duration_dirty: false,
initialized: false,
last_progress: 0.0,
lines: None,
}
}
fn progress(&self, partial: f32) -> f32 {
if self.duration <= 0 {
1.0
} else {
(((self.age - self.start) as f32 + partial) / self.duration as f32).clamp(0.0, 1.0)
}
}
pub fn metadata(&mut self, values: &[(u8, i32, Value)]) {
self.start_dirty |= values.iter().any(|v| v.0 == 8);
self.duration_dirty |= values.iter().any(|v| v.0 == 9);
self.dirty |= values.iter().any(|v| renders(v.0));
self.data.apply(values);
}
fn tick(&mut self) {
self.age += 1;
self.previous = self.position;
self.old_rotation = self.rotation;
if self.steps > 0 {
self.position += (self.target - self.position) / f64::from(self.steps);
for i in 0..2 {
self.rotation[i] +=
angle(self.target_rotation[i] - self.rotation[i]) / self.steps as f32;
}
self.steps -= 1;
}
if self.start_dirty {
self.start = self.age + i64::from(self.data.interpolation_delay);
}
if self.duration_dirty {
self.duration = self.data.interpolation_duration;
}
if self.dirty {
self.from = if self.initialized && self.duration > 0 {
self.shown.blend(&self.from, self.last_progress)
} else {
self.data.clone()
};
self.shown = self.data.clone();
self.initialized = true;
self.lines = None;
}
self.dirty = false;
self.start_dirty = false;
self.duration_dirty = false;
}
fn move_to(&mut self, position: [f64; 3], rotation: Option<(f32, f32)>) {
self.target = position.into();
if let Some((yaw, pitch)) = rotation {
self.target_rotation = [yaw, pitch];
}
self.steps = self.data.teleport_duration.clamp(0, 59);
if self.steps == 0 {
self.position = self.target;
self.previous = self.target;
self.rotation = self.target_rotation;
self.old_rotation = self.rotation;
}
}
}
fn angle(d: f32) -> f32 {
(d + 180.0).rem_euclid(360.0) - 180.0
}
impl Game<'_> {
pub(super) fn move_remote_text_display(
&mut self,
id: i32,
position: [f64; 3],
rotation: Option<(f32, f32)>,
) {
if let Some(d) = self.text_displays.get_mut(&id) {
d.move_to(position, rotation);
}
}
pub(super) fn tick_text_displays(&mut self) {
for display in self.text_displays.values_mut() {
display.tick();
}
}
pub(super) fn text_display_geometry(
&mut self,
camera: glam::DVec3,
partial: f64,
normal: &mut Vec<Vertex>,
see_through: &mut Vec<Vertex>,
) {
self.text_display_vertices = 0;
let atlas = &self.gui.atlas;
let front = self.camera_type == super::perspective::CameraType::ThirdPersonFront;
let angles = if front {
[self.yaw + 180.0, -self.pitch]
} else {
[self.yaw, self.pitch]
};
for display in self.text_displays.values_mut() {
display.last_progress = display.progress(partial as f32);
let d = display.shown.blend(&display.from, display.last_progress);
let position = display.previous.lerp(display.position, partial);
let range = f64::from(d.view_range) * 64.0 * f64::from(self.settings.entity_distance);
if position.distance_squared(camera) >= range * range || d.text.is_empty() {
continue;
}
let rotation = std::array::from_fn(|i| {
display.old_rotation[i]
+ angle(display.rotation[i] - display.old_rotation[i]) * partial as f32
});
let mut matrix = glam::Mat4::from_translation((position - camera).as_vec3())
* d.matrix(rotation, angles)
* glam::Mat4::from_rotation_y(std::f32::consts::PI)
* glam::Mat4::from_scale(glam::Vec3::splat(-0.025));
if !matrix.is_finite() {
continue;
}
// TextDisplayRenderer.splitLines: Font.split, no indent on wrapped lines.
let Lines { lines, width } = display.lines.get_or_insert_with(|| {
let lines: Vec<_> = atlas
.font
.split(&d.text, d.line_width as f32)
.into_iter()
.map(|line| {
let width = atlas.font.width_text(&line).ceil();
(line, width)
})
.collect();
let width = lines.iter().map(|l| l.1).fold(0.0, f32::max);
Lines { lines, width }
});
let width = *width;
let height = lines.len() as f32 * 10.0 - 1.0;
matrix *=
glam::Mat4::from_translation(glam::Vec3::new(1.0 - width / 2.0, -height, 0.0));
let mut batch = HudBatch::new([1, 1]);
let bg = if d.flags & 4 != 0 {
self.settings.text_background(0.25)
} else {
d.background as u32
};
if bg != 0 {
batch.fill(
atlas,
-1.0,
-1.0,
width + 1.0,
height + 1.0,
rgba(
(bg >> 16) as u8,
(bg >> 8) as u8,
bg as u8,
(bg >> 24) as u8,
),
);
}
let align = d.align();
for (i, (line, line_width)) in lines.iter().enumerate() {
let x = match align {
Align::Left => 0.0,
Align::Right => width - line_width,
Align::Center => (width - line_width) / 2.0,
};
batch.text_styled(
atlas,
line,
x,
i as f32 * 10.0,
rgba(255, 255, 255, d.opacity as u8),
d.flags & 1 != 0,
);
}
let world_light = self.world.light(
position.x.floor() as i32,
position.y.floor() as i32,
position.z.floor() as i32,
);
let light = if d.brightness == -1 {
horizon_client::block_entity_render::vertex_light(world_light)
} else {
((d.brightness as u32 >> 20 & 15) * 16)
| (((d.brightness as u32 >> 4 & 15) * 16) << 8)
};
let through = d.flags & 2 != 0;
let out = if through {
&mut *see_through
} else {
&mut *normal
};
for quad in batch.vertices.as_chunks::<6>().0 {
for i in [0, 1, 2, 5] {
let v = quad[i];
let point =
matrix.transform_point3(glam::Vec3::new(v.pos[0], v.pos[1], v.depth));
out.push(Vertex {
pos: point.to_array(),
uv: v.uv,
color: v.color,
light,
layer: if through { 2 } else { 0 },
normal: [0; 4],
});
}
}
self.text_display_vertices += batch.vertices.len() / 6 * 4;
self.map_patches.extend(batch.patches);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn metadata_and_position_interpolation_keep_their_separate_clocks() {
let mut d = RemoteTextDisplay::new([0.0; 3], 0.0);
d.tick();
d.metadata(&[
(8, 1, Value::Int(0)),
(9, 1, Value::Int(4)),
(10, 1, Value::Int(2)),
(11, 0, Value::Floats(vec![4.0, 0.0, 0.0])),
]);
d.tick();
assert_eq!(d.shown.blend(&d.from, d.progress(0.5)).translation[0], 0.5);
d.move_to([2.0, 0.0, 0.0], Some((90.0, 0.0)));
d.tick();
assert_eq!(d.position.x, 1.0);
assert_eq!(d.rotation[0], 45.0);
d.tick();
assert_eq!(d.position.x, 2.0);
assert_eq!(d.shown.blend(&d.from, d.progress(0.0)).translation[0], 2.0);
}
}
+45
View File
@@ -69,6 +69,51 @@ impl Default for TextDisplayData {
}
impl TextDisplayData {
/// DisplayRenderer's billboard orientation followed by Transformation's T*L*S*R.
pub fn matrix(&self, entity: [f32; 2], camera: [f32; 2]) -> glam::Mat4 {
let [yaw, pitch] = match self.billboard {
1 => [camera[0] - 180.0, entity[1]],
2 => [entity[0], -camera[1]],
3 => [camera[0] - 180.0, -camera[1]],
_ => entity,
};
let orientation = glam::Quat::from_euler(
glam::EulerRot::YXZ,
-yaw * 0.017_453_292,
pitch * 0.017_453_292,
0.0,
);
glam::Mat4::from_quat(orientation)
* glam::Mat4::from_translation(self.translation.into())
* glam::Mat4::from_quat(glam::Quat::from_array(self.left_rotation))
* glam::Mat4::from_scale(self.scale.into())
* glam::Mat4::from_quat(glam::Quat::from_array(self.right_rotation))
}
pub fn blend(&self, from: &Self, t: f32) -> Self {
let mut out = self.clone();
for i in 0..3 {
out.translation[i] =
from.translation[i] + t * (self.translation[i] - from.translation[i]);
out.scale[i] = from.scale[i] + t * (self.scale[i] - from.scale[i]);
}
out.left_rotation = glam::Quat::from_array(from.left_rotation)
.slerp(glam::Quat::from_array(self.left_rotation), t)
.to_array();
out.right_rotation = glam::Quat::from_array(from.right_rotation)
.slerp(glam::Quat::from_array(self.right_rotation), t)
.to_array();
let int = |a: i32, b: i32| a + (t * (b as f32 - a as f32)).floor() as i32;
out.opacity = int(i32::from(from.opacity), i32::from(self.opacity)) as i8;
out.background = (0..4).fold(0, |color, channel| {
color
| (int(
(from.background as u32 >> (channel * 8) & 255) as i32,
(self.background as u32 >> (channel * 8) & 255) as i32,
) << (channel * 8))
});
out
}
/// Omitted fields retain their previous values.
pub fn apply(&mut self, values: &[(u8, i32, Value)]) {
for (id, _, value) in values {
@@ -0,0 +1,44 @@
package dev.horizonlab.parity;
import com.google.gson.*;
import io.netty.buffer.Unpooled;
import java.nio.file.*;
import java.util.*;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
import net.minecraft.world.entity.Display;
import org.joml.*;
/** Invoke the real client renderer's private billboard method without opening a window. */
public final class VanillaTextDisplayTransformOracle {
public static void main(String[] args) throws Exception {
ParityData.init();
var unsafeField = Class.forName("sun.misc.Unsafe").getDeclaredField("theUnsafe"); unsafeField.setAccessible(true);
Object unsafe = unsafeField.get(null);
var allocate = unsafe.getClass().getMethod("allocateInstance", Class.class);
var level = (VanillaTextDisplayOracle.Level) allocate.invoke(unsafe, VanillaTextDisplayOracle.Level.class);
Class<?> renderer = Class.forName("net.minecraft.client.renderer.entity.DisplayRenderer");
Class<?> concrete = Class.forName("net.minecraft.client.renderer.entity.DisplayRenderer$TextDisplayRenderer");
Class<?> renderState = Class.forName("net.minecraft.client.renderer.entity.state.DisplayEntityRenderState");
Object instance = allocate.invoke(unsafe, concrete);
var orientation = renderer.getDeclaredMethod("calculateOrientation", Display.RenderState.class, renderState, Quaternionf.class);
orientation.setAccessible(true);
var fresh = Display.class.getDeclaredMethod("createFreshRenderState"); fresh.setAccessible(true);
var output = new ArrayList<String>();
var type = net.minecraft.core.registries.BuiltInRegistries.ENTITY_TYPE.getValue(net.minecraft.resources.Identifier.parse("minecraft:text_display"));
for (String line : Files.readAllLines(Path.of(args[0]))) {
JsonObject input = JsonParser.parseString(line).getAsJsonObject();
var display = new Display.TextDisplay(type, level);
var buf = new RegistryFriendlyByteBuf(Unpooled.wrappedBuffer(HexFormat.of().parseHex(input.get("wire").getAsString())), ParityData.access);
display.getEntityData().assignValues(ClientboundSetEntityDataPacket.STREAM_CODEC.decode(buf).packedItems()); buf.release();
Object state = Class.forName("net.minecraft.client.renderer.entity.state.TextDisplayEntityRenderState").getConstructor().newInstance();
String[] fields = {"entityYRot", "entityXRot", "cameraYRot", "cameraXRot"};
for (int i=0;i<4;i++) renderState.getField(fields[i]).setFloat(state,input.getAsJsonArray("angles").get(i).getAsFloat());
var data = (Display.RenderState) fresh.invoke(display);
var q = (Quaternionf) orientation.invoke(instance,data,state,new Quaternionf());
float[] matrix = new Matrix4f().rotate(q).mul(data.transformation().get(1).getMatrix()).get(new float[16]);
output.add(new Gson().toJson(matrix));
}
Files.write(Path.of(args[1]),output);
}
}
@@ -0,0 +1,55 @@
#!/usr/bin/env python3
"""Compare text-display matrices with the real Java client renderer (no window)."""
import json
import os
try:
from . import lab, test_rust_parity as base, oracle_harness as harness, test_text_display_parity as metadata
except ImportError:
import lab
import test_rust_parity as base
import oracle_harness as harness
import test_text_display_parity as metadata
def main():
metadata.main()
build = lab.LOCAL / 'text-display-transform-parity'
build.mkdir(exist_ok=True)
(build / 'report.json').unlink(missing_ok=True)
lock, jars, libraries = base.verify_artifacts()
cp = os.pathsep.join(map(str, [jars['client'], jars['server'], *libraries]))
classes = build / 'classes'
classes.mkdir(exist_ok=True)
oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/VanillaTextDisplayTransformOracle.java'
sources = [oracle, lab.ROOT/'crates/horizon-client/src/text_display.rs',
lab.ROOT/'crates/horizon-client/src/game/text_displays.rs']
tested = base.source_hashes(sources)
source = lab.LOCAL / 'text-display-parity/java.jsonl'
# Every billboard mode, coupled and independent entity/camera rotations.
all_states = [json.loads(line) for line in source.read_text().splitlines()]
cases = []
for state in all_states[1:5]:
for yaw in (-180, 0, 37):
for pitch in (-45, 0, 75):
for camera in ((0, 0), (97, -60), (-150, 42)):
cases.append(dict(state, angles=[yaw, pitch, *camera]))
inputs = build / 'cases.jsonl'
inputs.write_text(''.join(json.dumps(case) + '\n' for case in cases))
harness.run_logged(['javac','--release',lock['minecraft']['java_major'],'-proc:none','-sourcepath',lab.ROOT/'mods/horizon/src/test/java','-cp',cp,'-d',classes,oracle],build,'compile-java')
harness.run_logged(['java','-Xmx1G','-cp',str(classes)+os.pathsep+cp,'dev.horizonlab.parity.VanillaTextDisplayTransformOracle',inputs,build/'java.jsonl'],build,'java')
command = harness.build_probe(build,'text_display_fixture')
harness.run_logged([*command,inputs,build/'rust.jsonl'],build,'rust')
java = [json.loads(line) for line in (build/'java.jsonl').read_text().splitlines()]
rust = [json.loads(line) for line in (build/'rust.jsonl').read_text().splitlines()]
assert len(java) == len(rust) == len(cases)
for i,(a,b) in enumerate(zip(java,rust)):
# JOML and glam evaluate float matrix products in different orders.
assert all(abs(x-y) <= 0.00002 for x,y in zip(a,b)), (i,a,b)
base.ensure_sources_unchanged(sources, tested)
lab.write_json(build/'report.json', {'pass':True,'cases':len(cases),'matrix_absolute_tolerance':0.00002,
'sources_sha256':tested})
print(f'Text-display transforms: {len(cases)} cases match the native client renderer')
if __name__ == '__main__':
main()