home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 18759 / ROE.7z / QSSHADER_QSSkinning.h < prev    next >
Encoding:
C/C++ Source or Header  |  2020-09-17  |  6.6 KB  |  202 lines

  1. #ifndef QSSHADER_QSSkinning_H
  2. #define QSSHADER_QSSkinning_H
  3.  
  4. //We will use texture bone now. so, just use SkinBone as PreSkinBone.
  5. //It will cause to much constant buffer.
  6. //float4x3 PreSkinBone[MAX_BONES];
  7. #if SKINNING
  8. sampler2D    BoneTex;
  9. sampler2D    PreBoneTex;
  10. float4        BoneTexUv; //xy: current frame boneUV, zw: previous frame boneUV
  11.  
  12. #define BONE_NUM_WIDTH 128.0f
  13. #define BONE_WIDTH (BONE_NUM_WIDTH*3)
  14. float4x3 ReadBoneData(int i)
  15. {
  16.     float4 uv = float4(BoneTexUv.xy, 0, 0);
  17.     uv.x += i/BONE_NUM_WIDTH;
  18.     
  19.     float4x3 result;
  20.     float4 v0 = tex2Dlod(BoneTex, NoMipMapPointClamp, uv).rgba;    
  21.     uv.x +=1.0f/BONE_WIDTH;
  22.     float4 v1 = tex2Dlod(BoneTex, NoMipMapPointClamp, uv).rgba;    
  23.     uv.x +=1.0f/BONE_WIDTH;
  24.     float4 v2 = tex2Dlod(BoneTex, NoMipMapPointClamp, uv).rgba;
  25.     
  26.     result._m00_m10_m20_m30 = v0;    
  27.     result._m01_m11_m21_m31 = v1;
  28.     result._m02_m12_m22_m32 = v2;
  29.     return result;
  30. }
  31.  
  32. float4x3 ReadBoneData(int i, float2 boneUv)
  33. {
  34.     float4 uv = float4(boneUv,0.0f,0.0f);
  35.     uv.x += i/BONE_NUM_WIDTH;
  36.  
  37.     float4x3 result;
  38.     float4 v0 = tex2Dlod(BoneTex, NoMipMapPointClamp, uv).rgba;    
  39.     uv.x +=1.0f/BONE_WIDTH;
  40.     float4 v1 = tex2Dlod(BoneTex, NoMipMapPointClamp, uv).rgba;    
  41.     uv.x +=1.0f/BONE_WIDTH;
  42.     float4 v2 = tex2Dlod(BoneTex, NoMipMapPointClamp, uv).rgba;
  43.  
  44.     result._m00_m10_m20_m30 = v0;    
  45.     result._m01_m11_m21_m31 = v1;
  46.     result._m02_m12_m22_m32 = v2;
  47.     return result;
  48. }
  49.  
  50. float4x3 DecompressSkinBoneMat( float4x3 vectors )
  51. {
  52.     return vectors;
  53. }
  54.  
  55. float4 DecompressBlendWeights( int4 val )
  56. {
  57.     float4 weights;
  58.     weights.xyzw = val.xyzw * 0.003921569f;
  59.     //weights.w = min(0.0f, abs(1.0f - weights[0] - weights[1] - weights[2]) );
  60.  
  61.     return weights;
  62. }
  63.  
  64. float4x3 SkinMatrixCalc(float3 blendWeight,int4 blendIndices)
  65. {
  66.     float4x3 SkinBoneTransform;
  67.  
  68.     float weight4 = 1.0f - blendWeight[0] - blendWeight[1] - blendWeight[2];
  69.  
  70.     SkinBoneTransform = blendWeight[0] * ReadBoneData(blendIndices[0]);
  71.     SkinBoneTransform += blendWeight[1] * ReadBoneData(blendIndices[1]);
  72.     SkinBoneTransform += blendWeight[2] * ReadBoneData(blendIndices[2]);
  73.     SkinBoneTransform += weight4 * ReadBoneData(blendIndices[3]);
  74.     return SkinBoneTransform;
  75. }
  76.  
  77. float4x3 SkinMatrixCalc(float3 blendWeight,int4 blendIndices, float2 boneUv)
  78. {
  79.     float4x3 SkinBoneTransform;
  80.  
  81.     float weight4 = 1.0f - blendWeight[0] - blendWeight[1] - blendWeight[2];
  82.  
  83.     SkinBoneTransform  = blendWeight[0] * ReadBoneData(blendIndices[0], boneUv);
  84.     SkinBoneTransform += blendWeight[1] * ReadBoneData(blendIndices[1], boneUv);
  85.     SkinBoneTransform += blendWeight[2] * ReadBoneData(blendIndices[2], boneUv);
  86.     SkinBoneTransform += weight4 * ReadBoneData(blendIndices[3], boneUv);
  87.     return SkinBoneTransform;
  88. }
  89.  
  90. float4x3 SkinMatrixCalcCompressed(int4 blendWeight,int4 blendIndices)
  91. {
  92.     float4x3 SkinBoneTransform;
  93.  
  94.     float4 weight = DecompressBlendWeights( blendWeight );
  95.     SkinBoneTransform = weight[0] * ReadBoneData(blendIndices[0]);
  96.     SkinBoneTransform += weight[1] * ReadBoneData(blendIndices[1]);
  97.     SkinBoneTransform += weight[2] * ReadBoneData(blendIndices[2]);
  98.     SkinBoneTransform += weight[3] * ReadBoneData(blendIndices[3]);
  99.     return SkinBoneTransform;
  100. }
  101.  
  102. float4x3 SkinMatrixCalcCompressed(int4 blendWeight,int4 blendIndices,float2 boneUv)
  103. {
  104.     float4x3 SkinBoneTransform;
  105.  
  106.     float4 weight = DecompressBlendWeights( blendWeight );
  107.     SkinBoneTransform  = weight[0] * ReadBoneData(blendIndices[0], boneUv);
  108.     SkinBoneTransform += weight[1] * ReadBoneData(blendIndices[1], boneUv);
  109.     SkinBoneTransform += weight[2] * ReadBoneData(blendIndices[2], boneUv);
  110.     SkinBoneTransform += weight[3] * ReadBoneData(blendIndices[3], boneUv);
  111.     return SkinBoneTransform;
  112. }
  113.  
  114.  
  115. float4x3 ReadPreBoneData(int i)
  116. {
  117.     float4 uv = float4(BoneTexUv.zw, 0, 0);
  118.     uv.x += i/BONE_NUM_WIDTH;
  119.     
  120.     float4x3 result;
  121.     float4 v0 = tex2Dlod(PreBoneTex, NoMipMapPointClamp, uv).rgba;    
  122.     uv.x +=1.0f/BONE_WIDTH;
  123.     float4 v1 = tex2Dlod(PreBoneTex, NoMipMapPointClamp, uv).rgba;    
  124.     uv.x +=1.0f/BONE_WIDTH;
  125.     float4 v2 = tex2Dlod(PreBoneTex, NoMipMapPointClamp, uv).rgba;
  126.     
  127.     result._m00_m10_m20_m30 = v0;    
  128.     result._m01_m11_m21_m31 = v1;
  129.     result._m02_m12_m22_m32 = v2;
  130.     return result;
  131. }
  132.  
  133. float4x3 ReadPreBoneData(int i, float2 boneUv)
  134. {
  135.     float4 uv = float4(boneUv,0.0f,0.0f);
  136.     uv.x += i/BONE_NUM_WIDTH;
  137.  
  138.     float4x3 result;
  139.     float4 v0 = tex2Dlod(PreBoneTex, NoMipMapPointClamp, uv).rgba;    
  140.     uv.x +=1.0f/BONE_WIDTH;
  141.     float4 v1 = tex2Dlod(PreBoneTex, NoMipMapPointClamp, uv).rgba;    
  142.     uv.x +=1.0f/BONE_WIDTH;
  143.     float4 v2 = tex2Dlod(PreBoneTex, NoMipMapPointClamp, uv).rgba;
  144.  
  145.     result._m00_m10_m20_m30 = v0;    
  146.     result._m01_m11_m21_m31 = v1;
  147.     result._m02_m12_m22_m32 = v2;
  148.     return result;
  149. }
  150.  
  151. float4x3 PreSkinMatrixCalc(float3 blendWeight,int4 blendIndices)
  152. {
  153.     float4x3 SkinBoneTransform;
  154.  
  155.     float weight4 = 1.0f - blendWeight[0] - blendWeight[1] - blendWeight[2];
  156.  
  157.     SkinBoneTransform = blendWeight[0] * ReadPreBoneData(blendIndices[0]);
  158.     SkinBoneTransform += blendWeight[1] * ReadPreBoneData(blendIndices[1]);
  159.     SkinBoneTransform += blendWeight[2] * ReadPreBoneData(blendIndices[2]);
  160.     SkinBoneTransform += weight4 * ReadPreBoneData(blendIndices[3]);
  161.     return SkinBoneTransform;
  162. }
  163.  
  164. float4x3 PreSkinMatrixCalc(float3 blendWeight,int4 blendIndices, float2 boneUv)
  165. {
  166.     float4x3 SkinBoneTransform;
  167.  
  168.     float weight4 = 1.0f - blendWeight[0] - blendWeight[1] - blendWeight[2];
  169.  
  170.     SkinBoneTransform  = blendWeight[0] * ReadPreBoneData(blendIndices[0], boneUv);
  171.     SkinBoneTransform += blendWeight[1] * ReadPreBoneData(blendIndices[1], boneUv);
  172.     SkinBoneTransform += blendWeight[2] * ReadPreBoneData(blendIndices[2], boneUv);
  173.     SkinBoneTransform += weight4 * ReadPreBoneData(blendIndices[3], boneUv);
  174.     return SkinBoneTransform;
  175. }
  176.  
  177. float4x3 PreSkinMatrixCalcCompressed(int4 blendWeight,int4 blendIndices)
  178. {
  179.     float4x3 SkinBoneTransform;
  180.  
  181.     float4 weight = DecompressBlendWeights( blendWeight );
  182.     SkinBoneTransform = weight[0] * ReadPreBoneData(blendIndices[0]);
  183.     SkinBoneTransform += weight[1] * ReadPreBoneData(blendIndices[1]);
  184.     SkinBoneTransform += weight[2] * ReadPreBoneData(blendIndices[2]);
  185.     SkinBoneTransform += weight[3] * ReadPreBoneData(blendIndices[3]);
  186.     return SkinBoneTransform;
  187. }
  188.  
  189. float4x3 PreSkinMatrixCalcCompressed(int4 blendWeight,int4 blendIndices,float2 boneUv)
  190. {
  191.     float4x3 SkinBoneTransform;
  192.  
  193.     float4 weight = DecompressBlendWeights( blendWeight );
  194.     SkinBoneTransform  = weight[0] * ReadPreBoneData(blendIndices[0], boneUv);
  195.     SkinBoneTransform += weight[1] * ReadPreBoneData(blendIndices[1], boneUv);
  196.     SkinBoneTransform += weight[2] * ReadPreBoneData(blendIndices[2], boneUv);
  197.     SkinBoneTransform += weight[3] * ReadPreBoneData(blendIndices[3], boneUv);
  198.     return SkinBoneTransform;
  199. }
  200.  
  201. #endif
  202. #endif//QSSHADER_QSSkinning_H