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

Improved removeUnnecessaryJoints #256

Merged
merged 5 commits into from
Dec 27, 2019
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
2 changes: 2 additions & 0 deletions examples/animations.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@

( gltf ) => {

THREE.VRMUtils.removeUnnecessaryJoints( gltf.scene );

THREE.VRM.from( gltf ).then( ( vrm ) => {

scene.add( vrm.scene );
Expand Down
3 changes: 3 additions & 0 deletions examples/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
// called when the resource is loaded
( gltf ) => {

// calling this function greatly improves the performance
THREE.VRMUtils.removeUnnecessaryJoints( gltf.scene );

// generate VRM instance from gltf
THREE.VRM.from( gltf ).then( ( vrm ) => {

Expand Down
2 changes: 2 additions & 0 deletions examples/blendshapes.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@

( gltf ) => {

THREE.VRMUtils.removeUnnecessaryJoints( gltf.scene );

THREE.VRM.from( gltf ).then( ( vrm ) => {

scene.add( vrm.scene );
Expand Down
2 changes: 2 additions & 0 deletions examples/bones.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@

( gltf ) => {

THREE.VRMUtils.removeUnnecessaryJoints( gltf.scene );

THREE.VRM.from( gltf ).then( ( vrm ) => {

scene.add( vrm.scene );
Expand Down
2 changes: 2 additions & 0 deletions examples/dnd.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@

( gltf ) => {

THREE.VRMUtils.removeUnnecessaryJoints( gltf.scene );

THREE.VRM.from( gltf ).then( ( vrm ) => {

if ( currentVrm ) {
Expand Down
2 changes: 2 additions & 0 deletions examples/envmap.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@
// called when the resource is loaded
( gltf ) => {

THREE.VRMUtils.removeUnnecessaryJoints( gltf.scene );

// generate VRM instance from gltf
THREE.VRM.from(
gltf,
Expand Down
2 changes: 2 additions & 0 deletions examples/firstperson.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@

( gltf ) => {

THREE.VRMUtils.removeUnnecessaryJoints( gltf.scene );

THREE.VRM.from( gltf ).then( ( vrm ) => {

scene.add( vrm.scene );
Expand Down
2 changes: 2 additions & 0 deletions examples/lookat-advanced.html
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@

( gltf ) => {

THREE.VRMUtils.removeUnnecessaryJoints( gltf.scene );

const lookAtImporter = new VRMSmoothLookAtImporter();
THREE.VRM.from( gltf, { lookAtImporter } ).then( ( vrm ) => {

Expand Down
2 changes: 2 additions & 0 deletions examples/lookat.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@

( gltf ) => {

THREE.VRMUtils.removeUnnecessaryJoints( gltf.scene );

THREE.VRM.from( gltf ).then( ( vrm ) => {

scene.add( vrm.scene );
Expand Down
2 changes: 2 additions & 0 deletions examples/materials-debug.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@

( gltf ) => {

THREE.VRMUtils.removeUnnecessaryJoints( gltf.scene );

THREE.VRM.from( gltf ).then( ( vrm ) => {

scene.add( vrm.scene );
Expand Down
2 changes: 2 additions & 0 deletions examples/meta.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
// called when the resource is loaded
( gltf ) => {

THREE.VRMUtils.removeUnnecessaryJoints( gltf.scene );

// generate VRM instance from gltf
THREE.VRM.from( gltf ).then( ( vrm ) => {

Expand Down
2 changes: 2 additions & 0 deletions examples/mouse.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@

( gltf ) => {

THREE.VRMUtils.removeUnnecessaryJoints( gltf.scene );

THREE.VRM.from( gltf ).then( ( vrm ) => {

scene.add( vrm.scene );
Expand Down
9 changes: 9 additions & 0 deletions src/vrm/VRMUtils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { removeUnnecessaryJoints } from './removeUnnecessaryJoints';

export class VRMUtils {
private constructor() {
// this class is not meant to be instantiated
}

public static removeUnnecessaryJoints = removeUnnecessaryJoints;
}
61 changes: 61 additions & 0 deletions src/vrm/VRMUtils/removeUnnecessaryJoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import * as THREE from 'three';

/**
* Traverse given object and remove unnecessarily bound joints from every `THREE.SkinnedMesh`.
* Some environments like mobile devices have a lower limit of bones and might be unable to perform mesh skinning, this function might resolve such an issue.
* Also this function might greatly improve the performance of mesh skinning.
*
* @param root Root object that will be traversed
*/
export function removeUnnecessaryJoints(root: THREE.Object3D): void {
// some meshes might share a same skinIndex attribute and this map prevents to convert the attribute twice
const skeletonList: Map<THREE.BufferAttribute, THREE.Skeleton> = new Map();

// Traverse an entire tree
root.traverse((obj) => {
if (obj.type !== 'SkinnedMesh') {
return;
}

const mesh = obj as THREE.SkinnedMesh;
const geometry = mesh.geometry as THREE.BufferGeometry;
const attribute = geometry.getAttribute('skinIndex') as THREE.BufferAttribute;

// look for existing skeleton
let skeleton = skeletonList.get(attribute);

if (!skeleton) {
// generate reduced bone list
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 as number[];
for (let i = 0; i < array.length; i++) {
const index = array[i];

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

array[i] = boneIndexMap[index];
}

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

// replace with new indices
skeleton = new THREE.Skeleton(bones, boneInverses);
skeletonList.set(attribute, skeleton);
}

mesh.bind(skeleton, new THREE.Matrix4());
// ^^^^^^^^^^^^^^^^^^^ transform of meshes should be ignored
// See: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skins
});
}
2 changes: 1 addition & 1 deletion src/vrm/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from './VRM';
export * from './VRMImporter';
export * from './removeUnnecessaryJoints';
export * from './VRMUtils';
export * from './blendshape';
export * from './debug';
export * from './firstperson';
Expand Down
43 changes: 0 additions & 43 deletions src/vrm/removeUnnecessaryJoints.ts

This file was deleted.