Advertisement
Guest User

Untitled

a guest
Jul 21st, 2015
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.35 KB | None | 0 0
  1. NAMESPACE_ENTER(CFX)
  2. #include CFX_SETTINGS_DEF
  3.  
  4. #if (USE_GRAIN == 1)
  5.  
  6. static const float permTexUnit = 1.0/256.0;        // Perm texture texel-size
  7. static const float permTexUnitHalf = 0.5/256.0;    // Half perm texture texel-size
  8.  
  9. float width = BUFFER_WIDTH;
  10. float height = BUFFER_HEIGHT;
  11. uniform float timer < source = "timer"; >;
  12.  
  13. static const float grainamount = GrainPower;
  14. float colored = GrainColored;
  15. float coloramount = GrainColorAmount;
  16. float grainsize = GrainSize;
  17. float lumamount = GrainLuma;
  18.  
  19. float fract (float inp)                         //GeDoSato did not (to my knowledge) have a fract function, so I implemented one.  
  20. {
  21.     return (inp-floor(inp));
  22. }
  23.  
  24. float4 mix (float x, float y, float a)          //GeDoSaTo did not to my knowledge have a mix function either, so I implemented one.
  25. {
  26.     return x * (1 - a) + y * a;
  27. }
  28.    
  29. // a random texture generator, but you can also use a pre-computed perturbation texture
  30. float4 rnm(in float2 tc)
  31. {
  32.     float noise = sin(dot(float3(tc.x, tc.y, timer), float3(12.9898, 78.233, 0.0025216))) * 43758.5453;
  33.  
  34.     float noiseR =  frac(noise)*2.0-1.0;
  35.     float noiseG =  frac(noise*1.2154)*2.0-1.0;
  36.     float noiseB =  frac(noise*1.3453)*2.0-1.0;
  37.     float noiseA =  frac(noise*1.3647)*2.0-1.0;
  38.    
  39.     return float4(noiseR,noiseG,noiseB,noiseA);
  40. }
  41.  
  42. float fade(in float t) {
  43.     return t*t*t*(t*(t*6.0-15.0)+10.0);
  44. }
  45.  
  46. float pnoise3D(in float3 p)
  47. {
  48.     float3 pi = permTexUnit*floor(p)+permTexUnitHalf; // Integer part, scaled so +1 moves permTexUnit texel
  49.     // and offset 1/2 texel to sample texel centers
  50.     float3 pf = frac(p);     // Fractional part for interpolation
  51.  
  52.     // Noise contributions from (x=0, y=0), z=0 and z=1
  53.     float perm00 = rnm(pi.xy).a ;
  54.     float3  grad000 = rnm(float2(perm00, pi.z)).rgb * 4.0 - 1.0;
  55.     float n000 = dot(grad000, pf);
  56.     float3  grad001 = rnm(float2(perm00, pi.z + permTexUnit)).rgb * 4.0 - 1.0;
  57.     float n001 = dot(grad001, pf - float3(0.0, 0.0, 1.0));
  58.  
  59.     // Noise contributions from (x=0, y=1), z=0 and z=1
  60.     float perm01 = rnm(pi.xy + float2(0.0, permTexUnit)).a ;
  61.     float3  grad010 = rnm(float2(perm01, pi.z)).rgb * 4.0 - 1.0;
  62.     float n010 = dot(grad010, pf - float3(0.0, 1.0, 0.0));
  63.     float3  grad011 = rnm(float2(perm01, pi.z + permTexUnit)).rgb * 4.0 - 1.0;
  64.     float n011 = dot(grad011, pf - float3(0.0, 1.0, 1.0));
  65.  
  66.     // Noise contributions from (x=1, y=0), z=0 and z=1
  67.     float perm10 = rnm(pi.xy + float2(permTexUnit, 0.0)).a ;
  68.     float3  grad100 = rnm(float2(perm10, pi.z)).rgb * 4.0 - 1.0;
  69.     float n100 = dot(grad100, pf - float3(1.0, 0.0, 0.0));
  70.     float3  grad101 = rnm(float2(perm10, pi.z + permTexUnit)).rgb * 4.0 - 1.0;
  71.     float n101 = dot(grad101, pf - float3(1.0, 0.0, 1.0));
  72.  
  73.     // Noise contributions from (x=1, y=1), z=0 and z=1
  74.     float perm11 = rnm(pi.xy + float2(permTexUnit, permTexUnit)).a ;
  75.     float3  grad110 = rnm(float2(perm11, pi.z)).rgb * 4.0 - 1.0;
  76.     float n110 = dot(grad110, pf - float3(1.0, 1.0, 0.0));
  77.     float3  grad111 = rnm(float2(perm11, pi.z + permTexUnit)).rgb * 4.0 - 1.0;
  78.     float n111 = dot(grad111, pf - float3(1.0, 1.0, 1.0));
  79.  
  80.     // Blend contributions along x
  81.     float4 n_x = lerp(float4(n000, n001, n010, n011), float4(n100, n101, n110, n111), fade(pf.x));
  82.  
  83.     // Blend contributions along y
  84.     float2 n_xy = lerp(n_x.xy, n_x.zw, fade(pf.y));
  85.  
  86.     // Blend contributions along z
  87.     float n_xyz = lerp(n_xy.x, n_xy.y, fade(pf.z));
  88.  
  89.     // We're done, return the final noise value.
  90.     return n_xyz;
  91. }
  92.  
  93. //2d coordinate orientation thing
  94. float2 coordRot(in float2 tc, in float angle)
  95. {
  96.     float aspectr = width/height;
  97.     float rotX = ((tc.x*2.0-1.0)*aspectr*cos(angle)) - ((tc.y*2.0-1.0)*sin(angle));
  98.     float rotY = ((tc.y*2.0-1.0)*cos(angle)) + ((tc.x*2.0-1.0)*aspectr*sin(angle));
  99.     rotX = ((rotX/aspectr)*0.5+0.5);
  100.     rotY = rotY*0.5+0.5;
  101.     return float2(rotX,rotY);
  102. }
  103.  
  104. float4 GrainPass(float4 position : SV_Position, float2 texcoord : TEXCOORD0) : SV_Target
  105. {
  106.     float3 rotOffset = float3(1.425,3.892,5.835); //rotation offset values  
  107.     float2 rotCoordsR = coordRot(texcoord, timer + rotOffset.x);
  108.     float2 rot = rotCoordsR*float2(width/grainsize,height/grainsize);
  109.     float pNoise = pnoise3D(float3(rot.x,rot.y,0.0));
  110.     float3 noise = float3(pNoise, pNoise, pNoise);
  111.  
  112.     if (colored == 1)
  113.     {
  114.         float2 rotCoordsG = coordRot(texcoord, timer + rotOffset.y);
  115.         float2 rotCoordsB = coordRot(texcoord, timer + rotOffset.z);
  116.         noise.g = lerp(noise.r,pnoise3D(float3(rotCoordsG*float2(width/grainsize,height/grainsize),1.0)),coloramount);
  117.         noise.b = lerp(noise.r,pnoise3D(float3(rotCoordsB*float2(width/grainsize,height/grainsize),2.0)),coloramount);
  118.     }
  119.    
  120.     float3 col = position.rgb;
  121.  
  122.     //noisiness response curve based on scene luminance
  123.     float3 lumcoeff = float3(0.299,0.587,0.114);
  124.     float luminance = lerp(0.0,dot(col, lumcoeff),lumamount);
  125.     float lum = smoothstep(0.2,0.0,luminance);
  126.     lum += luminance;
  127.    
  128.     float2 thepow = pow(lum, 4.0);
  129.    
  130.     noise = lerp(noise,float3(0.0, 0.0, 0.0),pow(lum,4.0));
  131.     col += noise*grainamount;
  132.    
  133.     return float4(col,1.0);
  134. }
  135.  
  136. technique Grain_Tech <bool enabled = RFX_Start_Enabled; int toggle = Cel_ToggleKey; >
  137. {
  138.     pass Grain_Pass
  139.     {
  140.         VertexShader = RFX_VS_PostProcess;
  141.         PixelShader = GrainPass;
  142.     }
  143. }
  144.  
  145. #endif
  146. #include CFX_SETTINGS_UNDEF
  147. NAMESPACE_LEAVE()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement