Advertisement
Guest User

Untitled

a guest
Jul 4th, 2012
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. //------------------------------------------------------------------------------
  2. // File: BasicRender.fx
  3. //
  4. // Copyright (c) Microsoft Corporation. All rights reserved.
  5. //------------------------------------------------------------------------------
  6.  
  7. float3 lightPosition; // Position of light
  8. float4 lightDiffuse; // Light's diffuse color
  9. texture tex0; // Color texture for mesh
  10. texture cubeShadowMap; // Shadow map texture for lighting
  11.  
  12. float4x4 worldMat; // World matrix for object
  13. float4x4 worldViewProjMat;
  14.  
  15. float geometry_scale;
  16. float depth_bias;
  17.  
  18. //------------------------------------------------------------------------------
  19. // Texture samplers
  20. //------------------------------------------------------------------------------
  21. sampler sampler0 =
  22. sampler_state
  23. {
  24. Texture = <tex0>;
  25. MipFilter = LINEAR;
  26. MinFilter = LINEAR;
  27. MagFilter = LINEAR;
  28. };
  29. sampler cubeShadowMapSampler =
  30. sampler_state
  31. {
  32. Texture = <cubeShadowMap>;
  33. MinFilter = POINT;
  34. MagFilter = POINT;
  35. MipFilter = POINT;
  36. AddressU = Clamp;
  37. AddressV = Clamp;
  38. };
  39.  
  40. //------------------------------------------------------------------------------
  41. // Vertex shader output structure
  42. //------------------------------------------------------------------------------
  43. struct VS_OUTPUT
  44. {
  45. float4 Position : POSITION0; // vertex position
  46. float2 TextureUV : TEXCOORD0; // vertex texture coords
  47. float3 wNormal : TEXCOORD1;
  48. float4 vPos : TEXCOORD2;
  49. float3 wPos : TEXCOORD3;
  50. float4 Colour : COLOR0;
  51. };
  52.  
  53. struct PS_INPUT
  54. {
  55. float2 TextureUV : TEXCOORD0; // vertex texture coords
  56. float3 wNormal : TEXCOORD1;
  57. float4 vPos : TEXCOORD2;
  58. float3 wPos : TEXCOORD3;
  59. float4 Colour : COLOR0;
  60. };
  61.  
  62. struct VS_SHADOW_OUTPUT
  63. {
  64. float4 Position : POSITION;
  65. float2 Depth : TEXCOORD0;
  66. };
  67.  
  68. VS_SHADOW_OUTPUT RenderShadowMapVS(float4 vPos: POSITION)
  69. {
  70. VS_SHADOW_OUTPUT Out;
  71.  
  72. float4 posH = mul(vPos, worldViewProjMat);
  73.  
  74. Out.Position = posH;
  75. Out.Depth = posH.zw;
  76.  
  77. return Out;
  78. }
  79.  
  80. float4 RenderShadowMapPS( VS_SHADOW_OUTPUT In ) : COLOR0
  81. {
  82. return In.Depth.x;
  83. }
  84.  
  85. struct PS_OUTPUT
  86. {
  87. float4 RGBColor : COLOR0; // Pixel color
  88. };
  89.  
  90. VS_OUTPUT RenderShadowsVS(
  91. float3 position : POSITION,
  92. float3 normal : NORMAL,
  93. float2 TexCoord0 : TEXCOORD0,
  94. float4 colour : COLOR0)
  95. {
  96. VS_OUTPUT Output;
  97.  
  98. //transform the input position to the output
  99. Output.Position = mul(float4(position, 1.0), worldViewProjMat);
  100.  
  101. //transform the normal to world space
  102. Output.wNormal = mul(normal, worldMat);
  103.  
  104. //do not transform the position needed for the
  105. //shadow map determination
  106. Output.vPos = float4(position,1.0);
  107.  
  108. Output.wPos = mul(float4(position, 1.0), worldMat).xyz;
  109.  
  110. //pass the texture coordinate as-is
  111. Output.TextureUV = TexCoord0;
  112.  
  113. Output.Colour = colour;
  114.  
  115. //return the output structure
  116. return Output;
  117. }
  118.  
  119. PS_OUTPUT RenderShadowsPS( PS_INPUT In )
  120. {
  121. PS_OUTPUT Output;
  122.  
  123. float4 vTotalLightDiffuse = float4(0,0,0,0);
  124. float3 lightVecW = normalize(lightPosition - In.wPos);
  125.  
  126. float diffuseLightIntensity = dot(In.wNormal, lightVecW);
  127. vTotalLightDiffuse += lightDiffuse * max(0, diffuseLightIntensity);
  128.  
  129. float4 vPos = mul(In.vPos, worldMat);
  130.  
  131. float shadowdepth = texCUBE(cubeShadowMapSampler, -lightVecW.xyz).x;
  132.  
  133. float pixelDepth = length(lightPosition - vPos);
  134.  
  135. if (shadowdepth > pixelDepth)
  136. {
  137. // we're in shadow, cut the light
  138. vTotalLightDiffuse = float4(0,0,0,1);
  139. }
  140.  
  141. Output.RGBColor = tex2D(sampler0, In.TextureUV) * In.Colour * vTotalLightDiffuse;
  142.  
  143. return Output;
  144. }
  145.  
  146. PS_OUTPUT DiffuseOnlyPS(VS_OUTPUT In)
  147. {
  148. PS_OUTPUT Output;
  149. //calculate per-pixel diffuse
  150. float3 directionToLight = normalize(lightPosition - In.vPos);
  151. float diffuseIntensity = saturate( dot(directionToLight, In.wNormal));
  152. float4 diffuse = lightDiffuse * diffuseIntensity;
  153.  
  154. float4 color = diffuse;
  155. color.a = 1.0;
  156.  
  157. Output.RGBColor = tex2D(sampler0, In.TextureUV) * color;
  158.  
  159. return Output;
  160. }
  161.  
  162. PS_OUTPUT TextureOnlyPS(float2 TextureUV : TEXCOORD0)
  163. {
  164. PS_OUTPUT Output;
  165. Output.RGBColor = tex2D(sampler0, TextureUV);
  166.  
  167. return Output;
  168. }
  169.  
  170. struct AmbientVSOutput
  171. {
  172. float4 Pos : POSITION;
  173. float2 Tex : TEXCOORD0;
  174. float4 Colour : COLOR0;
  175. };
  176.  
  177. AmbientVSOutput ambient_VS(
  178. float4 inPosition : POSITION,
  179. float4 inColour : COLOR0,
  180. float2 tex : TEXCOORD0)
  181. {
  182. AmbientVSOutput output;
  183.  
  184. output.Pos = mul(inPosition, worldViewProjMat);
  185. output.Tex = tex;
  186. output.Colour = inColour;
  187.  
  188. return output;
  189. }
  190.  
  191. float4 ambient_PS(AmbientVSOutput input) : COLOR0
  192. {
  193. float4 ambient = float4(0.1f, 0.1f, 0.1f, 1.0f);
  194.  
  195. return ambient * (input.Colour * tex2D(sampler0, input.Tex));
  196. }
  197.  
  198. technique cubicShadowMapping
  199. {
  200. pass P0
  201. {
  202. ZEnable = true;
  203. ZWriteEnable = true;
  204. AlphaBlendEnable = true;
  205. SrcBlend = One;
  206. DestBlend = One;
  207.  
  208. VertexShader = compile vs_2_0 RenderShadowsVS();
  209. PixelShader = compile ps_2_0 RenderShadowsPS();
  210. }
  211. }
  212. technique depthMap
  213. {
  214. pass P0
  215. {
  216. CullMode = NONE;
  217. ZEnable = true;
  218. ZWriteEnable = true;
  219. AlphaBlendEnable = true;
  220. SrcBlend = One;
  221. DestBlend = One;
  222.  
  223. VertexShader = compile vs_2_0 RenderShadowMapVS();
  224. PixelShader = compile ps_2_0 RenderShadowMapPS();
  225.  
  226. }
  227. }
  228.  
  229. technique ambient
  230. {
  231. pass P0
  232. {
  233. ZEnable = true;
  234. AlphaBlendEnable = false;
  235. ZWriteEnable = true;
  236. SrcBlend = One;
  237. DestBlend = Zero;
  238.  
  239. VertexShader = compile vs_2_0 ambient_VS( );
  240. PixelShader = compile ps_2_0 ambient_PS( );
  241. }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement