SkinnedVertexInput.bslinc 6.7 KB

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