问题描述
有没有办法将网格附加到骨骼上?例如,我加载了动画 .js 角色,并且我想将武器附加到它的手上.
Is there any way to attach mesh to bone? For example I load animated .js character and i want to attach weapon to it's hand.
推荐答案
可以在 Bone 和 Object3D 原型上使用一些简单的技巧.由于骨骼继承自 Object3D,它们可能有子元素,因此我们可以轻松地 .add() 向任何骨骼添加网格.
It is possible using a few simple hacks on the Bone and Object3D prototype. Since bones inherit from Object3D, they may have children, so we can easily .add() a mesh to any bone.
然而,SkinnedMesh 只会在非 Bone 子节点上调用 updateMatrixWorld(),而为 Bone 子节点调用 update().此外,骨骼为其每个子元素调用 update()(无论它是否为骨骼).以下代码段改变了这种行为:
However, the SkinnedMesh will only call updateMatrixWorld() on non-Bone children, and update() for Bone children. Additionally, bones call update() for each of its children (regardless of wether it is a bone or not). The following snippets change that behavior:
// modify bone update function to also update its world matrix
// possibly do this only to skeletons you need it for, not for all bones
var update = THREE.Bone.prototype.update;
THREE.Bone.prototype.update = function(parentSkinMatrix, forceUpdate) {
update.call(this, parentSkinMatrix, forceUpdate);
this.updateMatrixWorld( true );
};
// add noop update function to Object3D prototype
// so any Object3D may be a child of a Bone
THREE.Object3D.prototype.update = function() {};
// create buffalo ...
// create a mesh and add it to some bone
var mesh = new THREE.Mesh( new THREE.SphereGeometry(20), new THREE.MeshNormalMaterial() );
var bone = buffalo.bones[5];
bone.add(mesh);
这篇关于Three.js 将对象附加到骨骼的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!