ShadowDepthBase.bslinc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "$ENGINE$\PerObjectData.bslinc"
  2. #include "$ENGINE$\SkinnedVertexInput.bslinc"
  3. #include "$ENGINE$\NormalVertexInput.bslinc"
  4. #define USE_BLEND_SHAPES
  5. #include "$ENGINE$\SkinnedVertexInput.bslinc"
  6. #include "$ENGINE$\NormalVertexInput.bslinc"
  7. #undef USE_BLEND_SHAPES
  8. mixin ShadowDepthBase
  9. {
  10. code
  11. {
  12. struct ShadowVStoFS
  13. {
  14. float4 position : SV_Position;
  15. #ifdef USES_GS
  16. float4 worldPos : TEXCOORD0;
  17. #else
  18. #ifndef LINEAR_DEPTH_RANGE
  19. float shadowPos : TEXCOORD0;
  20. #endif
  21. #endif
  22. };
  23. [internal]
  24. cbuffer ShadowParams
  25. {
  26. float4x4 gMatViewProj;
  27. float2 gNDCZToDeviceZ;
  28. float gDepthBias;
  29. float gInvDepthRange;
  30. };
  31. /** Converts Z value from device range ([0, 1]) to NDC space. */
  32. float DeviceZToNDCZ(float deviceZ)
  33. {
  34. return deviceZ / gNDCZToDeviceZ.x - gNDCZToDeviceZ.y;
  35. }
  36. /** Converts Z value from NDC space to device Z value in range [0, 1]. */
  37. float NDCZToDeviceZ(float ndcZ)
  38. {
  39. return (ndcZ + gNDCZToDeviceZ.y) * gNDCZToDeviceZ.x;
  40. }
  41. void linearizeDepth(inout float4 clipPos)
  42. {
  43. #ifdef CLAMP_TO_NEAR_PLANE
  44. float ndcZ = clipPos.z / clipPos.w;
  45. float deviceZ = NDCZToDeviceZ(ndcZ);
  46. // Clamp to near plane if behind it
  47. if (deviceZ < 0)
  48. {
  49. clipPos.z = DeviceZToNDCZ(0);
  50. clipPos.w = 1.0f;
  51. }
  52. #endif
  53. // Output linear depth
  54. #ifdef LINEAR_DEPTH_RANGE
  55. float linearDepth = -clipPos.z * gInvDepthRange + gDepthBias;
  56. clipPos.z = linearDepth * clipPos.w;
  57. #endif
  58. }
  59. ShadowVStoFS vsmain(VertexInput_PO input)
  60. {
  61. ShadowVStoFS output;
  62. float4 worldPosition = getVertexWorldPosition(input);
  63. #ifdef USES_GS
  64. output.worldPos = worldPosition;
  65. output.position = worldPosition;
  66. #else
  67. float4 clipPos = mul(gMatViewProj, worldPosition);
  68. linearizeDepth(clipPos);
  69. #ifndef LINEAR_DEPTH_RANGE
  70. output.shadowPos = clipPos.z;
  71. #endif
  72. output.position = clipPos;
  73. #endif
  74. return output;
  75. }
  76. };
  77. };
  78. technique ShadowDepth
  79. {
  80. mixin PerObjectData;
  81. mixin NormalVertexInput;
  82. mixin ShadowDepthBase;
  83. mixin ShadowDepth;
  84. };
  85. technique ShadowDepthSkinned
  86. {
  87. mixin PerObjectData;
  88. mixin SkinnedVertexInput;
  89. mixin ShadowDepthBase;
  90. mixin ShadowDepth;
  91. tags = { "Skinned" };
  92. };
  93. technique ShadowDepthMorph
  94. {
  95. mixin PerObjectData;
  96. mixin MorphVertexInput;
  97. mixin ShadowDepthBase;
  98. mixin ShadowDepth;
  99. tags = { "Morph" };
  100. };
  101. technique ShadowDepthSkinnedMorph
  102. {
  103. mixin PerObjectData;
  104. mixin SkinnedMorphVertexInput;
  105. mixin ShadowDepthBase;
  106. mixin ShadowDepth;
  107. tags = { "SkinnedMorph" };
  108. };