home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the 3D Game Programming Gurus / gurus.iso / DirectX / dx9sdkcp.exe / SDK (C++) / Samples / Media / SkinnedMesh.fx < prev    next >
Encoding:
Text File  |  2002-11-12  |  3.3 KB  |  110 lines

  1. //
  2. // Skinned Mesh Effect file 
  3. // Copyright (c) 2000-2002 Microsoft Corporation. All rights reserved.
  4. //
  5.  
  6. float4 lhtDir = {0.0f, 0.0f, -1.0f, 1.0f};    //light Direction 
  7. float4 lightDiffuse = {0.6f, 0.6f, 0.6f, 1.0f}; // Light Diffuse
  8. float4 MaterialAmbient : MATERIALAMBIENT = {0.1f, 0.1f, 0.1f, 1.0f};
  9. float4 MaterialDiffuse : MATERIALDIFFUSE = {0.8f, 0.8f, 0.8f, 1.0f};
  10.  
  11. // Matrix Pallette
  12. static const int MAX_MATRICES = 26;
  13. float4x3    mWorldMatrixArray[MAX_MATRICES] : WORLDMATRIXARRAY;
  14. float4x4    mViewProj : VIEWPROJECTION;
  15.  
  16. ///////////////////////////////////////////////////////
  17. struct VS_INPUT
  18. {
  19.     float4  Pos             : POSITION;
  20.     float4  BlendWeights    : BLENDWEIGHT;
  21.     float4  BlendIndices    : BLENDINDICES;
  22.     float3  Normal          : NORMAL;
  23.     float3  Tex0            : TEXCOORD0;
  24. };
  25.  
  26. struct VS_OUTPUT
  27. {
  28.     float4  Pos     : POSITION;
  29.     float4  Diffuse : COLOR;
  30.     float2  Tex0    : TEXCOORD0;
  31. };
  32.  
  33.  
  34. float3 Diffuse(float3 Normal)
  35. {
  36.     float CosTheta;
  37.     
  38.     // N.L Clamped
  39.     CosTheta = max(0.0f, dot(Normal, lhtDir.xyz));
  40.        
  41.     // propogate scalar result to vector
  42.     return (CosTheta);
  43. }
  44.  
  45.  
  46. VS_OUTPUT VShade(VS_INPUT i, uniform int NumBones)
  47. {
  48.     VS_OUTPUT   o;
  49.     float3      Pos = 0.0f;
  50.     float3      Normal = 0.0f;    
  51.     float       LastWeight = 0.0f;
  52.      
  53.     // Compensate for lack of UBYTE4 on Geforce3
  54.     int4 IndexVector = D3DCOLORtoUBYTE4(i.BlendIndices);
  55.  
  56.     // cast the vectors to arrays for use in the for loop below
  57.     float BlendWeightsArray[4] = (float[4])i.BlendWeights;
  58.     int   IndexArray[4]        = (int[4])IndexVector;
  59.     
  60.     // calculate the pos/normal using the "normal" weights 
  61.     //        and accumulate the weights to calculate the last weight
  62.     for (int iBone = 0; iBone < NumBones-1; iBone++)
  63.     {
  64.         LastWeight = LastWeight + BlendWeightsArray[iBone];
  65.         
  66.         Pos += mul(i.Pos, mWorldMatrixArray[IndexArray[iBone]]) * BlendWeightsArray[iBone];
  67.         Normal += mul(i.Normal, mWorldMatrixArray[IndexArray[iBone]]) * BlendWeightsArray[iBone];
  68.     }
  69.     LastWeight = 1.0f - LastWeight; 
  70.  
  71.     // Now that we have the calculated weight, add in the final influence
  72.     Pos += (mul(i.Pos, mWorldMatrixArray[IndexArray[NumBones-1]]) * LastWeight);
  73.     Normal += (mul(i.Normal, mWorldMatrixArray[IndexArray[NumBones-1]]) * LastWeight); 
  74.     
  75.     // transform position from world space into view and then projection space
  76.     o.Pos = mul(float4(Pos.xyz, 1.0f), mViewProj);
  77.  
  78.     // normalize normals
  79.     Normal = normalize(Normal);
  80.  
  81.     // Shade (Ambient + etc.)
  82.     o.Diffuse.xyz = MaterialAmbient.xyz + Diffuse(Normal) * MaterialDiffuse.xyz;
  83.     o.Diffuse.w = 1.0f;
  84.  
  85.     // copy the input texture coordinate through
  86.     o.Tex0  = i.Tex0.xy;
  87.  
  88.     return o;
  89. }
  90.  
  91. int CurNumBones = 2;
  92. VertexShader vsArray[4] = { compile vs_1_1 VShade(1), 
  93.                             compile vs_1_1 VShade(2),
  94.                             compile vs_1_1 VShade(3),
  95.                             compile vs_1_1 VShade(4)
  96.                           };
  97.  
  98.  
  99. //////////////////////////////////////
  100. // Techniques specs follow
  101. //////////////////////////////////////
  102. technique t0
  103. {
  104.     pass p0
  105.     {
  106.         VertexShader = (vsArray[CurNumBones]);
  107.     }
  108. }
  109.  
  110.