Jump to content

Skeleton animation with ShaderMaterial


strmnati
 Share

Recommended Posts

Hello everyone.

 

I've found some issue.

 

I have a mesh with skeleton animation. Also, I have a specific shader used in ShaderMaterial.

 

When I assign the ShaderMaterial to the mesh. The animation disappear, but the effect of shader is working.

 

How do I have the animation with my shader?

Link to comment
Share on other sites

  • 1 month later...

Hi!

I'm working on this right now, and managed to make it work, somehow.

 

First of all, having a look at the basic shader source we can see we need to inject 3 values to the shader:

attribute vec4 matricesIndices;attribute vec4 matricesWeights;uniform mat4 mBones[BonesPerMesh];

Fortunately, Babylon will do this work for us. All we have to do is specify this values in the attributes and uniforms list when defining the shader, and Babylon will fill in this values automaticly. Like this:

var myMaterial = new BABYLON.ShaderMaterial("myMaterial", scene, "/path/to/myShader", {  attributes: ["position", "uv", "normal", "matricesIndices", "matricesWeights"],  uniforms: ["worldViewProjection", "world", "cameraPosition", "lightPosition", "ambient",  "mBones"]});

You can check Babylon source code to see how this values are filled, if curious. It's relatively easy to understand.

 

Going back to the basic shader source, let's see how this values are used:

void main(void) {    mat4 finalWorld = world;    #ifdef BONES        mat4 m0 = mBones[int(matricesIndices.x)] * matricesWeights.x;        mat4 m1 = mBones[int(matricesIndices.y)] * matricesWeights.y;        mat4 m2 = mBones[int(matricesIndices.z)] * matricesWeights.z;        #ifdef BONES4            mat4 m3 = mBones[int(matricesIndices.w)] * matricesWeights.w;            finalWorld = finalWorld * (m0 + m1 + m2 + m3);        #else            finalWorld = finalWorld * (m0 + m1 + m2);        #endif    #endif    gl_Position = worldViewProjection * finalWorld * vec4(position, 1.0);}

BONES and BONES4 defines will be also be generated automaticly if bones deformations by shader is set. 

So, basicly, the mat4 finalWorld will hold the bone deformation and you multiply it by the original vertex position. You can check if you remove it from the operation, the mesh will remain static.

 

And that's all. You will have your shader ready for other operations you want to apply.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...