Skip to content

Commit

Permalink
Fixed loading avatars with interleaved buffers (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcusLongmuir authored Jul 17, 2024
1 parent 2a37862 commit f0a158e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
33 changes: 23 additions & 10 deletions packages/3d-web-avatar/src/character/MMLCharacter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { ModelLoadResult } from "@mml-io/model-loader";
import { Bone, BufferAttribute, Group, MathUtils, Object3D, Skeleton, SkinnedMesh } from "three";
import {
Bone,
BufferAttribute,
Group,
InterleavedBufferAttribute,
MathUtils,
Object3D,
Skeleton,
SkinnedMesh,
} from "three";

import { MMLCharacterDescriptionPart } from "../helpers/parseMMLDescription";

Expand Down Expand Up @@ -33,20 +42,24 @@ export class MMLCharacter {
const boneIndexMap = this.createBoneIndexMap(originSkeleton, targetSkeleton);

const newSkinIndexArray = [];
for (let i = 0; i < originGeometry.attributes.skinIndex.array.length; i++) {
const originIndex = originGeometry.attributes.skinIndex.array[i];
const missingBoneIndices = new Set();

const skinIndexAttribute = originGeometry.attributes.skinIndex;
for (let i = 0; i < skinIndexAttribute.count; i++) {
const originIndex = skinIndexAttribute.getComponent(i, 0);
const targetIndex = boneIndexMap.get(originIndex);
if (targetIndex !== undefined) {
newSkinIndexArray.push(targetIndex);
skinIndexAttribute.setComponent(i, 0, targetIndex);
} else {
console.error("Missing bone index", originIndex);
newSkinIndexArray.push(0);
missingBoneIndices.add(originIndex);
}
}
skinnedMesh.geometry.attributes.skinIndex = new BufferAttribute(
new Uint8Array(newSkinIndexArray),
4,
);

if (missingBoneIndices.size > 0) {
console.warn(
`Missing bone indices in skinIndex attribute: ${Array.from(missingBoneIndices).join(", ")}`,
);
}
}

public async mergeBodyParts(
Expand Down
6 changes: 3 additions & 3 deletions packages/3d-web-client-core/src/character/Character.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ export class Character extends Group {

private async load(): Promise<void> {
const previousModel = this.model;
if (previousModel && previousModel.mesh) {
this.remove(previousModel.mesh!);
}
this.model = new CharacterModel({
characterDescription: this.config.characterDescription,
animationConfig: this.config.animationConfig,
Expand All @@ -79,9 +82,6 @@ export class Character extends Group {
isLocal: this.config.isLocal,
});
await this.model.init();
if (previousModel && previousModel.mesh) {
this.remove(previousModel.mesh!);
}
this.add(this.model.mesh!);
if (this.speakingIndicator === null) {
this.speakingIndicator = new CharacterSpeakingIndicator(this.config.composer.postPostScene);
Expand Down

0 comments on commit f0a158e

Please sign in to comment.