Material.hlsl 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "../Common.hlsl"
  2. void VS(float4 iPos : POSITION,
  3. #ifdef SKINNED
  4. float4 iBlendWeights : BLENDWEIGHT,
  5. int4 iBlendIndices : BLENDINDICES,
  6. #endif
  7. #ifdef INSTANCED
  8. float4x3 iModelInstance : TEXCOORD2,
  9. #endif
  10. #ifdef VERTEXCOLOR
  11. float4 iColor : COLOR0,
  12. out float4 oColor : COLOR0,
  13. #endif
  14. float2 iTexCoord : TEXCOORD0,
  15. out float2 oTexCoord : TEXCOORD0,
  16. out float4 oWorldPos : TEXCOORD1,
  17. out float4 oScreenPos : TEXCOORD2,
  18. out float4 oPos : POSITION)
  19. {
  20. float4 pos;
  21. #if (!defined(SKINNED)) && (!defined(INSTANCED))
  22. pos = GetPosition(iPos, oPos);
  23. #endif
  24. #ifdef SKINNED
  25. pos = GetPositionSkinned(iPos, iBlendWeights, iBlendIndices, oPos);
  26. #endif
  27. #ifdef INSTANCED
  28. pos = GetPositionInstanced(iPos, iModelInstance, oPos);
  29. #endif
  30. #ifdef VERTEXCOLOR
  31. oColor = iColor;
  32. #endif
  33. // Store world-oriented view position in case it is needed
  34. oWorldPos = float4(pos.xyz - cCameraPos, GetDepth(oPos));
  35. oScreenPos = GetScreenPos(oPos);
  36. oTexCoord = GetTexCoord(iTexCoord);
  37. }
  38. void PS(float2 iTexCoord : TEXCOORD0,
  39. float4 iWorldPos : TEXCOORD1,
  40. float4 iScreenPos : TEXCOORD2,
  41. #ifdef VERTEXCOLOR
  42. float4 iColor : COLOR0,
  43. #endif
  44. out float4 oColor : COLOR0)
  45. {
  46. #ifdef DIFFMAP
  47. float4 diffInput = tex2D(sDiffMap, iTexCoord);
  48. #ifdef ALPHAMASK
  49. if (diffInput.a < 0.5)
  50. discard;
  51. #endif
  52. float3 diffColor = cMatDiffColor.rgb * diffInput.rgb;
  53. #else
  54. float3 diffColor = cMatDiffColor.rgb;
  55. #endif
  56. #ifdef VERTEXCOLOR
  57. diffColor *= iColor.rgb;
  58. #endif
  59. #ifdef SPECMAP
  60. float specStrength = cMatSpecProperties.x * tex2D(sSpecMap, iTexCoord).g;
  61. #else
  62. float specStrength = cMatSpecProperties.x;
  63. #endif
  64. // Lights are accumulated at half intensity. Bring back to full intensity now
  65. float4 lightInput = 2.0 * tex2Dproj(sLightBuffer, iScreenPos);
  66. float3 lightDiffColor = cAmbientColor + lightInput.rgb;
  67. // Divide light color by intensity to reconstruct the specular color
  68. float3 lightSpecColor = lightInput.a * (lightInput.rgb / GetIntensity(lightInput.rgb));
  69. float3 finalColor = lightDiffColor * diffColor + lightSpecColor * specStrength;
  70. // Store coarse depth to alpha channel for deferred antialiasing
  71. oColor = float4(GetFog(finalColor, iWorldPos.w), iWorldPos.w);
  72. }