SkinnedVertexInput.bslinc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. Common =
  121. {
  122. varying vec2 uv0;
  123. varying vec3 tangentToWorldZ;
  124. varying vec4 tangentToWorldX;
  125. };
  126. Vertex =
  127. {
  128. in vec3 bs_position;
  129. in vec3 bs_normal;
  130. in vec4 bs_tangent;
  131. in vec2 bs_texcoord0;
  132. in uvec4 bs_blendindices;
  133. in vec4 bs_blendweights;
  134. #ifdef USE_BLEND_SHAPES
  135. in vec3 bs_position1;
  136. in vec4 bs_normal1;
  137. #endif
  138. uniform samplerBuffer boneMatrices;
  139. struct VertexIntermediate
  140. {
  141. mat4x3 blendMatrix;
  142. vec3 worldNormal;
  143. vec4 worldTangent;
  144. };
  145. out gl_PerVertex
  146. {
  147. vec4 gl_Position;
  148. };
  149. void getBoneMatrix(uint idx, out mat4x3 result)
  150. {
  151. mat3x4 temp;
  152. temp[0] = texelFetch(boneMatrices, idx * 3 + 0);
  153. temp[1] = texelFetch(boneMatrices, idx * 3 + 1);
  154. temp[2] = texelFetch(boneMatrices, idx * 3 + 2);
  155. result = transpose(temp);
  156. }
  157. void getBlendMatrix(out mat4x3 result)
  158. {
  159. mat4x3 boneMatrix;
  160. getBoneMatrix(bs_blendindices.x, out boneMatrix);
  161. result = bs_blendweights.x * boneMatrix;
  162. getBoneMatrix(bs_blendindices.y, out boneMatrix);
  163. result += bs_blendweights.y * boneMatrix;
  164. getBoneMatrix(bs_blendindices.z, out boneMatrix);
  165. result += bs_blendweights.z * boneMatrix;
  166. getBoneMatrix(bs_blendindices.w, out boneMatrix);
  167. result += bs_blendweights.w * boneMatrix;
  168. }
  169. void getSkinnedTangentToLocal(mat4x3 blendMatrix, out float tangentSign, out mat3x3 tangentToLocal)
  170. {
  171. tangentSign = bs_tangent.w * 2.0f - 1.0f;
  172. vec3 normal = bs_normal * 2.0f - 1.0f;
  173. vec3 tangent = bs_tangent.xyz * 2.0f - 1.0f;
  174. #ifdef USE_BLEND_SHAPES
  175. vec3 deltaNormal = (bs_normal1.xyz * 2.0f - 1.0f) * 2.0f;
  176. normal = normalize(normal + deltaNormal * bs_normal1.w);
  177. tangent = normalize(tangent - dot(tangent, normal) * normal);
  178. #endif
  179. normal = (blendMatrix * vec4(normal, 0.0f)).xyz;
  180. tangent = (blendMatrix * vec4(tangent, 0.0f)).xyz;
  181. vec3 bitangent = cross(normal, tangent) * tangentSign;
  182. tangentSign *= gWorldDeterminantSign;
  183. tangentToLocal[0] = tangent.xyz;
  184. tangentToLocal[1] = bitangent;
  185. tangentToLocal[2] = normal;
  186. }
  187. void getVertexIntermediate(out VertexIntermediate result)
  188. {
  189. getBlendMatrix(result.blendMatrix);
  190. float tangentSign;
  191. mat3 tangentToLocal;
  192. getSkinnedTangentToLocal(tangentSign, tangentToLocal);
  193. mat3 tangentToWorld = mat3(gMatWorldNoScale) * tangentToLocal;
  194. result.worldNormal = tangentToWorld[2]; // Normal basis vector
  195. result.worldTangent = vec4(tangentToWorld[0].xyz, tangentSign); // Tangent basis vector
  196. }
  197. void getVertexWorldPosition(VertexIntermediate intermediate, out vec4 result)
  198. {
  199. #ifdef USE_BLEND_SHAPES
  200. vec4 position = vec4(bs_position + bs_position1, 1.0f);
  201. #else
  202. vec4 position = vec4(bs_position, 1.0f);
  203. #endif
  204. position = vec4(intermediate.blendMatrix * position, 1.0f);
  205. result = gMatWorld * position;
  206. }
  207. void populateVertexOutput(VertexIntermediate intermediate)
  208. {
  209. uv0 = bs_texcoord0;
  210. tangentToWorldZ = intermediate.worldNormal;
  211. tangentToWorldX = intermediate.worldTangent;
  212. }
  213. };
  214. };
  215. };