Guest User

Untitled

a guest
Sep 27th, 2014
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.62 KB | None | 0 0
  1. //
  2. // Example shader - snow_ground.fx
  3. //
  4.  
  5.  
  6. //---------------------------------------------------------------------
  7. // settings
  8. //---------------------------------------------------------------------
  9. texture sNoiseTexture;
  10. float sFadeStart = 10;          // Near point where distance fading will start
  11. float sFadeEnd = 80;            // Far point where distance fading will complete (i.e. effect will not be visible past this point)
  12.  
  13. float sSnowMult = 2;
  14. float sSnowAdd = 0.2;
  15.  
  16. //---------------------------------------------------------------------
  17. // Include some common stuff
  18. //---------------------------------------------------------------------
  19. #define GENERATE_NORMALS      // Uncomment for normals to be generated
  20. #include "mta-helper.fx"
  21.  
  22.  
  23. //---------------------------------------------------------------------
  24. // Sampler for the main texture
  25. //---------------------------------------------------------------------
  26. sampler Sampler0 = sampler_state
  27. {
  28.     Texture = (gTexture0);
  29. };
  30.  
  31. //---------------------------------------------------------------------
  32. // Sampler for the noise texture
  33. //---------------------------------------------------------------------
  34. sampler3D SamplerNoise = sampler_state
  35. {
  36.    Texture = (sNoiseTexture);
  37.    MAGFILTER = LINEAR;
  38.    MINFILTER = LINEAR;
  39.    MIPFILTER = LINEAR;
  40.    MIPMAPLODBIAS = 0.000000;
  41. };
  42.  
  43. //---------------------------------------------------------------------
  44. // Structure of data sent to the vertex shader
  45. //---------------------------------------------------------------------
  46. struct VSInput
  47. {
  48.   float3 Position : POSITION0;
  49.   float3 Normal : NORMAL0;
  50.   float4 Diffuse : COLOR0;
  51.   float2 TexCoord : TEXCOORD0;
  52. };
  53.  
  54. //---------------------------------------------------------------------
  55. // Structure of data sent to the pixel shader ( from the vertex shader )
  56. //---------------------------------------------------------------------
  57. struct PSInput
  58. {
  59.   float4 Position : POSITION0;
  60.   float4 Diffuse : COLOR0;
  61.   float2 TexCoord : TEXCOORD0;
  62.   float2 NoiseCoord : TEXCOORD1;
  63.   float DistFade : TEXCOORD3;
  64. };
  65.  
  66.  
  67. //------------------------------------------------------------------------------------------
  68. // VertexShaderFunction
  69. //  1. Read from VS structure
  70. //  2. Process
  71. //  3. Write to PS structure
  72. //------------------------------------------------------------------------------------------
  73. PSInput VertexShaderFunction(VSInput VS)
  74. {
  75.     PSInput PS = (PSInput)0;
  76.  
  77.     // Make sure normal is valid
  78.     MTAFixUpNormal( VS.Normal );
  79.  
  80.     // Calculate screen pos of vertex
  81.     PS.Position = MTACalcScreenPosition ( VS.Position );
  82.  
  83.     // Pass through tex coord
  84.     PS.TexCoord = VS.TexCoord;
  85.  
  86.     // Calculate GTA lighting for buildings
  87.     PS.Diffuse = MTACalcGTABuildingDiffuse( VS.Diffuse );
  88.  
  89.     // Distance fade calculation
  90.     float DistanceFromCamera = MTACalcCameraDistance( gCameraPosition, MTACalcWorldPosition( VS.Position ) );
  91.     PS.DistFade = MTAUnlerp ( sFadeEnd, sFadeStart, DistanceFromCamera );
  92.  
  93.     // Less snow on upright surfaces
  94.     float3 WorldNormal = MTACalcWorldNormal( VS.Normal );
  95.     PS.DistFade *= WorldNormal.z;
  96.  
  97.     // Noise texture coords
  98.     float3 WorldPos = MTACalcWorldPosition( VS.Position );
  99.     PS.NoiseCoord.x = WorldPos.x / 48;
  100.     PS.NoiseCoord.y = WorldPos.y / 48;
  101.  
  102.     return PS;
  103. }
  104.  
  105. //------------------------------------------------------------------------------------------
  106. // PixelShaderFunction
  107. //  1. Read from PS structure
  108. //  2. Process
  109. //  3. Return pixel color
  110. //------------------------------------------------------------------------------------------
  111. float4 PixelShaderFunction(PSInput PS) : COLOR0
  112. {
  113.     // Get texture pixel
  114.     float4 texel = tex2D(Sampler0, PS.TexCoord);
  115.     float3 texelNoise = tex3D(SamplerNoise, float3(PS.NoiseCoord.xy,1)).rgb;
  116.  
  117.     float4 texelSnow = saturate( texel.g * sSnowMult + sSnowAdd );
  118.  
  119.     float distFade = saturate( PS.DistFade.x );
  120.  
  121.     float amount = texelNoise.y * texelNoise.y * 3;
  122.     amount *= distFade;
  123.     float4 finalColor = lerp( texel, texelSnow, amount );
  124.  
  125.     finalColor = finalColor * PS.Diffuse;
  126.     finalColor.a = texel.a * PS.Diffuse.a;
  127.     return finalColor;
  128. }
  129.  
  130.  
  131. //------------------------------------------------------------------------------------------
  132. // Techniques
  133. //------------------------------------------------------------------------------------------
  134. technique snowground
  135. {
  136.     pass P0
  137.     {
  138.         VertexShader = compile vs_2_0 VertexShaderFunction();
  139.         PixelShader = compile ps_2_0 PixelShaderFunction();
  140.     }
  141. }
  142.  
  143. // Fallback
  144. technique fallback
  145. {
  146.     pass P0
  147.     {
  148.         // Just draw normally
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment