testShader.fx 3.7 KB

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