feat(lines): multiple lines to a node
This commit is contained in:
@@ -26,20 +26,28 @@ struct LinePointRaw {
|
|||||||
thickness: f32,
|
thickness: f32,
|
||||||
color: [f32; 4],
|
color: [f32; 4],
|
||||||
}
|
}
|
||||||
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
#[derive(Debug, Clone)]
|
struct Line {
|
||||||
struct LineData {
|
|
||||||
points: Vec<LinePointRaw>,
|
points: Vec<LinePointRaw>,
|
||||||
cyclic: bool,
|
cyclic: bool,
|
||||||
}
|
}
|
||||||
|
impl Line {
|
||||||
|
fn degamma(&mut self) {
|
||||||
|
for p in &mut self.points {
|
||||||
|
p.color[0] = p.color[0].powf(2.2);
|
||||||
|
p.color[1] = p.color[1].powf(2.2);
|
||||||
|
p.color[2] = p.color[2].powf(2.2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Lines {
|
pub struct Lines {
|
||||||
enabled: Arc<AtomicBool>,
|
enabled: Arc<AtomicBool>,
|
||||||
space: Arc<Spatial>,
|
space: Arc<Spatial>,
|
||||||
data: Mutex<LineData>,
|
data: Mutex<Vec<Line>>,
|
||||||
}
|
}
|
||||||
impl Lines {
|
impl Lines {
|
||||||
fn add_to(node: &Arc<Node>, points: Vec<LinePointRaw>, cyclic: bool) -> Result<Arc<Lines>> {
|
fn add_to(node: &Arc<Node>, lines: Vec<Line>) -> Result<Arc<Lines>> {
|
||||||
ensure!(
|
ensure!(
|
||||||
node.drawable.get().is_none(),
|
node.drawable.get().is_none(),
|
||||||
"Internal: Node already has a drawable attached!"
|
"Internal: Node already has a drawable attached!"
|
||||||
@@ -47,9 +55,13 @@ impl Lines {
|
|||||||
|
|
||||||
let _ = node.spatial.get().unwrap().bounding_box_calc.set(|node| {
|
let _ = node.spatial.get().unwrap().bounding_box_calc.set(|node| {
|
||||||
let mut bounds = Bounds::default();
|
let mut bounds = Bounds::default();
|
||||||
let Some(Drawable::Lines(lines)) = node.drawable.get() else {return bounds};
|
let Some(Drawable::Lines(lines)) = node.drawable.get() else {
|
||||||
for point in &lines.data.lock().points {
|
return bounds;
|
||||||
bounds = bounds_grow_to_fit_pt(bounds, point.point);
|
};
|
||||||
|
for line in &*lines.data.lock() {
|
||||||
|
for point in &line.points {
|
||||||
|
bounds = bounds_grow_to_fit_pt(bounds, point.point);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bounds
|
bounds
|
||||||
@@ -58,10 +70,9 @@ impl Lines {
|
|||||||
let lines = LINES_REGISTRY.add(Lines {
|
let lines = LINES_REGISTRY.add(Lines {
|
||||||
enabled: node.enabled.clone(),
|
enabled: node.enabled.clone(),
|
||||||
space: node.get_aspect("Lines", "spatial", |n| &n.spatial)?.clone(),
|
space: node.get_aspect("Lines", "spatial", |n| &n.spatial)?.clone(),
|
||||||
data: Mutex::new(LineData { points, cyclic }),
|
data: Mutex::new(lines),
|
||||||
});
|
});
|
||||||
node.add_local_signal("set_points", Lines::set_points_flex);
|
node.add_local_signal("set_lines", Lines::set_lines_flex);
|
||||||
node.add_local_signal("set_cyclic", Lines::set_cyclic_flex);
|
|
||||||
let _ = node.drawable.set(Drawable::Lines(lines.clone()));
|
let _ = node.drawable.set(Drawable::Lines(lines.clone()));
|
||||||
|
|
||||||
Ok(lines)
|
Ok(lines)
|
||||||
@@ -70,57 +81,57 @@ impl Lines {
|
|||||||
fn draw(&self, draw_ctx: &impl StereoKitDraw) {
|
fn draw(&self, draw_ctx: &impl StereoKitDraw) {
|
||||||
let transform_mat = self.space.global_transform();
|
let transform_mat = self.space.global_transform();
|
||||||
let data = self.data.lock().clone();
|
let data = self.data.lock().clone();
|
||||||
let mut points: VecDeque<SkLinePoint> = data
|
for line in &data {
|
||||||
.points
|
let mut points: VecDeque<SkLinePoint> = line
|
||||||
.iter()
|
.points
|
||||||
.map(|p| SkLinePoint {
|
.iter()
|
||||||
pt: transform_mat.transform_point3a(Vec3A::from(p.point)).into(),
|
.map(|p| SkLinePoint {
|
||||||
thickness: p.thickness,
|
pt: transform_mat.transform_point3a(Vec3A::from(p.point)).into(),
|
||||||
color: p.color.map(|c| (c * 255.0) as u8).into(),
|
thickness: p.thickness,
|
||||||
})
|
color: p.color.map(|c| (c * 255.0) as u8).into(),
|
||||||
.collect();
|
})
|
||||||
if data.cyclic && !points.is_empty() {
|
.collect();
|
||||||
let first = data.points.first().unwrap();
|
if line.cyclic && !points.is_empty() {
|
||||||
let last = data.points.last().unwrap();
|
let first = line.points.first().unwrap();
|
||||||
let color = Rgba::from_slice(&first.color).lerp(&Rgba::from_slice(&last.color), 0.5);
|
let last = line.points.last().unwrap();
|
||||||
let connect_point = SkLinePoint {
|
let color =
|
||||||
pt: transform_mat
|
Rgba::from_slice(&first.color).lerp(&Rgba::from_slice(&last.color), 0.5);
|
||||||
.transform_point3a(Vec3A::from(first.point).lerp(Vec3A::from(last.point), 0.5))
|
let connect_point = SkLinePoint {
|
||||||
|
pt: transform_mat
|
||||||
|
.transform_point3a(
|
||||||
|
Vec3A::from(first.point).lerp(Vec3A::from(last.point), 0.5),
|
||||||
|
)
|
||||||
|
.into(),
|
||||||
|
thickness: (first.thickness + last.thickness) * 0.5,
|
||||||
|
color: Color128::from([
|
||||||
|
color.red(),
|
||||||
|
color.green(),
|
||||||
|
color.blue(),
|
||||||
|
color.alpha(),
|
||||||
|
])
|
||||||
.into(),
|
.into(),
|
||||||
thickness: (first.thickness + last.thickness) * 0.5,
|
};
|
||||||
color: Color128::from([color.red(), color.green(), color.blue(), color.alpha()])
|
points.push_front(connect_point.clone());
|
||||||
.into(),
|
points.push_back(connect_point);
|
||||||
};
|
}
|
||||||
points.push_front(connect_point.clone());
|
draw_ctx.line_add_listv(points.make_contiguous());
|
||||||
points.push_back(connect_point);
|
|
||||||
}
|
}
|
||||||
draw_ctx.line_add_listv(points.make_contiguous());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_points_flex(
|
pub fn set_lines_flex(
|
||||||
node: &Node,
|
node: &Node,
|
||||||
_calling_client: Arc<Client>,
|
_calling_client: Arc<Client>,
|
||||||
message: Message,
|
message: Message,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let Some(Drawable::Lines(lines)) = node.drawable.get() else {bail!("Not a drawable??")};
|
let Some(Drawable::Lines(lines)) = node.drawable.get() else {
|
||||||
|
bail!("Not a drawable??")
|
||||||
|
};
|
||||||
|
|
||||||
let mut points: Vec<LinePointRaw> = deserialize(message.as_ref())?;
|
let mut new_lines: Vec<Line> = deserialize(message.as_ref())?;
|
||||||
for p in &mut points {
|
for l in &mut new_lines {
|
||||||
p.color[0] = p.color[0].powf(2.2);
|
l.degamma();
|
||||||
p.color[1] = p.color[1].powf(2.2);
|
|
||||||
p.color[2] = p.color[2].powf(2.2);
|
|
||||||
}
|
}
|
||||||
lines.data.lock().points = points;
|
*lines.data.lock() = new_lines;
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
pub fn set_cyclic_flex(
|
|
||||||
node: &Node,
|
|
||||||
_calling_client: Arc<Client>,
|
|
||||||
message: Message,
|
|
||||||
) -> Result<()> {
|
|
||||||
let Some(Drawable::Lines(lines)) = node.drawable.get() else {bail!("Not a drawable??")};
|
|
||||||
|
|
||||||
lines.data.lock().cyclic = deserialize(message.as_ref())?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -140,26 +151,23 @@ pub fn draw_all(draw_ctx: &impl StereoKitDraw) {
|
|||||||
|
|
||||||
pub fn create_flex(_node: &Node, calling_client: Arc<Client>, message: Message) -> Result<()> {
|
pub fn create_flex(_node: &Node, calling_client: Arc<Client>, message: Message) -> Result<()> {
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct CreateTextInfo<'a> {
|
struct CreateLinesInfo<'a> {
|
||||||
name: &'a str,
|
name: &'a str,
|
||||||
parent_path: &'a str,
|
parent_path: &'a str,
|
||||||
transform: Transform,
|
transform: Transform,
|
||||||
points: Vec<LinePointRaw>,
|
lines: Vec<Line>,
|
||||||
cyclic: bool,
|
|
||||||
}
|
}
|
||||||
let mut info: CreateTextInfo = deserialize(message.as_ref())?;
|
let mut info: CreateLinesInfo = deserialize(message.as_ref())?;
|
||||||
let node = Node::create(&calling_client, "/drawable/lines", info.name, true);
|
let node = Node::create(&calling_client, "/drawable/lines", info.name, true);
|
||||||
let parent = find_spatial_parent(&calling_client, info.parent_path)?;
|
let parent = find_spatial_parent(&calling_client, info.parent_path)?;
|
||||||
let transform = parse_transform(info.transform, true, true, true);
|
let transform = parse_transform(info.transform, true, true, true);
|
||||||
|
|
||||||
for p in &mut info.points {
|
for l in &mut info.lines {
|
||||||
p.color[0] = p.color[0].powf(2.2);
|
l.degamma();
|
||||||
p.color[1] = p.color[1].powf(2.2);
|
|
||||||
p.color[2] = p.color[2].powf(2.2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let node = node.add_to_scenegraph()?;
|
let node = node.add_to_scenegraph()?;
|
||||||
Spatial::add_to(&node, Some(parent), transform, false)?;
|
Spatial::add_to(&node, Some(parent), transform, false)?;
|
||||||
Lines::add_to(&node, info.points, info.cyclic)?;
|
Lines::add_to(&node, info.lines)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user