testShader.fx 3.9 KB

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