SkinnedVertexInput.bslinc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. Parameters =
  2. {
  3. StructBuffer boneMatrices : auto("BoneMatrices");
  4. };
  5. Technique
  6. #ifdef USE_BLEND_SHAPES
  7. : base("SkinnedMorphVertexInput") =
  8. #else
  9. : base("SkinnedVertexInput") =
  10. #endif
  11. {
  12. Language = "HLSL11";
  13. Pass =
  14. {
  15. Common =
  16. {
  17. struct VStoFS
  18. {
  19. float4 position : SV_Position;
  20. float2 uv0 : TEXCOORD0;
  21. float3 tangentToWorldZ : NORMAL; // Note: Half-precision could be used
  22. float4 tangentToWorldX : TANGENT; // Note: Half-precision could be used
  23. };
  24. };
  25. Vertex =
  26. {
  27. StructuredBuffer<float4> boneMatrices;
  28. struct VertexInput
  29. {
  30. float3 position : POSITION;
  31. float3 normal : NORMAL; // Note: Half-precision could be used
  32. float4 tangent : TANGENT; // Note: Half-precision could be used
  33. float2 uv0 : TEXCOORD0;
  34. uint4 blendIndices : BLENDINDICES;
  35. float4 blendWeights : BLENDWEIGHT;
  36. #ifdef USE_BLEND_SHAPES
  37. float3 deltaPosition : POSITION1;
  38. float4 deltaNormal : NORMAL1;
  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(VertexInput input)
  55. {
  56. float3x4 result = input.blendWeights.x * getBoneMatrix(input.blendIndices.x);
  57. result += input.blendWeights.y * getBoneMatrix(input.blendIndices.y);
  58. result += input.blendWeights.z * getBoneMatrix(input.blendIndices.z);
  59. result += input.blendWeights.w * getBoneMatrix(input.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);
  84. float tangentSign;
  85. float3x3 tangentToLocal = getSkinnedTangentToLocal(input, result.blendMatrix, tangentSign);
  86. float3x3 tangentToWorld = mul((float3x3)gMatWorldNoScale, tangentToLocal);
  87. result.worldNormal = float3(tangentToWorld._m02_m12_m22); // Normal basis vector
  88. result.worldTangent = float4(tangentToWorld._m00_m10_m20, tangentSign); // Tangent basis vector
  89. return result;
  90. }
  91. float4 getVertexWorldPosition(VertexInput input, VertexIntermediate intermediate)
  92. {
  93. #ifdef USE_BLEND_SHAPES
  94. float4 position = float4(input.position + input.deltaPosition, 1.0f);
  95. #else
  96. float4 position = float4(input.position, 1.0f);
  97. #endif
  98. position = float4(mul(intermediate.blendMatrix, position), 1.0f);
  99. return mul(gMatWorld, position);
  100. }
  101. void populateVertexOutput(VertexInput input, VertexIntermediate intermediate, inout VStoFS result)
  102. {
  103. result.uv0 = input.uv0;
  104. result.tangentToWorldZ = intermediate.worldNormal;
  105. result.tangentToWorldX = intermediate.worldTangent;
  106. }
  107. };
  108. };
  109. };
  110. Technique
  111. #ifdef USE_BLEND_SHAPES
  112. : base("SkinnedMorphVertexInput") =
  113. #else
  114. : base("SkinnedVertexInput") =
  115. #endif
  116. {
  117. Language = "GLSL";
  118. Pass =
  119. {
  120. Vertex =
  121. {
  122. layout(location = 0) in vec3 bs_position;
  123. layout(location = 1) in vec3 bs_normal;
  124. layout(location = 2) in vec4 bs_tangent;
  125. layout(location = 3) in vec2 bs_texcoord0;
  126. layout(location = 4) in ivec4 bs_blendindices;
  127. layout(location = 5) in vec4 bs_blendweights;
  128. #ifdef USE_BLEND_SHAPES
  129. layout(location = 6) in vec3 bs_position1;
  130. layout(location = 7) in vec4 bs_normal1;
  131. #endif
  132. layout(location = 0) out vec2 uv0;
  133. layout(location = 1) out vec3 tangentToWorldZ;
  134. layout(location = 2) out vec4 tangentToWorldX;
  135. layout(binding = 3) uniform samplerBuffer boneMatrices;
  136. struct VertexIntermediate
  137. {
  138. mat4x3 blendMatrix;
  139. vec3 worldNormal;
  140. vec4 worldTangent;
  141. };
  142. out gl_PerVertex
  143. {
  144. vec4 gl_Position;
  145. };
  146. void getBoneMatrix(int idx, out mat4x3 result)
  147. {
  148. mat3x4 temp;
  149. temp[0] = texelFetch(boneMatrices, idx * 3 + 0);
  150. temp[1] = texelFetch(boneMatrices, idx * 3 + 1);
  151. temp[2] = texelFetch(boneMatrices, idx * 3 + 2);
  152. result = transpose(temp);
  153. }
  154. void getBlendMatrix(out mat4x3 result)
  155. {
  156. mat4x3 boneMatrix;
  157. getBoneMatrix(bs_blendindices.x, boneMatrix);
  158. result = bs_blendweights.x * boneMatrix;
  159. getBoneMatrix(bs_blendindices.y, boneMatrix);
  160. result += bs_blendweights.y * boneMatrix;
  161. getBoneMatrix(bs_blendindices.z, boneMatrix);
  162. result += bs_blendweights.z * boneMatrix;
  163. getBoneMatrix(bs_blendindices.w, boneMatrix);
  164. result += bs_blendweights.w * boneMatrix;
  165. }
  166. void getSkinnedTangentToLocal(mat4x3 blendMatrix, out float tangentSign, out mat3x3 tangentToLocal)
  167. {
  168. tangentSign = bs_tangent.w * 2.0f - 1.0f;
  169. vec3 normal = bs_normal * 2.0f - 1.0f;
  170. vec3 tangent = bs_tangent.xyz * 2.0f - 1.0f;
  171. #ifdef USE_BLEND_SHAPES
  172. vec3 deltaNormal = (bs_normal1.xyz * 2.0f - 1.0f) * 2.0f;
  173. normal = normalize(normal + deltaNormal * bs_normal1.w);
  174. tangent = normalize(tangent - dot(tangent, normal) * normal);
  175. #endif
  176. normal = (blendMatrix * vec4(normal, 0.0f)).xyz;
  177. tangent = (blendMatrix * vec4(tangent, 0.0f)).xyz;
  178. vec3 bitangent = cross(normal, tangent) * tangentSign;
  179. tangentSign *= gWorldDeterminantSign;
  180. tangentToLocal[0] = tangent.xyz;
  181. tangentToLocal[1] = bitangent;
  182. tangentToLocal[2] = normal;
  183. }
  184. void getVertexIntermediate(out VertexIntermediate result)
  185. {
  186. getBlendMatrix(result.blendMatrix);
  187. float tangentSign;
  188. mat3 tangentToLocal;
  189. getSkinnedTangentToLocal(result.blendMatrix, tangentSign, tangentToLocal);
  190. mat3 tangentToWorld = mat3(gMatWorldNoScale) * tangentToLocal;
  191. result.worldNormal = tangentToWorld[2]; // Normal basis vector
  192. result.worldTangent = vec4(tangentToWorld[0].xyz, tangentSign); // Tangent basis vector
  193. }
  194. void getVertexWorldPosition(VertexIntermediate intermediate, out vec4 result)
  195. {
  196. #ifdef USE_BLEND_SHAPES
  197. vec4 position = vec4(bs_position + bs_position1, 1.0f);
  198. #else
  199. vec4 position = vec4(bs_position, 1.0f);
  200. #endif
  201. position = vec4(intermediate.blendMatrix * position, 1.0f);
  202. result = gMatWorld * position;
  203. }
  204. void populateVertexOutput(VertexIntermediate intermediate)
  205. {
  206. uv0 = bs_texcoord0;
  207. tangentToWorldZ = intermediate.worldNormal;
  208. tangentToWorldX = intermediate.worldTangent;
  209. }
  210. };
  211. };
  212. };