Gugis Posted October 21, 2013 Share Posted October 21, 2013 Is there any way to attach 3D object to bone? For example attaching sword to hand, so sword would move along hand. Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted October 21, 2013 Share Posted October 21, 2013 You can use parenting (sword.parent = hand). Doing that the sword will inherit all hand's transformations Quote Link to comment Share on other sites More sharing options...
vbillet Posted November 1, 2013 Share Posted November 1, 2013 I Assume sword is a mesh (BABYLON.Mesh) and Armature is a skeleton (BABYLON.Skeleton) containing one bone (BABYLON.bones). In engine.runRenderLoop function, I use the following code :engine.runRenderLoop(function() { sword.parent=Armature.bones[0]; scene.render();});It result the following error : Uncaught TypeError: Object [object Object] has no method 'isEnabled' And if I use the following code : engine.runRenderLoop(function() { scene.render(); sword.parent=Armature.bones[0];});It result the following error : Uncaught TypeError: Object [object Object] has no method '_needToSynchonizeChildren' In both cases, sword does not follow bone's motion. Any Idea about this ? Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted November 1, 2013 Share Posted November 1, 2013 Hello,a bone cannot be attached this way. You have to set sword.skeleton=skeletonbut you also have to define matrices weights and indices into your sword Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted November 1, 2013 Share Posted November 1, 2013 https://github.com/BabylonJS/Babylon.js/wiki/How-to-use-bones-and-skeletons Quote Link to comment Share on other sites More sharing options...
vbillet Posted November 1, 2013 Share Posted November 1, 2013 Hello,a bone cannot be attached this way. You have to set sword.skeleton=skeletonbut you also have to define matrices weights and indices into your swordHow can I do this ?matricesWeights = ???;floatIndices = ???;sword.setVerticesData(matricesWeights, BABYLON.VertexBuffer.MatricesWeightsKind, false);sword.setVerticesData(floatIndices, BABYLON.VertexBuffer.MatricesIndicesKind, false); It's not easy to understand.Is it 4 float per vertex ? only 4 floats ?finally an example could be fine .... Quote Link to comment Share on other sites More sharing options...
vbillet Posted November 1, 2013 Share Posted November 1, 2013 Thanks for your help.Finally here is my code : function mount(obj,ske,boneName){ matricesWeights =[]; floatIndices =[]; boneIndice=-1; // Find bone's Indice for (ii=0;ii<ske.bones.length;ii++) { if (ske.bones[ii].name==boneName) { boneIndice=ii; break; } } if (boneIndice==-1) {console.error("Unable to find bone : "+boneName); return;} // Build matrices and indices buffers. for (ii=0;ii<obj._totalVertices;ii++) { matricesWeights[ii*4+0]=1.0; matricesWeights[ii*4+1]=0.0; matricesWeights[ii*4+2]=0.0; matricesWeights[ii*4+3]=0.0; floatIndices[ii*4+0]=boneIndice; floatIndices[ii*4+1]=boneIndice; floatIndices[ii*4+2]=boneIndice; floatIndices[ii*4+3]=boneIndice; } // Mounting the object on the skeleton obj.skeleton = ske; obj.setVerticesData(matricesWeights, BABYLON.VertexBuffer.MatricesWeightsKind, false); obj.setVerticesData(floatIndices, BABYLON.VertexBuffer.MatricesIndicesKind, false);}mounted = false;engine.runRenderLoop(function() { if ((sword!=undefined) && (Armature!=undefined)) { if (!mounted) { mount(sword,Armature,"Bone"); mounted=true; } } scene.render();});And that's done. @TODO : unmount function Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted November 1, 2013 Share Posted November 1, 2013 EXCELLENT Could you mark the question as answered? PS: To unmount, just call sword.skeleton = null Quote Link to comment Share on other sites More sharing options...
vbillet Posted November 2, 2013 Share Posted November 2, 2013 Could you mark the question as answered? I can't mark it as answered.Could it be mark as answered ? Yes .... But.... NO. If I want to mount a bow or a crossbow on my bone, it might be difficult, because my bow need also a skeleton. This skeleton will be used for fire motion. Can we have multiple skeleton for a single mesh ? Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted November 2, 2013 Share Posted November 2, 2013 you can have only one skeleton but with many bones (one for bow, one for sword)A mesh can only have one skeleton but you can change the active skeleton when you want:) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.