TYPeee

SkeletonUtils - Cloning 3D Model in Three.js

To clone a model in Three.js we need to use 'SkeletonUtils' module. The 'SkeletonUtils' module in three.js provides utilities for working with skinned meshes and skeletons. These utilities are particularly useful for tasks such as cloning skinned meshes while preserving their skeletons. This can be important when you need to create multiple instances of a character or object with complex animations and deformations.

SkeletonUtils.Clone

The SkeletonUtils.clone function is designed to create a deep copy of a skinned mesh, including its skeleton. This is necessary because a simple .clone on a skinned mesh won't correctly duplicate the skeleton's hierarchical structure and the relationships between the bones.

 

1. import 'SkeletonUtils'

javascript
1import * as SkeletonUtils from 'three/examples/jsm/utils/SkeletonUtils.js';

 

2. Load a Model

javascript
1import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
2
3const loader = new GLTFLoader();
4let originalModel;
5
6loader.load('path/to/your/model.glb', (gltf) => {
7   originalModel = gltf.scene;
8   scene.add(originalModel);
9});

 


3. Clone Model

javascript
1function cloneModel(model) {
2   const clonedModel = SkeletonUtils.clone(model);
3   scene.add(clonedModel);
4   return clonedModel;
5}
6
7// Example usage
8if (originalModel) {
9   const clonedModel = cloneModel(originalModel);
10   clonedModel.position.set(0, 0, 10); // Move the cloned model to a different position
11}

Related Posts