alex/beta-minecraft-rust
Minecraft Beta 1.7.3 Rewrite
BRANCH main
SNAPSHOT Draw terrain GPU-driven from packed per-section buffers with GPU occlusion culling, add a render distance slider (2-128) with a quad/disk shape, cut chunk bookkeeping at high render distances, and fix a light underflow at y=0
DOWNLOAD
root crates client src render scene map.rs
use super::Geometry;
use common::math::cos;
use glam::{Mat4, Vec3};
impl Geometry {
/// `bob` already sways with the swing.
pub(super) fn held_map(
&mut self,
bob: Mat4,
map: Option<&common::map::Map>,
equipped: f32,
[root, squared]: [f32; 2],
pitch: f32,
brightness: f32,
) {
let tilt =
-cos((1.0 - pitch / 45.0 + 0.1).clamp(0.0, 1.0) * std::f32::consts::PI) * 0.5 + 0.5;
let mut transform =
bob * Mat4::from_translation(Vec3::new(
0.0,
-(1.0 - equipped) * 1.2 - tilt * 0.5 + 0.04,
-0.72,
)) * Mat4::from_rotation_y(90_f32.to_radians())
* Mat4::from_rotation_z((-tilt * 85.0).to_radians());
for sign in [-1.0, 1.0] {
let hand = transform
* Mat4::from_translation(Vec3::new(0.0, -0.6, 1.1 * sign))
* Mat4::from_rotation_x((-45.0_f32 * sign).to_radians())
* Mat4::from_rotation_z((-31.0_f32).to_radians())
* Mat4::from_rotation_y((-65.0_f32 * sign).to_radians());
self.arm(hand, brightness);
}
transform *= Mat4::from_rotation_y((-squared * 20.0).to_radians())
* Mat4::from_rotation_z((-root * 20.0).to_radians())
* Mat4::from_rotation_x((-root * 80.0).to_radians())
* Mat4::from_scale(Vec3::splat(0.38))
* Mat4::from_rotation_y(90_f32.to_radians())
* Mat4::from_rotation_z(180_f32.to_radians())
* Mat4::from_translation(Vec3::new(-1.0, -1.0, 0.0))
* Mat4::from_scale(Vec3::splat(1.0 / 64.0));
self.map_transform = Some(transform);
let color = super::gray(brightness);
let quad = |x0, y0, x1, y1, z| {
[[x0, y1], [x1, y1], [x1, y0], [x0, y0]]
.map(|[x, y]| transform.transform_point3(Vec3::new(x, y, z)))
};
self.quad(
quad(-7.0, -7.0, 135.0, 135.0, 0.0),
[0.0, 1.0, 1.0, -1.0],
crate::assets::entity_texture("misc/mapbg.png"),
color,
);
self.quad(
quad(0.0, 0.0, 128.0, 128.0, -0.01),
[0.0, 1.0, 1.0, -1.0],
-2,
color,
);
if let Some(map) = map {
for &[packed, x, z] in &map.icons {
let icon = packed as u8 & 15;
let rotation = f32::from((packed as u8) >> 4) * std::f32::consts::TAU / 16.0;
let icon_transform = transform
* Mat4::from_translation(Vec3::new(
f32::from(x) / 2.0 + 64.0,
f32::from(z) / 2.0 + 64.0,
-0.02,
))
* Mat4::from_rotation_z(rotation)
* Mat4::from_scale(Vec3::new(4.0, 4.0, 3.0))
* Mat4::from_translation(Vec3::new(-0.125, 0.125, 0.0));
let points = [[-1.0, 1.0], [1.0, 1.0], [1.0, -1.0], [-1.0, -1.0]]
.map(|[x, y]| icon_transform.transform_point3(Vec3::new(x, y, 0.0)));
self.quad(
points,
[
f32::from(icon % 4) / 4.0,
f32::from(icon / 4) / 4.0,
0.25,
0.25,
],
crate::assets::entity_texture("misc/mapicons.png"),
color,
);
}
}
}
}
impl Geometry {
pub fn map_label(&mut self, font: &crate::gui::Geometry) {
let (Some(id), Some(transform)) = (self.map, self.map_transform) else {
return;
};
let start = self.vertices.len() as u32;
let glyphs = font.layout(&format!("map_{id}"), 0xff000000).0;
self.glyphs(glyphs, Vec3::new(0.0, 0.0, -0.04), transform);
self.map_text = start..self.vertices.len() as u32;
}
}