2
0

testShader.fx 3.6 KB

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