{"record":{"id":"4b3f3a35a39753a3","repo":"bevyengine/bevy","slug":"mesh-access-error-0-4b3f3a","errorCode":null,"errorMessage":"Mesh access error: {0}","messagePattern":"Mesh access error: (.+?)","errorType":"exception","errorClass":"GenerateTangentsError","httpStatus":null,"severity":"error","filePath":"crates/bevy_mesh/src/mikktspace.rs","lineNumber":75,"sourceCode":"        let idx = self.index(face, vert);\n        self.tangents[idx] = tangent_space.unwrap_or_default().tangent_encoded();\n    }\n}\n\n#[derive(Error, Debug)]\n/// Failed to generate tangents for the mesh.\npub enum GenerateTangentsError {\n    #[error(\"cannot generate tangents for {0:?}\")]\n    UnsupportedTopology(PrimitiveTopology),\n    #[error(\"missing indices\")]\n    MissingIndices,\n    #[error(\"missing vertex attributes '{0}'\")]\n    MissingVertexAttribute(&'static str),\n    #[error(\"the '{0}' vertex attribute should have {1:?} format\")]\n    InvalidVertexAttributeFormat(&'static str, VertexFormat),\n    #[error(\"mesh not suitable for tangent generation\")]\n    MikktspaceError(#[from] bevy_mikktspace::GenerateTangentSpaceError),\n    #[error(\"Mesh access error: {0}\")]\n    MeshAccessError(#[from] MeshAccessError),\n}\n\npub(crate) fn generate_tangents_for_mesh(\n    mesh: &Mesh,\n) -> Result<Vec<[f32; 4]>, GenerateTangentsError> {\n    match mesh.primitive_topology() {\n        PrimitiveTopology::TriangleList => {}\n        other => return Err(GenerateTangentsError::UnsupportedTopology(other)),\n    };\n\n    let positions = mesh.try_attribute_option(Mesh::ATTRIBUTE_POSITION)?.ok_or(\n        GenerateTangentsError::MissingVertexAttribute(Mesh::ATTRIBUTE_POSITION.name),\n    )?;\n    let VertexAttributeValues::Float32x3(positions) = positions else {\n        return Err(GenerateTangentsError::InvalidVertexAttributeFormat(\n            Mesh::ATTRIBUTE_POSITION.name,\n            VertexFormat::Float32x3,","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/bevyengine/bevy/blob/396ca727080776bd313bb892423b7d94e03b81b4/crates/bevy_mesh/src/mikktspace.rs#L57-L93","documentation":"GenerateTangentsError::MeshAccessError (crates/bevy_mesh/src/mikktspace.rs:75) propagates from the try_attribute_option calls at the start of generate_tangents_for_mesh. It is the render-world-extraction case: the mesh was created without MAIN_WORLD in its RenderAssetUsages, the renderer moved its data away, and tangent generation can no longer read POSITION/NORMAL/UV_0.","triggerScenarios":"Calling mesh.compute_tangents() on a Mesh created with RenderAssetUsages::RENDER_WORLD after it has been prepared by the render pipeline at least once.","commonSituations":"Memory-tight projects marking generated meshes render-world-only; systems that add normal maps and re-tangent meshes at runtime, running after the first frame has already extracted the asset.","solutions":["Create the mesh with RenderAssetUsages::default() so CPU data survives extraction","Run compute_tangents() during asset preparation, before the mesh is handed to the renderer","Regenerate the mesh from source data if extraction already happened"],"exampleFix":"// before\nlet mut mesh = Mesh::new(PrimitiveTopology::TriangleList, RenderAssetUsages::RENDER_WORLD);\n// ...extracted...\nmesh.compute_tangents(); // Err(MeshAccessError::ExtractedToRenderWorld)\n\n// after\nlet mut mesh = Mesh::new(PrimitiveTopology::TriangleList, RenderAssetUsages::default());\nmesh.compute_tangents().unwrap();","handlingStrategy":"validation","validationCode":"if mesh.asset_usage().contains(RenderAssetUsages::MAIN_WORLD) {\n    mesh.compute_tangents()?;\n} else {\n    // recreate mesh with RenderAssetUsages::default() first\n}","typeGuard":null,"tryCatchPattern":"if let Err(GenerateTangentsError::MeshAccessError(MeshAccessError::ExtractedToRenderWorld)) =\n    mesh.compute_tangents()\n{\n    // re-run generation on a fresh CPU-resident mesh\n}","preventionTips":["compute_tangents is CPU work: do it before extraction, on MAIN_WORLD-readable meshes","Reserve RenderAssetUsages::RENDER_WORLD for meshes past all processing","Schedule tangent baking in asset-preparation systems that run before rendering"],"tags":["bevy","mesh","tangents","render-asset-usage","asset-lifetime"],"backgroundTag":"mesh-extracted-to-render-world","analyzedSha":"396ca727080776bd313bb892423b7d94e03b81b4","analyzedAt":"2026-08-20T16:12:39.808Z","contentChangedAt":"2026-08-20T16:12:39.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}