{"record":{"id":"4fc07602693c428c","repo":"bevyengine/bevy","slug":"face-index-data-references-vertices-that-do-not-ex","errorCode":null,"errorMessage":"Face index data references vertices that do not exist","messagePattern":"Face index data references vertices that do not exist","errorType":"exception","errorClass":"MeshTrianglesError","httpStatus":null,"severity":"error","filePath":"crates/bevy_mesh/src/index.rs","lineNumber":74,"sourceCode":"    /// This error occurs when you try to invert the winding for a mesh with\n    /// * [`PrimitiveTopology::TriangleList`](super::PrimitiveTopology::TriangleList), but the indices are not in chunks of 3.\n    /// * [`PrimitiveTopology::LineList`](super::PrimitiveTopology::LineList), but the indices are not in chunks of 2.\n    #[error(\"Indices weren't in chunks according to topology\")]\n    AbruptIndicesEnd,\n    #[error(\"Mesh access error: {0}\")]\n    MeshAccessError(#[from] MeshAccessError),\n}\n\n/// An error that occurred while trying to extract a collection of triangles from a [`Mesh`](super::Mesh).\n#[derive(Debug, Error)]\npub enum MeshTrianglesError {\n    #[error(\"Source mesh does not have primitive topology TriangleList or TriangleStrip\")]\n    WrongTopology,\n\n    #[error(\"Source mesh position data is not Float32x3\")]\n    PositionsFormat,\n\n    #[error(\"Face index data references vertices that do not exist\")]\n    BadIndices,\n    #[error(\"mesh access error: {0}\")]\n    MeshAccessError(#[from] MeshAccessError),\n}\n\n/// An array of indices into the [`VertexAttributeValues`](super::VertexAttributeValues) for a mesh.\n///\n/// It describes the order in which the vertex attributes should be joined into faces.\n#[derive(Debug, Clone, Reflect, PartialEq)]\n#[reflect(Clone)]\n#[cfg_attr(feature = \"serialize\", derive(Serialize, Deserialize))]\npub enum Indices {\n    U16(Vec<u16>),\n    U32(Vec<u32>),\n}\n\nimpl Indices {\n    /// Returns an iterator over the indices.","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/bevyengine/bevy/blob/396ca727080776bd313bb892423b7d94e03b81b4/crates/bevy_mesh/src/index.rs#L56-L92","documentation":"MeshTrianglesError::BadIndices reports that face index data references vertices that do not exist: an index in the Indices buffer is greater than or equal to the vertex count. Triangle-producing mesh APIs use this variant when they validate the index buffer against the vertex attribute buffers.","triggerScenarios":"A mesh whose index buffer contains values >= mesh.count_vertices(); stale indices left in place after the position attribute was truncated or replaced; concatenated meshes where indices were not offset.","commonSituations":"Editing vertex buffers without rebuilding indices; merging meshes while forgetting index offsets; hand-authored index lists referencing deleted vertices; corrupted imported assets.","solutions":["Validate indices against mesh.count_vertices() before calling triangle-extraction APIs: every index must be < vertex count","Rebuild or remap the index buffer whenever vertex data is edited","Filter out or clamp out-of-range indices when repairing imported data"],"exampleFix":"// before\nlet tris: Vec<Triangle3d> = mesh.triangles()?.collect(); // Err(BadIndices) on stale indices\n\n// after (validate before extracting)\nlet vertex_count = mesh.count_vertices();\nlet valid = mesh.indices().is_none_or(|i| match i {\n    Indices::U16(v) => v.iter().all(|&i| (i as usize) < vertex_count),\n    Indices::U32(v) => v.iter().all(|&i| (i as usize) < vertex_count),\n});\nif valid {\n    let tris: Vec<Triangle3d> = mesh.triangles()?.collect();\n}","handlingStrategy":"validation","validationCode":"fn indices_in_bounds(mesh: &Mesh) -> bool {\n    let vertex_count = mesh.count_vertices();\n    match mesh.indices() {\n        Some(Indices::U16(v)) => v.iter().all(|&i| (i as usize) < vertex_count),\n        Some(Indices::U32(v)) => v.iter().all(|&i| (i as usize) < vertex_count),\n        None => true,\n    }\n}","typeGuard":null,"tryCatchPattern":"match mesh.triangles() {\n    Ok(iter) => { /* use triangles */ }\n    Err(MeshTrianglesError::BadIndices) => { /* rebuild the index buffer against count_vertices(), then retry */ }\n    Err(other) => { /* topology / format / access errors */ }\n}","preventionTips":["Rebuild indices whenever vertex attributes are truncated or replaced","When merging meshes, offset all indices by the running vertex count","Assert max(index) < vertex_count at asset load time"],"tags":["bevy","bevy-mesh","rust","mesh","indices","out-of-bounds","triangles"],"backgroundTag":"mesh-index-out-of-bounds","analyzedSha":"396ca727080776bd313bb892423b7d94e03b81b4","analyzedAt":"2026-08-20T16:12:39.808Z","contentChangedAt":"2026-08-20T16:12:39.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}