use std::{
collections::HashMap,
io::{self, Write},
ops::{Add, Div, Mul, Sub},
};
pub const BRONZE_SHADOW: [f32; 3] = [34.0, 28.0, 19.0];
pub const BRONZE_BODY: [f32; 3] = [104.0, 86.0, 58.0];
pub const BRONZE_GLINT: [f32; 3] = [196.0, 170.0, 124.0];
pub const DARK_REFLECTION_CELL: f32 = 4.0;
pub const LIGHT_Y: f32 = -0.5;
pub const LIGHT_Z: f32 = 0.866_025_4;
pub const HALF_Y: f32 = -0.258_819_04;
pub const HALF_Z: f32 = 0.965_925_8;
pub const METAL_SHINE: f32 = 14.0;
pub const MIRROR_SHINE: f32 = 72.0;
pub const DARK_AMBIENT: f32 = 0.13;
pub const DARK_DIFFUSE_WEIGHT: f32 = 0.32;
pub const DARK_BROAD_WEIGHT: f32 = 0.01;
pub const DARK_BROAD_SHINE: f32 = 12.0;
pub const DARK_GLINT_WEIGHT: f32 = 3.0;
pub const DARK_GLINT_SHINE: f32 = 128.0;
pub const DARK_TONE_CEILING: f32 = 0.72;
pub const DARK_EXPOSURE: f32 = 1.20;
pub const EYE_Z: f32 = 80.0;
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Vec3 {
pub x: f32,
pub y: f32,
pub z: f32,
}
impl Vec3 {
pub const ZERO: Self = Self::new(0.0, 0.0, 0.0);
pub const fn new(x: f32, y: f32, z: f32) -> Self {
Self { x, y, z }
}
pub fn dot(self, rhs: Self) -> f32 {
self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
}
pub fn cross(self, rhs: Self) -> Self {
Self::new(
self.y * rhs.z - self.z * rhs.y,
self.z * rhs.x - self.x * rhs.z,
self.x * rhs.y - self.y * rhs.x,
)
}
pub fn length(self) -> f32 {
self.dot(self).sqrt()
}
pub fn normalized(self) -> Self {
self / self.length().max(f32::EPSILON)
}
pub fn rotate_z(self, angle: f32) -> Self {
let (sin, cos) = angle.sin_cos();
Self::new(
self.x * cos - self.y * sin,
self.x * sin + self.y * cos,
self.z,
)
}
pub const fn array(self) -> [f32; 3] {
[self.x, self.y, self.z]
}
}
impl Add for Vec3 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
}
}
impl Sub for Vec3 {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
}
}
impl Mul<f32> for Vec3 {
type Output = Self;
fn mul(self, rhs: f32) -> Self::Output {
Self::new(self.x * rhs, self.y * rhs, self.z * rhs)
}
}
impl Div<f32> for Vec3 {
type Output = Self;
fn div(self, rhs: f32) -> Self::Output {
Self::new(self.x / rhs, self.y / rhs, self.z / rhs)
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Vertex {
pub position: Vec3,
pub normal: Vec3,
}
impl Vertex {
pub const fn new(position: Vec3, normal: Vec3) -> Self {
Self { position, normal }
}
}
#[derive(Clone, Debug, Default)]
pub struct Model {
pub triangles: Vec<[Vertex; 3]>,
}
impl Model {
pub fn triangle(&mut self, a: Vertex, b: Vertex, c: Vertex) {
self.triangles.push([a, b, c]);
}
pub fn quad(&mut self, [a, b, c, d]: [Vertex; 4]) {
self.triangle(a, b, c);
self.triangle(a, c, d);
}
pub fn append(&mut self, mut rhs: Self) {
self.triangles.append(&mut rhs.triangles);
}
pub fn transformed(
&self,
position: impl Fn(Vec3) -> Vec3,
normal: impl Fn(Vec3) -> Vec3,
) -> Self {
Self {
triangles: self
.triangles
.iter()
.map(|triangle| {
triangle
.map(|vertex| Vertex::new(position(vertex.position), normal(vertex.normal)))
})
.collect(),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Pixel {
pub position: [f32; 2],
pub color: [u8; 4],
}
#[derive(Default)]
pub struct Mesh {
vertices: Vec<Pixel>,
indices: Vec<u32>,
intern: HashMap<([u32; 2], [u8; 4]), u32>,
}
#[derive(Clone, Copy, Debug)]
pub enum RustReach {
Module,
Crate,
Public,
}
#[derive(Clone, Copy, Debug)]
pub struct RustDialect {
vertex: &'static str,
mesh: &'static str,
}
impl RustDialect {
pub const FORGED: Self = Self::new("ForgedVertex", "ForgedMesh");
pub const fn new(vertex: &'static str, mesh: &'static str) -> Self {
Self { vertex, mesh }
}
}
impl RustReach {
const fn token(self) -> &'static str {
match self {
Self::Module => "pub(super)",
Self::Crate => "pub(crate)",
Self::Public => "pub",
}
}
}
impl Mesh {
pub fn vertices(&self) -> &[Pixel] {
&self.vertices
}
pub fn indices(&self) -> &[u32] {
&self.indices
}
pub fn vertex(&mut self, pixel: Pixel) -> u32 {
let key = (
[pixel.position[0].to_bits(), pixel.position[1].to_bits()],
pixel.color,
);
if let Some(index) = self.intern.get(&key) {
return *index;
}
assert!(
u32::try_from(self.vertices.len()).is_ok(),
"forged mesh exceeds u32 indexing"
);
let index = self.vertices.len() as u32;
self.vertices.push(pixel);
let _prior = self.intern.insert(key, index);
index
}
pub fn triangle(&mut self, pixels: [Pixel; 3]) {
let indices = pixels.map(|pixel| self.vertex(pixel));
self.indices.extend(indices);
}
}
pub fn emit_rust(
out: &mut impl Write,
name: &str,
mesh: &Mesh,
reach: RustReach,
) -> io::Result<()> {
emit_rust_as(out, name, mesh, reach, RustDialect::FORGED)
}
pub fn emit_rust_as(
out: &mut impl Write,
name: &str,
mesh: &Mesh,
reach: RustReach,
dialect: RustDialect,
) -> io::Result<()> {
assert!(
!name.is_empty()
&& name
.bytes()
.all(|byte| byte == b'_' || byte.is_ascii_uppercase() || byte.is_ascii_digit()),
"forged Rust binding must be an uppercase identifier"
);
let RustDialect {
vertex: vertex_type,
mesh: mesh_type,
} = dialect;
assert!(
[vertex_type, mesh_type]
.into_iter()
.all(valid_rust_type_name),
"forged Rust dialect must contain bare type identifiers"
);
writeln!(out, "static {name}_VERTICES: &[{vertex_type}] = &[")?;
for vertex in &mesh.vertices {
let [x, y] = vertex.position;
let [r, g, b, a] = vertex.color;
writeln!(
out,
"{vertex_type}::new([{}, {}], [{r}, {g}, {b}, {a}]),",
scalar(x),
scalar(y)
)?;
}
writeln!(out, "];")?;
writeln!(out, "static {name}_INDICES: &[u32] = &[")?;
for chunk in mesh.indices.chunks(24) {
for index in chunk {
write!(out, "{index},")?;
}
writeln!(out)?;
}
writeln!(out, "];")?;
writeln!(
out,
"{} static {name}: {mesh_type} = {mesh_type}::new({name}_VERTICES, {name}_INDICES);",
reach.token()
)
}
fn valid_rust_type_name(name: &str) -> bool {
name.bytes().enumerate().all(|(index, byte)| {
byte == b'_' || byte.is_ascii_alphabetic() || (index > 0 && byte.is_ascii_digit())
}) && !name.is_empty()
}
fn scalar(value: f32) -> String {
let bits = value.to_bits();
format!("f32::from_bits(0x{:04x}_{:04x})", bits >> 16, bits & 0xffff)
}
#[derive(Clone, Copy, Debug)]
pub enum Charge {
Bronze(f32),
Darkened(f32),
Polished,
}
pub fn forge(model: &Model, charge: Charge) -> Mesh {
compile_with(model, |vertex| match charge {
Charge::Bronze(exposure) => lit(vertex, exposure),
Charge::Darkened(exposure) => darkened_lit(vertex, exposure),
Charge::Polished => polished_lit(vertex),
})
}
pub fn compile_with(model: &Model, illuminate: impl Fn(Vertex) -> [u8; 4]) -> Mesh {
let mut facets = model
.triangles
.iter()
.filter(|triangle| visible(triangle))
.collect::<Vec<_>>();
facets.sort_by(|a, b| depth(a).total_cmp(&depth(b)));
let mut compiled = Mesh::default();
for triangle in facets {
compiled.triangle(triangle.map(|vertex| Pixel {
position: project(vertex.position),
color: illuminate(vertex),
}));
}
compiled
}
pub fn shadow(model: &Model, receiver_z: f32, alpha: u8) -> Mesh {
let light = Vec3::new(0.0, LIGHT_Y, LIGHT_Z);
let mut compiled = Mesh::default();
for triangle in &model.triangles {
let normal = triangle
.iter()
.fold(Vec3::ZERO, |sum, vertex| sum + vertex.normal)
.normalized();
if normal.dot(light) <= 0.0
|| triangle
.iter()
.any(|vertex| vertex.position.z <= receiver_z)
{
continue;
}
compiled.triangle(triangle.map(|vertex| {
let distance = (vertex.position.z - receiver_z) / LIGHT_Z;
Pixel {
position: project(vertex.position - light * distance),
color: [0, 0, 0, alpha],
}
}));
}
compiled
}
pub fn shadow_source(model: &Model, receiver_ceiling: f32, alpha: u8) -> Mesh {
let light = Vec3::new(0.0, LIGHT_Y, LIGHT_Z);
let slope = -LIGHT_Y / LIGHT_Z;
let mut compiled = Mesh::default();
for triangle in &model.triangles {
let normal = triangle
.iter()
.fold(Vec3::ZERO, |sum, vertex| sum + vertex.normal)
.normalized();
if normal.dot(light) <= 0.0
|| triangle
.iter()
.any(|vertex| vertex.position.z <= receiver_ceiling)
{
continue;
}
compiled.triangle(triangle.map(|vertex| Pixel {
position: [
vertex.position.x,
vertex.position.y + slope * vertex.position.z,
],
color: [0, 0, 0, alpha],
}));
}
compiled
}
pub fn project(point: Vec3) -> [f32; 2] {
let scale = EYE_Z / (EYE_Z - point.z).max(1.0);
[point.x * scale, point.y * scale]
}
pub fn visible(triangle: &[Vertex; 3]) -> bool {
let center = triangle
.iter()
.fold(Vec3::ZERO, |sum, vertex| sum + vertex.position)
/ 3.0;
let normal = triangle
.iter()
.fold(Vec3::ZERO, |sum, vertex| sum + vertex.normal)
.normalized();
normal.dot((Vec3::new(0.0, 0.0, EYE_Z) - center).normalized()) > 0.0
}
pub fn depth(triangle: &[Vertex; 3]) -> f32 {
triangle.iter().map(|vertex| vertex.position.z).sum::<f32>() / 3.0
}
pub fn lit(vertex: Vertex, exposure: f32) -> [u8; 4] {
expose(
bronze_rgb(metal_tone(vertex.position.array(), vertex.normal.array())),
exposure,
)
}
pub fn lit_with_key(vertex: Vertex, visibility: f32) -> [u8; 4] {
opaque(bronze_rgb(metal_tone_with_key(
vertex.position.array(),
vertex.normal.array(),
visibility,
)))
}
pub fn darkened_lit(vertex: Vertex, exposure: f32) -> [u8; 4] {
expose(
darkened_bronze_rgb(darkened_metal_tone(
vertex.position.array(),
vertex.normal.array(),
)),
exposure,
)
}
pub fn darkened_lit_with_key(vertex: Vertex, visibility: f32) -> [u8; 4] {
opaque(darkened_bronze_rgb(darkened_metal_tone_with_key(
vertex.position.array(),
vertex.normal.array(),
visibility,
)))
}
pub fn polished_lit(vertex: Vertex) -> [u8; 4] {
opaque(bronze_rgb(polished_metal_tone(
vertex.position.array(),
vertex.normal.array(),
)))
}
fn opaque([r, g, b]: [u8; 3]) -> [u8; 4] {
[r, g, b, 255]
}
fn expose(rgb: [u8; 3], exposure: f32) -> [u8; 4] {
let channel = |value: u8| (f32::from(value) * exposure).round().clamp(0.0, 255.0) as u8;
[channel(rgb[0]), channel(rgb[1]), channel(rgb[2]), 255]
}
pub fn bronze_rgb(tone: f32) -> [u8; 3] {
let tone = tone.clamp(0.0, 1.0);
let (lo, hi, t) = if tone < 0.6 {
(BRONZE_SHADOW, BRONZE_BODY, tone / 0.6)
} else {
(BRONZE_BODY, BRONZE_GLINT, (tone - 0.6) / 0.4)
};
let channel = |i: usize| (lo[i] + (hi[i] - lo[i]) * t).round() as u8;
[channel(0), channel(1), channel(2)]
}
pub fn darkened_bronze_rgb(tone: f32) -> [u8; 3] {
bronze_rgb(tone).map(|channel| {
(f32::from(channel) * DARK_EXPOSURE)
.round()
.clamp(0.0, 255.0) as u8
})
}
pub fn metal_tone(position: [f32; 3], normal: [f32; 3]) -> f32 {
metal_tone_with_key(position, normal, 1.0)
}
pub fn polished_metal_tone(position: [f32; 3], normal: [f32; 3]) -> f32 {
let (diffuse, reflection) = material_terms(position, normal);
(0.13 + 0.43 * diffuse + 1.85 * reflection.powf(MIRROR_SHINE)).min(1.0)
}
pub fn darkened_metal_tone(position: [f32; 3], normal: [f32; 3]) -> f32 {
darkened_metal_tone_with_key(position, normal, 1.0)
}
pub fn metal_tone_with_key(position: [f32; 3], normal: [f32; 3], key_visibility: f32) -> f32 {
let (diffuse, reflection) = material_terms(position, normal);
0.16 + key_visibility.clamp(0.0, 1.0) * (0.5 * diffuse + 0.8 * reflection.powf(METAL_SHINE))
}
pub fn darkened_metal_tone_with_key(
position: [f32; 3],
normal: [f32; 3],
key_visibility: f32,
) -> f32 {
let (diffuse, reflection) = material_terms(position, normal);
(DARK_AMBIENT
+ key_visibility.clamp(0.0, 1.0)
* (DARK_DIFFUSE_WEIGHT * diffuse
+ DARK_BROAD_WEIGHT * reflection.powf(DARK_BROAD_SHINE)
+ DARK_GLINT_WEIGHT * reflection.powf(DARK_GLINT_SHINE)))
.min(DARK_TONE_CEILING)
}
pub fn material_terms(position: [f32; 3], normal: [f32; 3]) -> (f32, f32) {
let normalize = |[x, y, z]: [f32; 3]| {
let length = (x * x + y * y + z * z).sqrt().max(f32::EPSILON);
[x / length, y / length, z / length]
};
let [nx, ny, nz] = normalize(normal);
let [vx, vy, vz] = normalize([-position[0], -position[1], EYE_Z - position[2]]);
let [hx, hy, hz] = normalize([vx, vy + LIGHT_Y, vz + LIGHT_Z]);
let diffuse = (ny * LIGHT_Y + nz * LIGHT_Z).max(0.0);
let reflection = (nx * hx + ny * hy + nz * hz).max(0.0);
(diffuse, reflection)
}