| 317 | } |
| 318 | |
| 319 | inline runtime_skinned_mesh gltf_load_skinned_primitive(const cgltf_data * data, const cgltf_primitive * prim, const cgltf_skin * skin) |
| 320 | { |
| 321 | runtime_skinned_mesh mesh; |
| 322 | |
| 323 | if (!prim) return mesh; |
| 324 | |
| 325 | runtime_mesh base_mesh = gltf_load_primitive(prim); |
| 326 | mesh.vertices = std::move(base_mesh.vertices); |
| 327 | mesh.normals = std::move(base_mesh.normals); |
| 328 | mesh.tangents = std::move(base_mesh.tangents); |
| 329 | mesh.bitangents = std::move(base_mesh.bitangents); |
| 330 | mesh.texcoord0 = std::move(base_mesh.texcoord0); |
| 331 | mesh.texcoord1 = std::move(base_mesh.texcoord1); |
| 332 | mesh.colors = std::move(base_mesh.colors); |
| 333 | mesh.faces = std::move(base_mesh.faces); |
| 334 | mesh.material = std::move(base_mesh.material); |
| 335 | |
| 336 | for (size_t i = 0; i < prim->attributes_count; ++i) |
| 337 | { |
| 338 | const cgltf_attribute * attr = &prim->attributes[i]; |
| 339 | |
| 340 | if (attr->type == cgltf_attribute_type_joints && attr->index == 0) |
| 341 | { |
| 342 | mesh.boneIndices = extract_ivec4_accessor(attr->data); |
| 343 | } |
| 344 | else if (attr->type == cgltf_attribute_type_weights && attr->index == 0) |
| 345 | { |
| 346 | mesh.boneWeights = extract_vec4_accessor(attr->data); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | if (skin) |
| 351 | { |
| 352 | mesh.bones.resize(skin->joints_count); |
| 353 | for (size_t i = 0; i < skin->joints_count; ++i) |
| 354 | { |
| 355 | bone & b = mesh.bones[i]; |
| 356 | const cgltf_node * joint = skin->joints[i]; |
| 357 | |
| 358 | if (joint->name) b.name = joint->name; |
| 359 | b.parentIndex = 0xFFFFFFFF; |
| 360 | |
| 361 | if (joint->parent) |
| 362 | { |
| 363 | for (size_t j = 0; j < skin->joints_count; ++j) |
| 364 | { |
| 365 | if (skin->joints[j] == joint->parent) |
| 366 | { |
| 367 | b.parentIndex = static_cast<uint32_t>(j); |
| 368 | break; |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | b.initialPose = get_node_world_transform(joint); |
| 374 | |
| 375 | if (skin->inverse_bind_matrices) |
| 376 | { |
no test coverage detected