VertexCommon.bslinc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. mixin VertexCommon
  2. {
  3. code
  4. {
  5. struct VStoFS
  6. {
  7. float4 position : SV_Position;
  8. float2 uv0 : TEXCOORD0;
  9. #if LIGHTING_DATA
  10. float3 worldPosition : TEXCOORD1;
  11. float3 tangentToWorldZ : NORMAL; // Note: Half-precision could be used
  12. float4 tangentToWorldX : TANGENT; // Note: Half-precision could be used
  13. #endif
  14. #if CLIP_POS
  15. float4 clipPos : TEXCOORD2;
  16. #endif
  17. #if PREV_CLIP_POS
  18. float4 prevClipPos : TEXCOORD3;
  19. #endif
  20. };
  21. struct VertexInput
  22. {
  23. #if POSITION_2D
  24. float2 position : POSITION;
  25. #else
  26. float3 position : POSITION;
  27. #endif
  28. float2 uv0 : TEXCOORD0;
  29. #if LIGHTING_DATA
  30. float3 normal : NORMAL; // Note: Half-precision could be used
  31. float4 tangent : TANGENT; // Note: Half-precision could be used
  32. #endif
  33. #if SKINNED
  34. uint4 blendIndices : BLENDINDICES;
  35. float4 blendWeights : BLENDWEIGHT;
  36. #endif
  37. #if MORPH
  38. float3 deltaPosition : POSITION1;
  39. float4 deltaNormal : NORMAL1;
  40. #endif
  41. };
  42. // Vertex input containing only position data
  43. struct VertexInput_PO
  44. {
  45. #if POSITION_2D
  46. float2 position : POSITION;
  47. #else
  48. float3 position : POSITION;
  49. #endif
  50. #if SKINNED
  51. uint4 blendIndices : BLENDINDICES;
  52. float4 blendWeights : BLENDWEIGHT;
  53. #endif
  54. #if MORPH
  55. float3 deltaPosition : POSITION1;
  56. #endif
  57. };
  58. struct VertexIntermediate
  59. {
  60. #if SKINNED
  61. float3x4 blendMatrix;
  62. #if PREV_CLIP_POS
  63. float3x4 prevBlendMatrix;
  64. #endif
  65. #endif
  66. #if LIGHTING_DATA
  67. float3 worldNormal; // Note: Half-precision could be used
  68. float4 worldTangent; // Note: Half-precision could be used
  69. #endif
  70. };
  71. #if SKINNED
  72. Buffer<float4> boneMatrices;
  73. Buffer<float4> prevBoneMatrices;
  74. float3x4 getBoneMatrix(uint idx)
  75. {
  76. float4 row0 = boneMatrices[idx * 3 + 0];
  77. float4 row1 = boneMatrices[idx * 3 + 1];
  78. float4 row2 = boneMatrices[idx * 3 + 2];
  79. return float3x4(row0, row1, row2);
  80. }
  81. float3x4 getPrevBoneMatrix(uint idx)
  82. {
  83. float4 row0 = prevBoneMatrices[idx * 3 + 0];
  84. float4 row1 = prevBoneMatrices[idx * 3 + 1];
  85. float4 row2 = prevBoneMatrices[idx * 3 + 2];
  86. return float3x4(row0, row1, row2);
  87. }
  88. float3x4 getBlendMatrix(float4 blendWeights, uint4 blendIndices)
  89. {
  90. float3x4 result = blendWeights.x * getBoneMatrix(blendIndices.x);
  91. result += blendWeights.y * getBoneMatrix(blendIndices.y);
  92. result += blendWeights.z * getBoneMatrix(blendIndices.z);
  93. result += blendWeights.w * getBoneMatrix(blendIndices.w);
  94. return result;
  95. }
  96. float3x4 getPrevBlendMatrix(float4 blendWeights, uint4 blendIndices)
  97. {
  98. float3x4 result = blendWeights.x * getPrevBoneMatrix(blendIndices.x);
  99. result += blendWeights.y * getPrevBoneMatrix(blendIndices.y);
  100. result += blendWeights.z * getPrevBoneMatrix(blendIndices.z);
  101. result += blendWeights.w * getPrevBoneMatrix(blendIndices.w);
  102. return result;
  103. }
  104. #endif
  105. #if LIGHTING_DATA
  106. float3x3 getTangentToLocal(VertexInput input, out float tangentSign
  107. #if SKINNED
  108. , float3x4 blendMatrix
  109. #endif
  110. )
  111. {
  112. float3 normal = input.normal * 2.0f - 1.0f;
  113. float3 tangent = input.tangent.xyz * 2.0f - 1.0f;
  114. #if MORPH
  115. float3 deltaNormal = (input.deltaNormal.xyz * 2.0f - 1.0f) * 2.0f;
  116. normal = normalize(normal + deltaNormal * input.deltaNormal.w);
  117. tangent = normalize(tangent - dot(tangent, normal) * normal);
  118. #endif
  119. #if SKINNED
  120. normal = mul(blendMatrix, float4(normal, 0.0f)).xyz;
  121. tangent = mul(blendMatrix, float4(tangent, 0.0f)).xyz;
  122. #endif
  123. tangentSign = input.tangent.w < 0.5f ? -1.0f : 1.0f;
  124. float3 bitangent = cross(normal, tangent) * tangentSign;
  125. tangentSign *= gWorldDeterminantSign;
  126. // Note: Maybe it's better to store everything in row vector format?
  127. float3x3 result = float3x3(tangent, bitangent, normal);
  128. result = transpose(result);
  129. return result;
  130. }
  131. float3 calcWorldNormal(VStoFS input, float3 surfaceNormal)
  132. {
  133. float3 tangentToWorldX = input.tangentToWorldX.xyz;
  134. float3 tangentToWorldZ = input.tangentToWorldZ;
  135. float3 tangentToWorldY = cross(tangentToWorldZ, tangentToWorldX) * input.tangentToWorldX.w;
  136. float3x3 tangentToWorld = float3x3(tangentToWorldX, tangentToWorldY, tangentToWorldZ);
  137. // Multiplication order flipped because we stored basis vectors as rows
  138. return normalize(mul(surfaceNormal, tangentToWorld));
  139. }
  140. #endif
  141. VertexIntermediate getVertexIntermediate(VertexInput input)
  142. {
  143. VertexIntermediate result;
  144. float tangentSign;
  145. #if SKINNED
  146. result.blendMatrix = getBlendMatrix(input.blendWeights, input.blendIndices);
  147. float3x3 tangentToLocal = getTangentToLocal(input, tangentSign, result.blendMatrix);
  148. #if PREV_CLIP_POS
  149. result.prevBlendMatrix = getPrevBlendMatrix(input.blendWeights, input.blendIndices);
  150. #endif
  151. #else
  152. float3x3 tangentToLocal = getTangentToLocal(input, tangentSign);
  153. #endif
  154. #if LIGHTING_DATA
  155. float3x3 tangentToWorld = mul((float3x3)gMatWorldNoScale, tangentToLocal);
  156. // Note: Consider transposing these externally, for easier reads
  157. result.worldNormal = float3(tangentToWorld[0][2], tangentToWorld[1][2], tangentToWorld[2][2]); // Normal basis vector
  158. result.worldTangent = float4(tangentToWorld[0][0], tangentToWorld[1][0], tangentToWorld[2][0], tangentSign); // Tangent basis vector
  159. #endif
  160. return result;
  161. }
  162. void populateVertexOutput(VertexInput input, VertexIntermediate intermediate, inout VStoFS result)
  163. {
  164. result.uv0 = input.uv0;
  165. #if LIGHTING_DATA
  166. result.tangentToWorldZ = intermediate.worldNormal;
  167. result.tangentToWorldX = intermediate.worldTangent;
  168. #endif
  169. #if CLIP_POS
  170. result.clipPos = result.position;
  171. #endif
  172. }
  173. };
  174. };