DrawModel.fx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //-----------------------------------------------------------------------------
  2. // DrawModel.fx
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. float4x4 World;
  8. float4x4 View;
  9. float4x4 Projection;
  10. float4x4 LightViewProj;
  11. float3 LightDirection;
  12. float4 AmbientColor = float4(0.15, 0.15, 0.15, 0);
  13. float DepthBias = 0.001f;
  14. texture Texture;
  15. sampler TextureSampler = sampler_state
  16. {
  17. Texture = (Texture);
  18. };
  19. texture ShadowMap;
  20. sampler ShadowMapSampler = sampler_state
  21. {
  22. Texture = <ShadowMap>;
  23. };
  24. struct DrawWithShadowMap_VSIn
  25. {
  26. float4 Position : POSITION0;
  27. float3 Normal : NORMAL0;
  28. float2 TexCoord : TEXCOORD0;
  29. };
  30. struct DrawWithShadowMap_VSOut
  31. {
  32. float4 Position : POSITION0;
  33. float3 Normal : TEXCOORD0;
  34. float2 TexCoord : TEXCOORD1;
  35. float4 WorldPos : TEXCOORD2;
  36. };
  37. struct CreateShadowMap_VSOut
  38. {
  39. float4 Position : POSITION;
  40. float Depth : TEXCOORD0;
  41. };
  42. // Transforms the model into light space an renders out the depth of the object
  43. CreateShadowMap_VSOut CreateShadowMap_VertexShader(float4 Position: POSITION)
  44. {
  45. CreateShadowMap_VSOut Out;
  46. Out.Position = mul(Position, mul(World, LightViewProj));
  47. Out.Depth = Out.Position.z / Out.Position.w;
  48. return Out;
  49. }
  50. // Saves the depth value out to the 32bit floating point texture
  51. float4 CreateShadowMap_PixelShader(CreateShadowMap_VSOut input) : COLOR
  52. {
  53. return float4(input.Depth, 0, 0, 0);
  54. }
  55. // Draws the model with shadows
  56. DrawWithShadowMap_VSOut DrawWithShadowMap_VertexShader(DrawWithShadowMap_VSIn input)
  57. {
  58. DrawWithShadowMap_VSOut Output;
  59. float4x4 WorldViewProj = mul(mul(World, View), Projection);
  60. // Transform the models verticies and normal
  61. Output.Position = mul(input.Position, WorldViewProj);
  62. Output.Normal = normalize(mul(input.Normal, World));
  63. Output.TexCoord = input.TexCoord;
  64. // Save the vertices postion in world space
  65. Output.WorldPos = mul(input.Position, World);
  66. return Output;
  67. }
  68. // Determines the depth of the pixel for the model and checks to see
  69. // if it is in shadow or not
  70. float4 DrawWithShadowMap_PixelShader(DrawWithShadowMap_VSOut input) : COLOR
  71. {
  72. // Color of the model
  73. float4 diffuseColor = tex2D(TextureSampler, input.TexCoord);
  74. // Intensity based on the direction of the light
  75. float diffuseIntensity = saturate(dot(LightDirection, input.Normal));
  76. // Final diffuse color with ambient color added
  77. float4 diffuse = diffuseIntensity * diffuseColor + AmbientColor;
  78. // Find the position of this pixel in light space
  79. float4 lightingPosition = mul(input.WorldPos, LightViewProj);
  80. // Find the position in the shadow map for this pixel
  81. float2 ShadowTexCoord = 0.5 * lightingPosition.xy /
  82. lightingPosition.w + float2( 0.5, 0.5 );
  83. ShadowTexCoord.y = 1.0f - ShadowTexCoord.y;
  84. // Get the current depth stored in the shadow map
  85. float shadowdepth = tex2D(ShadowMapSampler, ShadowTexCoord).r;
  86. // Calculate the current pixel depth
  87. // The bias is used to prevent folating point errors that occur when
  88. // the pixel of the occluder is being drawn
  89. float ourdepth = (lightingPosition.z / lightingPosition.w) - DepthBias;
  90. // Check to see if this pixel is in front or behind the value in the shadow map
  91. if (shadowdepth < ourdepth)
  92. {
  93. // Shadow the pixel by lowering the intensity
  94. diffuse *= float4(0.5,0.5,0.5,0);
  95. };
  96. return diffuse;
  97. }
  98. // Technique for creating the shadow map
  99. technique CreateShadowMap
  100. {
  101. pass Pass1
  102. {
  103. VertexShader = compile vs_2_0 CreateShadowMap_VertexShader();
  104. PixelShader = compile ps_2_0 CreateShadowMap_PixelShader();
  105. }
  106. }
  107. // Technique for drawing with the shadow map
  108. technique DrawWithShadowMap
  109. {
  110. pass Pass1
  111. {
  112. VertexShader = compile vs_2_0 DrawWithShadowMap_VertexShader();
  113. PixelShader = compile ps_2_0 DrawWithShadowMap_PixelShader();
  114. }
  115. }