aboutsummaryrefslogtreecommitdiff
path: root/src/classes/mesh.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/classes/mesh.rs')
-rw-r--r--src/classes/mesh.rs41
1 files changed, 39 insertions, 2 deletions
diff --git a/src/classes/mesh.rs b/src/classes/mesh.rs
index f0b2173..f828b7c 100644
--- a/src/classes/mesh.rs
+++ b/src/classes/mesh.rs
@@ -1,7 +1,8 @@
use super::streaminginfo::StreamingInfo;
use crate::object::{Value, parser::FromValue};
use anyhow::{Result, anyhow, bail};
-use glam::Mat4;
+use glam::{Mat4, Vec3, Vec3A};
+use log::debug;
use serde::Serialize;
use std::mem::transmute;
@@ -154,7 +155,7 @@ impl VertexData {
let component_offset = channel.offset as usize;
let component_size = channel.format.component_size();
let (offset, stride) = self.stream_layout()[channel.stream as usize];
-
+ debug!("reading {channel:?} vertex channel (stride={stride}, offset={offset})");
let mut out = Vec::new();
for vi in 0..self.vertex_count as usize {
for di in 0..channel.dimension as usize {
@@ -178,6 +179,42 @@ impl VertexData {
}
Some((channel.dimension as usize, out))
}
+ pub fn read_channel_vec<T: VectorType>(
+ &self,
+ channel: VertexDataChannel,
+ ) -> Result<Option<Vec<T>>> {
+ let Some((dim, data)) = self.read_channel(channel) else {
+ return Ok(None);
+ };
+ if dim != T::DIM {
+ bail!(
+ "dimension mismatch reading {channel:?} channel ({} != {})",
+ dim,
+ T::DIM
+ );
+ }
+ Ok(Some(VectorType::convert_array(data)))
+ }
+}
+
+pub trait VectorType: Sized {
+ const DIM: usize;
+ fn convert_array(a: Vec<f32>) -> Vec<Self>;
+}
+impl VectorType for Vec3A {
+ const DIM: usize = 3;
+ fn convert_array(a: Vec<f32>) -> Vec<Self> {
+ a.into_iter()
+ .array_chunks()
+ .map(Vec3A::from_array)
+ .collect()
+ }
+}
+impl VectorType for Vec3 {
+ const DIM: usize = 3;
+ fn convert_array(a: Vec<f32>) -> Vec<Self> {
+ a.into_iter().array_chunks().map(Vec3::from_array).collect()
+ }
}
impl FromValue for ChannelInfo {