testShader.fx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // ==OPENGL
  2. float4x4 World;
  3. float4x4 View;
  4. float4x4 Projection;
  5. float3 camWorld;
  6. texture ModelTexture;
  7. float bendValue = 1.4;
  8. float3 bendOrigin = { 0, 0, 0 };
  9. float bendDistance = 350.0;
  10. float3 bendVector = { 0, -0.01, 0 };
  11. float3 skyColor = { 1.0, 1.0, 1.0 };
  12. //pre-set;
  13. float Transparency = 1;
  14. float4 fogColor =
  15. {
  16. 0.39215,
  17. 0.58431,
  18. 0.92941,
  19. 0
  20. };
  21. sampler2D textureSampler = sampler_state
  22. {
  23. Texture = (ModelTexture);
  24. MinFilter = Point; //Controls sampling. None, Linear, or Point.
  25. MagFilter = Point; //Controls sampling. None, Linear, or Point.
  26. MipFilter = Point; //Controls how the mips are generated. None, Linear, or Point.
  27. AddressU = Wrap;
  28. AddressV = Wrap;
  29. };
  30. struct VertexShaderInput
  31. {
  32. float4 Position : SV_POSITION;
  33. float4 TexCoord : TEXCOORD0;
  34. };
  35. struct VertexShaderOutput
  36. {
  37. float4 Position : SV_POSITION;
  38. float2 TextureCoordinate : TEXCOORD0;
  39. float4 vertPos : TEXCOORD1;
  40. };
  41. VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
  42. {
  43. VertexShaderOutput output;
  44. float4 worldPosition = mul(input.Position, World);
  45. //curve test
  46. float4 world = worldPosition;
  47. float3 bendOrigin = camWorld;
  48. float dist = length(world.xyz - bendOrigin);
  49. float newDist = dist - bendDistance;
  50. dist = max(0.0, newDist);
  51. dist = pow(dist, bendValue);
  52. world.xyz += dist * bendVector;
  53. output.vertPos = worldPosition; //that will be our position in world- however we need to get the camera position too
  54. worldPosition = world;
  55. float4 viewPosition = mul(worldPosition, View);
  56. float4 projectionPosition = mul(viewPosition, Projection);
  57. output.Position = projectionPosition;
  58. output.TextureCoordinate = input.TexCoord;
  59. return output;
  60. }
  61. float4 ApplyFog(float4 textureColor, float3 vertPosition, float3 camPosition)
  62. {
  63. float4 fogDist = distance(vertPosition, camPosition);
  64. fogDist -= 250;
  65. fogDist /= 1800.0; //you can change that 800f to find best value
  66. fogDist = clamp(fogDist, 0, 0.4); //this prevents inversion of colours when at high distance;
  67. textureColor.xyz = lerp(textureColor.xyz, fogColor.xyz, fogDist.xyz); //we lerp the texture color to fogColor (changeable) with fogDistancw
  68. return textureColor;
  69. }
  70. void ApplyAlphaMasking(float4 textureColor)
  71. {
  72. clip(textureColor.a - 0.01); //0.01 - I'm not really sure how does it work, why 0.01 magically makes it work perfectly ;-;
  73. }
  74. float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
  75. {
  76. float4 textureColor = tex2D(textureSampler, input.TextureCoordinate);
  77. float3 vertPosition = input.vertPos;
  78. float3 camPosition = camWorld;
  79. //textureColor = ApplyFog(textureColor, vertPosition, camPosition);
  80. ApplyAlphaMasking(textureColor);
  81. float4 skyColorExtension = { skyColor.rgb, Transparency };
  82. textureColor *= skyColorExtension;
  83. return textureColor;
  84. }
  85. float4 PixelShaderFunction_Water(VertexShaderOutput input) : COLOR0
  86. {
  87. float4 textureColor = tex2D(textureSampler, input.TextureCoordinate);
  88. float3 vertPosition = input.vertPos;
  89. float3 camPosition = camWorld;
  90. textureColor = ApplyFog(textureColor, vertPosition, camPosition);
  91. ApplyAlphaMasking(textureColor);
  92. float4 skyColorExtension = { skyColor.rgb, Transparency };
  93. textureColor *= skyColorExtension;
  94. return textureColor;
  95. }
  96. technique Texture_fog_bend
  97. {
  98. pass Pass1
  99. {
  100. AlphaBlendEnable = TRUE;
  101. SrcBlend = SRCALPHA;
  102. DestBlend = INVSRCALPHA;
  103. VertexShader = compile vs_2_0 VertexShaderFunction();
  104. PixelShader = compile ps_2_0 PixelShaderFunction();
  105. }
  106. }
  107. technique Texture_fog_bend_waterAnim
  108. {
  109. pass Pass1
  110. {
  111. AlphaBlendEnable = TRUE;
  112. SrcBlend = SRCALPHA;
  113. DestBlend = INVSRCALPHA;
  114. VertexShader = compile vs_2_0 VertexShaderFunction();
  115. PixelShader = compile ps_2_0 PixelShaderFunction_Water();
  116. }
  117. }