RotateEnvironmentMap.fx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //////////////////////////////////////////////////////////////////////////////
  2. // ©2008 Electronic Arts Inc
  3. //
  4. // FX Shader for Rotating the EnvironmentTexture Cubemap
  5. //////////////////////////////////////////////////////////////////////////////
  6. #define SUPPORT_GLOBAL_LIGHTS 1
  7. #include "Common.fxh"
  8. // ----------------------------------------------------------------------------
  9. // Source Environment map
  10. // ----------------------------------------------------------------------------
  11. SAMPLER_CUBE_BEGIN( EnvironmentTexture,
  12. string SasBindAddress = "Environment.SourceTexture";
  13. string ResourceType = "Cube";
  14. )
  15. MinFilter = Linear;
  16. MagFilter = Linear;
  17. MipFilter = Linear;
  18. AddressU = Clamp;
  19. AddressV = Clamp;
  20. AddressW = Clamp;
  21. SAMPLER_CUBE_END
  22. // ----------------------------------------------------------------------------
  23. struct VSOutput
  24. {
  25. float4 Position : POSITION;
  26. float3 PixelDirection : TEXCOORD0; // direction in the cubemap associated with this pixel
  27. };
  28. // ----------------------------------------------------------------------------
  29. // Vertex Shader, rotate the face to the correct frame
  30. // ----------------------------------------------------------------------------
  31. VSOutput VS_RotateEnv(float3 Position : POSITION, uniform float3 faceDir, uniform float3 upDir)
  32. {
  33. VSOutput Out;
  34. Out.Position = float4(Position, 1);
  35. float3 direction = float3(Position.xy,1);
  36. // rotate the default quad (along positive z) to align with the face direction
  37. float3 faceCross = normalize(cross(faceDir, upDir));
  38. float3 faceCross2 = cross(faceDir, faceCross);
  39. float3x3 worldToFace = float3x3(faceCross, faceCross2, faceDir);
  40. Out.PixelDirection = mul(direction, worldToFace);
  41. return Out;
  42. }
  43. // ----------------------------------------------------------------------------
  44. // Pixel/Fragment Shader, rotate vector by light, load and return
  45. // ----------------------------------------------------------------------------
  46. float4 PS_RotateEnv(VSOutput In) : COLOR
  47. {
  48. USE_DIRECTIONAL_LIGHT_INTERACTIVE(DirectionalLight, 0);
  49. // rotate the cube map to align with the light direction
  50. float3 lightDir = DirectionalLight[0].Direction;
  51. float3 lightCross = normalize(cross(lightDir, float3(1, 0, 0)));
  52. float3 lightCross2 = cross(lightDir, lightCross);
  53. float3x3 worldToLight = float3x3(lightCross, lightCross2, lightDir);
  54. float3 vect = mul(worldToLight, In.PixelDirection);
  55. return texCUBE( SAMPLER(EnvironmentTexture), vect );
  56. }
  57. // ----------------------------------------------------------------------------
  58. // these are hardcoded frames defined by the vector towards the face and a perpendicular "up" vector
  59. // ----------------------------------------------------------------------------
  60. technique CubeFace0
  61. {
  62. pass p0
  63. {
  64. VertexShader = compile VS_2_0 VS_RotateEnv(float3(1,0,0),float3(0,-1,0));
  65. PixelShader = compile PS_2_0 PS_RotateEnv();
  66. ZEnable = false;
  67. ZWriteEnable = false;
  68. CullMode = None;
  69. AlphaTestEnable = false;
  70. AlphaBlendEnable = false;
  71. }
  72. }
  73. // ----------------------------------------------------------------------------
  74. technique CubeFace1
  75. {
  76. pass p0
  77. {
  78. VertexShader = compile VS_2_0 VS_RotateEnv(float3(-1,0,0),float3(0,-1,0));
  79. PixelShader = compile PS_2_0 PS_RotateEnv();
  80. ZEnable = false;
  81. ZWriteEnable = false;
  82. CullMode = None;
  83. AlphaTestEnable = false;
  84. AlphaBlendEnable = false;
  85. }
  86. }
  87. // ----------------------------------------------------------------------------
  88. technique CubeFace2
  89. {
  90. pass p0
  91. {
  92. VertexShader = compile VS_2_0 VS_RotateEnv(float3(0,1,0),float3(0,0,1));
  93. PixelShader = compile PS_2_0 PS_RotateEnv();
  94. ZEnable = false;
  95. ZWriteEnable = false;
  96. CullMode = None;
  97. AlphaTestEnable = false;
  98. AlphaBlendEnable = false;
  99. }
  100. }
  101. // ----------------------------------------------------------------------------
  102. technique CubeFace3
  103. {
  104. pass p0
  105. {
  106. VertexShader = compile VS_2_0 VS_RotateEnv(float3(0,-1,0),float3(0,0,-1));
  107. PixelShader = compile PS_2_0 PS_RotateEnv();
  108. ZEnable = false;
  109. ZWriteEnable = false;
  110. CullMode = None;
  111. AlphaTestEnable = false;
  112. AlphaBlendEnable = false;
  113. }
  114. }
  115. // ----------------------------------------------------------------------------
  116. technique CubeFace4
  117. {
  118. pass p0
  119. {
  120. VertexShader = compile VS_2_0 VS_RotateEnv(float3(0,0,1),float3(0,-1,0));
  121. PixelShader = compile PS_2_0 PS_RotateEnv();
  122. ZEnable = false;
  123. ZWriteEnable = false;
  124. CullMode = None;
  125. AlphaTestEnable = false;
  126. AlphaBlendEnable = false;
  127. }
  128. }
  129. // ----------------------------------------------------------------------------
  130. technique CubeFace5
  131. {
  132. pass p0
  133. {
  134. VertexShader = compile VS_2_0 VS_RotateEnv(float3(0,0,-1),float3(0,-1,0));
  135. PixelShader = compile PS_2_0 PS_RotateEnv();
  136. ZEnable = false;
  137. ZWriteEnable = false;
  138. CullMode = None;
  139. AlphaTestEnable = false;
  140. AlphaBlendEnable = false;
  141. }
  142. }