NormalVertexInput.bslinc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. Technique
  2. #ifdef USE_BLEND_SHAPES
  3. : base("MorphVertexInput") =
  4. #else
  5. : base("NormalVertexInput") =
  6. #endif
  7. {
  8. Pass =
  9. {
  10. Common =
  11. {
  12. struct VStoFS
  13. {
  14. float4 position : SV_Position;
  15. float2 uv0 : TEXCOORD0;
  16. float3 worldPosition : TEXCOORD1;
  17. float3 tangentToWorldZ : NORMAL; // Note: Half-precision could be used
  18. float4 tangentToWorldX : TANGENT; // Note: Half-precision could be used
  19. };
  20. };
  21. Vertex =
  22. {
  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. #ifdef USE_BLEND_SHAPES
  30. float3 deltaPosition : POSITION1;
  31. float4 deltaNormal : NORMAL1;
  32. #endif
  33. };
  34. // Vertex input containing only position data
  35. struct VertexInput_PO
  36. {
  37. float3 position : POSITION;
  38. #ifdef USE_BLEND_SHAPES
  39. float3 deltaPosition : POSITION1;
  40. #endif
  41. };
  42. struct VertexIntermediate
  43. {
  44. float3 worldNormal; // Note: Half-precision could be used
  45. float4 worldTangent; // Note: Half-precision could be used
  46. float tangentSign;
  47. };
  48. float3x3 getTangentToLocal(VertexInput input, out float tangentSign)
  49. {
  50. float3 normal = input.normal * 2.0f - 1.0f;
  51. float3 tangent = input.tangent.xyz * 2.0f - 1.0f;
  52. #ifdef USE_BLEND_SHAPES
  53. float3 deltaNormal = (input.deltaNormal.xyz * 2.0f - 1.0f) * 2.0f;
  54. normal = normalize(normal + deltaNormal * input.deltaNormal.w);
  55. tangent = normalize(tangent - dot(tangent, normal) * normal);
  56. #endif
  57. float3 bitangent = cross(normal, tangent) * input.tangent.w;
  58. tangentSign = input.tangent.w * gWorldDeterminantSign;
  59. // Note: Maybe it's better to store everything in row vector format?
  60. float3x3 result = float3x3(tangent, bitangent, normal);
  61. result = transpose(result);
  62. return result;
  63. }
  64. VertexIntermediate getVertexIntermediate(VertexInput input)
  65. {
  66. VertexIntermediate result;
  67. float tangentSign;
  68. float3x3 tangentToLocal = getTangentToLocal(input, tangentSign);
  69. float3x3 tangentToWorld = mul((float3x3)gMatWorldNoScale, tangentToLocal);
  70. // Note: Consider transposing these externally, for easier reads
  71. result.worldNormal = float3(tangentToWorld[0][2], tangentToWorld[1][2], tangentToWorld[2][2]); // Normal basis vector
  72. result.worldTangent = float4(tangentToWorld[0][0], tangentToWorld[1][0], tangentToWorld[2][0], tangentSign); // Tangent basis vector
  73. return result;
  74. }
  75. float4 getVertexWorldPosition(VertexInput input, VertexIntermediate intermediate)
  76. {
  77. #ifdef USE_BLEND_SHAPES
  78. float4 position = float4(input.position + input.deltaPosition, 1.0f);
  79. #else
  80. float4 position = float4(input.position, 1.0f);
  81. #endif
  82. return mul(gMatWorld, position);
  83. }
  84. float4 getVertexWorldPosition(VertexInput_PO input)
  85. {
  86. #ifdef USE_BLEND_SHAPES
  87. float4 position = float4(input.position + input.deltaPosition, 1.0f);
  88. #else
  89. float4 position = float4(input.position, 1.0f);
  90. #endif
  91. return mul(gMatWorld, position);
  92. }
  93. void populateVertexOutput(VertexInput input, VertexIntermediate intermediate, inout VStoFS result)
  94. {
  95. result.uv0 = input.uv0;
  96. result.tangentToWorldZ = intermediate.worldNormal;
  97. result.tangentToWorldX = intermediate.worldTangent;
  98. }
  99. };
  100. };
  101. };