VertexInput.bslinc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. mixin VertexInput
  2. {
  3. variations
  4. {
  5. SKINNED = { false, true };
  6. MORPH = { false, true };
  7. };
  8. code
  9. {
  10. struct VStoFS
  11. {
  12. float4 position : SV_Position;
  13. float2 uv0 : TEXCOORD0;
  14. float3 worldPosition : TEXCOORD1;
  15. float3 tangentToWorldZ : NORMAL; // Note: Half-precision could be used
  16. float4 tangentToWorldX : TANGENT; // Note: Half-precision could be used
  17. };
  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. #if SKINNED
  25. uint4 blendIndices : BLENDINDICES;
  26. float4 blendWeights : BLENDWEIGHT;
  27. #endif
  28. #if MORPH
  29. float3 deltaPosition : POSITION1;
  30. float4 deltaNormal : NORMAL1;
  31. #endif
  32. };
  33. // Vertex input containing only position data
  34. struct VertexInput_PO
  35. {
  36. float3 position : POSITION;
  37. #if SKINNED
  38. uint4 blendIndices : BLENDINDICES;
  39. float4 blendWeights : BLENDWEIGHT;
  40. #endif
  41. #if MORPH
  42. float3 deltaPosition : POSITION1;
  43. #endif
  44. };
  45. struct VertexIntermediate
  46. {
  47. #if SKINNED
  48. float3x4 blendMatrix;
  49. #endif
  50. float3 worldNormal; // Note: Half-precision could be used
  51. float4 worldTangent; // Note: Half-precision could be used
  52. };
  53. #if SKINNED
  54. Buffer<float4> boneMatrices;
  55. float3x4 getBoneMatrix(uint idx)
  56. {
  57. float4 row0 = boneMatrices[idx * 3 + 0];
  58. float4 row1 = boneMatrices[idx * 3 + 1];
  59. float4 row2 = boneMatrices[idx * 3 + 2];
  60. return float3x4(row0, row1, row2);
  61. }
  62. float3x4 getBlendMatrix(float4 blendWeights, uint4 blendIndices)
  63. {
  64. float3x4 result = blendWeights.x * getBoneMatrix(blendIndices.x);
  65. result += blendWeights.y * getBoneMatrix(blendIndices.y);
  66. result += blendWeights.z * getBoneMatrix(blendIndices.z);
  67. result += blendWeights.w * getBoneMatrix(blendIndices.w);
  68. return result;
  69. }
  70. #endif
  71. float3x3 getTangentToLocal(VertexInput input, out float tangentSign
  72. #if SKINNED
  73. , float3x4 blendMatrix
  74. #endif
  75. )
  76. {
  77. float3 normal = input.normal * 2.0f - 1.0f;
  78. float3 tangent = input.tangent.xyz * 2.0f - 1.0f;
  79. #if MORPH
  80. float3 deltaNormal = (input.deltaNormal.xyz * 2.0f - 1.0f) * 2.0f;
  81. normal = normalize(normal + deltaNormal * input.deltaNormal.w);
  82. tangent = normalize(tangent - dot(tangent, normal) * normal);
  83. #endif
  84. #if SKINNED
  85. normal = mul(blendMatrix, float4(normal, 0.0f)).xyz;
  86. tangent = mul(blendMatrix, float4(tangent, 0.0f)).xyz;
  87. #endif
  88. float3 bitangent = cross(normal, tangent) * input.tangent.w;
  89. tangentSign = input.tangent.w * gWorldDeterminantSign;
  90. // Note: Maybe it's better to store everything in row vector format?
  91. float3x3 result = float3x3(tangent, bitangent, normal);
  92. result = transpose(result);
  93. return result;
  94. }
  95. VertexIntermediate getVertexIntermediate(VertexInput input)
  96. {
  97. VertexIntermediate result;
  98. float tangentSign;
  99. #if SKINNED
  100. result.blendMatrix = getBlendMatrix(input.blendWeights, input.blendIndices);
  101. float3x3 tangentToLocal = getTangentToLocal(input, tangentSign, result.blendMatrix);
  102. #else
  103. float3x3 tangentToLocal = getTangentToLocal(input, tangentSign);
  104. #endif
  105. float3x3 tangentToWorld = mul((float3x3)gMatWorldNoScale, tangentToLocal);
  106. // Note: Consider transposing these externally, for easier reads
  107. result.worldNormal = float3(tangentToWorld[0][2], tangentToWorld[1][2], tangentToWorld[2][2]); // Normal basis vector
  108. result.worldTangent = float4(tangentToWorld[0][0], tangentToWorld[1][0], tangentToWorld[2][0], tangentSign); // Tangent basis vector
  109. return result;
  110. }
  111. float4 getVertexWorldPosition(VertexInput input, VertexIntermediate intermediate)
  112. {
  113. #if MORPH
  114. float4 position = float4(input.position + input.deltaPosition, 1.0f);
  115. #else
  116. float4 position = float4(input.position, 1.0f);
  117. #endif
  118. #if SKINNED
  119. position = float4(mul(intermediate.blendMatrix, position), 1.0f);
  120. #endif
  121. return mul(gMatWorld, position);
  122. }
  123. float4 getVertexWorldPosition(VertexInput_PO input)
  124. {
  125. #if MORPH
  126. float4 position = float4(input.position + input.deltaPosition, 1.0f);
  127. #else
  128. float4 position = float4(input.position, 1.0f);
  129. #endif
  130. #if SKINNED
  131. float3x4 blendMatrix = getBlendMatrix(input.blendWeights, input.blendIndices);
  132. position = float4(mul(blendMatrix, position), 1.0f);
  133. #endif
  134. return mul(gMatWorld, position);
  135. }
  136. void populateVertexOutput(VertexInput input, VertexIntermediate intermediate, inout VStoFS result)
  137. {
  138. result.uv0 = input.uv0;
  139. result.tangentToWorldZ = intermediate.worldNormal;
  140. result.tangentToWorldX = intermediate.worldTangent;
  141. }
  142. };
  143. };