How would I go about getting the position of a bone or “node” (I guess) of a GLTF Model? I want to attach an object to a GLTF Model, but only have it follow one object in that GLTF. It has animations, so I want it to follow the object’s animation. (I know this is confusing) If you need the website or some code, feel free to ask! Thanks a ton!
I think you’ll be able to use something such as this
const boneWorldPos = new THREE.Vector3()
function animate() {
bone.getWorldPosition(boneWorldPos)
object.position.copy(boneWorldPos)
}
This would get the world position of the bone and assign it to boneWorldPos
which can then be copied to the position of your object…
How would I go about actually getting that bone’s position from the GLTF model’s bones? I know how to move objects to a position, but Im not sure how to actually get that bones position in the GLTF model (or node, I guess)
are you using plain three or r3f? in plain three you can do something like this…
let bone
const boneWorldPos = new THREE.Vector3()
gltfLoader.load( 'modelURL', function ( gltf ) {
gltf.scene.traverse( function ( child ) {
if(child.name === 'theNameOfTheBone'){
bone = child
}
})
}
function animate() {
bone.getWorldPosition(boneWorldPos)
object.position.copy(boneWorldPos)
}
I am using A-FRAME for this project. I will try to use that and form it around A-FRAME. would I still need the scene in gltf.scene.traverse...
Or would I need to get the scene element? I just wanted to make sure, and then I will go ahead and try that.
im not sure how A-Frame works tbh.
you may be able to simplify it and use…
let bone = gltf.scene.getObjectByName('theNameOfTheBone')
essentially you just need to specify which bone you’re after and use bone.position
, however the suggestion above to use bone.getWorldPosition(boneWorldPos)
will return the absolute world position of the bone in question