Draw End portals and gateways with vanilla's end portal shader

The entity shader gains rendertype_end_portal as layers 9 (15 portal
layers) and 10 (16 for gateways): end_sky and the rotating, scrolling
layers of end_portal.png projected from the screen, repeated inside their
atlas sprites, with vanilla's GameTime uniform. The world-space
approximation and its portal block scan are gone; the block-entity
renderer draws both blocks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-12 13:16:32 +02:00
parent 613702c0a7
commit 51d4598632
5 changed files with 78 additions and 221 deletions
+52 -1
View File
@@ -11,6 +11,10 @@ struct Globals {
sun: vec4<f32>,
// x: sky fog end, y: cloud fog end
fog_ends: vec4<f32>,
// Atlas rects (u0, v0, width, height) of end_sky.png and end_portal.png.
end_portal_sprites: array<vec4<f32>, 2>,
// x: vanilla's GameTime ((game time % 24000 + partial tick) / 24000)
time: vec4<f32>,
}
@group(0) @binding(0) var<uniform> globals: Globals;
@group(0) @binding(1) var atlas: texture_2d<f32>;
@@ -27,11 +31,14 @@ struct Out {
@location(4) @interpolate(flat) overlay: u32,
// Overlay 6: the sprite's v origin and height, for v repeated inside it.
@location(6) @interpolate(flat) wrap: vec2<f32>,
// Overlays 9 and 10: projection_from_position(gl_Position), the End portal's texProj0.
@location(7) proj: vec4<f32>,
}
@vertex fn vs_entity(@location(0) position: vec3<f32>, @location(1) uv: vec2<f32>,
@location(2) overlay: u32, @location(3) color: vec4<f32>, @location(4) light: vec4<u32>) -> Out {
var out: Out;
out.clip = globals.view_proj * vec4(position, 1.0);
out.proj = vec4(out.clip.xy * 0.5 + out.clip.w * 0.5, out.clip.zw);
out.uv = uv;
let l = vec2(f32(light.y), f32(light.x));
let lm = textureSampleLevel(lightmap, lightmap_sampler,
@@ -52,6 +59,41 @@ struct Out {
if overlay == 4u { out.spherical = length(position); }
return out;
}
// rendertype_end_portal.fsh: the layer colours.
const PORTAL_COLORS = array<vec3<f32>, 16>(
vec3(0.022087, 0.098399, 0.110818),
vec3(0.011892, 0.095924, 0.089485),
vec3(0.027636, 0.101689, 0.100326),
vec3(0.046564, 0.109883, 0.114838),
vec3(0.064901, 0.117696, 0.097189),
vec3(0.063761, 0.086895, 0.123646),
vec3(0.084817, 0.111994, 0.166380),
vec3(0.097489, 0.154120, 0.091064),
vec3(0.106152, 0.131144, 0.195191),
vec3(0.097721, 0.110188, 0.187229),
vec3(0.133516, 0.138278, 0.148582),
vec3(0.070006, 0.243332, 0.235792),
vec3(0.196766, 0.142899, 0.214696),
vec3(0.047281, 0.315338, 0.321970),
vec3(0.204675, 0.390010, 0.302066),
vec3(0.080955, 0.314821, 0.661491),
);
// textureProj on a repeating texture that lives in the atlas at `rect`.
fn portal_sample(rect: vec4<f32>, p: vec4<f32>) -> vec3<f32> {
let st = fract(p.xy / p.w);
return textureSampleLevel(atlas, atlas_sampler, rect.xy + st * rect.zw, 0.0).rgb;
}
// texProj0 * end_portal_layer(layer): the row vector through mat4(scale * rotate),
// translate and SCALE_TRANSLATE in turn.
fn portal_layer(p: vec4<f32>, layer: f32) -> vec4<f32> {
let a = radians((layer * layer * 4321.0 + layer * 9.0) * 2.0);
let c = cos(a);
let s = sin(a);
let k = (4.5 - layer / 4.0) * 2.0;
var v = vec4(k * (c * p.x - s * p.y), k * (s * p.x + c * p.y), p.z, p.w);
v = vec4(v.x + 17.0 / layer * v.w, v.y + (2.0 + layer / 1.5) * (globals.time.x * 1.5) * v.w, v.z, v.w);
return vec4(0.5 * v.x + 0.25 * v.w, 0.5 * v.y + 0.25 * v.w, v.z, v.w);
}
fn linear_fog(d: f32, start: f32, end: f32) -> f32 {
if d <= start { return 0.0; }
if d >= end { return 1.0; }
@@ -61,7 +103,16 @@ fn linear_fog(d: f32, start: f32, end: f32) -> f32 {
var uv = in.uv;
if in.overlay == 6u { uv.y = in.wrap.x + fract(in.uv.y) * in.wrap.y; }
var color = textureSample(atlas, atlas_sampler, uv) * in.color;
if in.overlay == 4u {
if in.overlay == 9u || in.overlay == 10u {
// rendertype_end_portal: end_sky and PORTAL_LAYERS (15 for portals, 16
// for gateways) layers of end_portal.png projected from the screen.
let layers = select(15, 16, in.overlay == 10u);
var c = portal_sample(globals.end_portal_sprites[0], in.proj) * PORTAL_COLORS[0];
for (var i = 0; i < layers; i++) {
c += portal_sample(globals.end_portal_sprites[1], portal_layer(in.proj, f32(i + 1))) * PORTAL_COLORS[i];
}
color = vec4(c, 1.0);
} else if in.overlay == 4u {
// clouds.fsh: alpha fades out towards the cloud fog end.
return vec4(color.rgb, color.a * (1.0 - linear_fog(in.spherical, 0.0, globals.fog_ends.y)));
}
+8
View File
@@ -7080,6 +7080,14 @@ impl Game<'_> {
hand: &hand,
screen: &screen,
game_time: self.world.game_time as f32,
shader_game_time: ((self.world.game_time % 24000) as f32 + alpha as f32) / 24000.0,
end_portal_sprites: ["environment/end_sky", "entity/end_portal/end_portal"].map(|n| {
self.res
.atlas
.sprites
.get(n)
.map_or([0.0; 4], |r| [r.u0, r.v0, r.u1 - r.u0, r.v1 - r.v0])
}),
ticks: self.ticks,
darkness: self.darkness_lighting(alpha as f32),
night_vision: self.night_vision_intensity(),
+3 -220
View File
@@ -37,10 +37,6 @@ pub(super) struct EndState {
job: Option<GatewayJob>,
/// Ticks until the player can use a gateway again (TheEndGatewayBlockEntity cooldown).
gateway_cooldown: u32,
/// End portal and gateway blocks per loaded chunk (true = gateway), for rendering.
portals: HashMap<(i32, i32), Vec<([i32; 3], bool)>>,
/// Chunks left to scan for portal blocks in the current round.
scan: Vec<(i32, i32)>,
/// The Overworld generator, for stronghold positions (ServerLevel.findNearestMapStructure).
locator: std::sync::Mutex<Option<Arc<VanillaTerrain>>>,
/// Thrown eyes of ender in flight.
@@ -132,11 +128,6 @@ impl EyeOfEnder {
}
}
/// Chunks scanned for portal blocks per tick.
const SCAN_PER_TICK: usize = 6;
/// Radius (chunks) around the player in which portal blocks are rendered.
const PORTAL_RADIUS: i32 = 10;
impl Game<'_> {
/// The environment attributes of the current dimension.
pub(super) fn dim_environment(&self) -> &Environment {
@@ -295,7 +286,6 @@ impl Game<'_> {
)?;
}
}
self.rescan_portals_around(c, 1);
// Global LevelEvent 1038: END_PORTAL_SPAWN.
self.audio
.play("minecraft:block.end_portal.spawn", None, 1.0, 1.0);
@@ -350,7 +340,6 @@ impl Game<'_> {
self.tick_fight();
self.tick_dragon_fireballs();
}
self.scan_portals();
self.tick_eyes();
if self.dim == Dim::End {
self.end.flash.tick(self.world.game_time);
@@ -645,7 +634,6 @@ impl Game<'_> {
let id = registry.resolve(&state)?;
self.level_set([origin[0] + d[0], origin[1] + 2, origin[2] + d[1]], id)?;
}
self.rescan_portals_around(origin, 4);
Ok(())
}
@@ -669,7 +657,6 @@ impl Game<'_> {
}
}
}
self.rescan_portals_around(origin, 0);
Ok(())
}
@@ -887,214 +874,10 @@ impl Game<'_> {
Ok(())
}
/// Scan a few loaded chunks per tick for End portal and gateway blocks.
fn scan_portals(&mut self) {
let Some(p) = self.player.as_ref() else {
return;
};
let (pcx, pcz) = (
(p.position.x.floor() as i32) >> 4,
(p.position.z.floor() as i32) >> 4,
);
let chunks = &self.world.chunks;
self.end.portals.retain(|k, _| chunks.contains_key(k));
if self.end.scan.is_empty() {
let mut queue: Vec<(i32, i32)> = chunks
.keys()
.copied()
.filter(|&(x, z)| {
(x - pcx).abs() <= PORTAL_RADIUS && (z - pcz).abs() <= PORTAL_RADIUS
})
.collect();
// Nearest last: they are popped first.
queue.sort_by_key(|&(x, z)| std::cmp::Reverse((x - pcx).pow(2) + (z - pcz).pow(2)));
self.end.scan = queue;
}
for _ in 0..SCAN_PER_TICK {
let Some((cx, cz)) = self.end.scan.pop() else {
break;
};
self.scan_portal_chunk(cx, cz);
}
}
fn scan_portal_chunk(&mut self, cx: i32, cz: i32) {
let registry = self.res.assets.registry;
let (Some(portal), Some(gateway)) = (
registry.default_state("minecraft:end_portal"),
registry.default_state("minecraft:end_gateway"),
) else {
return;
};
let (portal, gateway) = (portal.0 as u16, gateway.0 as u16);
let Some(chunk) = self.world.chunks.get(&(cx, cz)) else {
self.end.portals.remove(&(cx, cz));
return;
};
let mut found = Vec::new();
for (s, section) in chunk.sections.iter().enumerate() {
if !section
.blocks
.maybe_has(|v| v == u32::from(portal) || v == u32::from(gateway))
{
continue;
}
let mut blocks = Box::new([0u16; 4096]);
section.unpack(&mut blocks);
for (i, &b) in blocks.iter().enumerate() {
if b == portal || b == gateway {
let y = horizon_client::chunk::MIN_Y + s as i32 * 16 + (i >> 8) as i32;
found.push((
[
cx * 16 + (i & 15) as i32,
y,
cz * 16 + ((i >> 4) & 15) as i32,
],
b == gateway,
));
}
}
}
if found.is_empty() {
self.end.portals.remove(&(cx, cz));
} else {
self.end.portals.insert((cx, cz), found);
}
}
fn rescan_portals_around(&mut self, p: [i32; 3], r: i32) {
for cz in (p[2] - r) >> 4..=(p[2] + r) >> 4 {
for cx in (p[0] - r) >> 4..=(p[0] + r) >> 4 {
self.scan_portal_chunk(cx, cz);
}
}
}
/// End portal and gateway surfaces (TheEndPortalRenderer: the starfield
/// approximated by end_portal.png in world space, unanimated) and the End
/// crystals on the spikes, as camera-relative entity geometry.
/// The End crystals on the spikes and thrown eyes of ender, as
/// camera-relative entity geometry (End portals and gateways are block
/// entities).
pub(super) fn end_geometry(&self, camera: glam::DVec3, out: &mut Vec<Vertex>) {
let atlas = &self.res.atlas;
if let Some(r) = atlas.sprites.get("entity/end_portal/end_portal").copied() {
let full = (15 * 16) | ((15 * 16) << 8);
// Each block shows a quarter of the texture so neighbours join up.
let quarter = |a: i32, b: i32| {
let (fa, fb) = (a.rem_euclid(4) as f32 / 4.0, b.rem_euclid(4) as f32 / 4.0);
let (du, dv) = (r.u1 - r.u0, r.v1 - r.v0);
(
r.u0 + du * fa,
r.u0 + du * (fa + 0.25),
r.v0 + dv * fb,
r.v0 + dv * (fb + 0.25),
)
};
for blocks in self.end.portals.values() {
for &(p, gateway) in blocks {
let base = glam::DVec3::new(f64::from(p[0]), f64::from(p[1]), f64::from(p[2]))
- camera;
if base.length_squared() > 160.0 * 160.0 {
continue;
}
let b = base.as_vec3();
// Portal: top face at 12/16 and bottom at 6/16; gateway: the whole cube.
let (y0, y1) = if gateway { (0.0, 1.0) } else { (0.375, 0.75) };
let color = if gateway { 0xFFE0_E0A0 } else { 0xFFD0_E070 };
let mut quad =
|corners: [glam::Vec3; 4], (u0, u1, v0, v1): (f32, f32, f32, f32)| {
let uv = [[u0, v0], [u1, v0], [u1, v1], [u0, v1]];
for (c, uv) in corners.into_iter().zip(uv) {
out.push(Vertex {
pos: (b + c).to_array(),
uv,
layer: 0,
color,
light: full,
});
}
};
let v = glam::Vec3::new;
let open = |d: [i32; 3]| {
let n = [p[0] + d[0], p[1] + d[1], p[2] + d[2]];
!self
.res
.facts
.get(self.world.block(n[0], n[1], n[2]))
.solid_render
};
let uv_xz = quarter(p[0], p[2]);
if !gateway || open([0, 1, 0]) {
quad(
[
v(0.0, y1, 0.0),
v(1.0, y1, 0.0),
v(1.0, y1, 1.0),
v(0.0, y1, 1.0),
],
uv_xz,
);
}
if !gateway || open([0, -1, 0]) {
quad(
[
v(0.0, y0, 0.0),
v(1.0, y0, 0.0),
v(1.0, y0, 1.0),
v(0.0, y0, 1.0),
],
uv_xz,
);
}
if gateway {
let uv_x = quarter(p[2], p[1]);
let uv_z = quarter(p[0], p[1]);
if open([-1, 0, 0]) {
quad(
[
v(0.0, 0.0, 0.0),
v(0.0, 0.0, 1.0),
v(0.0, 1.0, 1.0),
v(0.0, 1.0, 0.0),
],
uv_x,
);
}
if open([1, 0, 0]) {
quad(
[
v(1.0, 0.0, 0.0),
v(1.0, 0.0, 1.0),
v(1.0, 1.0, 1.0),
v(1.0, 1.0, 0.0),
],
uv_x,
);
}
if open([0, 0, -1]) {
quad(
[
v(0.0, 0.0, 0.0),
v(1.0, 0.0, 0.0),
v(1.0, 1.0, 0.0),
v(0.0, 1.0, 0.0),
],
uv_z,
);
}
if open([0, 0, 1]) {
quad(
[
v(0.0, 0.0, 1.0),
v(1.0, 0.0, 1.0),
v(1.0, 1.0, 1.0),
v(0.0, 1.0, 1.0),
],
uv_z,
);
}
}
}
}
}
if self.dim == Dim::End {
self.end_crystal_geometry(camera, out);
}
+11
View File
@@ -26,6 +26,10 @@ struct Globals {
sun: [f32; 4],
/// x: sky fog end, y: cloud fog end, z: dark disc (camera below the horizon), w: unused.
fog_ends: [f32; 4],
/// Atlas rects (u0, v0, width, height) of end_sky.png and end_portal.png.
end_portal_sprites: [[f32; 4]; 2],
/// x: vanilla's `GameTime` uniform; yzw unused.
time: [f32; 4],
}
pub type SectionKey = (i32, i32, i32);
struct SectionGpu {
@@ -77,6 +81,11 @@ pub struct Frame<'a> {
/// Full-screen camera effects drawn over the world and hand, under the HUD.
pub screen: &'a [crate::screen_effects::ScreenQuad],
pub game_time: f32,
/// `GlobalSettingsUniform` GameTime: (game time % 24000 + partial tick) / 24000.
pub shader_game_time: f32,
/// Atlas rects (u0, v0, width, height) of end_sky.png and end_portal.png
/// (`rendertype_end_portal`'s Sampler0 and Sampler1).
pub end_portal_sprites: [[f32; 4]; 2],
pub ticks: u64,
/// The darkness effect on the lightmap: the brightness it takes off the
/// gamma option and the pulsing scale subtracted from the light.
@@ -1281,6 +1290,8 @@ impl Renderer {
if frame.dark_disc { 1.0 } else { 0.0 },
0.0,
],
end_portal_sprites: frame.end_portal_sprites,
time: [frame.shader_game_time, 0.0, 0.0, 0.0],
};
self.queue
.write_buffer(&self.globals, 0, bytemuck::bytes_of(&globals));
+4
View File
@@ -14,6 +14,10 @@ struct Globals {
sun: vec4<f32>,
// x: sky fog end, y: cloud fog end, z: dark disc flag
fog_ends: vec4<f32>,
// Atlas rects (u0, v0, width, height) of end_sky.png and end_portal.png.
end_portal_sprites: array<vec4<f32>, 2>,
// x: vanilla's GameTime ((game time % 24000 + partial tick) / 24000)
time: vec4<f32>,
}
@group(0) @binding(0) var<uniform> globals: Globals;
@group(0) @binding(1) var sprites: texture_2d_array<f32>;