joshcamas

Attach mesh to bone

Recommended Posts

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);};

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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. :(

Share this post


Link to post
Share on other sites

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... 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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);};

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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 :(

Share this post


Link to post
Share on other sites

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? :D

Share this post


Link to post
Share on other sites

No problem :) I'm used to it :D

 

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.js

I 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

Share this post


Link to post
Share on other sites

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>

Share this post


Link to post
Share on other sites

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.;) 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

@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!

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.