Advertisement
Guest User

shader code

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