| 37 | } |
| 38 | |
| 39 | void Skeleton::applyAnimation(const BlendStateWithClipData& blend_state) |
| 40 | { |
| 41 | if (!m_bones) |
| 42 | { |
| 43 | return; |
| 44 | } |
| 45 | resetSkeleton(); |
| 46 | for (size_t clip_index = 0; clip_index < 1; clip_index++) |
| 47 | { |
| 48 | const AnimationClip& animation_clip = blend_state.blend_clip[clip_index]; |
| 49 | const float phase = blend_state.blend_ratio[clip_index]; |
| 50 | const AnimSkelMap& anim_skel_map = blend_state.blend_anim_skel_map[clip_index]; |
| 51 | |
| 52 | float exact_frame = phase * (animation_clip.total_frame - 1); |
| 53 | int current_frame_low = floor(exact_frame); |
| 54 | int current_frame_high = ceil(exact_frame); |
| 55 | float lerp_ratio = exact_frame - current_frame_low; |
| 56 | // for (size_t node_index = 0; node_index < 0; node_index++) |
| 57 | for (size_t node_index = 0; |
| 58 | node_index < animation_clip.node_count && node_index < anim_skel_map.convert.size(); |
| 59 | node_index++) |
| 60 | { |
| 61 | AnimationChannel channel = animation_clip.node_channels[node_index]; |
| 62 | size_t bone_index = anim_skel_map.convert[node_index]; |
| 63 | float weight = 1; // blend_state.blend_weight[clip_index]->blend_weight[bone_index]; |
| 64 | weight = 1; |
| 65 | if (fabs(weight) < 0.0001f) |
| 66 | { |
| 67 | continue; |
| 68 | } |
| 69 | if (bone_index == std::numeric_limits<size_t>().max()) |
| 70 | { |
| 71 | // LOG_WARNING |
| 72 | continue; |
| 73 | } |
| 74 | Bone* bone = &m_bones[bone_index]; |
| 75 | if (channel.position_keys.size() <= current_frame_high) |
| 76 | { |
| 77 | current_frame_high = channel.position_keys.size() - 1; |
| 78 | } |
| 79 | if (channel.scaling_keys.size() <= current_frame_high) |
| 80 | { |
| 81 | current_frame_high = channel.scaling_keys.size() - 1; |
| 82 | } |
| 83 | if (channel.rotation_keys.size() <= current_frame_high) |
| 84 | { |
| 85 | current_frame_high = channel.rotation_keys.size() - 1; |
| 86 | } |
| 87 | current_frame_low = (current_frame_low < current_frame_high) ? current_frame_low : current_frame_high; |
| 88 | Vector3 position = Vector3::lerp( |
| 89 | channel.position_keys[current_frame_low], channel.position_keys[current_frame_high], lerp_ratio); |
| 90 | Vector3 scaling = Vector3::lerp( |
| 91 | channel.scaling_keys[current_frame_low], channel.scaling_keys[current_frame_high], lerp_ratio); |
| 92 | Quaternion rotation = Quaternion::nLerp(lerp_ratio, |
| 93 | channel.rotation_keys[current_frame_low], |
| 94 | channel.rotation_keys[current_frame_high], |
| 95 | true); |
| 96 | |