Items: sample the glint from its own repeating texture

Vanilla's GlintSampler repeats enchanted_glint_item per pixel, and the
GLINT transform's scale 8 spans about 8 repeats across an item. Folding
the UV per vertex into the shared atlas could only interpolate between
the four corners, so the pattern rendered as one wide diagonal band
instead of the texture's fine weave. The glint now binds as its own
128x128 texture with a repeat, linear, mip-free sampler (the texture's
mcmeta: clamp false, blur true) in the HUD and entity pipelines, and
`Glint` passes the unwrapped `GLINT_TEXTURING` UV through.

Co-Authored-By: ZCode <agent@zcode.dev>
This commit is contained in:
masa-masa-0314
2026-10-27 01:45:23 +08:00
parent a1761dcb65
commit 1c6c6c0b97
8 changed files with 154 additions and 24 deletions
+4 -1
View File
@@ -27,6 +27,9 @@ struct Globals {
@group(0) @binding(2) var atlas_sampler: sampler;
@group(0) @binding(3) var lightmap: texture_2d<f32>;
@group(0) @binding(4) var lightmap_sampler: sampler;
// enchanted_glint_item on its own repeating texture (vanilla's GlintSampler).
@group(0) @binding(5) var glint_tex: texture_2d<f32>;
@group(0) @binding(6) var glint_sampler: sampler;
struct Out {
@builtin(position) clip: vec4<f32>,
@location(0) uv: vec2<f32>,
@@ -214,7 +217,7 @@ fn crumbling_uv(uv: vec2<f32>, packed_origin: vec2<f32>, sprite_size: vec2<f32>,
// Enchantment glint: item.fsh's GLINT path, sampled from
// enchanted_glint_item at the `GLINT_TEXTURING` UV.
let alpha = globals.fog_ends.w;
let glint = alpha * textureSample(atlas, atlas_sampler, in.glint_uv);
let glint = alpha * textureSample(glint_tex, glint_sampler, in.glint_uv);
color = vec4(color.rgb + glint.rgb * glint.rgb, max(color.a, alpha));
}
let f = max(linear_fog(in.spherical, globals.fog.x, globals.fog.y),
+7
View File
@@ -13385,6 +13385,7 @@ fn build_renderer(
&assets.sprite_mips,
&assets.animations,
(&atlas.pixels, atlas.width, atlas.height),
(&atlas.glint.0, atlas.glint.1, atlas.glint.2),
(&pack.forcefield.2, pack.forcefield.0),
&gui.panorama,
prepared,
@@ -13411,6 +13412,7 @@ fn loading_renderer(
) -> Result<Renderer> {
static FORCEFIELD: [u8; 4] = [0; 4];
static EMPTY_HUD: [u8; 4] = [0, 0, 0, 255];
static EMPTY_GLINT: [u8; 4] = [0; 4];
let empty_panorama = horizon_client::gfx::panorama::Faces {
size: 1,
pixels: [0u8; 4].repeat(6),
@@ -13421,6 +13423,10 @@ fn loading_renderer(
Some(atlas) => (atlas.pixels.as_slice(), atlas.width, atlas.height),
None => (EMPTY_HUD.as_slice(), 1, 1),
};
let (glint_pixels, glint_width, glint_height) = match atlas {
Some(atlas) => (atlas.glint.0.as_slice(), atlas.glint.1, atlas.glint.2),
None => (EMPTY_GLINT.as_slice(), 1, 1),
};
// A glyph rasterized into an earlier texture is not in these pixels; the
// new texture gets it again when the character is drawn (see
// `build_renderer`).
@@ -13437,6 +13443,7 @@ fn loading_renderer(
&[],
&[],
(pixels, width, height),
(glint_pixels, glint_width, glint_height),
(&FORCEFIELD[..], 1),
panorama,
None,
+1 -2
View File
@@ -307,8 +307,7 @@ impl Game<'_> {
.sprites
.get("misc/enchanted_glint_item")
.copied()
.map(|rect| item_render::Glint {
rect,
.map(|_| item_render::Glint {
speed: self.settings.file.double("glintSpeed") as f32,
alpha: self.settings.file.double("glintStrength") as f32,
millis: self.started.elapsed().as_secs_f32() * 1000.0,
+70 -1
View File
@@ -1023,6 +1023,7 @@ impl Renderer {
sprite_mips: &[crate::mipmap::Settings],
animations: &[SpriteAnimation],
hud_atlas: (&[u8], u32, u32),
glint: (&[u8], u32, u32),
forcefield: (&[u8], u32),
panorama_faces: &panorama::Faces,
prepared: Option<PreparedTextures>,
@@ -1576,7 +1577,45 @@ impl Renderer {
atlas_pixels,
);
let atlas_view = atlas.create_view(&Default::default());
let mut hud = HudRenderer::new(&device, config.format.remove_srgb_suffix(), &atlas_view)?;
// vanilla's `GlintSampler`: enchanted_glint_item as its own texture,
// repeated per pixel (`clamp: false`), linear without mipmaps
// (`blur: true`).
let (glint_pixels, glint_width, glint_height) = glint;
let glint_texture = device.create_texture_with_data(
&queue,
&wgpu::TextureDescriptor {
label: Some("Enchanted glint"),
size: wgpu::Extent3d {
width: glint_width,
height: glint_height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8Unorm,
usage: wgpu::TextureUsages::TEXTURE_BINDING,
view_formats: &[],
},
wgpu::util::TextureDataOrder::LayerMajor,
glint_pixels,
);
let glint_view = glint_texture.create_view(&Default::default());
let glint_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
label: Some("Glint"),
address_mode_u: wgpu::AddressMode::Repeat,
address_mode_v: wgpu::AddressMode::Repeat,
mag_filter: wgpu::FilterMode::Linear,
min_filter: wgpu::FilterMode::Linear,
..Default::default()
});
let mut hud = HudRenderer::new(
&device,
config.format.remove_srgb_suffix(),
&atlas_view,
&glint_view,
&glint_sampler,
)?;
hud.atlas_texture = Some(atlas.clone());
let entity_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("Horizon entities"),
@@ -1622,6 +1661,20 @@ impl Renderer {
vf,
wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
),
entry(
5,
vf,
wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Float { filterable: true },
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
),
entry(
6,
vf,
wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
),
],
});
let weather = weather::Weather::new(
@@ -1658,6 +1711,14 @@ impl Renderer {
binding: 4,
resource: wgpu::BindingResource::Sampler(&lightmap_sampler),
},
wgpu::BindGroupEntry {
binding: 5,
resource: wgpu::BindingResource::TextureView(&glint_view),
},
wgpu::BindGroupEntry {
binding: 6,
resource: wgpu::BindingResource::Sampler(&glint_sampler),
},
],
});
let hand_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
@@ -1684,6 +1745,14 @@ impl Renderer {
binding: 4,
resource: wgpu::BindingResource::Sampler(&lightmap_sampler),
},
wgpu::BindGroupEntry {
binding: 5,
resource: wgpu::BindingResource::TextureView(&glint_view),
},
wgpu::BindGroupEntry {
binding: 6,
resource: wgpu::BindingResource::Sampler(&glint_sampler),
},
],
});
let entity_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
+42 -1
View File
@@ -67,6 +67,9 @@ pub struct HudAtlas {
pub fire_animations: Vec<(Rect, crate::sprites::SpriteAnimation)>,
/// The default font (bitmap sheets in this atlas, unifont rasterized on demand).
pub font: crate::font::Font,
/// `misc/enchanted_glint_item` as its own image: vanilla's `GlintSampler`
/// repeats it per pixel, which one sprite inside this atlas cannot.
pub glint: (Vec<u8>, u32, u32),
white: Rect,
}
/// Atlas rows reserved for glyphs rasterized at run time.
@@ -526,6 +529,7 @@ impl HudAtlas {
}
}
let white = sprites["horizon/white"];
let glint = Self::glint_texture(root, defaults);
let font = crate::font::Font::load(
root,
&sprites,
@@ -542,10 +546,20 @@ impl HudAtlas {
translucent,
flags,
fire_animations,
glint,
font,
white,
})
}
/// `misc/enchanted_glint_item.png` on its own, or a transparent texel if
/// the resource pack lacks it.
fn glint_texture(root: &Path, defaults: &Path) -> (Vec<u8>, u32, u32) {
let mut path = root.join("assets/minecraft/textures/misc/enchanted_glint_item.png");
if !path.exists() && defaults != root {
path = defaults.join("assets/minecraft/textures/misc/enchanted_glint_item.png");
}
decode(&path).map_or((vec![0; 4], 1, 1), |(w, h, pixels)| (pixels, w, h))
}
}
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
@@ -554,7 +568,8 @@ pub struct HudVertex {
pub uv: [f32; 2],
pub color: u32,
pub depth: f32,
/// The glint sprite UV (`GLINT_TEXTURING`), unused unless `uv.x >= 2`.
/// The `GLINT_TEXTURING` UV into the repeating glint texture, unused
/// unless `uv.x >= 2`.
pub glint_uv: [f32; 2],
}
/// The GUI Scale option (0 = auto).
@@ -1080,6 +1095,8 @@ impl HudRenderer {
device: &wgpu::Device,
format: wgpu::TextureFormat,
view: &wgpu::TextureView,
glint_view: &wgpu::TextureView,
glint_sampler: &wgpu::Sampler,
) -> Result<Self> {
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("Horizon HUD"),
@@ -1124,6 +1141,22 @@ impl HudRenderer {
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 3,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Float { filterable: true },
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 4,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
count: None,
},
],
});
let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
@@ -1142,6 +1175,14 @@ impl HudRenderer {
binding: 2,
resource: wgpu::BindingResource::Sampler(&sampler),
},
wgpu::BindGroupEntry {
binding: 3,
resource: wgpu::BindingResource::TextureView(glint_view),
},
wgpu::BindGroupEntry {
binding: 4,
resource: wgpu::BindingResource::Sampler(glint_sampler),
},
],
});
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
+4 -1
View File
@@ -7,6 +7,9 @@ struct Hud {
@group(0) @binding(0) var<uniform> hud: Hud;
@group(0) @binding(1) var atlas: texture_2d<f32>;
@group(0) @binding(2) var atlas_sampler: sampler;
// enchanted_glint_item on its own repeating texture (vanilla's GlintSampler).
@group(0) @binding(3) var glint_tex: texture_2d<f32>;
@group(0) @binding(4) var glint_sampler: sampler;
struct Out {
@builtin(position) clip: vec4<f32>,
@location(0) uv: vec2<f32>,
@@ -31,7 +34,7 @@ struct Out {
if in.uv.x >= 2.0 {
let mask = textureSample(atlas, atlas_sampler, in.uv - vec2(2.0, 0.0)).a;
if mask < 0.1 { discard; }
let glint = hud.size.w * textureSample(atlas, atlas_sampler, in.glint_uv);
let glint = hud.size.w * textureSample(glint_tex, glint_sampler, in.glint_uv);
return vec4(glint.rgb * glint.rgb, mask);
}
let c = textureSample(atlas, atlas_sampler, in.uv) * in.color;
+9 -12
View File
@@ -50,10 +50,9 @@ pub struct ItemQuad {
/// `MaterialInfo.lightEmission` (block light floor).
pub light_emission: u8,
}
/// `TextureTransform.setupGlintTexturing`'s inputs and the glint sprite.
/// `TextureTransform.setupGlintTexturing`'s inputs.
#[derive(Clone, Copy, Debug)]
pub struct Glint {
pub rect: crate::hud::Rect,
/// `options.glintSpeed` (vanilla default 0.5).
pub speed: f32,
/// `options.glintStrength` / shader `GlintAlpha` (vanilla default 0.75).
@@ -65,21 +64,19 @@ pub struct Glint {
pub scale: f32,
}
impl Glint {
/// `TextureTransform.setupGlintTexturing(scale)`: translation(-o0, o1) *
/// rotateZ(PI/18) * scale, wrapped and mapped into the glint sprite.
pub fn atlas_uv(&self, model: [f32; 2]) -> [f32; 2] {
/// `TextureTransform.setupGlintTexturing(scale)` applied to the model UV:
/// translation(-o0, o1) * rotateZ(PI/18) * scale. The result repeats:
/// the shader samples the dedicated glint texture with vanilla's
/// repeating `GlintSampler`, so the UVs stay unwrapped.
pub fn uv(&self, model: [f32; 2]) -> [f32; 2] {
let millis = (f64::from(self.millis) * f64::from(self.speed) * 8.0) as i64;
let offset0 = (millis % 110000) as f32 / 110000.0;
let offset1 = (millis % 30000) as f32 / 30000.0;
let angle = std::f32::consts::PI / 18.0;
let (sin, cos) = angle.sin_cos();
let x = model[0] * self.scale * cos - model[1] * self.scale * sin - offset0;
let y = model[0] * self.scale * sin + model[1] * self.scale * cos + offset1;
let u = (x - x.floor()).clamp(0.5 / self.rect.w as f32, 1.0 - 0.5 / self.rect.w as f32);
let v = (y - y.floor()).clamp(0.5 / self.rect.h as f32, 1.0 - 0.5 / self.rect.h as f32);
[
self.rect.u0 + u * (self.rect.u1 - self.rect.u0),
self.rect.v0 + v * (self.rect.v1 - self.rect.v0),
model[0] * self.scale * cos - model[1] * self.scale * sin - offset0,
model[0] * self.scale * sin + model[1] * self.scale * cos + offset1,
]
}
}
@@ -739,7 +736,7 @@ impl ItemSink for WorldSink<'_> {
};
let overlay = if glint.is_some() { 7 } else { 0 };
for (i, (p, uv)) in q.pos.iter().zip(q.uv).enumerate() {
let glint_uv = glint.map_or([0.0, 0.0], |glint| glint.atlas_uv(q.foil_uv[i]));
let glint_uv = glint.map_or([0.0, 0.0], |glint| glint.uv(q.foil_uv[i]));
self.out.push(Vertex {
normal: [0; 4],
pos: p.to_array(),
+17 -6
View File
@@ -111,7 +111,7 @@ fn emit(batch: &mut HudBatch, fan: &[Point], color: impl Fn(Vec2) -> u32, glint:
batch.quad_glint(
corners.map(|v| v.p.to_array()),
corners.map(|v| [v.uv[0] + 2.0, v.uv[1]]),
corners.map(|v| glint.atlas_uv(v.glint_uv)),
corners.map(|v| glint.uv(v.glint_uv)),
u32::MAX,
);
batch.blend_since(start, crate::hud::Blend::Additive);
@@ -157,7 +157,18 @@ pub fn draw(
return;
}
let start = batch.vertices.len();
draw_now(state, renderer, sprites, host, batch, x, y, time);
draw_now(
state,
renderer,
sprites,
host,
batch,
x,
y,
time,
glint_speed,
glint_alpha,
);
if let Some(key) = key
&& let Ok(mut drawn) = renderer.gui_drawn.lock()
{
@@ -231,6 +242,8 @@ fn draw_now(
x: f32,
y: f32,
time: f32,
glint_speed: f32,
glint_alpha: f32,
) {
let pose = Mat4::from_translation(Vec3::new(x + 8.0, y + 8.0, 0.0))
* Mat4::from_scale(Vec3::new(16.0, -16.0, 16.0));
@@ -252,10 +265,8 @@ fn draw_now(
[x, y, x + 16.0, y + 16.0]
};
let glint = sprites
.get("misc/enchanted_glint_item")
.copied()
.map(|rect| Glint {
rect,
.contains_key("misc/enchanted_glint_item")
.then_some(Glint {
speed: glint_speed,
alpha: glint_alpha,
millis: time * 1000.0,