SkinnedVertexInput.bslinc 7.4 KB

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