joshcamas Posted March 29, 2015 Share Posted March 29, 2015 Hello guys! I'm currently needing a way to attach a mesh to a specific bone in an armature. I found this link, but it seems to be outdated. Thanks! Quote Link to comment Share on other sites More sharing options...
Dad72 Posted March 30, 2015 Share Posted March 30, 2015 HI, Here is what I use and it works: var attachToBone = function(obj, ske, boneName) { var matricesWeights = []; var floatIndices = []; var boneIndice = -1; for (var i = 0; i < ske.bones.length; i++) { if (ske.bones[i].name == boneName) { boneIndice=i; break;}} if (boneIndice == -1) return; for (var ii = 0; ii < obj.getTotaleVertices(); 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; } obj.skeleton = ske; obj.setVerticesData(matricesWeights, BABYLON.VertexBuffer.MatricesWeightsKind, false); obj.setVerticesData(floatIndices, BABYLON.VertexBuffer.MatricesIndicesKind, false);}; Quote Link to comment Share on other sites More sharing options...
joshcamas Posted March 30, 2015 Author Share Posted March 30, 2015 Hmmmmmmmmmmm It doesn't work.However, it does something - it seems that when the bone moves locally, (such as in an animation) the obj attached does indeed move up and down with the bone does.However whenever I move the skeleton itself, the obj doesn't move. :C Quote Link to comment Share on other sites More sharing options...
Dad72 Posted March 31, 2015 Share Posted March 31, 2015 This is another problem then. maybe he should also use 'parent' for the object to follow the parent mesh while following the animation arm. Quote Link to comment Share on other sites More sharing options...
joshcamas Posted March 31, 2015 Author Share Posted March 31, 2015 Doesn't work. It attaches to the origin of the mesh. Basically the function only effects whatever changes happen to the bone locally, not whatever changes globally - so if the bone itself moves, it works - but if a parent bone, the armature, or whatever moves, the hat or object does not. Quote Link to comment Share on other sites More sharing options...
Dad72 Posted March 31, 2015 Share Posted March 31, 2015 Arf, I did not study this issue further. I have no idea. Can be Deltakosh able to give us advice on that. Quote Link to comment Share on other sites More sharing options...
joshcamas Posted March 31, 2015 Author Share Posted March 31, 2015 I've spent hours trying to find out how to even get the global position of a bone, to no avail. :C :C :C :C Maybe at some point anyone who wants to work on this should meet up in a chat or somethin... Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted March 31, 2015 Share Posted March 31, 2015 So basically here are the steps:- Add weights and indices to your mesh. This is the complex part unless your mesh already have it- Specify a skeleton in mesh.skeleton You're done Some things to consider:- Your mesh must be centered at the origin Quote Link to comment Share on other sites More sharing options...
joshcamas Posted March 31, 2015 Author Share Posted March 31, 2015 So why don't this work? Samac.Tools.mount = function(obj, ske, boneName) { var matricesWeights = []; var floatIndices = []; var boneIndice = -1; for (var i = 0; i < ske.bones.length; i++) { if (ske.bones[i].name == boneName) { boneIndice=i; break;}} if (boneIndice == -1) return; for (var ii = 0; ii < obj.getTotalVertices(); 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; } obj.skeleton = ske; obj.setVerticesData(BABYLON.VertexBuffer.MatricesWeightsKind, matricesWeights, false); obj.setVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind, floatIndices, false);}; Quote Link to comment Share on other sites More sharing options...
joshcamas Posted March 31, 2015 Author Share Posted March 31, 2015 I'm thinking some work needs to be done on the bone system in babylon itself. It seems very, very weak - an example of this would be what if your game uses clothes, and these clothes/armor are separate files? If these meshes have complex vertex groups, which need to be attached to certain bones on a human individually, it sounds like this is impossible. Unless I'm wrong? Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted April 1, 2015 Share Posted April 1, 2015 Feel free to contribute And no this is not impossible at all. The bones system is simple but could easily address this. You have issues in your case, I can understand that. Please share a sample somewhere that references babylon.max.js and we will all try to help you as always. You conclusion ("It seems very, very weak") is a little bit nasty Quote Link to comment Share on other sites More sharing options...
joshcamas Posted April 1, 2015 Author Share Posted April 1, 2015 Sorry if I sounded mean or something! I'm often very critical with everything, especially if it's my own work... i sincerely apologize for that. Yesterday was a bad day for me. EDIT: Okay! Very interesting! If you look at this little playground:http://www.babylonjs-playground.com/#11BH6Z You see something very interesmesting. First, notice that I scaled the dude mesh down A LOT. After I did this, I noticed that the mounted mesh moved A LOT. I thought this meant the bone hadn't been scaled along with the mesh, so I also tried scaling the skeleton, but that didn't work as well. I think I might understand this now. *maybe*. *probably not*. *we'll see*. It looks as if the bones don't move globally. *mind blows* So when a bone moves, it's deforming vertexes locally as well. So when the mesh moves, the vertexes move just like normal. So... why does all the vertexes move? Because it moves globally! In other words, we need to attach the object in the global position we want... aka the supposedly global position of a bone... This hurts my brain. xD Most likely all of you already know all this, and I'm the dumb one. So I guess it all lays to... how can you compute the position of a bone/joint? Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted April 2, 2015 Share Posted April 2, 2015 No problem I'm used to it So to be clearer: bones work on the mesh's local space. So by changing the mehs's position/rotation you can still have your skeleton that affects the mesh Look for instance the rabbits here:https://github.com/BabylonJS/Samples/blob/master/Scenes/Customs/bones.jsI moved rabbits but bones still works For me bones are just local deformations, you never need to change globally the position of bones. Only affected mesh should be changed. And btw, a skeleton has NO position or rotation propery. It is only a hierarchical collection of bones where a bone is ONLY a matrix that will be applied to specific vertices based on weightIndices property of every vertex Quote Link to comment Share on other sites More sharing options...
Dad72 Posted April 2, 2015 Share Posted April 2, 2015 No it does not work DK. requires that the bone is fixed to the mesh. but if you want to add a sword, for example on the hand of the character animated. So the sword is attached to the bone of the hand. but if it then moves the position of the character, sword not going to follow the character, it will carry out the movements while remaining on site. <fr>Non cela ne fonctionne pas DK. il faut que les os soit joint au maillage. mais si on veux ajouter un épée par exemple sur la main du personnage animé. on attache donc l’épée sur l'os de la main. mais si on déplace ensuite la position du personnage, l'épée ne vas pas suivre le personnage, elle effectuera les mouvements tout en restant sur place.</fr> Quote Link to comment Share on other sites More sharing options...
MarianG Posted April 2, 2015 Share Posted April 2, 2015 Hi joshcamas. I had the same problem like you, a few months ago, I spent days, not hours ) But finally I solve it in my way. Let me try to explain.I made the animation in maya.It contain a caracter and an object, and have two parts, one- to take the object (from frame x to y), and second- move with object(from y to z)When I save it with max, I obtain 2 meshes with 2 diferent skeletons. I put the same parent to the both meshes.And start the animation of childrens.And you can do what you want with parent, move it everywhere, and the animed childrens will follow it. Is a simple idea. Sorry my bad english. Quote Link to comment Share on other sites More sharing options...
Dad72 Posted April 2, 2015 Share Posted April 2, 2015 This idea is very limited I think. If this is just a weapon that does not change, this go, but in the case of a collection of weapons has changed, the task becomes tedious with your method. Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted April 2, 2015 Share Posted April 2, 2015 In this case the sword just be attached to the skeleton AND the mesh: sword.parent = mesh;sword.skeleton = mesh.skeleton; // Providing you already defined the right weightindices in the sword to target the right bone Quote Link to comment Share on other sites More sharing options...
joshcamas Posted April 3, 2015 Author Share Posted April 3, 2015 Ahhh so armatures have no global space at all! That clears up a lot! Closer, yes. However: http://www.babylonjs-playground.com/#11BH6Z#1 The attached mesh does not have the global position of the bone, it has the global position of the mesh. :C Also, since blender does allow global positioning of armatures, wouldn't it make sense if armatures in babylon did as well? Idk, just an idea. Problem is what will happen to all other games with armatures before this addition... But idk how anything else could be done to achieve this.This would allow you to compute global position, cause armature global position + bone local position = final value. ^For this to work, it would make sense to integrate "mounting" into babylon, because it needs to update every frame. Quote Link to comment Share on other sites More sharing options...
Dad72 Posted April 3, 2015 Share Posted April 3, 2015 I tried your approach Delta, the object is "attached" to point of pivot and not attached to the bones of the character.The use of the parent that the object is no longer attached to the bone. The object no longer follows the movements of the bones. http://www.babylonjs-playground.com/#11BH6Z#5 Quote Link to comment Share on other sites More sharing options...
reddozen Posted April 3, 2015 Share Posted April 3, 2015 I'll be watching this thread... haven't made it this far with the RO2 character models yet. It takes 3DS several hours to fully load all 300+ animations.... So yea, I haven't gotten this far yet. Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted April 3, 2015 Share Posted April 3, 2015 @Dad72: you missed to move the sphere to the hand. it is only attached to the mesh. You have to move the sphere RELATIVELY to the mesh to the position of the hand AND you also have to set sword only influenced by the hand's bone @Josh: THis will be counter-productive to update all mesh data every frame. And once again this is working the same way in other engines: vertex final position = position x Bones Matricesx world where world can be induced by parenting so: vertex final position = positionx Bones Matrices x localWord x parentWorld So this way, you get the opportunity to animate your mesh INDEPENDENTLY of its position, rotation and scaling and furthermore you can reuse the skeleton for instances and mesh's clone Once again: to add a son to a specific part of a mesh:- Move the son to the right place- Set son.parent = mesh (beware, you have then to call son.setAbsolutePosition to compensate the new parent)- Add bones info to the son You're done! Quote Link to comment Share on other sites More sharing options...
Dad72 Posted April 3, 2015 Share Posted April 3, 2015 There's better here. but why when you attach an object, it does not go to the exact position of the bone selected. I feel that this is because of the scale of the object that changes because of 'parent'. Almost there. http://www.babylonjs-playground.com/#11BH6Z#7 Quote Link to comment Share on other sites More sharing options...
joshcamas Posted April 3, 2015 Author Share Posted April 3, 2015 Ok, so... vertex final position = positionx Bones Matrices x localWord x parentWorld Sorry, but I'm still learning all this stuff works. Let me get my head wrapped around this... Are you saying that we shouldn't move the object itself at all, but we should simply move all the vertexes to the bone location, and keep the meshes position at the origin of the parent? Only problem is what if you wanted to rotate the object around the bone location, not the origin. Would we do that thing someone did before, when he wanted to rotate the mesh around a non-origin? EDIT: Wait. I just realized you said to do setAbsolutePosition. I'm going to try this for now, but I'm still probably wrong. xD GameMonetize 1 Quote Link to comment Share on other sites More sharing options...
joshcamas Posted April 6, 2015 Author Share Posted April 6, 2015 So DK... could you answer my question above? I'm stuck Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted April 6, 2015 Share Posted April 6, 2015 I say that you should first attach the sword to the player, then use setAbsolutePosition to move the sword where it should be then attach the skeleton This should not work actually depending on how the skeleton is done If this is not working then: update sword vertices to move to add an offset in order to have them at the right place when sword.position = (0, 0, 0) and then attach the skeleton 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.