Page 1 of 17

Bump mapping for Animated meshes

Posted: Tue Nov 19, 2013 1:46 pm
by robmar
I remember discussing this but can´t find the post, as Animated meshes in Irrlicht don´t support bump mapping.

Has anyone found a way to bump map an animated mesh in Irrlicht yet?

Re: Bump mapping for Animated meshes

Posted: Tue Nov 19, 2013 1:51 pm
by robmar
Found this post with google which seems to be a solution!

http://irrlicht.sourceforge.net/forum/v ... hp?t=36637

Has anyone tried it out?

Re: Bump mapping for Animated meshes

Posted: Tue Nov 19, 2013 6:38 pm
by robmar
Testing with an animated model with 1000 frames causes the system to run out of memory.

How to do this selectively for only those submeshes that need bump mapping?

Re: Bump mapping for Animated meshes

Posted: Wed Nov 20, 2013 9:21 am
by Nadro
You can apply material type per each mesh buffer eg. Node->getMaterial(neshBufferNumber);

Re: Bump mapping for Animated meshes

Posted: Wed Nov 20, 2013 9:47 am
by robmar
Yes I know, the problem is the tangent conversion for all the frames, which is needed as well.

I´m sort of confused... the model has bones which can be controlled through rotation, but it also has animation frames...

So the frames are what exactly? Are they meshes of pre-done bone rotations?

I guess for bump map to work on rotation, after each rotation, which must create a new mesh morph, then a call to convert..tangents would be needed, right?

So the frames all need tangentizing for them to work with bump mapping...

Is it possible to only convert the meshes say of one sub-mesh, say the hair? Can that submesh be processed from each frame?

Re: Bump mapping for Animated meshes

Posted: Wed Nov 20, 2013 11:05 am
by ent1ty
For a quick&dirty solution, you can "approximate" the tangent and binormal from the normal in the vertex shader. That way you dont need tangent meshes at all
Check out this: https://bitbucket.org/entity/irrrendere ... at=default

Of course, the quality is nowhere near a static mesh with pre-calculated tangents

Re: Bump mapping for Animated meshes

Posted: Wed Nov 20, 2013 12:13 pm
by mongoose7
You would still need to calculate the normal per frame.

Hmm, I don't think you should multiply the tangent by the normal matrix. The same for the binormal.

@robmar: The mesh, I think, is saved and the bone transformations are applied to it per frame in the meshbuffers. So you have to generate the normals on each frame. I don't know if the vertex type is retained, so I can't tell if you could just calculate the normal every nth frame.

Re: Bump mapping for Animated meshes

Posted: Wed Nov 20, 2013 1:13 pm
by robmar
The shader is the solution of course, offload the work to the GPU. Why isn't this already in Irrlicht?

Without this shader irrlicht models can´t be have bump mapped surfaces.

So maybe we could patch this line into this shader below and have a quality pixel shader with fast bump mapping!

vec3 Tangent = -vec3(abs(gl_Normal.y) + abs(gl_Normal.z), abs(gl_Normal.x), 0);

Code: Select all

 
// Bump map pixel shader HLSL
 
  sampler2D tex0;
  sampler2D tex1;
 
  struct v2p
  {
    float4 Position  : POSITION;
    float2 Texcoord0 : TEXCOORD0;
    float2 Texcoord1 : TEXCOORD1;
    float4 Diffuse   : COLOR0;
  };
 
// p2f is the final output data structure.
 
  struct p2f
  {
    float4 Color : COLOR0;
  };
 
// Main function has one input from vertex shader and two texture samplers. OUT is an object of p2f.
 
  void mainPS( in v2p IN, out p2f OUT )
  {
     float4 Color = tex2D(tex0, IN.Texcoord0);  // fetch base Color and Normal
     float4 Normal = tex2D(tex1, IN.Texcoord1);
 
     OUT.Color = Color*dot(2.0*(Normal-0.5), 2.0*(IN.Diffuse-0.5));  // set final color
  }
 
// Following code is the vertex shader
 
  float4x4  ModelWorld;
  float4x4  ModelViewProj;
  float4    vLight;
  float4    vEye;
  float4    vDiffuseMaterial;
  float4    vSpecularMaterial;
  float4    vAmbient;
  float     power;
 
global variables
   struct a2v {
    float4 Position : POSITION;
    float2 Texcoord : TEXCOORD0;
    float4 Normal   : NORMAL; 
    float4 Tangent  : TANGENT;      
  };
 
  struct v2p {
    float4 Position  : POSITION;
    float4 Color     : COLOR0;
    float2 Texcoord0 : TEXCOORD0;
    float2 Texcoord1 : TEXCOORD1;
  };
 
// Define mainVS function, get position
 
  void mainVS( in a2v IN, out v2p OUT )
  {
    OUT.Position = mul(IN.Position, ModelViewProj);
 
// transform normal from model-space to view-space
    float4 tangent = mul(float4(IN.Tangent.xyz,0.0),ModelWorld);
    float4 normal = mul(float4(IN.Normal.xyz,0.0),ModelWorld);
    float3 binormal = cross(normal.xyz,tangent.xyz);
 
// Position in World
    float4 posWorld = mul(IN.Position, ModelWorld);
 
// get normalize light, eye vector, and half angle vector
    float4 light = normalize(posWorld-vLight);
    float4 eye = normalize(vEye-light);
    float4 vhalf = normalize(eye-vLight);
 
// transform light and vhalf vectors to tangent space
    float3 L = float3(dot(tangent, light), dot(binormal, light.xyz), dot(normal, light));
    float3 H = float3(dot(tangent, vhalf), dot(binormal, vhalf.xyz), dot(normal, vhalf));
 
// calculate diffuse and specular components
    float diffuse = dot(normal, L);
    float specular = dot(normal, H);
    specular = pow(specular, power);
 
// combine diffuse and specular contributions and output final vertex color, set texture coordinates, and return output object.
    OUT.Color = 2.0*(diffuse*vDiffuseMaterial + specular*vSpecularMaterial) + 0.5 + vAmbient;
 
    OUT.Texcoord0 = IN.Texcoord;
    OUT.Texcoord1 = IN.Texcoord;
  }
 

Re: Bump mapping for Animated meshes

Posted: Wed Nov 20, 2013 1:19 pm
by Nadro
robmar wrote:Without this shader irrlicht models can´t be have bump mapped surfaces.
What? You can use built-in normal map material.

Re: Bump mapping for Animated meshes

Posted: Wed Nov 20, 2013 1:28 pm
by robmar
I think you´re missing the bit about calculating tangents for animated models totally overloading the CPU!

Re: Bump mapping for Animated meshes

Posted: Wed Nov 20, 2013 1:35 pm
by Nadro
Use skinning mesh then.

Re: Bump mapping for Animated meshes

Posted: Wed Nov 20, 2013 3:26 pm
by robmar
That's not what we´re talking about here.

Re: Bump mapping for Animated meshes

Posted: Wed Nov 20, 2013 3:37 pm
by Nadro
No, you said that bump mapping with animated models doesn't workd out of box, but it's not true. SkinnedMeshes are compatible with normal mapping, so I don't see any problems. Only small marigin of users need support normal mapping + key frame based animations. If someone really need it I don't see any problems to use custom shader.

Next time, before you will say something like 'it doesn't work' etc. you should check available solutions for that case, because as I said before you don't need your shader to get normal mapping on animated mesh in Irrlicht. Of course you can prepare your own (eg. more optimized for your game), but it isn't require.

Re: Bump mapping for Animated meshes

Posted: Wed Nov 20, 2013 3:50 pm
by robmar
So you are telling me that a B3D animated model with 96 bones, 1080 frames, and 18 materials and their submeshes can be bump mapped differently on each material (and animated) with Irrlicht SkinnedMeshes?

Re: Bump mapping for Animated meshes

Posted: Wed Nov 20, 2013 3:52 pm
by robmar
If this is possible, is there a sample we can look at that will work with this type of animated b3d model?