Skip to content

Commit

Permalink
filter first person skinned meshes
Browse files Browse the repository at this point in the history
  • Loading branch information
kayhhh committed Aug 4, 2024
1 parent 25655ec commit 47525d1
Showing 1 changed file with 53 additions and 5 deletions.
58 changes: 53 additions & 5 deletions crates/bevy_vrm/src/extensions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use bevy_gltf_kun::import::{extensions::BevyExtensionImport, gltf::document::Imp
use gltf_kun::{
extensions::ExtensionImport,
graph::{
gltf::{GltfDocument, GltfWeight, Material, Node, Primitive, Scene},
gltf::{
accessor::iter::AccessorIter, primitive::Semantic, GltfDocument, GltfWeight, Material,
Node, Primitive, Scene,
},
ByteNode, Edge, Extensions, Graph, Weight,
},
io::format::gltf::GltfFormat,
Expand Down Expand Up @@ -104,14 +107,19 @@ impl BevyExtensionImport<GltfDocument> for VrmExtensions {
let head_node = head.node(context.graph).unwrap();

for node in nodes {
let found = find_child(context.graph, node, head_node.children(context.graph));
let is_child = find_child(context.graph, node, head_node.children(context.graph));

if found {
if is_child {
flag = FirstPersonFlag::ThirdPersonOnly;
break;
}

if is_primitive_head_weighted(primitive, context.graph, node, head_node) {
flag = FirstPersonFlag::ThirdPersonOnly;
break;
} else {
flag = FirstPersonFlag::Both;
}

flag = FirstPersonFlag::Both;
}
}

Expand Down Expand Up @@ -310,6 +318,46 @@ fn find_child(graph: &Graph, target: Node, children: Vec<Node>) -> bool {
false
}

fn is_primitive_head_weighted(
primitive: Primitive,
graph: &Graph,
node: Node,
head_node: Node,
) -> bool {
if let Some(skin) = node.skin(graph) {
let joints = skin.joints(graph);
if let Some(accessor) = primitive.attribute(graph, Semantic::Joints(0)) {
match accessor.iter(graph) {
Ok(AccessorIter::U16x4(elements)) => {
for el in elements {
for idx in el {
let joint = joints[idx as usize];

let is_child = find_child(graph, joint, head_node.children(graph));

if is_child {
return true;
}
}
}
}
Ok(other) => {
warn!(
"Unsupported joints accessor type: {:?} {:?}",
other.component_type(),
other.element_type(),
);
}
Err(e) => {
error!("Error reading joints accessor: {}", e);
}
}
}
}

false
}

fn get_vrm_extension(graph: &Graph) -> Option<Vrm> {
let doc_idx = graph.node_indices().find(|n| {
let weight = graph.node_weight(*n);
Expand Down

0 comments on commit 47525d1

Please sign in to comment.