Better shader org, prep for gi
This commit is contained in:
+165
-1
@@ -1,8 +1,16 @@
|
||||
use std::f32::EPSILON;
|
||||
use std::f32::INFINITY;
|
||||
use std::fs::File;
|
||||
use std::path::Path;
|
||||
|
||||
use glam::Vec3;
|
||||
use itertools::Itertools;
|
||||
use noise::Fbm;
|
||||
use noise::NoiseFn;
|
||||
use noise::Perlin;
|
||||
use noise::Simplex;
|
||||
use noise::Worley;
|
||||
use rand::random;
|
||||
|
||||
use crate::sparse_tree::Color;
|
||||
use crate::voxel_cache::data::ExplicitNTreeNode;
|
||||
@@ -53,6 +61,7 @@ where
|
||||
|
||||
let mut children = vec![];
|
||||
let mut children_color = vec![];
|
||||
let mut children_normal = vec![];
|
||||
|
||||
let gnx = nx * node_size;
|
||||
let gny = ny * node_size;
|
||||
@@ -95,6 +104,7 @@ where
|
||||
|
||||
// let alpha = 1.;
|
||||
children.push(StructurePointer::new(subdivided, subdivided || alpha != 0.));
|
||||
children_normal.push((0., 0., 0.));
|
||||
children_color.push(Color(
|
||||
gvx as f32 / global_size as f32,
|
||||
gvy as f32 / global_size as f32,
|
||||
@@ -106,6 +116,7 @@ where
|
||||
ExplicitNTreeNode {
|
||||
structure: std::array::from_fn(|i| children[i]),
|
||||
colors: std::array::from_fn(|i| children_color[i]),
|
||||
normals: std::array::from_fn(|i| children_normal[i]),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -136,6 +147,7 @@ where
|
||||
let global_size = N.pow(self.chunk_power as u32);
|
||||
|
||||
let mut children = vec![];
|
||||
let mut children_normal = vec![];
|
||||
let mut children_color = vec![];
|
||||
|
||||
let gnx = nx * node_size;
|
||||
@@ -210,6 +222,7 @@ where
|
||||
};
|
||||
}
|
||||
|
||||
children_normal.push((0., 0., 0.));
|
||||
children_color.push(Color(
|
||||
(gcx + child_size / 2) as f32 / global_size as f32,
|
||||
(gcy + child_size / 2) as f32 / global_size as f32,
|
||||
@@ -221,6 +234,7 @@ where
|
||||
ExplicitNTreeNode {
|
||||
structure: std::array::from_fn(|i| children[i]),
|
||||
colors: std::array::from_fn(|i| children_color[i]),
|
||||
normals: std::array::from_fn(|i| children_normal[i]),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -418,6 +432,7 @@ where
|
||||
|
||||
let mut children = vec![StructurePointer(0); N * N * N];
|
||||
let mut children_color = vec![Color(0., 0., 0., 0.); N * N * N];
|
||||
let mut children_normals = vec![(0., 0., 0.); N * N * N];
|
||||
|
||||
let gnx = chunk_pos.0 * global_size + nx * node_size;
|
||||
let gny = chunk_pos.1 * global_size + ny * node_size;
|
||||
@@ -549,13 +564,16 @@ where
|
||||
}
|
||||
|
||||
children[index] = StructurePointer::new(subdivided, subdivided || alpha != 0.);
|
||||
children_color[index] = Color(color_avg.0, color_avg.1, color_avg.2, alpha);
|
||||
children_normals[index] = (0., 0., 0.);
|
||||
//children_color[index] = Color(color_avg.0, color_avg.1, color_avg.2, alpha);
|
||||
children_color[index] = Color(1., 1., 1., alpha);
|
||||
}
|
||||
}
|
||||
|
||||
ExplicitNTreeNode {
|
||||
structure: std::array::from_fn(|i| children[i]),
|
||||
colors: std::array::from_fn(|i| children_color[i]),
|
||||
normals: std::array::from_fn(|i| children_normals[i]),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -573,3 +591,149 @@ where
|
||||
chunk_pos: (usize, usize, usize),
|
||||
) -> ExplicitNTreeNode<N>;
|
||||
}
|
||||
|
||||
pub struct SdfGenerator<const N: usize>
|
||||
{
|
||||
chunk_power: usize,
|
||||
perlin: Perlin,
|
||||
}
|
||||
|
||||
impl<const N: usize> SdfGenerator<N>
|
||||
{
|
||||
pub fn new(chunk_power: usize) -> Self
|
||||
{
|
||||
Self {
|
||||
chunk_power,
|
||||
perlin: Perlin::new(67),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> ChunkedProducer<N> for SdfGenerator<N>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
fn produce_node(
|
||||
&self,
|
||||
depth: usize,
|
||||
nx: usize,
|
||||
ny: usize,
|
||||
nz: usize,
|
||||
chunk_pos: (usize, usize, usize),
|
||||
) -> ExplicitNTreeNode<N>
|
||||
{
|
||||
// let sdf = |x: f32, y: f32, z: f32| {
|
||||
// Vec3::new((x % 1024.) - 512., (y % 1024.) - 512., (z % 1024.) - 512.).length() - 512.
|
||||
// };
|
||||
|
||||
let sdf = |x: f32, y: f32, z: f32| {
|
||||
let tx = x / 512.;
|
||||
let ty = y / 512.;
|
||||
let tz = z / 512.;
|
||||
|
||||
let val = self.perlin.get([tx as f64, ty as f64, tz as f64]);
|
||||
let dist = val * 128.;
|
||||
return dist as f32;
|
||||
};
|
||||
|
||||
let node_size = N.pow((self.chunk_power - depth) as u32);
|
||||
let child_size = node_size / N;
|
||||
let global_size = N.pow(self.chunk_power as u32);
|
||||
|
||||
let mut children = vec![];
|
||||
let mut children_color = vec![];
|
||||
let mut children_normals = vec![];
|
||||
|
||||
let cx = chunk_pos.0 * global_size;
|
||||
let cy = chunk_pos.1 * global_size;
|
||||
let cz = chunk_pos.2 * global_size;
|
||||
let gnx = nx * node_size + cx;
|
||||
let gny = ny * node_size + cy;
|
||||
let gnz = nz * node_size + cz;
|
||||
// Iterate on children of this node
|
||||
for ((cz, cy), cx) in (0..N).cartesian_product(0..N).cartesian_product(0..N)
|
||||
{
|
||||
let gvx = gnx + cx * child_size + (child_size / 2);
|
||||
let gvy = gny + cy * child_size + (child_size / 2);
|
||||
let gvz = gnz + cz * child_size + (child_size / 2);
|
||||
|
||||
let dist = sdf(gvx as f32, gvy as f32, gvz as f32);
|
||||
let child_diagonal_length = (child_size as f32 / 2.) * f32::sqrt(3.);
|
||||
|
||||
let alpha;
|
||||
let subdivided;
|
||||
if dist.abs() < child_diagonal_length
|
||||
{
|
||||
//children.push(StructurePointer::new(depth <= 3, false, 0));
|
||||
subdivided = depth < (self.chunk_power - 1);
|
||||
alpha = if dist >= 0. { 0. } else { 1. };
|
||||
//alpha = 0.;
|
||||
}
|
||||
else if dist >= 0.
|
||||
{
|
||||
subdivided = false;
|
||||
alpha = 0.;
|
||||
}
|
||||
else
|
||||
{
|
||||
subdivided = false;
|
||||
alpha = 1.;
|
||||
}
|
||||
|
||||
let offset = 1. * (child_size as f32 / 2.);
|
||||
|
||||
let a = sdf(
|
||||
gvx as f32 + offset,
|
||||
gvy as f32 - offset,
|
||||
gvz as f32 - offset,
|
||||
);
|
||||
let b = sdf(
|
||||
gvx as f32 - offset,
|
||||
gvy as f32 - offset,
|
||||
gvz as f32 + offset,
|
||||
);
|
||||
let c = sdf(
|
||||
gvx as f32 - offset,
|
||||
gvy as f32 + offset,
|
||||
gvz as f32 - offset,
|
||||
);
|
||||
let d = sdf(
|
||||
gvx as f32 + offset,
|
||||
gvy as f32 + offset,
|
||||
gvz as f32 + offset,
|
||||
);
|
||||
|
||||
let normal = Vec3::new(a - b - c + d, -a - b + c + d, -a + b - c + d);
|
||||
|
||||
let normal = if normal.length() <= f32::EPSILON
|
||||
{
|
||||
Vec3::new(1., 0., 0.)
|
||||
}
|
||||
else
|
||||
{
|
||||
normal.normalize()
|
||||
};
|
||||
|
||||
let color = (normal.dot(Vec3::new(0., 1., 0.)) + 1.) * 0.5;
|
||||
|
||||
// let alpha = 1.;
|
||||
// children_color.push(Color(
|
||||
// (gvx - cx) as f32 / global_size as f32,
|
||||
// (gvy - cy) as f32 / global_size as f32,
|
||||
// (gvz - cz) as f32 / global_size as f32,
|
||||
// alpha,
|
||||
// ));
|
||||
children.push(StructurePointer::new(subdivided, subdivided || alpha != 0.));
|
||||
children_color.push(Color(color + random::<f32>() * 0.02, color, color, alpha));
|
||||
children_normals.push((normal.x, normal.y, normal.z));
|
||||
//children_color.push(Color(normal.x, normal.y, normal.z, alpha));
|
||||
}
|
||||
|
||||
ExplicitNTreeNode {
|
||||
structure: std::array::from_fn(|i| children[i]),
|
||||
colors: std::array::from_fn(|i| children_color[i]),
|
||||
normals: std::array::from_fn(|i| children_normals[i]),
|
||||
//normals: std::array::from_fn(|i| (1., 0., 0.)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user