99 lines
2.7 KiB
Rust
99 lines
2.7 KiB
Rust
use macroquad::prelude::Mat2;
|
|
use macroquad::prelude::draw_triangle;
|
|
use macroquad::prelude::DVec2;
|
|
use macroquad::prelude::draw_mesh;
|
|
use macroquad::prelude::Vec3;
|
|
use macroquad::models::Vertex;
|
|
use macroquad::prelude::Mesh;
|
|
use macroquad::prelude::Texture2D;
|
|
use macroquad::prelude::Vec2;
|
|
use macroquad::prelude::Image;
|
|
use macroquad::prelude::Color;
|
|
|
|
//pub const LIGHT_COLOR: Color = Color::new(0.2980, 0.2824, 0.1686, 1.);
|
|
pub const WHITE: Color = Color::new(1., 1., 1., 1.);
|
|
pub const BLACK: Color = Color::new(0., 0., 0., 1.);
|
|
|
|
pub fn get_gradient(radius: f32) -> Texture2D {
|
|
let dimension = (radius as u16) * 2 + 1;
|
|
let center = Vec2::new(radius, radius);
|
|
let mut img = Image::gen_image_color(dimension, dimension, BLACK);
|
|
for x in 0..(dimension as u32) {
|
|
for y in 0..(dimension as u32) {
|
|
let pix = Vec2::new(x as f32, y as f32);
|
|
let intensity = f32::max(0., (radius - pix.distance(center)) / radius);
|
|
let mut color = WHITE;
|
|
color.a = intensity;
|
|
img.set_pixel(x, y, color);
|
|
}
|
|
}
|
|
|
|
Texture2D::from_image(&img)
|
|
}
|
|
|
|
pub fn draw_wall(a: Vec2, b: Vec2, light_source: Vec2, radius: f32) {
|
|
|
|
let a_shft = a - light_source;
|
|
let b_shft = b - light_source;
|
|
|
|
let mag_a = a_shft.length();
|
|
let mag_b = b_shft.length();
|
|
|
|
let a_prime = a_shft.clamp_length_min(mag_a + radius);
|
|
let b_prime = b_shft.clamp_length_min(mag_b + radius);
|
|
let opposite_src = a_prime.lerp(b_prime, 0.5)
|
|
.clamp_length_min(radius * 1.5);
|
|
|
|
let a_prime = light_source + a_prime;
|
|
let b_prime = light_source + b_prime;
|
|
let opposite_src = light_source + opposite_src;
|
|
|
|
draw_triangle(a, b_prime, b, BLACK);
|
|
draw_triangle(a, b_prime, a_prime, BLACK);
|
|
draw_triangle(b_prime, a_prime, opposite_src, BLACK);
|
|
}
|
|
|
|
pub fn wall_needs_to_be_drawn(a: Vec2, b: Vec2, light_source: Vec2, radius: f32) -> bool {
|
|
let a = a - light_source;
|
|
let b = b - light_source;
|
|
let delta = a - b;
|
|
let unit = delta / delta.length();
|
|
|
|
let a_dot = unit.dot(a);
|
|
let b_dot = unit.dot(b);
|
|
|
|
let d = Mat2::from_cols(unit, a).determinant();
|
|
|
|
(d < radius && d > -radius) && (
|
|
a.length() < radius ||
|
|
b.length() < radius ||
|
|
(a_dot < 0. && b_dot > 0.) || (a_dot > 0. && b_dot < 0.)
|
|
)
|
|
}
|
|
|
|
/*/// Code adapted from https://github.com/not-fl3/macroquad/issues/174
|
|
pub fn draw_polygon(points: &[Vec2], color: Color) {
|
|
let points_length = points.len();
|
|
let mut vertices = Vec::<Vertex>::with_capacity(points_length as usize + 2);
|
|
let mut indices = Vec::<u16>::with_capacity(points_length as usize * 3);
|
|
|
|
for (i, point) in points.iter().enumerate() {
|
|
let vertex = Vertex {
|
|
position: Vec3::new(point.x, point.y, 0.0),
|
|
uv: Vec2::default(),
|
|
color
|
|
};
|
|
|
|
vertices.push(vertex);
|
|
indices.extend_from_slice(&[0, i as u16 + 1, i as u16 + 2]);
|
|
}
|
|
|
|
let mesh = Mesh {
|
|
vertices,
|
|
indices,
|
|
texture: None,
|
|
};
|
|
|
|
draw_mesh(&mesh);
|
|
}*/
|