SkinnedVertexInput.bslinc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #ifdef USE_BLEND_SHAPES
  2. mixin SkinnedMorphVertexInput
  3. #else
  4. mixin SkinnedVertexInput
  5. #endif
  6. {
  7. code
  8. {
  9. struct VStoFS
  10. {
  11. float4 position : SV_Position;
  12. float2 uv0 : TEXCOORD0;
  13. float3 worldPosition : TEXCOORD1;
  14. float3 tangentToWorldZ : NORMAL; // Note: Half-precision could be used
  15. float4 tangentToWorldX : TANGENT; // Note: Half-precision could be used
  16. };
  17. Buffer<float4> boneMatrices;
  18. struct VertexInput
  19. {
  20. float3 position : POSITION;
  21. float3 normal : NORMAL; // Note: Half-precision could be used
  22. float4 tangent : TANGENT; // Note: Half-precision could be used
  23. float2 uv0 : TEXCOORD0;
  24. uint4 blendIndices : BLENDINDICES;
  25. float4 blendWeights : BLENDWEIGHT;
  26. #ifdef USE_BLEND_SHAPES
  27. float3 deltaPosition : POSITION1;
  28. float4 deltaNormal : NORMAL1;
  29. #endif
  30. };
  31. // Vertex input containing only position data
  32. struct VertexInput_PO
  33. {
  34. float3 position : POSITION;
  35. uint4 blendIndices : BLENDINDICES;
  36. float4 blendWeights : BLENDWEIGHT;
  37. #ifdef USE_BLEND_SHAPES
  38. float3 deltaPosition : POSITION1;
  39. #endif
  40. };
  41. struct VertexIntermediate
  42. {
  43. float3x4 blendMatrix;
  44. float3 worldNormal; // Note: Half-precision could be used
  45. float4 worldTangent; // Note: Half-precision could be used
  46. };
  47. float3x4 getBoneMatrix(uint idx)
  48. {
  49. float4 row0 = boneMatrices[idx * 3 + 0];
  50. float4 row1 = boneMatrices[idx * 3 + 1];
  51. float4 row2 = boneMatrices[idx * 3 + 2];
  52. return float3x4(row0, row1, row2);
  53. }
  54. float3x4 getBlendMatrix(float4 blendWeights, uint4 blendIndices)
  55. {
  56. float3x4 result = blendWeights.x * getBoneMatrix(blendIndices.x);
  57. result += blendWeights.y * getBoneMatrix(blendIndices.y);
  58. result += blendWeights.z * getBoneMatrix(blendIndices.z);
  59. result += blendWeights.w * getBoneMatrix(blendIndices.w);
  60. return result;
  61. }
  62. float3x3 getSkinnedTangentToLocal(VertexInput input, float3x4 blendMatrix, out float tangentSign)
  63. {
  64. tangentSign = input.tangent.w * 2.0f - 1.0f;
  65. float3 normal = input.normal * 2.0f - 1.0f;
  66. float3 tangent = input.tangent.xyz * 2.0f - 1.0f;
  67. #ifdef USE_BLEND_SHAPES
  68. float3 deltaNormal = (input.deltaNormal.xyz * 2.0f - 1.0f) * 2.0f;
  69. normal = normalize(normal + deltaNormal * input.deltaNormal.w);
  70. tangent = normalize(tangent - dot(tangent, normal) * normal);
  71. #endif
  72. normal = mul(blendMatrix, float4(normal, 0.0f)).xyz;
  73. tangent = mul(blendMatrix, float4(tangent, 0.0f)).xyz;
  74. float3 bitangent = cross(normal, tangent) * tangentSign;
  75. tangentSign *= gWorldDeterminantSign;
  76. float3x3 result = float3x3(tangent, bitangent, normal);
  77. result = transpose(result);
  78. return result;
  79. }
  80. VertexIntermediate getVertexIntermediate(VertexInput input)
  81. {
  82. VertexIntermediate result;
  83. result.blendMatrix = getBlendMatrix(input.blendWeights, input.blendIndices);
  84. float tangentSign;
  85. float3x3 tangentToLocal = getSkinnedTangentToLocal(input, result.blendMatrix, tangentSign);
  86. float3x3 tangentToWorld = mul((float3x3)gMatWorldNoScale, tangentToLocal);
  87. // Note: Consider transposing these externally, for easier reads
  88. result.worldNormal = float3(tangentToWorld[0][2], tangentToWorld[1][2], tangentToWorld[2][2]); // Normal basis vector
  89. result.worldTangent = float4(tangentToWorld[0][0], tangentToWorld[1][0], tangentToWorld[2][0], tangentSign); // Tangent basis vector
  90. return result;
  91. }
  92. float4 getVertexWorldPosition(VertexInput input, VertexIntermediate intermediate)
  93. {
  94. #ifdef USE_BLEND_SHAPES
  95. float4 position = float4(input.position + input.deltaPosition, 1.0f);
  96. #else
  97. float4 position = float4(input.position, 1.0f);
  98. #endif
  99. position = float4(mul(intermediate.blendMatrix, position), 1.0f);
  100. return mul(gMatWorld, position);
  101. }
  102. float4 getVertexWorldPosition(VertexInput_PO input)
  103. {
  104. #ifdef USE_BLEND_SHAPES
  105. float4 position = float4(input.position + input.deltaPosition, 1.0f);
  106. #else
  107. float4 position = float4(input.position, 1.0f);
  108. #endif
  109. float3x4 blendMatrix = getBlendMatrix(input.blendWeights, input.blendIndices);
  110. position = float4(mul(blendMatrix, position), 1.0f);
  111. return mul(gMatWorld, position);
  112. }
  113. void populateVertexOutput(VertexInput input, VertexIntermediate intermediate, inout VStoFS result)
  114. {
  115. result.uv0 = input.uv0;
  116. result.tangentToWorldZ = intermediate.worldNormal;
  117. result.tangentToWorldX = intermediate.worldTangent;
  118. }
  119. };
  120. };