NormalVertexInput.bslinc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. Technique
  2. #ifdef USE_BLEND_SHAPES
  3. : base("MorphVertexInput") =
  4. #else
  5. : base("NormalVertexInput") =
  6. #endif
  7. {
  8. Language = "HLSL11";
  9. Pass =
  10. {
  11. Common =
  12. {
  13. struct VStoFS
  14. {
  15. float4 position : SV_Position;
  16. float2 uv0 : TEXCOORD0;
  17. float3 worldPosition : TEXCOORD1;
  18. float3 tangentToWorldZ : NORMAL; // Note: Half-precision could be used
  19. float4 tangentToWorldX : TANGENT; // Note: Half-precision could be used
  20. };
  21. };
  22. Vertex =
  23. {
  24. struct VertexInput
  25. {
  26. float3 position : POSITION;
  27. float3 normal : NORMAL; // Note: Half-precision could be used
  28. float4 tangent : TANGENT; // Note: Half-precision could be used
  29. float2 uv0 : TEXCOORD0;
  30. #ifdef USE_BLEND_SHAPES
  31. float3 deltaPosition : POSITION1;
  32. float4 deltaNormal : NORMAL1;
  33. #endif
  34. };
  35. struct VertexIntermediate
  36. {
  37. float3 worldNormal; // Note: Half-precision could be used
  38. float4 worldTangent; // Note: Half-precision could be used
  39. float tangentSign;
  40. };
  41. float3x3 getTangentToLocal(VertexInput input, out float tangentSign)
  42. {
  43. float3 normal = input.normal * 2.0f - 1.0f;
  44. float3 tangent = input.tangent.xyz * 2.0f - 1.0f;
  45. #ifdef USE_BLEND_SHAPES
  46. float3 deltaNormal = (input.deltaNormal.xyz * 2.0f - 1.0f) * 2.0f;
  47. normal = normalize(normal + deltaNormal * input.deltaNormal.w);
  48. tangent = normalize(tangent - dot(tangent, normal) * normal);
  49. #endif
  50. float3 bitangent = cross(normal, tangent) * input.tangent.w;
  51. tangentSign = input.tangent.w * gWorldDeterminantSign;
  52. // Note: Maybe it's better to store everything in row vector format?
  53. float3x3 result = float3x3(tangent, bitangent, normal);
  54. result = transpose(result);
  55. return result;
  56. }
  57. VertexIntermediate getVertexIntermediate(VertexInput input)
  58. {
  59. VertexIntermediate result;
  60. float tangentSign;
  61. float3x3 tangentToLocal = getTangentToLocal(input, tangentSign);
  62. float3x3 tangentToWorld = mul((float3x3)gMatWorldNoScale, tangentToLocal);
  63. result.worldNormal = float3(tangentToWorld._m02_m12_m22); // Normal basis vector
  64. result.worldTangent = float4(tangentToWorld._m00_m10_m20, tangentSign); // Tangent basis vector
  65. return result;
  66. }
  67. float4 getVertexWorldPosition(VertexInput input, VertexIntermediate intermediate)
  68. {
  69. #ifdef USE_BLEND_SHAPES
  70. float4 position = float4(input.position + input.deltaPosition, 1.0f);
  71. #else
  72. float4 position = float4(input.position, 1.0f);
  73. #endif
  74. return mul(gMatWorld, position);
  75. }
  76. void populateVertexOutput(VertexInput input, VertexIntermediate intermediate, inout VStoFS result)
  77. {
  78. result.uv0 = input.uv0;
  79. result.tangentToWorldZ = intermediate.worldNormal;
  80. result.tangentToWorldX = intermediate.worldTangent;
  81. }
  82. };
  83. };
  84. };
  85. Technique
  86. #ifdef USE_BLEND_SHAPES
  87. : base("MorphVertexInput") =
  88. #else
  89. : base("NormalVertexInput") =
  90. #endif
  91. {
  92. Language = "GLSL";
  93. Pass =
  94. {
  95. Vertex =
  96. {
  97. layout(location = 0) in vec3 bs_position;
  98. layout(location = 1) in vec3 bs_normal;
  99. layout(location = 2) in vec4 bs_tangent;
  100. layout(location = 3) in vec2 bs_texcoord0;
  101. #ifdef USE_BLEND_SHAPES
  102. layout(location = 4) in vec3 bs_position1;
  103. layout(location = 5) in vec4 bs_normal1;
  104. #endif
  105. layout(location = 0) out vec2 outUV0;
  106. layout(location = 1) out vec3 outWorldPosition;
  107. layout(location = 2) out vec3 outTangentToWorldZ;
  108. layout(location = 3) out vec4 outTangentToWorldX;
  109. out gl_PerVertex
  110. {
  111. vec4 gl_Position;
  112. };
  113. struct VertexIntermediate
  114. {
  115. vec3 worldNormal;
  116. vec4 worldTangent;
  117. };
  118. void getTangentToLocal(vec3 normal, vec3 tangent, float tangentSign, out mat3 tangentToLocal)
  119. {
  120. vec3 bitangent = cross(normal, tangent) * tangentSign;
  121. tangentToLocal[0] = tangent;
  122. tangentToLocal[1] = bitangent;
  123. tangentToLocal[2] = normal;
  124. }
  125. void getVertexIntermediate(out VertexIntermediate result)
  126. {
  127. vec3 normal = bs_normal * 2.0f - 1.0f;
  128. vec3 tangent = bs_tangent.xyz * 2.0f - 1.0f;
  129. #ifdef USE_BLEND_SHAPES
  130. vec3 deltaNormal = (bs_normal1.xyz * 2.0f - 1.0f) * 2.0f;
  131. normal = normalize(normal + deltaNormal * bs_normal1.w);
  132. tangent = normalize(tangent - dot(tangent, normal) * normal);
  133. #endif
  134. float tangentSign = bs_tangent.w;
  135. mat3 tangentToLocal;
  136. getTangentToLocal(normal, tangent, tangentSign, tangentToLocal);
  137. tangentSign *= gWorldDeterminantSign;
  138. mat3 tangentToWorld = mat3(gMatWorldNoScale) * tangentToLocal;
  139. result.worldNormal = tangentToWorld[2]; // Normal basis vector
  140. result.worldTangent = vec4(tangentToWorld[0].xyz, tangentSign); // Tangent basis vector
  141. }
  142. void getVertexWorldPosition(VertexIntermediate intermediate, out vec4 result)
  143. {
  144. #ifdef USE_BLEND_SHAPES
  145. vec4 position = vec4(bs_position + bs_position1, 1.0f);
  146. #else
  147. vec4 position = vec4(bs_position, 1.0f);
  148. #endif
  149. result = gMatWorld * position;
  150. }
  151. void populateVertexOutput(VertexIntermediate intermediate)
  152. {
  153. outUV0 = bs_texcoord0;
  154. outTangentToWorldZ = intermediate.worldNormal;
  155. outTangentToWorldX = intermediate.worldTangent;
  156. }
  157. };
  158. };
  159. };