mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-02 22:42:24 -06:00
G2: improve editing reliability and Windows rendering fidelity
This commit is contained in:
@@ -393,7 +393,9 @@ pub fn parse(bytes: &[u8], resolve: &dyn Fn(&str) -> Option<Vec<u8>>) -> Result<
|
||||
let inverse_bind = match idx(s, "inverseBindMatrices") {
|
||||
Some(a) => {
|
||||
let (f, _) = d.floats(a)?;
|
||||
f.chunks_exact(16)
|
||||
f.as_chunks::<16>()
|
||||
.0
|
||||
.iter()
|
||||
.map(|v| {
|
||||
let mut m = [[0.0; 4]; 4];
|
||||
for c in 0..4 {
|
||||
@@ -495,29 +497,29 @@ fn primitive(d: &Doc, p: &Value, materials: &[Material]) -> Result<Option<Primit
|
||||
let attrs = p.get("attributes").ok_or_else(|| perr("primitive without attributes"))?;
|
||||
let Some(pa) = idx(attrs, "POSITION") else { return Ok(None) };
|
||||
let (pos, _) = d.floats(pa)?;
|
||||
let positions: Vec<[f32; 3]> = pos.chunks_exact(3).map(|c| [c[0], c[1], c[2]]).collect();
|
||||
let positions: Vec<[f32; 3]> = pos.as_chunks::<3>().0.iter().map(|c| [c[0], c[1], c[2]]).collect();
|
||||
let n = positions.len();
|
||||
let vec3s = |a: Option<usize>| -> Result<Vec<[f32; 3]>> {
|
||||
Ok(match a {
|
||||
Some(a) => d.floats(a)?.0.chunks_exact(3).map(|c| [c[0], c[1], c[2]]).collect(),
|
||||
Some(a) => d.floats(a)?.0.as_chunks::<3>().0.iter().map(|c| [c[0], c[1], c[2]]).collect(),
|
||||
None => vec![],
|
||||
})
|
||||
};
|
||||
let mut normals = vec3s(idx(attrs, "NORMAL"))?;
|
||||
let uvs: Vec<[f32; 2]> = match idx(attrs, "TEXCOORD_0") {
|
||||
Some(a) => d.floats(a)?.0.chunks_exact(2).map(|c| [c[0], c[1]]).collect(),
|
||||
Some(a) => d.floats(a)?.0.as_chunks::<2>().0.iter().map(|c| [c[0], c[1]]).collect(),
|
||||
None => vec![],
|
||||
};
|
||||
let tangents: Vec<[f32; 4]> = match idx(attrs, "TANGENT") {
|
||||
Some(a) => d.floats(a)?.0.chunks_exact(4).map(|c| [c[0], c[1], c[2], c[3]]).collect(),
|
||||
Some(a) => d.floats(a)?.0.as_chunks::<4>().0.iter().map(|c| [c[0], c[1], c[2], c[3]]).collect(),
|
||||
None => vec![],
|
||||
};
|
||||
let joints: Vec<[u16; 4]> = match idx(attrs, "JOINTS_0") {
|
||||
Some(a) => d.ints(a)?.chunks_exact(4).map(|c| [c[0] as u16, c[1] as u16, c[2] as u16, c[3] as u16]).collect(),
|
||||
Some(a) => d.ints(a)?.as_chunks::<4>().0.iter().map(|c| [c[0] as u16, c[1] as u16, c[2] as u16, c[3] as u16]).collect(),
|
||||
None => vec![],
|
||||
};
|
||||
let weights: Vec<[f32; 4]> = match idx(attrs, "WEIGHTS_0") {
|
||||
Some(a) => d.floats(a)?.0.chunks_exact(4).map(|c| [c[0], c[1], c[2], c[3]]).collect(),
|
||||
Some(a) => d.floats(a)?.0.as_chunks::<4>().0.iter().map(|c| [c[0], c[1], c[2], c[3]]).collect(),
|
||||
None => vec![],
|
||||
};
|
||||
let raw: Vec<u32> = match idx(p, "indices") {
|
||||
@@ -540,7 +542,7 @@ fn primitive(d: &Doc, p: &Value, materials: &[Material]) -> Result<Option<Primit
|
||||
indices.extend([raw[0], raw[i - 1], raw[i]]);
|
||||
}
|
||||
}
|
||||
_ => indices.extend(raw.chunks_exact(3).flatten()),
|
||||
_ => indices.extend(raw.as_chunks::<3>().0.iter().flatten()),
|
||||
}
|
||||
if normals.len() != n {
|
||||
normals.clear();
|
||||
|
||||
@@ -169,7 +169,7 @@ impl Primitive {
|
||||
/// Smooth vertex normals from the triangles (area weighted), for meshes without normals.
|
||||
pub fn compute_normals(&mut self) {
|
||||
let mut n = vec![[0.0f32; 3]; self.positions.len()];
|
||||
for t in self.indices.chunks_exact(3) {
|
||||
for t in self.indices.as_chunks::<3>().0 {
|
||||
let (a, b, c) = (self.positions[t[0] as usize], self.positions[t[1] as usize], self.positions[t[2] as usize]);
|
||||
let f = cross(sub(b, a), sub(c, a));
|
||||
for &i in t {
|
||||
@@ -185,7 +185,7 @@ impl Primitive {
|
||||
/// primitive has none).
|
||||
pub fn flat_normals(&mut self) {
|
||||
let mut p = Primitive { material: self.material, ..Default::default() };
|
||||
for t in self.indices.chunks_exact(3) {
|
||||
for t in self.indices.as_chunks::<3>().0 {
|
||||
let (a, b, c) = (self.positions[t[0] as usize], self.positions[t[1] as usize], self.positions[t[2] as usize]);
|
||||
let f = normalize(cross(sub(b, a), sub(c, a))).unwrap_or([0.0, 0.0, 1.0]);
|
||||
for &i in t {
|
||||
@@ -213,7 +213,7 @@ impl Primitive {
|
||||
}
|
||||
let mut tan = vec![[0.0f32; 3]; n];
|
||||
let mut bit = vec![[0.0f32; 3]; n];
|
||||
for t in self.indices.chunks_exact(3) {
|
||||
for t in self.indices.as_chunks::<3>().0 {
|
||||
let (i0, i1, i2) = (t[0] as usize, t[1] as usize, t[2] as usize);
|
||||
let (p0, p1, p2) = (self.positions[i0], self.positions[i1], self.positions[i2]);
|
||||
let (w0, w1, w2) = (self.uvs[i0], self.uvs[i1], self.uvs[i2]);
|
||||
|
||||
@@ -205,7 +205,7 @@ pub fn parse(bytes: &[u8], resolve: &dyn Fn(&str) -> Option<Vec<u8>>) -> Result<
|
||||
let (_, prim, map, need) = &mut prims[slot];
|
||||
let mut ids = vec![];
|
||||
for (vi, ti, ni) in refs {
|
||||
let k = Key { v: vi, t: ti.map_or(u32::MAX, |x| x), n: ni.map_or(u32::MAX, |x| x), s };
|
||||
let k = Key { v: vi, t: ti.unwrap_or(u32::MAX), n: ni.unwrap_or(u32::MAX), s };
|
||||
let id = *map.entry(k).or_insert_with(|| {
|
||||
prim.positions.push(v[vi as usize]);
|
||||
prim.uvs.push(ti.map_or([0.0, 0.0], |t| vt[t as usize]));
|
||||
|
||||
Reference in New Issue
Block a user