Deferred.hlsl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "Uniforms.hlsl"
  2. #include "Samplers.hlsl"
  3. #include "Transform.hlsl"
  4. #include "Lighting.hlsl"
  5. #include "Fog.hlsl"
  6. void VS(float4 iPos : POSITION,
  7. float3 iNormal : NORMAL,
  8. #ifdef NORMALMAP
  9. float4 iTangent : TANGENT0,
  10. #endif
  11. float2 iTexCoord : TEXCOORD0,
  12. #ifdef VERTEXCOLOR
  13. float4 iColor : COLOR0,
  14. #endif
  15. #ifdef SKINNED
  16. float4 iBlendWeights : BLENDWEIGHT,
  17. int4 iBlendIndices : BLENDINDICES,
  18. #endif
  19. #ifdef INSTANCED
  20. float4x3 iModelInstance : TEXCOORD2,
  21. #endif
  22. out float2 oTexCoord : TEXCOORD0,
  23. out float4 oVertexLighting : TEXCOORD1,
  24. out float3 oNormal : TEXCOORD2,
  25. #ifdef NORMALMAP
  26. out float3 oTangent : TEXCOORD3,
  27. out float3 oBitangent : TEXCOORD4,
  28. #endif
  29. #ifdef VERTEXCOLOR
  30. out float4 oColor : COLOR0,
  31. #endif
  32. out float4 oPos : POSITION)
  33. {
  34. float4x3 modelMatrix = iModelMatrix;
  35. float3 worldPos = GetWorldPos(modelMatrix);
  36. oPos = GetClipPos(worldPos);
  37. oTexCoord = GetTexCoord(iTexCoord);
  38. oVertexLighting = float4(GetAmbient(GetZonePos(worldPos)), GetDepth(oPos));
  39. #ifdef NUMVERTEXLIGHTS
  40. float3 normal = GetWorldNormal(modelMatrix);
  41. for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
  42. oVertexLighting.rgb += GetVertexLight(i, worldPos, normal) * cVertexLights[i * 3].rgb;
  43. #endif
  44. oNormal = GetWorldNormal(modelMatrix);
  45. #ifdef NORMALMAP
  46. oTangent = GetWorldTangent(modelMatrix);
  47. oBitangent = cross(oTangent, oNormal) * iTangent.w;
  48. #endif
  49. #ifdef VERTEXCOLOR
  50. oColor = iColor;
  51. #endif
  52. }
  53. void PS(
  54. float2 iTexCoord : TEXCOORD0,
  55. float4 iVertexLighting : TEXCOORD1,
  56. float3 iNormal : TEXCOORD2,
  57. #ifdef NORMALMAP
  58. float3 iTangent : TEXCOORD3,
  59. float3 iBitangent : TEXCOORD4,
  60. #endif
  61. #ifdef VERTEXCOLOR
  62. float4 iColor : COLOR0,
  63. #endif
  64. out float4 oAlbedo : COLOR1,
  65. out float4 oNormal : COLOR2,
  66. #ifndef HWDEPTH
  67. out float4 oDepth : COLOR3,
  68. #endif
  69. out float4 oColor : COLOR0)
  70. {
  71. #ifdef DIFFMAP
  72. float4 diffInput = tex2D(sDiffMap, iTexCoord);
  73. #ifdef ALPHAMASK
  74. if (diffInput.a < 0.5)
  75. discard;
  76. #endif
  77. float3 diffColor = cMatDiffColor.rgb * diffInput.rgb;
  78. #else
  79. float3 diffColor = cMatDiffColor.rgb;
  80. #endif
  81. #ifdef VERTEXCOLOR
  82. diffColor *= iColor.rgb;
  83. #endif
  84. #ifdef NORMALMAP
  85. float3x3 tbn = float3x3(iTangent, iBitangent, iNormal);
  86. float3 normal = mul(DecodeNormal(tex2D(sNormalMap, iTexCoord)), tbn);
  87. #else
  88. float3 normal = iNormal;
  89. #endif
  90. // If using SM2, light volume shader may not have instructions left to normalize the normal. Therefore do it here
  91. #if !defined(SM3)
  92. normal = normalize(normal);
  93. #endif
  94. #ifdef SPECMAP
  95. float specIntensity = cMatSpecColor.g * tex2D(sSpecMap, iTexCoord).g;
  96. #else
  97. float specIntensity = cMatSpecColor.g;
  98. #endif
  99. float specPower = cMatSpecColor.a / 255.0;
  100. oColor = float4(GetFog(iVertexLighting.rgb * diffColor, iVertexLighting.a), 1.0);
  101. oAlbedo = GetFogFactor(iVertexLighting.a) * float4(diffColor, specIntensity);
  102. oNormal = float4(normal * 0.5 + 0.5, specPower);
  103. #ifndef HWDEPTH
  104. oDepth = iVertexLighting.a;
  105. #endif
  106. }