Advertisement
Guest User

Untitled

a guest
Aug 26th, 2014
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.09 KB | None | 0 0
  1. // ------------------------- Grain --------------------------------------------
  2.  
  3. //uniform sampler2D bgl_RenderedTexture; //rendered scene sampler
  4. //uniform float bgl_RenderedTextureWidth; //scene sampler width
  5. //uniform float bgl_RenderedTextureHeight; //scene sampler height
  6. //uniform float timer;
  7.  
  8. const float permTexUnit = 1.0/256.0;        // Perm texture texel-size
  9. const float permTexUnitHalf = 0.5/256.0;    // Half perm texture texel-size
  10.  
  11. //float width = tex.x;
  12. //float height = tex.y;
  13.  
  14. const float grainamount = 0.05; //grain amount
  15. bool colored = false; //colored noise?
  16. float coloramount = 0.6;
  17. float grainsize = 1.6; //grain particle size (1.5 - 2.5)
  18. float lumamount = 1.0; //
  19.  
  20. float fract (float inp)
  21. {
  22.     return (inp-floor(inp));
  23. }
  24.  
  25. float4 mix (float x, float y, float a)
  26. {
  27.     return x * (1 - a) + y * a;
  28. }
  29.    
  30. //a random texture generator, but you can also use a pre-computed perturbation texture
  31. float4 rnm(in float2 tc)
  32. {
  33.     float noise =  sin(dot(tc + float2(timer,timer),float2(12.9898,78.233))) * 43758.5453;
  34.  
  35.     float noiseR =  fract(noise)*2.0-1.0;
  36.     float noiseG =  fract(noise*1.2154)*2.0-1.0;
  37.     float noiseB =  fract(noise*1.3453)*2.0-1.0;
  38.     float noiseA =  fract(noise*1.3647)*2.0-1.0;
  39.    
  40.     return float4(noiseR,noiseG,noiseB,noiseA);
  41. }
  42.  
  43. float fade(in float t) {
  44.     return t*t*t*(t*(t*6.0-15.0)+10.0);
  45. }
  46.  
  47. float pnoise3D(in float3 p)
  48. {
  49.     float3 pi = permTexUnit*floor(p)+permTexUnitHalf; // Integer part, scaled so +1 moves permTexUnit texel
  50.     // and offset 1/2 texel to sample texel centers
  51.     float3 pf = fract(p);     // Fractional part for interpolation
  52.  
  53.     // Noise contributions from (x=0, y=0), z=0 and z=1
  54.     float perm00 = rnm(pi.xy).a ;
  55.     float3  grad000 = rnm(float2(perm00, pi.z)).rgb * 4.0 - 1.0;
  56.     float n000 = dot(grad000, pf);
  57.     float3  grad001 = rnm(float2(perm00, pi.z + permTexUnit)).rgb * 4.0 - 1.0;
  58.     float n001 = dot(grad001, pf - float3(0.0, 0.0, 1.0));
  59.  
  60.     // Noise contributions from (x=0, y=1), z=0 and z=1
  61.     float perm01 = rnm(pi.xy + float2(0.0, permTexUnit)).a ;
  62.     float3  grad010 = rnm(float2(perm01, pi.z)).rgb * 4.0 - 1.0;
  63.     float n010 = dot(grad010, pf - float3(0.0, 1.0, 0.0));
  64.     float3  grad011 = rnm(float2(perm01, pi.z + permTexUnit)).rgb * 4.0 - 1.0;
  65.     float n011 = dot(grad011, pf - float3(0.0, 1.0, 1.0));
  66.  
  67.     // Noise contributions from (x=1, y=0), z=0 and z=1
  68.     float perm10 = rnm(pi.xy + float2(permTexUnit, 0.0)).a ;
  69.     float3  grad100 = rnm(float2(perm10, pi.z)).rgb * 4.0 - 1.0;
  70.     float n100 = dot(grad100, pf - float3(1.0, 0.0, 0.0));
  71.     float3  grad101 = rnm(float2(perm10, pi.z + permTexUnit)).rgb * 4.0 - 1.0;
  72.     float n101 = dot(grad101, pf - float3(1.0, 0.0, 1.0));
  73.  
  74.     // Noise contributions from (x=1, y=1), z=0 and z=1
  75.     float perm11 = rnm(pi.xy + float2(permTexUnit, permTexUnit)).a ;
  76.     float3  grad110 = rnm(float2(perm11, pi.z)).rgb * 4.0 - 1.0;
  77.     float n110 = dot(grad110, pf - float3(1.0, 1.0, 0.0));
  78.     float3  grad111 = rnm(float2(perm11, pi.z + permTexUnit)).rgb * 4.0 - 1.0;
  79.     float n111 = dot(grad111, pf - float3(1.0, 1.0, 1.0));
  80.  
  81.     // Blend contributions along x
  82.     float4 n_x = mix(float4(n000, n001, n010, n011), float4(n100, n101, n110, n111), fade(pf.x));
  83.  
  84.     // Blend contributions along y
  85.     float2 n_xy = mix(n_x.xy, n_x.zw, fade(pf.y));
  86.  
  87.     // Blend contributions along z
  88.     float n_xyz = mix(n_xy.x, n_xy.y, fade(pf.z));
  89.  
  90.     // We're done, return the final noise value.
  91.     return n_xyz;
  92. }
  93.  
  94. //2d coordinate orientation thing
  95. float2 coordRot(in float2 tc, in float angle, in float width, in float height)
  96. {
  97.     float aspectr = width/height;
  98.     float rotX = ((tc.x*2.0-1.0)*aspectr*cos(angle)) - ((tc.y*2.0-1.0)*sin(angle));
  99.     float rotY = ((tc.y*2.0-1.0)*cos(angle)) + ((tc.x*2.0-1.0)*aspectr*sin(angle));
  100.     rotX = ((rotX/aspectr)*0.5+0.5);
  101.     rotY = rotY*0.5+0.5;
  102.     return float2(rotX,rotY);
  103. }
  104.  
  105. float4 GrainPass( float4 colorInput, float2 tex )
  106. {
  107.     float width = tex.x;
  108.     float height = tex.y;
  109.     //float aspectr = (width / height);
  110.     float2 texCoord = float2 (tex[0], tex[1]);
  111.    
  112.     float3 rotOffset = float3(1.425,3.892,5.835); //rotation offset values 
  113.     float2 rotCoordsR = coordRot(texCoord, timer + rotOffset.x, width, height);
  114.     //float3 noise = float3(pnoise3D(float3(rotCoordsR*float2(width/grainsize,height/grainsize),0.0)));
  115.     float2 rot = rotCoordsR*float2(width/grainsize,height/grainsize);
  116.     float pNoise = pnoise3D(float3(rot.x,rot.y,0.0));
  117.     float3 noise = float3(pNoise, pNoise, pNoise);
  118.  
  119.     if (colored)
  120.     {
  121.         float2 rotCoordsG = coordRot(texCoord, timer + rotOffset.y, width, height);
  122.         float2 rotCoordsB = coordRot(texCoord, timer + rotOffset.z, width, height);
  123.         noise.g = mix(noise.r,pnoise3D(float3(rotCoordsG*float2(width/grainsize,height/grainsize),1.0)),coloramount);
  124.         noise.b = mix(noise.r,pnoise3D(float3(rotCoordsB*float2(width/grainsize,height/grainsize),2.0)),coloramount);
  125.     }
  126.  
  127.     float3 col = colorInput.rgb;
  128.  
  129.     //noisiness response curve based on scene luminance
  130.     float3 lumcoeff = float3(0.299,0.587,0.114);
  131.     float luminance = mix(0.0,dot(col, lumcoeff),lumamount);
  132.     float lum = smoothstep(0.2,0.0,luminance);
  133.     lum += luminance;
  134.    
  135.    
  136.     noise = mix(noise,0.0,pow(lum,4.0));
  137.     col = col+noise*grainamount;
  138.    
  139.     return float4(col,1.0);
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement