Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed loading avatars with interleaved buffers (#159) #161

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading