#! Contains reading a mesh's geometry: its points, `Vertices`; its polygons, #! `PolygonVertexIndex`, the last corner of each written as its point's bits #! turned over, so negative; and its layers, normals, places on textures, #! colors, and materials, each given by polygon corner, by point, by polygon, #! or once for all, directly or through indices, spread here to one for each #! corner, or each polygon of materials. And a blend shape's shapes: the #! points they move and how far. import malunal::linear; module malunal::fbx; # Reads a mesh. def scene::read_mesh(&mut this, object: u32, id: i64, name: strview): result { var made := mesh{ id = id, name = string::from(name, nothing{}), node = object }; if this.document.child(object, "Vertices") is some(vertices) { val values := this.document.reals(vertices); made.positions.reserve(values.length / 3); for i in 0..values.length / 3 { made.positions.append(vec3{ values[i * 3] as f32, values[i * 3 + 1] as f32, values[i * 3 + 2] as f32 }); } } if this.document.child(object, "PolygonVertexIndex") is some(indices) { val values := this.document.integers(indices); made.corners.reserve(values.length); var first: u64 = 0; for i in 0..values.length { var point := values[i]; val ends := point < 0; if ends { point = -point - 1; } if point as u64 >= made.positions.length { return failed(failure::malformed, $"{name}: a polygon names point {point}, of the {made.positions.length} it has.".span); } made.corners.append(point as u32); if ends { made.polygons.append(polygon{ first = first, count = (i + 1 - first) as u32 }); first = i + 1; } } if first < values.length { made.polygons.append(polygon{ first = first, count = (values.length - first) as u32 }); } } # The polygon of each corner, which layers given by polygon need. var polygon_of := vector::::new(); polygon_of.reserve(made.corners.length); for p in 0..made.polygons.length { for _ in 0..made.polygons[p].count as u64 { polygon_of.append(p as u32); } } val count := this.document.nodes[object as u64].children.length; for i in 0..count { val layer := this.document.nodes[object as u64].children[i]; var kind := string::from(this.document.nodes[layer as u64].name.span, nothing{}); if kind.span == "LayerElementNormal" && made.normals.length == 0 { val values := this.layer_values(layer, "Normals", "NormalsIndex", 3, &made.corners, &polygon_of); for c in 0..values.length / 3 { made.normals.append(vec3{ values[c * 3] as f32, values[c * 3 + 1] as f32, values[c * 3 + 2] as f32 }); } } else if kind.span == "LayerElementUV" { val values := this.layer_values(layer, "UV", "UVIndex", 2, &made.corners, &polygon_of); continue if values.length == 0 && made.corners.length != 0; var set := uv_set{ name = string::from(this.document.text_of(layer, "Name"), nothing{}) }; set.uvs.reserve(values.length / 2); for c in 0..values.length / 2 { set.uvs.append(vec2{ values[c * 2] as f32, values[c * 2 + 1] as f32 }); } made.uv_sets.append(move(set)); } else if kind.span == "LayerElementColor" && made.colors.length == 0 { val values := this.layer_values(layer, "Colors", "ColorIndex", 4, &made.corners, &polygon_of); for c in 0..values.length / 4 { made.colors.append(vec4{ values[c * 4] as f32, values[c * 4 + 1] as f32, values[c * 4 + 2] as f32, values[c * 4 + 3] as f32 }); } } else if kind.span == "LayerElementMaterial" && made.materials.length == 0 { made.materials = this.layer_materials(layer, made.polygons.length); } } this.meshes.append(move(made)); return this.meshes.length - 1; } # A layer's values, one for each corner, `width` numbers each: by how it maps # them, `MappingInformationType`, and whether through indices, # `ReferenceInformationType`. A corner whose index names nothing is zeros. It's # empty where the layer maps them by edge, which isn't read. def scene::layer_values(&this, layer: u32, data_name: strview, index_name: strview, width: u64, corners: &vector, polygon_of: &vector): vector { var made := vector::::new(); val data_node := this.document.child(layer, data_name) else { return made; }; val data := this.document.reals(data_node); val mapping := this.document.text_of(layer, "MappingInformationType"); val reference := this.document.text_of(layer, "ReferenceInformationType"); val indexed := reference == "IndexToDirect" || reference == "Index"; var indices := vector::::new(); if indexed { if this.document.child(layer, index_name) is some(found) { indices = this.document.integers(found); } } val by_corner := mapping == "ByPolygonVertex"; val by_point := mapping == "ByVertice" || mapping == "ByVertex" || mapping == "ByControlPoint"; val by_polygon := mapping == "ByPolygon"; val all_same := mapping == "AllSame"; return made if !(by_corner || by_point || by_polygon || all_same); val count := data.length / width; made.resize(corners.length * width, 0); for c in 0..corners.length { var element: u64 = 0; if by_corner { element = c; } else if by_point { element = corners[c] as u64; } else if by_polygon { element = polygon_of[c] as u64; } var value := element; if indexed { continue if element >= indices.length || indices[element] < 0; value = indices[element] as u64; } continue if value >= count; for k in 0..width { made[c * width + k] = data[value * width + k]; } } return made; } # A layer's materials, one for each polygon, by polygon or once for all. def scene::layer_materials(&this, layer: u32, polygons: u64): vector { var made := vector::::new(); val data_node := this.document.child(layer, "Materials") else { return made; }; val values := this.document.integers(data_node); return made if values.length == 0; val all_same := this.document.text_of(layer, "MappingInformationType") == "AllSame"; made.reserve(polygons); for p in 0..polygons { val value := all_same || p >= values.length ? values[0] : values[p]; made.append(value < 0 ? 0 : value as u32); } return made; } # Reads a shape a blend shape channel blends toward. def scene::read_shape(&mut this, object: u32, id: i64, name: strview): u64 { var made := shape{ id = id, name = string::from(name, nothing{}), node = object }; if this.document.child(object, "Indexes") is some(indexes) { val values := this.document.integers(indexes); for i in 0..values.length { made.points.append(values[i] as u32); } } if this.document.child(object, "Vertices") is some(vertices) { val values := this.document.reals(vertices); for i in 0..values.length / 3 { made.offsets.append(vec3{ values[i * 3] as f32, values[i * 3 + 1] as f32, values[i * 3 + 2] as f32 }); } } if this.document.child(object, "Normals") is some(normals) { val values := this.document.reals(normals); for i in 0..values.length / 3 { made.normals.append(vec3{ values[i * 3] as f32, values[i * 3 + 1] as f32, values[i * 3 + 2] as f32 }); } } this.shapes.append(move(made)); return this.shapes.length - 1; }