Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions native/shared/examples/validate_layered_gltf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fn main() -> ExitCode {
let authored_tangents = model
.meshes
.iter()
.flat_map(|mesh| &mesh.vertices)
.flat_map(|mesh| mesh.vertices.to_vertices())
.filter(|vertex| {
let tangent = vertex.tangent;
tangent[0] * tangent[0] + tangent[1] * tangent[1] + tangent[2] * tangent[2] > 1e-6
Expand Down Expand Up @@ -150,7 +150,7 @@ fn main() -> ExitCode {
let mut normal_maximum = [f32::NEG_INFINITY; 3];
let mut position_minimum = [f32::INFINITY; 3];
let mut position_maximum = [f32::NEG_INFINITY; 3];
for vertex in &mesh.vertices {
for vertex in &mesh.vertices.to_vertices() {
for channel in 0..3 {
normal_minimum[channel] = normal_minimum[channel].min(vertex.normal[channel]);
normal_maximum[channel] = normal_maximum[channel].max(vertex.normal[channel]);
Expand All @@ -166,7 +166,7 @@ fn main() -> ExitCode {
(position_minimum[2] + position_maximum[2]) * 0.5,
];
let mut radial_normal_dot = [f32::INFINITY, f32::NEG_INFINITY, 0.0];
for vertex in &mesh.vertices {
for vertex in &mesh.vertices.to_vertices() {
let radial = [
vertex.position[0] - center[0],
vertex.position[1] - center[1],
Expand Down
4 changes: 2 additions & 2 deletions native/shared/src/ffi_core/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ macro_rules! __bloom_ffi_scene {
pub extern "C" fn bloom_scene_node_vertex_count(handle: f64) -> f64 {
$crate::ffi::guard("bloom_scene_node_vertex_count", move || {
match engine().scene.nodes.get(handle) {
Some(node) => node.vertices().len() as f64,
Some(node) => node.vertex_count() as f64,
None => -1.0,
}
})
Expand Down Expand Up @@ -751,7 +751,7 @@ macro_rules! __bloom_ffi_scene {
let eng = engine();
if let Some(node) = eng.scene.nodes.get(handle) {
let current = $crate::geometry::GeometryData {
vertices: node.vertices().to_vec(),
vertices: node.vertices().into_owned(),
indices: node.indices().to_vec(),
};
let result = $crate::geometry::subtract_box(
Expand Down
16 changes: 11 additions & 5 deletions native/shared/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ use crate::handles::HandleRegistry;
use crate::renderer::Vertex3D;
use std::sync::Arc;

#[path = "models_vertex_store.rs"]
mod vertex_store;
pub use vertex_store::VertexStore;

#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub enum MaterialAlphaMode {
#[default]
Expand Down Expand Up @@ -443,7 +447,9 @@ pub(crate) fn exclusive_texture_indices(meshes: &[Arc<MeshData>]) -> Vec<u32> {

#[derive(Clone)]
pub struct MeshData {
pub vertices: Vec<Vertex3D>,
/// The CPU vertices, by field and compact (models_vertex_store.rs): use
/// `positions()` where positions do, `to_vertices()` for whole ones.
pub vertices: VertexStore,
/// Compact glTF TEXCOORD_1 sidecar retained only when an active physical
/// transmission/thickness texture requests it. Keeping it out of
/// `Vertex3D` preserves every ordinary vertex ABI and fetch footprint.
Expand Down Expand Up @@ -840,7 +846,7 @@ impl ModelManager {

let model = ModelData {
meshes: vec![Arc::new(MeshData {
vertices,
vertices: vertices.into(),
secondary_tex_coords: None,
indices,
texture_idx: None,
Expand Down Expand Up @@ -964,7 +970,7 @@ impl ModelManager {

let model = ModelData {
meshes: vec![Arc::new(MeshData {
vertices,
vertices: vertices.into(),
secondary_tex_coords: None,
indices,
texture_idx: None,
Expand Down Expand Up @@ -1037,7 +1043,7 @@ impl ModelManager {
let indices = index_data.to_vec();
let model = ModelData {
meshes: vec![Arc::new(MeshData {
vertices,
vertices: vertices.into(),
secondary_tex_coords: None,
indices,
texture_idx: None,
Expand Down Expand Up @@ -1172,7 +1178,7 @@ impl ModelManager {

let model = ModelData {
meshes: vec![Arc::new(MeshData {
vertices,
vertices: vertices.into(),
secondary_tex_coords: None,
indices,
texture_idx: None,
Expand Down
3 changes: 2 additions & 1 deletion native/shared/src/models_cooked_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ fn convert_primitive(
weights: vertex.weights,
tangent: vertex.tangent,
})
.collect(),
.collect::<Vec<_>>()
.into(),
secondary_tex_coords: primitive.secondary_tex_coords,
indices: primitive.indices,
texture_idx: remap_texture(material.base_color_texture, texture_handles)?,
Expand Down
6 changes: 3 additions & 3 deletions native/shared/src/models_gltf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@ fn import_gltf_with_textures(
};
let morph = morph_slots.read(&reader, &vertices, skin_vertex_scale, mesh.index());
let source = Arc::new(MeshData {
vertices,
vertices: vertices.into(),
secondary_tex_coords,
indices,
texture_idx: tex_idx,
Expand Down Expand Up @@ -1450,7 +1450,7 @@ fn load_gltf_staged_impl(
source_meshes[mesh.index()].push((
primitive.index() as u32,
MeshData {
vertices,
vertices: vertices.into(),
secondary_tex_coords,
indices,
texture_idx: tex_idx,
Expand Down Expand Up @@ -1644,7 +1644,7 @@ pub(super) fn load_gltf(data: &[u8]) -> Option<ModelData> {
source_meshes[mesh.index()].push((
primitive.index() as u32,
MeshData {
vertices,
vertices: vertices.into(),
secondary_tex_coords,
indices,
texture_idx: None,
Expand Down
19 changes: 9 additions & 10 deletions native/shared/src/models_gltf_bake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn vertex_is_skinned(vertex: &Vertex3D) -> bool {
}

fn mesh_is_skinned(mesh: &MeshData) -> bool {
mesh.vertices.iter().any(vertex_is_skinned)
(0..mesh.vertices.len()).any(|i| mesh.vertices.weight(i).iter().sum::<f32>() > 0.01)
}

pub(super) fn shared_or_owned_instance(
Expand All @@ -80,7 +80,8 @@ pub(super) fn shared_or_owned_instance(
fn bake_owned_mesh_instance(mut instance: MeshData, world: &[[f32; 4]; 4]) -> MeshData {
let normal_transform = mat4_inverse_transpose_3x3(world);
let has_skinning = mesh_is_skinned(&instance);
for vertex in &mut instance.vertices {
let mut vertices = instance.vertices.to_vertices();
for vertex in &mut vertices {
if vertex_is_skinned(vertex) {
continue;
}
Expand All @@ -101,11 +102,7 @@ fn bake_owned_mesh_instance(mut instance: MeshData, world: &[[f32; 4]; 4]) -> Me
// the node's linear part (positions, tangents) and its inverse
// transpose (normals). Skinned vertices' deltas stay in bind space.
if let Some(morph) = instance.morph.as_ref() {
let rigid: Vec<bool> = instance
.vertices
.iter()
.map(|v| !vertex_is_skinned(v))
.collect();
let rigid: Vec<bool> = vertices.iter().map(|v| !vertex_is_skinned(v)).collect();
if rigid.iter().any(|r| *r) {
instance.morph = morph
.mapped(|v, d| {
Expand All @@ -118,6 +115,7 @@ fn bake_owned_mesh_instance(mut instance: MeshData, world: &[[f32; 4]; 4]) -> Me
.map(Arc::new);
}
}
instance.vertices = vertices.into();
instance
}

Expand Down Expand Up @@ -206,8 +204,8 @@ pub(super) fn model_bounds(
.get(index)
.copied()
.unwrap_or(crate::renderer::IDENTITY_MAT4);
for vertex in &mesh.vertices {
let position = mat4_transform_point(&transform, &vertex.position);
for vertex in mesh.vertices.positions() {
let position = mat4_transform_point(&transform, vertex);
for axis in 0..3 {
minimum[axis] = minimum[axis].min(position[axis]);
maximum[axis] = maximum[axis].max(position[axis]);
Expand Down Expand Up @@ -235,7 +233,8 @@ mod tests {
joints: [0.0; 4],
weights: [0.0; 4],
tangent: [0.0; 4],
}],
}]
.into(),
secondary_tex_coords: None,
indices: vec![0],
texture_idx: None,
Expand Down
9 changes: 5 additions & 4 deletions native/shared/src/models_gltf_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ fn staged_mask_loader_routes_coverage_variant_and_keeps_plain_loader_image_neutr
assert_eq!(staged.textures[0].alpha_coverage_reference, Some(0.625));
assert_eq!(staged.model.meshes[0].texture_idx, Some(1));
assert!(staged.model.meshes[0].alpha_coverage_mips);
let color = staged.model.meshes[0].vertices[0].color;
let color = staged.model.meshes[0].vertices.get(0).color;
for (actual, expected) in color.into_iter().zip([0.1, 0.1, 0.45, 0.4]) {
assert!(
(actual - expected).abs() < 1e-6,
Expand Down Expand Up @@ -1071,6 +1071,7 @@ fn physical_metadata_round_trips_through_plain_and_staged_glb_loaders() {
assert_eq!(mesh.layered_pbr.anisotropy_rotation, 0.75, "{label}");
assert!(
mesh.vertices
.to_vertices()
.iter()
.all(|vertex| vertex.tangent == [1.0, 0.0, 0.0, -1.0]),
"{label}: authored mirrored tangents were not preserved"
Expand Down Expand Up @@ -1117,8 +1118,8 @@ fn shared_static_node_scale_preserves_world_bounds_and_volume_contract() {
] {
assert_eq!(model.meshes.len(), 1, "{label}");
let mesh = &model.meshes[0];
assert_eq!(mesh.vertices[1].position, [1.0, 0.0, 0.0], "{label}");
assert_eq!(mesh.vertices[2].position, [0.0, 1.0, 0.0], "{label}");
assert_eq!(mesh.vertices.get(1).position, [1.0, 0.0, 0.0], "{label}");
assert_eq!(mesh.vertices.get(2).position, [0.0, 1.0, 0.0], "{label}");
assert_eq!(model.mesh_transform(0)[0][0], 2.0, "{label}");
assert_eq!(model.mesh_transform(0)[1][1], 2.0, "{label}");
assert_eq!(model.mesh_transform(0)[2][2], 2.0, "{label}");
Expand Down Expand Up @@ -1207,7 +1208,7 @@ fn physical_texcoord_1_is_retained_lazily_and_exactly() {
"{label} loader must preserve authored TEXCOORD_1 exactly"
);
assert_eq!(
model.meshes[0].vertices[1].uv,
model.meshes[0].vertices.get(1).uv,
[1.0, 0.0],
"{label} loader must leave the established UV0 vertex ABI intact"
);
Expand Down
Loading
Loading