EnvironmentMap.fx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //-----------------------------------------------------------------------------
  2. // EnvironmentMap.fx
  3. //
  4. // Microsoft Game Technology Group
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. float4x4 World;
  8. float4x4 View;
  9. float4x4 Projection;
  10. float3 LightDirection = normalize(float3(1, 1, 1));
  11. float3 LightColor = float3(0.6, 0.5, 0.4);
  12. float3 AmbientColor = float3(0.1, 0.15, 0.25);
  13. texture Texture;
  14. texture EnvironmentMap;
  15. bool TextureEnabled;
  16. struct VS_INPUT
  17. {
  18. float4 Position : POSITION0;
  19. float3 Normal : NORMAL0;
  20. float2 TexCoord : TEXCOORD0;
  21. };
  22. struct VS_OUTPUT
  23. {
  24. float4 Position : POSITION0;
  25. float2 TexCoord : TEXCOORD0;
  26. float3 Reflection : TEXCOORD1;
  27. float3 Fresnel : COLOR0;
  28. float3 Lighting : COLOR1;
  29. };
  30. VS_OUTPUT VertexShaderFunction(VS_INPUT input)
  31. {
  32. VS_OUTPUT output;
  33. // Transform the input values.
  34. float4 worldPosition = mul(input.Position, World);
  35. float3 worldNormal = mul(input.Normal, World);
  36. output.Position = mul(mul(worldPosition, View), Projection);
  37. output.TexCoord = input.TexCoord;
  38. // Compute a reflection vector for the environment map.
  39. float3 eyePosition = mul(-View._m30_m31_m32, transpose(View));
  40. float3 viewVector = worldPosition - eyePosition;
  41. output.Reflection = reflect(viewVector, worldNormal);
  42. // Approximate a Fresnel coefficient for the environment map.
  43. // This makes the surface less reflective when you are looking
  44. // straight at it, and more reflective when it is viewed edge-on.
  45. output.Fresnel = saturate(1 + dot(normalize(viewVector), worldNormal));
  46. // Compute lighting.
  47. float lightAmount = max(dot(worldNormal, LightDirection), 0);
  48. output.Lighting = AmbientColor + lightAmount * LightColor;
  49. return output;
  50. }
  51. struct PS_INPUT
  52. {
  53. float2 TexCoord : TEXCOORD0;
  54. float3 Reflection : TEXCOORD1;
  55. float3 Fresnel : COLOR0;
  56. float3 Lighting : COLOR1;
  57. };
  58. sampler TextureSampler = sampler_state
  59. {
  60. Texture = (Texture);
  61. MinFilter = Linear;
  62. MagFilter = Linear;
  63. MipFilter = Linear;
  64. AddressU = Wrap;
  65. AddressV = Wrap;
  66. };
  67. sampler EnvironmentMapSampler = sampler_state
  68. {
  69. Texture = (EnvironmentMap);
  70. MinFilter = Linear;
  71. MagFilter = Linear;
  72. MipFilter = Linear;
  73. AddressU = Clamp;
  74. AddressV = Clamp;
  75. };
  76. float4 PixelShaderFunction(PS_INPUT input) : COLOR0
  77. {
  78. // Sample the texture and environment map.
  79. float3 color;
  80. if (TextureEnabled)
  81. color = tex2D(TextureSampler, input.TexCoord);
  82. else
  83. color = float3(0, 0, 0);
  84. float3 envmap = texCUBE(EnvironmentMapSampler, input.Reflection);
  85. // Use the Fresnel coefficient to interpolate between texture and environment map.
  86. color = lerp(color, envmap, input.Fresnel);
  87. // Apply lighting.
  88. color *= input.Lighting * 2;
  89. return float4(color, 1);
  90. }
  91. technique EnvironmentMapTechnique
  92. {
  93. pass Pass1
  94. {
  95. VertexShader = compile vs_2_0 VertexShaderFunction();
  96. PixelShader = compile ps_2_0 PixelShaderFunction();
  97. }
  98. }