pixel_weaver/
sample_pixel_functions.rs

1use crate::image_functions::*;
2use crate::ImageData;
3use cgmath::*;
4
5/// This function renders a centered UV square
6pub fn uv_square(image_data: &ImageData, coord: &Vector2<u32>) -> Vector3<u8> {
7    let uv = uv(image_data, coord);
8    vec3(normal_to_rgb(uv.x), normal_to_rgb(uv.y), 0)
9}
10
11/// This function renders a faded black circle on a white background
12pub fn faded_circle(image_data: &ImageData, coord: &Vector2<u32>) -> Vector3<u8> {
13    let uv = uv(image_data, coord);
14    let len = length(uv);
15    let col = vec3(len, len, len);
16    f64_vector3_to_u8(col)
17}
18
19/// This function renders 3 spheres on top of a place
20pub fn spheres(image_data: &ImageData, pixel_cooray_direction: &Vector2<u32>) -> Vector3<u8> {
21    let uv = uv(&image_data, pixel_cooray_direction);
22
23    let ray_origin = vec3(0., 0., -3.);
24    let ray_direction = vec3(uv.x, uv.y, 1.).normalize();
25    let mut dist = 0.; 
26
27    for _ in 0..80 {
28        let p = ray_origin + ray_direction * dist;
29        let d: f64 = dist_to_surf(p);
30        dist += d;
31        if d < 0.001 {break;};
32        if dist > 1000.0 {break;};
33    }
34    dist *= 0.2;
35
36    f64_vector3_to_u8(vec3(dist, dist, dist))
37}
38
39fn dist_to_surf(p: Vector3<f64>) -> f64 {
40    let sphere = (p.x.powi(2) + p.y.powi(2) + p.z.powi(2)).sqrt() - 1.;
41    let sphere_2 = ((p.x + 1.0).powi(2) + (p.y + 1.0).powi(2) + (p.z + 1.0).powi(2)).sqrt() - 1.;
42    let sphere_3 = ((p.x - 1.0).powi(2) + (p.y - 1.0).powi(2) + (p.z - 1.5).powi(2)).sqrt() - 1.;
43    let plane = p.y + 1.;
44    f64::min(sphere, plane).min(sphere_2).min(sphere_3)
45}