speed up gfx, colored lighting is out for now

This commit is contained in:
asrael 2026-02-05 02:42:58 -06:00
parent 3c3e961995
commit 01e6059dd1
17 changed files with 1055 additions and 250 deletions

View file

@ -5,8 +5,7 @@ use alloc::vec::Vec;
use libm::sqrtf;
use crate::bsp::{Bsp, BspBuilder, CellPortals, Edge, Face, Leaf, Node, Plane, Portal, Vertex};
use crate::math::{Vec3, Vec3Ext};
use crate::pxl8::{pxl8_vec3_cross, pxl8_vec3_dot, pxl8_vec3_normalize, pxl8_vec3_scale, pxl8_vec3_add, pxl8_vec3_sub};
use crate::math::Vec3;
pub const CELL_SIZE: f32 = 64.0;
pub const WALL_HEIGHT: f32 = 128.0;
@ -408,11 +407,11 @@ const AO_RAY_LENGTH: f32 = 48.0;
fn generate_hemisphere_samples(normal: Vec3) -> [Vec3; AO_NUM_SAMPLES] {
let tangent = if normal.y.abs() < 0.9 {
unsafe { pxl8_vec3_normalize(pxl8_vec3_cross(normal, Vec3::new(0.0, 1.0, 0.0))) }
normal.cross(Vec3::new(0.0, 1.0, 0.0)).normalize()
} else {
unsafe { pxl8_vec3_normalize(pxl8_vec3_cross(normal, Vec3::new(1.0, 0.0, 0.0))) }
normal.cross(Vec3::new(1.0, 0.0, 0.0)).normalize()
};
let bitangent = unsafe { pxl8_vec3_cross(normal, tangent) };
let bitangent = normal.cross(tangent);
let mut samples = [Vec3::new(0.0, 0.0, 0.0); AO_NUM_SAMPLES];
for i in 0..AO_NUM_SAMPLES {
@ -425,46 +424,44 @@ fn generate_hemisphere_samples(normal: Vec3) -> [Vec3; AO_NUM_SAMPLES] {
let local_y = cos_theta;
let local_z = sin_theta * sin_phi;
unsafe {
let t_contrib = pxl8_vec3_scale(tangent, local_x);
let n_contrib = pxl8_vec3_scale(normal, local_y);
let b_contrib = pxl8_vec3_scale(bitangent, local_z);
samples[i] = pxl8_vec3_normalize(pxl8_vec3_add(pxl8_vec3_add(t_contrib, n_contrib), b_contrib));
}
let t_contrib = tangent * local_x;
let n_contrib = normal * local_y;
let b_contrib = bitangent * local_z;
samples[i] = (t_contrib + n_contrib + b_contrib).normalize();
}
samples
}
fn ray_triangle_intersect(origin: Vec3, dir: Vec3, v0: Vec3, v1: Vec3, v2: Vec3, max_dist: f32) -> bool {
let edge1 = unsafe { pxl8_vec3_sub(v1, v0) };
let edge2 = unsafe { pxl8_vec3_sub(v2, v0) };
let h = unsafe { pxl8_vec3_cross(dir, edge2) };
let a = unsafe { pxl8_vec3_dot(edge1, h) };
let edge1 = v1 - v0;
let edge2 = v2 - v0;
let h = dir.cross(edge2);
let a = edge1.dot(h);
if a > -0.0001 && a < 0.0001 {
return false;
}
let f = 1.0 / a;
let s = unsafe { pxl8_vec3_sub(origin, v0) };
let u = f * unsafe { pxl8_vec3_dot(s, h) };
let s = origin - v0;
let u = f * s.dot(h);
if u < 0.0 || u > 1.0 {
return false;
}
let q = unsafe { pxl8_vec3_cross(s, edge1) };
let v = f * unsafe { pxl8_vec3_dot(dir, q) };
let q = s.cross(edge1);
let v = f * dir.dot(q);
if v < 0.0 || u + v > 1.0 {
return false;
}
let t = f * unsafe { pxl8_vec3_dot(edge2, q) };
let t = f * edge2.dot(q);
t > 0.001 && t < max_dist
}
fn compute_vertex_ao(bsp: &BspBuilder, pos: Vec3, normal: Vec3) -> f32 {
let samples = generate_hemisphere_samples(normal);
let offset_pos = unsafe { pxl8_vec3_add(pos, pxl8_vec3_scale(normal, 0.5)) };
let offset_pos = pos + normal * 0.5;
let mut occluded = 0;
@ -526,16 +523,15 @@ fn compute_vertex_light(
let mut total = 0.0;
for light in lights {
let to_light = unsafe { pxl8_vec3_sub(light.position, pos) };
let dist = unsafe { pxl8_vec3_dot(to_light, to_light) };
let dist = sqrtf(dist).max(1.0);
let to_light = light.position - pos;
let dist = sqrtf(to_light.dot(to_light)).max(1.0);
if dist > light.radius {
continue;
}
let light_dir = unsafe { pxl8_vec3_normalize(to_light) };
let ndotl = unsafe { pxl8_vec3_dot(normal, light_dir) }.max(0.0);
let light_dir = to_light.normalize();
let ndotl = normal.dot(light_dir).max(0.0);
let attenuation = (1.0 - dist / light.radius).max(0.0);
let attenuation = attenuation * attenuation;