Advertisement
Guest User

Untitled

a guest
Jul 17th, 2015
638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.43 KB | None | 0 0
  1.  
  2. /* -------------------- Interface -----------------------------------------------
  3. Interface section included here simply because it is used by default in GeDoSaTo.  This is not part of the original film grain shader
  4. */
  5. texture2D thisframeTex;
  6.  
  7. static float2 rcpres = PIXEL_SIZE;
  8.  
  9. const float timer;
  10.  
  11. #ifndef USE_SRGB
  12. #define USE_SRGB true
  13. #endif
  14.  
  15. sampler s0 = sampler_state
  16. {
  17.     texture = <thisframeTex>;
  18.     AddressU = CLAMP;
  19.     AddressV = CLAMP;
  20.     MINFILTER = LINEAR;
  21.     MAGFILTER = LINEAR;
  22.     SRGBTexture = USE_SRGB;
  23. };
  24.  
  25. struct VSOUT
  26. {
  27.     float4 vertPos : POSITION;
  28.     float2 UVCoord : TEXCOORD0;
  29. };
  30.  
  31. struct VSIN
  32. {
  33.     float4 vertPos : POSITION0;
  34.     float2 UVCoord : TEXCOORD0;
  35. };
  36.  
  37. VSOUT FrameVS(VSIN IN)
  38. {
  39.     VSOUT OUT;
  40.     OUT.vertPos = IN.vertPos;
  41.     OUT.UVCoord = IN.UVCoord;
  42.     return OUT;
  43. }
  44.  
  45. //----GRAIN----
  46.  
  47. #define USE_GRAIN             0 //[0 or 1] Film Grain: Adds an active graininess to the image.  Helps with banding and can add the illusion of detail
  48.  
  49. // Grain Settings
  50.     #define GrainPower   0.05 //[0.00 to 1.00] Intensity of applied grain
  51.     #define GrainColored   0 //[0 or 1] Whether grain should be colored or not
  52.     #define GrainColorAmount   0.6//[0.00 to 1.00] Amount of color to add to grain
  53.     #define GrainSize   1.6//[1.50 to 2.50] Size of individual pieces of grain
  54.     #define GrainLuma   1.0//[0.00 to 1.00] Grain brightness
  55.    
  56. /* ------------------------- Grain --------------------------------------------
  57. */
  58.  
  59. static const float permTexUnit = 1.0/256.0;        // Perm texture texel-size
  60. static const float permTexUnitHalf = 0.5/256.0;    // Half perm texture texel-size
  61.  
  62. float width = SCREEN_SIZE.x;
  63. float height = SCREEN_SIZE.y;
  64.  
  65. static const float grainamount = GrainPower;
  66. float colored = GrainColored;
  67. float coloramount = GrainColorAmount;
  68. float grainsize = GrainSize;
  69. float lumamount = GrainLuma;
  70.  
  71. float fract (float inp)                 //GeDoSato did not (to my knowledge) have a fract function, so I implemented one.  
  72. {
  73.     return (inp-floor(inp));
  74. }
  75.  
  76. float4 mix (float x, float y, float a)          //GeDoSaTo did not to my knowledge have a mix function either, so I implemented one.
  77. {
  78.     return x * (1 - a) + y * a;
  79. }
  80.    
  81. // a random texture generator, but you can also use a pre-computed perturbation texture
  82. float4 rnm(in float2 tc)
  83. {
  84.     float noise = sin(dot(float3(tc.x, tc.y, timer), float3(12.9898, 78.233, 0.0025216))) * 43758.5453;
  85.  
  86.     float noiseR =  frac(noise)*2.0-1.0;
  87.     float noiseG =  frac(noise*1.2154)*2.0-1.0;
  88.     float noiseB =  frac(noise*1.3453)*2.0-1.0;
  89.     float noiseA =  frac(noise*1.3647)*2.0-1.0;
  90.    
  91.     return float4(noiseR,noiseG,noiseB,noiseA);
  92. }
  93.  
  94. float fade(in float t) {
  95.     return t*t*t*(t*(t*6.0-15.0)+10.0);
  96. }
  97.  
  98. float pnoise3D(in float3 p)
  99. {
  100.     float3 pi = permTexUnit*floor(p)+permTexUnitHalf; // Integer part, scaled so +1 moves permTexUnit texel
  101.     // and offset 1/2 texel to sample texel centers
  102.     float3 pf = frac(p);     // Fractional part for interpolation
  103.  
  104.     // Noise contributions from (x=0, y=0), z=0 and z=1
  105.     float perm00 = rnm(pi.xy).a ;
  106.     float3  grad000 = rnm(float2(perm00, pi.z)).rgb * 4.0 - 1.0;
  107.     float n000 = dot(grad000, pf);
  108.     float3  grad001 = rnm(float2(perm00, pi.z + permTexUnit)).rgb * 4.0 - 1.0;
  109.     float n001 = dot(grad001, pf - float3(0.0, 0.0, 1.0));
  110.  
  111.     // Noise contributions from (x=0, y=1), z=0 and z=1
  112.     float perm01 = rnm(pi.xy + float2(0.0, permTexUnit)).a ;
  113.     float3  grad010 = rnm(float2(perm01, pi.z)).rgb * 4.0 - 1.0;
  114.     float n010 = dot(grad010, pf - float3(0.0, 1.0, 0.0));
  115.     float3  grad011 = rnm(float2(perm01, pi.z + permTexUnit)).rgb * 4.0 - 1.0;
  116.     float n011 = dot(grad011, pf - float3(0.0, 1.0, 1.0));
  117.  
  118.     // Noise contributions from (x=1, y=0), z=0 and z=1
  119.     float perm10 = rnm(pi.xy + float2(permTexUnit, 0.0)).a ;
  120.     float3  grad100 = rnm(float2(perm10, pi.z)).rgb * 4.0 - 1.0;
  121.     float n100 = dot(grad100, pf - float3(1.0, 0.0, 0.0));
  122.     float3  grad101 = rnm(float2(perm10, pi.z + permTexUnit)).rgb * 4.0 - 1.0;
  123.     float n101 = dot(grad101, pf - float3(1.0, 0.0, 1.0));
  124.  
  125.     // Noise contributions from (x=1, y=1), z=0 and z=1
  126.     float perm11 = rnm(pi.xy + float2(permTexUnit, permTexUnit)).a ;
  127.     float3  grad110 = rnm(float2(perm11, pi.z)).rgb * 4.0 - 1.0;
  128.     float n110 = dot(grad110, pf - float3(1.0, 1.0, 0.0));
  129.     float3  grad111 = rnm(float2(perm11, pi.z + permTexUnit)).rgb * 4.0 - 1.0;
  130.     float n111 = dot(grad111, pf - float3(1.0, 1.0, 1.0));
  131.  
  132.     // Blend contributions along x
  133.     float4 n_x = lerp(float4(n000, n001, n010, n011), float4(n100, n101, n110, n111), fade(pf.x));
  134.  
  135.     // Blend contributions along y
  136.     float2 n_xy = lerp(n_x.xy, n_x.zw, fade(pf.y));
  137.  
  138.     // Blend contributions along z
  139.     float n_xyz = lerp(n_xy.x, n_xy.y, fade(pf.z));
  140.  
  141.     // We're done, return the final noise value.
  142.     return n_xyz;
  143. }
  144.  
  145. //2d coordinate orientation thing
  146. float2 coordRot(in float2 tc, in float angle)
  147. {
  148.     float aspectr = width/height;
  149.     float rotX = ((tc.x*2.0-1.0)*aspectr*cos(angle)) - ((tc.y*2.0-1.0)*sin(angle));
  150.     float rotY = ((tc.y*2.0-1.0)*cos(angle)) + ((tc.x*2.0-1.0)*aspectr*sin(angle));
  151.     rotX = ((rotX/aspectr)*0.5+0.5);
  152.     rotY = rotY*0.5+0.5;
  153.     return float2(rotX,rotY);
  154. }
  155.  
  156. float4 GrainPass( float4 colorInput, float2 tex )
  157. {
  158.     float3 rotOffset = float3(1.425,3.892,5.835); //rotation offset values  
  159.     float2 rotCoordsR = coordRot(tex, timer + rotOffset.x);
  160.     float2 rot = rotCoordsR*float2(width/grainsize,height/grainsize);
  161.     float pNoise = pnoise3D(float3(rot.x,rot.y,0.0));
  162.     float3 noise = float3(pNoise, pNoise, pNoise);
  163.  
  164.     if (colored == 1)
  165.     {
  166.         float2 rotCoordsG = coordRot(tex, timer + rotOffset.y);
  167.         float2 rotCoordsB = coordRot(tex, timer + rotOffset.z);
  168.         noise.g = lerp(noise.r,pnoise3D(float3(rotCoordsG*float2(width/grainsize,height/grainsize),1.0)),coloramount);
  169.         noise.b = lerp(noise.r,pnoise3D(float3(rotCoordsB*float2(width/grainsize,height/grainsize),2.0)),coloramount);
  170.     }
  171.    
  172.     float3 col = colorInput.rgb;
  173.  
  174.     //noisiness response curve based on scene luminance
  175.     float3 lumcoeff = float3(0.299,0.587,0.114);
  176.     float luminance = lerp(0.0,dot(col, lumcoeff),lumamount);
  177.     float lum = smoothstep(0.2,0.0,luminance);
  178.     lum += luminance;
  179.    
  180.     float2 thepow = pow(lum, 4.0);
  181.    
  182.     noise = lerp(noise,float3(0.0, 0.0, 0.0),pow(lum,4.0));
  183.     col += noise*grainamount;
  184.    
  185.     return float4(col,1.0);
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement