createActor static method
Creates an animated actor, ready for openworld's Actor/Mob functions.
animations is the name of a set in animationSets (e.g. "man") or a
map of animation name -> definition. The skeleton (bones and bind
pose) is taken from the set's "idle" animation, or its first one, as
in MudClient3D; if no animation is given the mesh's own joints are used.
Implementation
static Future<THREE.Object3D> createActor(
String path, {
dynamic animations,
Map<String, String>? texturereplace,
String? action = 'idle',
bool basic = false,
bool castshadow = false,
double? randomduration,
double? startat,
double? duration,
double? z,
Set<String> hidegroups = const {},
}) async {
final m = await loadMs3d(path);
final Map<String, Ms3dAnimDef> anims = animations is String
? (animationSets[animations] ?? const {})
: (animations is Map<String, Ms3dAnimDef> ? animations : const {});
// Skeleton source.
List<Ms3dJoint> joints = m.joints;
if (anims.isNotEmpty) {
final skelDef = anims['idle'] ?? anims.values.first;
if (skelDef.filename.toLowerCase().endsWith('.smd')) {
joints = SmdFile.parse(await AssetPack.loadString(skelDef.filename)).toJoints();
} else {
joints = (await loadMs3d(skelDef.filename)).joints;
}
}
final mats = await _materials(path, m, texturereplace: texturereplace, basic: basic);
final fallback = THREE.MeshStandardMaterial();
final root = THREE.Group();
final holder = THREE.Group();
holder.rotation.y = math.pi;
root.add(holder);
// Bones: an identity root bone (for vertices without a joint) + joints.
final rootBone = THREE.Bone();
rootBone.name = 'ms3d_root';
holder.add(rootBone);
final bones = <THREE.Bone>[];
for (var i = 0; i < joints.length; i++) {
final j = joints[i];
final b = THREE.Bone();
b.name = 'ms3d_j$i';
b.position.setValues(j.position[0], j.position[1], j.position[2]);
final q = _quat(j.rotation[0], j.rotation[1], j.rotation[2]);
b.quaternion.set(q.x, q.y, q.z, q.w);
bones.add(b);
}
for (var i = 0; i < joints.length; i++) {
final p = joints[i].parentIndex;
(p >= 0 && p < bones.length ? bones[p] : rootBone).add(bones[i]);
}
// Bind pose inverses, computed relative to the holder.
root.updateMatrixWorld(true);
final holderInv = holder.matrixWorld.clone().invert();
final allBones = <THREE.Bone>[rootBone, ...bones];
final inverses = [
for (final b in allBones) holderInv.clone().multiply(b.matrixWorld).invert()
];
final skeleton = THREE.Skeleton(allBones, inverses);
for (final (name, mi, tris) in _meshGroups(m, false)) {
final (geo, corner) = _geometry(m, tris);
final n = corner.length;
final si = Uint16List(n * 4), sw = Float32List(n * 4);
for (var c = 0; c < n; c++) {
final b = m.boneIds[corner[c]];
si[c * 4] = (b >= 0 && b < bones.length) ? b + 1 : 0; // +1: slot 0 is ms3d_root
sw[c * 4] = 1.0;
}
geo.setAttributeFromString('skinIndex', _u16(si, 4));
geo.setAttributeFromString('skinWeight', _f32(sw, 4));
final mesh = THREE.SkinnedMesh(geo, mi >= 0 ? mats[mi] : fallback);
mesh.name = name;
mesh.extra['groupname'] = name;
mesh.castShadow = castshadow;
mesh.frustumCulled = false; // bounds move with the animation
mesh.visible = !hidegroups.contains(name);
holder.add(mesh);
mesh.bind(skeleton, THREE.Matrix4.identity());
}
// Clips.
final clips = <THREE.AnimationClip>[];
for (final e in anims.entries) {
try {
clips.add(await loadClip(e.key, e.value));
} catch (err) {
debugPrint('ms3d: animation ${e.value.filename} failed: $err');
}
}
root.extra['animations'] = clips;
root.extra['path'] = path;
root.extra['ms3d'] = m;
root.extra['ms3danimations'] = anims;
root.extra['origrotation'] = root.rotation;
root.castShadow = castshadow;
if (z != null) holder.position.y += z;
return root;
}