getSkeletonOffsets static method
Map<String, dynamic>
getSkeletonOffsets(
- dynamic target,
- dynamic source, [
- SkeletonUtilsOptions? options
Implementation
static Map<String,dynamic> getSkeletonOffsets(target, source, [SkeletonUtilsOptions? options]) {
options = options ?? SkeletonUtilsOptions();
final targetParentPos = Vector3(),
targetPos = Vector3(),
sourceParentPos = Vector3(),
sourcePos = Vector3(),
targetDir = Vector2(),
sourceDir = Vector2();
options.hip = options.hip;
options.names = options.names;
if (source is! Object3D) {
source = getHelperFromSkeleton(source);
}
final List<String> nameKeys = options.names.keys.toList();
final List<String> nameValues = options.names.values.toList() as List<String>;
final List<Bone> sourceBones = source.skeleton?.bones ?? [],//source is Object3D ? (source.skeleton?.bones ?? []) : getBones(source),
bones = target is Object3D ? (target.skeleton?.bones ?? []) : getBones(target);
Map<String,dynamic> offsets = {};
Bone bone;
Bone? boneTo;
String name;
int i;
target.skeleton.pose();
for (i = 0; i < bones.length; ++i) {
bone = bones[i];
name = options.names[bone.name] ?? bone.name;
boneTo = getBoneByNameList(name, sourceBones);
if (boneTo != null && name != options.hip) {
final boneParent = getNearestBone(bone.parent!, nameKeys.toList()),
boneToParent = getNearestBone(boneTo.parent!, nameValues.toList());
boneParent?.updateMatrixWorld();
boneToParent?.updateMatrixWorld();
targetParentPos.setFromMatrixPosition(boneParent!.matrixWorld);
targetPos.setFromMatrixPosition(bone.matrixWorld);
sourceParentPos.setFromMatrixPosition(boneToParent!.matrixWorld);
sourcePos.setFromMatrixPosition(boneTo.matrixWorld);
targetDir
.sub2(Vector2(targetPos.x, targetPos.y), Vector2(targetParentPos.x, targetParentPos.y))
.normalize();
sourceDir
.sub2(Vector2(sourcePos.x, sourcePos.y), Vector2(sourceParentPos.x, sourceParentPos.y))
.normalize();
final laterialAngle = targetDir.angle() - sourceDir.angle();
final offset = Matrix4().makeRotationFromEuler(Euler(0, 0, laterialAngle));
bone.matrix.multiply(offset);
bone.matrix.decompose(bone.position, bone.quaternion, bone.scale);
bone.updateMatrixWorld();
offsets[name] = offset;
}
}
return offsets;
}