Minecraft: Java Edition 26.3 rewritten from scratch in Rust, checked against vanilla with 281 differential harnesses. Get it with the Horizon launcher.
Rust differential-testinggame-enginegamedevlinuxminecraftminecraft-javareimplementationrustvoxelvulkanwgpu
OptiFine emissive textures: blocks glow too
some-du6e7cf29b6238cd
diff --git a/crates/horizon-client-render/src/assets.rs b/crates/horizon-client-render/src/assets.rsindex caeef1df9..4a63bd075 100644--- a/crates/horizon-client-render/src/assets.rs+++ b/crates/horizon-client-render/src/assets.rs@@ -93,6 +93,8 @@ pub struct RenderAssets {pub model_gallery: Vec<Value>,pub animations: Vec<crate::sprites::SpriteAnimation>,pub sprite_layers: std::collections::BTreeMap<String, u32>,+ /// OptiFine's emissive block and item overlays: base sprite and overlay sprite names.+ pub emissive: Vec<(String, String)>,pub translucent_layers: Vec<bool>,/// Each layer's `MipmapGenerator` settings.pub sprite_mips: Vec<crate::mipmap::Settings>,@@ -242,8 +244,10 @@ impl RenderAssets {for stage in 0..10 {sprites.insert(root, &format!("minecraft:block/destroy_stage_{stage}"))?;}+ let mut emissive = Vec::new();let connected = if draws {crate::native_emissive::load(root, &mut sprites);+ emissive = crate::native_emissive::load_optifine(root, &mut sprites);// A side's sprite, for rules that connect by tile: the first quad facing it.let catalog = &models.catalog;let mut found = Vec::new();@@ -289,6 +293,7 @@ impl RenderAssets {model_gallery: models.states,animations: sprites.animations,sprite_layers: sprites.layers,+ emissive,translucent_layers: sprites.translucent,sprite_mips: sprites.mips,pbr_normals: sprites.normals,diff --git a/crates/horizon-client-render/src/entity.wgsl b/crates/horizon-client-render/src/entity.wgslindex 67134753c..f6d125197 100644--- a/crates/horizon-client-render/src/entity.wgsl+++ b/crates/horizon-client-render/src/entity.wgsl@@ -24,7 +24,7 @@ struct Globals {dynamic_lights: array<vec4<f32>, 16>,colored_lights: array<array<vec4<f32>, 2>, 16>,colored_count: vec4<u32>,- emissive_layers: array<vec4<u32>, 32>,+ emissive_layers: array<vec4<u32>, 128>,emissive_count: vec4<u32>,sunrise_fan: array<vec4<f32>, 32>,filtering: vec4<f32>,diff --git a/crates/horizon-client-render/src/native_emissive.rs b/crates/horizon-client-render/src/native_emissive.rsindex 2ed843ec9..fa15442e1 100644--- a/crates/horizon-client-render/src/native_emissive.rs+++ b/crates/horizon-client-render/src/native_emissive.rs@@ -1,8 +1,13 @@-//! Optional, texture-matched native overlays. No extra draw calls or world light changes.+//! Optional, texture-matched overlays drawn full bright over their base texture in the same+//! terrain fragment: native mods' (`<ns>:emissive/...`) and OptiFine's emissive block textures+//! (`block/stone_e.png` beside `block/stone.png`). No extra draw calls or world light changes.use crate::sprites::Sprites;use std::{collections::BTreeMap, path::Path};-pub const MAX: usize = 32;+/// Overlay pairs the terrain shader reads (`Globals.emissive_layers`, terrain.wgsl).+pub const MAX: usize = 128;+/// Native mods' own `emissive/` textures, as their API documents.+const NATIVE: usize = 32;/// Load only bounded, explicitly named emissive textures from the active pack.pub fn load(root: &Path, sprites: &mut Sprites) {@@ -12,7 +17,7 @@ pub fn load(root: &Path, sprites: &mut Sprites) {name.split_once(':').is_some_and(|(_, p)| p.starts_with("emissive/"))})- .take(MAX)+ .take(NATIVE).collect();names.sort();for name in names {@@ -23,6 +28,37 @@ pub fn load(root: &Path, sprites: &mut Sprites) {}}+/// OptiFine's emissive overlays of the block and item sprites already loaded: for each+/// `<ns>:block/<name>` whose `<name><suffix>.png` sits beside it, the overlay is loaded too and+/// paired with it, first by name, at most [`MAX`]. A pack without such files adds nothing.+pub fn load_optifine(root: &Path, sprites: &mut Sprites) -> Vec<(String, String)> {+ let suffix = horizon_optifine::emissive::suffix(root);+ let bases: Vec<String> = sprites+ .layers+ .keys()+ .filter(|name| {+ name.split_once(':').is_some_and(|(_, p)| {+ (p.starts_with("block/") || p.starts_with("item/")) && !p.ends_with(&suffix)+ })+ })+ .cloned()+ .collect();+ let mut pairs = Vec::new();+ for base in bases {+ if pairs.len() == MAX {+ break;+ }+ let overlay = format!("{base}{suffix}");+ let file = crate::atlas_sources::texture_path(root, &overlay, "textures");+ if std::fs::metadata(file).is_ok_and(|m| m.is_file() && m.len() <= 1 << 20)+ && sprites.insert(root, &overlay).is_ok()+ {+ pairs.push((base, overlay));+ }+ }+ pairs+}+/// Only loaded base/overlay pairs are sent to the GPU; sorted for vertex lookup.pub fn mappings<'a>(layers: &BTreeMap<String, u32>,@@ -40,6 +76,72 @@ pub fn mappings<'a>(#[cfg(test)]mod tests {use super::*;++ fn png(path: &Path) {+ std::fs::create_dir_all(path.parent().unwrap()).unwrap();+ let file = std::io::BufWriter::new(std::fs::File::create(path).unwrap());+ let mut encoder = png::Encoder::new(file, 16, 16);+ encoder.set_color(png::ColorType::Rgba);+ encoder.set_depth(png::BitDepth::Eight);+ let mut writer = encoder.write_header().unwrap();+ writer.write_image_data(&[255; 16 * 16 * 4]).unwrap();+ }++ #[test]+ fn optifine_overlays_pair_with_their_loaded_blocks() {+ let root =+ std::env::temp_dir().join(format!("horizon-block-emissive-{}", std::process::id()));+ let _ = std::fs::remove_dir_all(&root);+ let block = root.join("assets/minecraft/textures/block");+ for name in [+ "stone",+ "stone_e",+ "dirt",+ "glowstone_e",+ "deepslate",+ "deepslate_glow",+ ] {+ png(&block.join(format!("{name}.png")));+ }+ png(&root.join("assets/minecraft/textures/item/stick.png"));+ png(&root.join("assets/minecraft/textures/item/stick_e.png"));+ let mut sprites = Sprites::default();+ for name in ["stone", "dirt", "deepslate"] {+ sprites+ .insert(&root, &format!("minecraft:block/{name}"))+ .unwrap();+ }+ sprites.insert(&root, "minecraft:item/stick").unwrap();+ // glowstone isn't loaded: its overlay pairs with nothing.+ let pairs = load_optifine(&root, &mut sprites);+ assert_eq!(+ pairs,+ [+ (+ "minecraft:block/stone".into(),+ "minecraft:block/stone_e".into()+ ),+ (+ "minecraft:item/stick".into(),+ "minecraft:item/stick_e".into()+ ),+ ]+ );+ assert!(sprites.layers.contains_key("minecraft:block/stone_e"));+ // The pack's own suffix.+ let props = root.join("assets/minecraft/optifine/emissive.properties");+ std::fs::create_dir_all(props.parent().unwrap()).unwrap();+ std::fs::write(&props, "suffix.emissive=_glow").unwrap();+ let pairs = load_optifine(&root, &mut sprites);+ assert_eq!(+ pairs,+ [(+ "minecraft:block/deepslate".into(),+ "minecraft:block/deepslate_glow".into()+ )]+ );+ std::fs::remove_dir_all(&root).unwrap();+ }#[test]fn only_existing_pairs_are_mapped() {let layers = [diff --git a/crates/horizon-client-render/src/terrain.wgsl b/crates/horizon-client-render/src/terrain.wgslindex ecc5aea83..7abf46c30 100644--- a/crates/horizon-client-render/src/terrain.wgsl+++ b/crates/horizon-client-render/src/terrain.wgsl@@ -26,7 +26,7 @@ struct Globals {dynamic_lights: array<vec4<f32>, 16>,colored_lights: array<array<vec4<f32>, 2>, 16>,colored_count: vec4<u32>,- emissive_layers: array<vec4<u32>, 32>,+ emissive_layers: array<vec4<u32>, 128>,emissive_count: vec4<u32>,// The sunrise fan's 16 triangles (gfx/sunrise_fan.rs): xyz the apex weight's normal, w the// triangle's volume with the camera; then each rim vertex's normal against the apex.@@ -370,7 +370,7 @@ fn terrain_vertex(position: vec3<f32>, uv: vec2<f32>, layer: u32, color: vec4<f3out.uv = uv;out.layer = layer;out.emissive_layer = 0xffffffffu;- for (var i = 0u; i < min(globals.emissive_count.x, 32u); i++) {+ for (var i = 0u; i < min(globals.emissive_count.x, 128u); i++) {if globals.emissive_layers[i].x == (layer & 0x7fffu) {out.emissive_layer = globals.emissive_layers[i].y;break;diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rsindex bed4ee906..9e05d3c0d 100644--- a/crates/horizon-client/src/game.rs+++ b/crates/horizon-client/src/game.rs@@ -15107,10 +15107,13 @@ impl Game<'_> {let emissive_layers = if self.title.is_none() {let mut requests = Vec::new();self.native_emissive(&mut requests);+ // Native mods' overlays first, then the packs' OptiFine emissive blocks.+ let optifine = self.pack.render.emissive.iter();horizon_client::native_emissive::mappings(&self.pack.render.sprite_layers,requests.into_iter()+ .chain(optifine.map(|(base, overlay)| (base.as_str(), overlay.as_str()))).take(horizon_client::native_emissive::MAX),)} else {diff --git a/crates/horizon-optifine/src/emissive.rs b/crates/horizon-optifine/src/emissive.rsindex b1c6ba0e2..e367b4521 100644--- a/crates/horizon-optifine/src/emissive.rs+++ b/crates/horizon-optifine/src/emissive.rs@@ -41,22 +41,7 @@ const PROPERTIES: [&str; 3] = [impl Emissive {pub fn scan(root: &Path, random: &RandomTextures) -> Self {- // The first file that sets it (read as Java reads properties: Latin-1 when not UTF-8).- // A suffix is a name's end: no path separators or `..`, so it can't leave the folder.- let suffix = PROPERTIES- .iter()- .find_map(|p| {- let path = root.join(p);- if fs::metadata(&path).ok()?.len() > MAX_PROPERTIES {- return None;- }- let text = decode(fs::read(path).ok()?);- Properties::parse(&text)- .get("suffix.emissive")- .map(str::to_owned)- })- .filter(|s| !s.is_empty() && !s.contains(['/', '\\']) && !s.contains(".."))- .unwrap_or_else(|| "_e".to_owned());+ let suffix = suffix(root);let mut out = Self {suffix,..Self::default()@@ -132,6 +117,27 @@ impl Emissive {}}+/// The pack's emissive suffix: `suffix.emissive` from the first of [`PROPERTIES`] that sets it,+/// `_e` without one.+pub fn suffix(root: &Path) -> String {+ // The first file that sets it (read as Java reads properties: Latin-1 when not UTF-8).+ // A suffix is a name's end: no path separators or `..`, so it can't leave the folder.+ PROPERTIES+ .iter()+ .find_map(|p| {+ let path = root.join(p);+ if fs::metadata(&path).ok()?.len() > MAX_PROPERTIES {+ return None;+ }+ let text = decode(fs::read(path).ok()?);+ Properties::parse(&text)+ .get("suffix.emissive")+ .map(str::to_owned)+ })+ .filter(|s| !s.is_empty() && !s.contains(['/', '\\']) && !s.contains(".."))+ .unwrap_or_else(|| "_e".to_owned())+}+#[cfg(test)]mod tests {use super::*;