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

Support InterleavedBufferAttributes in VRMUtils #1532

Merged
merged 1 commit into from
Nov 19, 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
26 changes: 13 additions & 13 deletions packages/three-vrm/src/VRMUtils/removeUnnecessaryJoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,29 @@ export function removeUnnecessaryJoints(
// Iterate over all skinned meshes and collect bones and boneInverses
for (const mesh of skinnedMeshes) {
const geometry = mesh.geometry;
const attribute = geometry.getAttribute('skinIndex') as THREE.BufferAttribute;
const attribute = geometry.getAttribute('skinIndex');

const bones: THREE.Bone[] = []; // new list of bone
const boneInverses: THREE.Matrix4[] = []; // new list of boneInverse
const boneIndexMap: { [index: number]: number } = {}; // map of old bone index vs. new bone index

// create a new bone map
const array = attribute.array;
for (let i = 0; i < array.length; i++) {
const index = array[i];

// new skinIndex buffer
if (boneIndexMap[index] == null) {
boneIndexMap[index] = bones.length;
bones.push(mesh.skeleton.bones[index]);
boneInverses.push(mesh.skeleton.boneInverses[index]);
for (let i = 0; i < attribute.count; i++) {
for (let j = 0; j < attribute.itemSize; j++) {
const index = attribute.getComponent(i, j);

// new skinIndex buffer
if (boneIndexMap[index] == null) {
boneIndexMap[index] = bones.length;
bones.push(mesh.skeleton.bones[index]);
boneInverses.push(mesh.skeleton.boneInverses[index]);
}

attribute.setComponent(i, j, boneIndexMap[index]);
}

array[i] = boneIndexMap[index];
}

// replace with new indices
attribute.copyArray(array);
attribute.needsUpdate = true;

// update boneList
Expand Down
59 changes: 40 additions & 19 deletions packages/three-vrm/src/VRMUtils/removeUnnecessaryVertices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export function removeUnnecessaryVertices(root: THREE.Object3D): void {
const mesh = obj as THREE.Mesh;
const geometry = mesh.geometry;

// if the geometry does not have an index buffer it does not need to process
const origianlIndex = geometry.index;
if (origianlIndex == null) {
// if the geometry does not have an index buffer it does not need to be processed
const originalIndex = geometry.index;
if (originalIndex == null) {
return;
}

Expand All @@ -36,6 +36,41 @@ export function removeUnnecessaryVertices(root: THREE.Object3D): void {
return;
}

// determine which vertices are used in the geometry
const vertexCount = Object.values(geometry.attributes)[0].count;
const vertexInUse = new Array(vertexCount);
let verticesUsed = 0;

const originalIndexArray = originalIndex.array;
for (let i = 0; i < originalIndexArray.length; i++) {
const index = originalIndexArray[i];
if (!vertexInUse[index]) {
vertexInUse[index] = true;
verticesUsed++;
}
}

// if the geometry uses all vertices it does not need to be processed
if (verticesUsed === vertexCount) {
return;
}

/** from original index to new index */
const originalIndexNewIndexMap: number[] = [];

/** from new index to original index */
const newIndexOriginalIndexMap: number[] = [];

// assign new indices
let indexHead = 0;
for (let i = 0; i < vertexInUse.length; i++) {
if (vertexInUse[i]) {
const newIndex = indexHead++;
originalIndexNewIndexMap[i] = newIndex;
newIndexOriginalIndexMap[newIndex] = i;
}
}

const newGeometry = new THREE.BufferGeometry();

// copy various properties
Expand All @@ -58,29 +93,15 @@ export function removeUnnecessaryVertices(root: THREE.Object3D): void {
// set to geometryMap
geometryMap.set(geometry, newGeometry);

/** from original index to new index */
const originalIndexNewIndexMap: number[] = [];

/** from new index to original index */
const newIndexOriginalIndexMap: number[] = [];

// reorganize indices
{
const originalIndexArray = origianlIndex.array;
const originalIndexArray = originalIndex.array;
const newIndexArray = new (originalIndexArray.constructor as any)(originalIndexArray.length);

let indexHead = 0;

for (let i = 0; i < originalIndexArray.length; i++) {
const originalIndex = originalIndexArray[i];

let newIndex = originalIndexNewIndexMap[originalIndex];
if (newIndex == null) {
originalIndexNewIndexMap[originalIndex] = indexHead;
newIndexOriginalIndexMap[indexHead] = originalIndex;
newIndex = indexHead;
indexHead++;
}
const newIndex = originalIndexNewIndexMap[originalIndex];
newIndexArray[i] = newIndex;
}

Expand Down