mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 15:55:14 +00:00
craft-prc: keep a wire's vertex colours (#1904)
This commit is contained in:
1 parent
79f862597d
commit
9e614b082f
4 files changed
+126
-23
No files matched your search
@@ -29,7 +29,7 @@ BitReader: bit, bits, character, unsigned_integer, integer, integer_vbn, double_
|
||||
- `Unsupported` names constructs the reader cannot step over (schema pointer fields, `Value_CurveIs3D`).
|
||||
- `Tessellation::Mesh` is a `TriangleMesh`: positions, triangles, one triangle range per face, the stored normals and
|
||||
texture coordinates with each triangle's corner indices into them, and per-triangle line-attribute styles.
|
||||
`Wire` is polylines; `Markup` is read past.
|
||||
`Wire` is a `WireTessellation`: polylines and, when stored, each point's RGBA colour; `Markup` is read past.
|
||||
- `Tessellation::Compressed` is read to its end so later entities decode. Recalculated-normal meshes in the accepted 3T form are rebuilt when their arrays fit; stored-normal meshes and unsupported traversals stay `Compressed`.
|
||||
- A face may carry at most 8 texture-coordinate sets; more is `Malformed`. Optimised vertex colours are `Unsupported`.
|
||||
- The product tree: each drawable tessellation placed by its occurrence path's locations and the item's coordinate
|
||||
|
||||
@@ -37,7 +37,7 @@ pub use container::{FileStructure, MAX_FILE_STRUCTURES, MAX_INFLATED_BYTES, PRC_
|
||||
pub use error::PrcError;
|
||||
pub use geometry::Matrix;
|
||||
pub use schema::Schema;
|
||||
pub use tess::{Tessellation, TriangleMesh, TriangleStyle};
|
||||
pub use tess::{Tessellation, TriangleMesh, TriangleStyle, WireTessellation};
|
||||
pub use tree::{EntityOverrides, MAX_PLACEMENTS, ModelNode, ModelUnits, NameSource, Placement, Rgba, StyleAlpha};
|
||||
pub use write::{
|
||||
MAX_NODE_DEPTH, MeshScene, PrcWriteError, PrcWriteReport, SceneMaterial, SceneMesh, SceneNode, ScenePrimitive, SceneTexture, TextureWrap,
|
||||
|
||||
+58
-15
@@ -40,6 +40,28 @@ const WIRE_IS_CONTINUOUS: u32 = 0x2000_0000;
|
||||
/// Normal angles wider than this are not a `ShortArray` [WD 7.8.9.7].
|
||||
const MAX_ANGLE_BITS: u32 = 16;
|
||||
|
||||
/// A `TESS_3D_Wire` [WD 7.8.7]: polylines in the entity's own frame. A closing wire ends on a
|
||||
/// repeat of its first point; a continuous one is already joined to the wire before it.
|
||||
#[derive(Debug, Clone, Default, PartialEq)]
|
||||
#[non_exhaustive]
|
||||
pub struct WireTessellation {
|
||||
/// The polylines; one of fewer than two points draws nothing.
|
||||
pub polylines: Vec<Vec<[f64; 3]>>,
|
||||
/// Each point's RGBA colour, parallel to `polylines` (RGB colours read opaque; a segment
|
||||
/// colour [WD 7.8.7.2] goes to both its points). Empty when the wire stores none.
|
||||
pub colours: Vec<Vec<[u8; 4]>>,
|
||||
}
|
||||
|
||||
/// `flat` cut into runs as long as each of `wires`; empty when there are no colours or the lengths
|
||||
/// disagree.
|
||||
fn split_like(wires: &[Vec<[f64; 3]>], flat: Vec<[u8; 4]>) -> Vec<Vec<[u8; 4]>> {
|
||||
if flat.is_empty() || flat.len() != wires.iter().map(Vec::len).sum::<usize>() {
|
||||
return Vec::new();
|
||||
}
|
||||
let mut rest = flat.into_iter();
|
||||
wires.iter().map(|w| rest.by_ref().take(w.len()).collect()).collect()
|
||||
}
|
||||
|
||||
/// One entry of the tessellation array; a representation item's `index_tessellation` selects
|
||||
/// one by position.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
@@ -47,8 +69,8 @@ const MAX_ANGLE_BITS: u32 = 16;
|
||||
pub enum Tessellation {
|
||||
/// A `TESS_3D` triangle mesh.
|
||||
Mesh(TriangleMesh),
|
||||
/// A `TESS_3D_Wire`: polylines.
|
||||
Wire(Vec<Vec<[f64; 3]>>),
|
||||
/// A `TESS_3D_Wire`: polylines and their colours.
|
||||
Wire(WireTessellation),
|
||||
/// A `TESS_Markup` (PMI drawing), read past but not decoded.
|
||||
Markup,
|
||||
/// A `TESS_3D_Compressed` mesh, read past but not rebuilt: it stores its normals, or its
|
||||
@@ -155,28 +177,51 @@ impl Ctx<'_, '_> {
|
||||
self.doubles("tessellation coordinates")
|
||||
}
|
||||
|
||||
/// `VertexColors` [WD 7.8.7.2] for `count` points, read past.
|
||||
fn vertex_colors(&mut self, count: usize, in_wire: bool) -> Result<(), PrcError> {
|
||||
/// `VertexColors` [WD 7.8.7.2] for `count` points. Kept colours are charged to the element
|
||||
/// budget before they are allocated; with `keep` false they are read past and none is stored.
|
||||
fn vertex_colors(&mut self, count: usize, in_wire: bool, keep: bool) -> Result<Vec<[u8; 4]>, PrcError> {
|
||||
let rgba = self.r.bit()?;
|
||||
// prc-rs: `is_segment_color` exists only inside a wire.
|
||||
let per_segment = in_wire && self.r.bit()?;
|
||||
if self.r.bit()? {
|
||||
return Err(PrcError::Unsupported("optimised vertex colours"));
|
||||
}
|
||||
let points = count;
|
||||
let count = if per_segment { count / 2 } else { count };
|
||||
let bytes = if rgba { 4 } else { 3 };
|
||||
// The first colour is `8 * bytes` bits; every later one is at least its reuse bit.
|
||||
if count > 0 && (count - 1).saturating_add(8 * bytes) > self.r.remaining() {
|
||||
return Err(PrcError::Truncated("vertex colours"));
|
||||
}
|
||||
for i in 0..count {
|
||||
if i == 0 || !self.r.bit()? {
|
||||
for _ in 0..bytes {
|
||||
self.r.character()?;
|
||||
if !keep {
|
||||
for i in 0..count {
|
||||
if i == 0 || !self.r.bit()? {
|
||||
for _ in 0..bytes {
|
||||
self.r.character()?;
|
||||
}
|
||||
}
|
||||
}
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
Ok(())
|
||||
self.r.take_elements(points)?;
|
||||
let mut out = Vec::with_capacity(points);
|
||||
let mut last = [0, 0, 0, 255];
|
||||
for i in 0..count {
|
||||
if i == 0 || !self.r.bit()? {
|
||||
for c in last.iter_mut().take(bytes) {
|
||||
*c = self.r.character()?;
|
||||
}
|
||||
}
|
||||
out.push(last);
|
||||
if per_segment {
|
||||
out.push(last);
|
||||
}
|
||||
}
|
||||
// An odd point count leaves the last point to the last segment.
|
||||
if per_segment && count > 0 && out.len() < points {
|
||||
out.push(last);
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// `TESS_3D` (172) after its type code [WD 7.8.5].
|
||||
@@ -237,7 +282,7 @@ impl Ctx<'_, '_> {
|
||||
let mut face = Face { flags, start, data, textures, styles, behaviour: 0 };
|
||||
if self.r.bit()? {
|
||||
let n = face.point_count()?;
|
||||
self.vertex_colors(n, false)?;
|
||||
self.vertex_colors(n, false, false)?;
|
||||
}
|
||||
if !face.styles.is_empty() {
|
||||
let bits = self.r.unsigned_integer()?;
|
||||
@@ -248,7 +293,7 @@ impl Ctx<'_, '_> {
|
||||
}
|
||||
|
||||
/// `TESS_3D_Wire` (175) after its type code [WD 7.8.7].
|
||||
fn wire(&mut self) -> Result<Vec<Vec<[f64; 3]>>, PrcError> {
|
||||
fn wire(&mut self) -> Result<WireTessellation, PrcError> {
|
||||
let pts = points(&self.base_tess_data()?)?;
|
||||
let words = self.uints("wire indices")?;
|
||||
let mut wires: Vec<Vec<[f64; 3]>> = Vec::new();
|
||||
@@ -279,11 +324,9 @@ impl Ctx<'_, '_> {
|
||||
_ => wires.push(wire),
|
||||
}
|
||||
}
|
||||
if self.r.bit()? {
|
||||
self.vertex_colors(colours, true)?;
|
||||
}
|
||||
let flat = if self.r.bit()? { self.vertex_colors(colours, true, true)? } else { Vec::new() };
|
||||
self.schema.skip_added_fields(TESS_3D_WIRE, &mut self.r)?;
|
||||
Ok(wires)
|
||||
Ok(WireTessellation { colours: split_like(&wires, flat), polylines: wires })
|
||||
}
|
||||
|
||||
/// `TESS_Markup` (176) after its type code [WD 7.8.8], read past.
|
||||
|
||||
@@ -266,9 +266,12 @@ fn wires_close_and_continue_and_colours_are_counted() {
|
||||
// A second entity: a wrong colour count would desync it.
|
||||
wire(&mut b, false);
|
||||
let t = decode(§ion(2, &b), &Schema::default(), 8137).unwrap();
|
||||
for e in &t {
|
||||
for (i, e) in t.iter().enumerate() {
|
||||
let Tessellation::Wire(w) = e else { panic!("not a wire") };
|
||||
assert_eq!(w, &[vec![[0., 0., 0.], [1., 0., 0.], [1., 1., 0.], [0., 1., 0.], [1., 1., 0.]]]);
|
||||
assert_eq!(w.polylines, [vec![[0., 0., 0.], [1., 0., 0.], [1., 1., 0.], [0., 1., 0.], [1., 1., 0.]]]);
|
||||
// Two red segment colours cover four points; the odd fifth takes the last one.
|
||||
let want = if colours && i == 0 { vec![vec![[255, 0, 0, 255]; 5]] } else { Vec::new() };
|
||||
assert_eq!(w.colours, want, "colours {colours}, entity {i}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -285,8 +288,28 @@ fn a_closing_wire_colours_its_implicit_point() {
|
||||
}
|
||||
wire(&mut b, false);
|
||||
let t = decode(§ion(2, &b), &Schema::default(), 8137).unwrap();
|
||||
let (Tessellation::Wire(a), Tessellation::Wire(b)) = (&t[0], &t[1]) else { panic!("not wires") };
|
||||
assert_eq!(a.polylines, b.polylines);
|
||||
assert_eq!(a.colours, [vec![[0, 0, 1, 255]; 5]]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn per_point_rgba_colours_are_split_across_separate_wires() {
|
||||
let mut b = W::default();
|
||||
square(&mut b, 175);
|
||||
// Two separate two-point wires.
|
||||
uints(&mut b, &[2, 0, 3, 2, 6, 9]);
|
||||
// Colours present, RGBA, per point, not optimised: green, a reuse, then two new colours.
|
||||
b.bit(true).bit(true).bit(false).bit(false).put(0x00ff_0080, 32);
|
||||
b.bit(true);
|
||||
b.bit(false).put(0x0102_0304, 32);
|
||||
b.bit(false).put(0xffff_ffff, 32);
|
||||
wire(&mut b, false);
|
||||
let t = decode(§ion(2, &b), &Schema::default(), 8137).unwrap();
|
||||
let Tessellation::Wire(w) = &t[0] else { panic!("not a wire") };
|
||||
assert_eq!(w.polylines, [vec![[0., 0., 0.], [1., 0., 0.]], vec![[1., 1., 0.], [0., 1., 0.]]]);
|
||||
assert_eq!(w.colours, [vec![[0, 255, 0, 128]; 2], vec![[1, 2, 3, 4], [255; 4]]]);
|
||||
assert!(matches!(t[1], Tessellation::Wire(_)));
|
||||
assert_eq!(t[0], t[1]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -311,8 +334,9 @@ fn a_wire_without_indices_is_the_coordinate_polyline() {
|
||||
wire(&mut b, false);
|
||||
let t = decode(§ion(2, &b), &Schema::default(), 8137).unwrap();
|
||||
let Tessellation::Wire(w) = &t[0] else { panic!("not a wire") };
|
||||
assert_eq!(w.len(), 1);
|
||||
assert_eq!(w[0].len(), 4);
|
||||
assert_eq!(w.polylines.len(), 1);
|
||||
assert_eq!(w.polylines[0].len(), 4);
|
||||
assert_eq!(w.colours, [vec![[0, 0, 1, 255]; 4]]);
|
||||
assert!(matches!(t[1], Tessellation::Wire(_)));
|
||||
}
|
||||
|
||||
@@ -332,7 +356,8 @@ fn reused_vertex_colours_may_be_the_last_data() {
|
||||
}
|
||||
let t = decode(§ion(1, &b), &Schema::default(), 8137).unwrap();
|
||||
let Tessellation::Wire(w) = &t[0] else { panic!("not a wire") };
|
||||
assert_eq!(w[0].len(), n as usize);
|
||||
assert_eq!(w.polylines[0].len(), n as usize);
|
||||
assert_eq!(w.colours[0].len(), n as usize);
|
||||
}
|
||||
|
||||
/// A square as one triangle plus one 4-vertex fan (two entities, three triangles), the face
|
||||
@@ -1005,3 +1030,38 @@ fn the_globals_schema_and_the_tessellations_share_one_allowance() {
|
||||
assert!(within(need).is_ok());
|
||||
assert!(matches!(within(need - 1), Err(PrcError::TooManyElements { .. })));
|
||||
}
|
||||
|
||||
/// A wire of [`SQUARE`]'s four points without indices, with four kept RGB colours or none.
|
||||
fn bare_wire(colours: bool) -> W {
|
||||
let mut b = W::default();
|
||||
square(&mut b, 175);
|
||||
b.uint(0).bit(colours);
|
||||
if colours {
|
||||
b.bit(false).bit(false).bit(false).put(1, 24);
|
||||
for _ in 1..4 {
|
||||
b.bit(true);
|
||||
}
|
||||
}
|
||||
section(1, &b)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn kept_wire_colours_are_charged_before_they_are_made() {
|
||||
let plain = spent(&bare_wire(false));
|
||||
assert_eq!(spent(&bare_wire(true)) - plain, 4);
|
||||
let bytes = bare_wire(true).bytes();
|
||||
let schema = Schema::default();
|
||||
let r = Ctx::new(BitReader::with_elements(&bytes, plain), &schema, 8137).file_structure_tessellation();
|
||||
assert!(matches!(r, Err(PrcError::TooManyElements { .. })), "{r:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn face_colours_that_are_read_past_are_not_charged() {
|
||||
assert_eq!(spent(&styled_face_section(&[], 7, 0x30)), spent(&styled_face_section(&[], 0, 0x30)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wires_without_colours_make_no_colour_lists() {
|
||||
assert!(split_like(&[Vec::new(), Vec::new()], Vec::new()).is_empty());
|
||||
assert_eq!(split_like(&[vec![[0.0; 3]]], vec![[1; 4]]), [vec![[1; 4]]]);
|
||||
}
|
||||
Reference in new issue
Block a user