| 194 | } |
| 195 | |
| 196 | void Bone3D::updateLocalMat() |
| 197 | { |
| 198 | if (_blendStates.size()) |
| 199 | { |
| 200 | Vec3 translate, scale; |
| 201 | Quaternion quat(Quaternion::ZERO); |
| 202 | |
| 203 | float total = 0.f; |
| 204 | for (const auto& it : _blendStates) |
| 205 | { |
| 206 | total += it.weight; |
| 207 | } |
| 208 | if (total) |
| 209 | { |
| 210 | if (_blendStates.size() == 1) |
| 211 | { |
| 212 | auto& state = _blendStates[0]; |
| 213 | translate = state.localTranslate; |
| 214 | scale = state.localScale; |
| 215 | quat = state.localRot; |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | float invTotal = 1.f / total; |
| 220 | for (const auto& it : _blendStates) |
| 221 | { |
| 222 | float weight = (it.weight * invTotal); |
| 223 | translate += it.localTranslate * weight; |
| 224 | scale.x += it.localScale.x * weight; |
| 225 | scale.y += it.localScale.y * weight; |
| 226 | scale.z += it.localScale.z * weight; |
| 227 | if (!quat.isZero()) |
| 228 | { |
| 229 | Quaternion& q = _blendStates[0].localRot; |
| 230 | if (q.x * quat.x + q.y * quat.y + q.z * quat.z + q.w * quat.w < 0) |
| 231 | weight = -weight; |
| 232 | } |
| 233 | quat = Quaternion(it.localRot.x * weight + quat.x, it.localRot.y * weight + quat.y, |
| 234 | it.localRot.z * weight + quat.z, it.localRot.w * weight + quat.w); |
| 235 | } |
| 236 | quat.normalize(); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | Mat4::createTranslation(translate, &_local); |
| 241 | _local.rotate(quat); |
| 242 | _local.scale(scale); |
| 243 | |
| 244 | _blendStates.clear(); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 249 | |