use crate::core::BufferGeometry;
use crate::geometries::box_geometry;
use crate::math::Vector3;
fn js_sign(x: f64) -> f64 {
if x > 0.0 {
1.0
} else if x < 0.0 {
-1.0
} else {
x
}
}
fn angle_to(a: &Vector3, b: &Vector3) -> f64 {
let denominator = (a.length_sq() * b.length_sq()).sqrt();
if denominator == 0.0 {
return std::f64::consts::PI / 2.0;
}
let theta = a.dot(b) / denominator;
theta.clamp(-1.0, 1.0).acos()
}
#[derive(Clone, Copy)]
enum Axis {
X,
Y,
Z,
}
fn get(v: &Vector3, axis: Axis) -> f64 {
match axis {
Axis::X => v.x,
Axis::Y => v.y,
Axis::Z => v.z,
}
}
fn set(v: &mut Vector3, axis: Axis, value: f64) {
match axis {
Axis::X => v.x = value,
Axis::Y => v.y = value,
Axis::Z => v.z = value,
}
}
fn get_uv(
face_dir_vector: &Vector3,
normal: &Vector3,
uv_axis: Axis,
projection_axis: Axis,
radius: f64,
side_length: f64,
) -> f64 {
let tot_arc_length = 2.0 * std::f64::consts::PI * radius / 4.0;
let center_length = (side_length - 2.0 * radius).max(0.0);
let half_arc = std::f64::consts::PI / 4.0;
let mut temp_normal = *normal;
set(&mut temp_normal, projection_axis, 0.0);
temp_normal.normalize();
let arc_uv_ratio = 0.5 * tot_arc_length / (tot_arc_length + center_length);
let arc_angle_ratio = 1.0 - (angle_to(&temp_normal, face_dir_vector) / half_arc);
if js_sign(get(&temp_normal, uv_axis)) == 1.0 {
arc_angle_ratio * arc_uv_ratio
} else {
let len_uv = center_length / (tot_arc_length + center_length);
len_uv + arc_uv_ratio + arc_uv_ratio * (1.0 - arc_angle_ratio)
}
}
pub fn rounded_box_geometry(
width: f64,
height: f64,
depth: f64,
segments: usize,
radius: f64,
) -> BufferGeometry {
let total_segments = segments * 2 + 1;
let radius = (width / 2.0).min(height / 2.0).min(depth / 2.0).min(radius);
let mut geometry = box_geometry(
1.0,
1.0,
1.0,
total_segments,
total_segments,
total_segments,
);
if total_segments == 1 {
return geometry;
}
let geometry2 = geometry.to_non_indexed();
geometry.index = None;
let positions_attr = geometry2
.position()
.cloned()
.expect("three-rs: the box geometry has a position attribute");
let normals = geometry2
.normal()
.cloned()
.expect("three-rs: the box geometry has a normal attribute");
let uvs = geometry2
.uv()
.cloned()
.expect("three-rs: the box geometry has a uv attribute");
let mut position = Vector3::ZERO;
let mut normal;
let mut box_v = Vector3::new(width, height, depth);
box_v.divide_scalar(2.0);
box_v.x -= radius;
box_v.y -= radius;
box_v.z -= radius;
let mut positions = positions_attr.array_mut();
let mut normals_array = normals.array_mut();
let mut uvs_array = uvs.array_mut();
let face_tris = positions.len() / 6;
let mut face_dir_vector = Vector3::ZERO;
let half_segment_size = 0.5 / total_segments as f64;
let mut i = 0usize;
let mut j = 0usize;
while i < positions.len() {
position.set(
positions[i] as f64,
positions[i + 1] as f64,
positions[i + 2] as f64,
);
normal = position;
normal.x -= js_sign(normal.x) * half_segment_size;
normal.y -= js_sign(normal.y) * half_segment_size;
normal.z -= js_sign(normal.z) * half_segment_size;
normal.normalize();
positions[i] = (box_v.x * js_sign(position.x) + normal.x * radius) as f32;
positions[i + 1] = (box_v.y * js_sign(position.y) + normal.y * radius) as f32;
positions[i + 2] = (box_v.z * js_sign(position.z) + normal.z * radius) as f32;
normals_array[i] = normal.x as f32;
normals_array[i + 1] = normal.y as f32;
normals_array[i + 2] = normal.z as f32;
let side = i / face_tris;
match side {
0 => {
face_dir_vector.set(1.0, 0.0, 0.0);
uvs_array[j] =
get_uv(&face_dir_vector, &normal, Axis::Z, Axis::Y, radius, depth) as f32;
uvs_array[j + 1] = (1.0
- get_uv(&face_dir_vector, &normal, Axis::Y, Axis::Z, radius, height))
as f32;
}
1 => {
face_dir_vector.set(-1.0, 0.0, 0.0);
uvs_array[j] = (1.0
- get_uv(&face_dir_vector, &normal, Axis::Z, Axis::Y, radius, depth))
as f32;
uvs_array[j + 1] = (1.0
- get_uv(&face_dir_vector, &normal, Axis::Y, Axis::Z, radius, height))
as f32;
}
2 => {
face_dir_vector.set(0.0, 1.0, 0.0);
uvs_array[j] = (1.0
- get_uv(&face_dir_vector, &normal, Axis::X, Axis::Z, radius, width))
as f32;
uvs_array[j + 1] =
get_uv(&face_dir_vector, &normal, Axis::Z, Axis::X, radius, depth) as f32;
}
3 => {
face_dir_vector.set(0.0, -1.0, 0.0);
uvs_array[j] = (1.0
- get_uv(&face_dir_vector, &normal, Axis::X, Axis::Z, radius, width))
as f32;
uvs_array[j + 1] = (1.0
- get_uv(&face_dir_vector, &normal, Axis::Z, Axis::X, radius, depth))
as f32;
}
4 => {
face_dir_vector.set(0.0, 0.0, 1.0);
uvs_array[j] = (1.0
- get_uv(&face_dir_vector, &normal, Axis::X, Axis::Y, radius, width))
as f32;
uvs_array[j + 1] = (1.0
- get_uv(&face_dir_vector, &normal, Axis::Y, Axis::X, radius, height))
as f32;
}
5 => {
face_dir_vector.set(0.0, 0.0, -1.0);
uvs_array[j] =
get_uv(&face_dir_vector, &normal, Axis::X, Axis::Y, radius, width) as f32;
uvs_array[j + 1] = (1.0
- get_uv(&face_dir_vector, &normal, Axis::Y, Axis::X, radius, height))
as f32;
}
_ => {}
}
i += 3;
j += 2;
}
drop(positions);
drop(normals_array);
drop(uvs_array);
geometry.set_attribute("position", positions_attr);
geometry.set_attribute("normal", normals);
geometry.set_attribute("uv", uvs);
geometry
}