DeferredBasicEffect.fx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "Macros.fxh"
  2. float4x4 World;
  3. float4x4 View;
  4. float4x4 Projection;
  5. float specularIntensity = 0.8f;
  6. float specularPower = 0.5f;
  7. texture Texture;
  8. sampler diffuseSampler = sampler_state
  9. {
  10. Texture = (Texture);
  11. MAGFILTER = LINEAR;
  12. MINFILTER = LINEAR;
  13. MIPFILTER = LINEAR;
  14. AddressU = Wrap;
  15. AddressV = Wrap;
  16. };
  17. texture SpecularMap;
  18. sampler specularSampler = sampler_state
  19. {
  20. Texture = (SpecularMap);
  21. MagFilter = LINEAR;
  22. MinFilter = LINEAR;
  23. Mipfilter = LINEAR;
  24. AddressU = Wrap;
  25. AddressV = Wrap;
  26. };
  27. texture NormalMap;
  28. sampler normalSampler = sampler_state
  29. {
  30. Texture = (NormalMap);
  31. MagFilter = LINEAR;
  32. MinFilter = LINEAR;
  33. Mipfilter = LINEAR;
  34. AddressU = Wrap;
  35. AddressV = Wrap;
  36. };
  37. struct VertexShaderInput
  38. {
  39. float4 Position : POSITION0;
  40. float3 Normal : NORMAL0;
  41. float2 TexCoord : TEXCOORD0;
  42. float3 Binormal : BINORMAL0;
  43. float3 Tangent : TANGENT0;
  44. };
  45. struct VertexShaderOutput
  46. {
  47. float4 Position : POSITION0;
  48. float2 TexCoord : TEXCOORD0;
  49. float2 Depth : TEXCOORD1;
  50. float3x3 tangentToWorld : TEXCOORD2;
  51. };
  52. VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
  53. {
  54. VertexShaderOutput output;
  55. float4 worldPosition = mul(float4(input.Position.xyz,1), World);
  56. float4 viewPosition = mul(worldPosition, View);
  57. output.Position = mul(viewPosition, Projection);
  58. output.TexCoord = input.TexCoord;
  59. output.Depth.x = output.Position.z;
  60. output.Depth.y = output.Position.w;
  61. // calculate tangent space to world space matrix using the world space tangent,
  62. // binormal, and normal as basis vectors
  63. output.tangentToWorld[0] = mul(input.Tangent, World);
  64. output.tangentToWorld[1] = mul(input.Binormal, World);
  65. output.tangentToWorld[2] = mul(input.Normal, World);
  66. return output;
  67. }
  68. struct PixelShaderOutput
  69. {
  70. half4 Color : COLOR0;
  71. half4 Normal : COLOR1;
  72. half4 Depth : COLOR2;
  73. };
  74. PixelShaderOutput PixelShaderFunction(VertexShaderOutput input)
  75. {
  76. PixelShaderOutput output;
  77. output.Color = tex2D(diffuseSampler, input.TexCoord);
  78. float4 specularAttributes = tex2D(specularSampler, input.TexCoord);
  79. //specular Intensity
  80. output.Color.a = specularAttributes.r;
  81. // read the normal from the normal map
  82. float3 normalFromMap = tex2D(normalSampler, input.TexCoord);
  83. //tranform to [-1,1]
  84. normalFromMap = 2.0f * normalFromMap - 1.0f;
  85. normalFromMap = float3(0,0,1); //if we don't have a normalMap do this!
  86. //transform into world space
  87. normalFromMap = mul(normalFromMap, input.tangentToWorld);
  88. //normalize the result
  89. normalFromMap = normalize(normalFromMap);
  90. //output the normal, in [0,1] space
  91. output.Normal.rgb = 0.5f * (normalFromMap + 1.0f);
  92. //specular Power
  93. output.Normal.a = specularAttributes.a;
  94. output.Depth = input.Depth.x / input.Depth.y;
  95. return output;
  96. }
  97. TECHNIQUE( Standard, VertexShaderFunction, PixelShaderFunction );