SHARE
TWEET

a-hdr

a guest Sep 15th, 2016 3,762 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Semi-fake HDR shader for MGE XE
  2. // Artificially extends the brightness range and then compresses it differently based on overall screen brightness
  3. // Made by 748923
  4. // Version 9.27.2012.2105
  5.  
  6. //////////////////////////////
  7. // Configurable variables:
  8.  
  9. // How much to artifically exaggerate the brightness range (1.0 would be none)
  10. // Increasing this makes the range of brightness per pixel go beyond the range of 0-1, the maximum range that may be displayed. Higher means more adaptation is necessary.
  11. // Recommended: 1.0-1.2
  12. static const float range = 1.05f;
  13. // How much to negatively offset the brightness range before adapting. Higher means brightest possible scene will be darker.
  14. static const float rangeoffset = 0.002f;
  15. // How much to brighten the scene when adapting
  16. static const float brightmult = 0.2f;
  17.  
  18. // Adaptation black level adjustment
  19. static const float minInputAdapt = 0.9f;
  20. // Adaptation gamma adjustment
  21. static const float gammaAdapt = 1.0f;
  22. // Overall black level adjustment
  23. static float minInput = 0.148f;
  24. // Overall gamma adjustment
  25. static float gamma = 1.0f;
  26. // Overall saturation adjustment ( >1 = less saturated and <1 = more saturated )
  27. static const float saturation = 1.15f;
  28.  
  29. // You can adjust the gamma for interiors and nights here if the switches are enabled
  30. #define NIGHTSWITCH 1
  31. #define INTERIORSWITCH 1
  32. // Night gamma
  33. static const float nightGamma = 0.92f;
  34. // Interior gamma
  35. static const float intGamma = 0.98f;
  36.  
  37. // You can adjust gamma based on distance here if the switch is enabled (only happens at night)
  38. #define DEPTHDARK 1
  39. // How much to offset the gamma - Recommended: <0.1
  40. static const float nightDepthGammaOffset = 0.02f;
  41. // Distance scale - Recommended: 0.0001-0.0004
  42. static const float nightDepthScale = 0.0002;
  43.  
  44. // End configurable variables
  45. //////////////////////////////
  46.  
  47. bool isInterior;
  48. static const float pi = 3.14159265f;
  49. static const float3 coef = {0.3f, 0.7f, 0.11f};
  50. #if NIGHTSWITCH > 0
  51. float3 suncol;
  52. #endif
  53. float4 HDR;
  54.  
  55. texture lastshader;
  56. #if NIGHTSWITCH == 1 && DEPTHDARK == 1
  57. texture depthframe;
  58. #endif
  59.  
  60. sampler s0 = sampler_state { texture=<lastshader>; minfilter = linear; magfilter = linear; mipfilter = linear; addressu=mirror; addressv=mirror; };
  61. #if NIGHTSWITCH == 1 && DEPTHDARK == 1
  62. sampler s1 = sampler_state {texture=<depthframe>; AddressU=Clamp; AddressV=Clamp;};
  63. #endif
  64.  
  65. float4 tone(float2 tex : TEXCOORD) : COLOR0
  66. {
  67.     #if NIGHTSWITCH == 1 && DEPTHDARK == 1
  68.     float depth = tex2D(s1, tex).r;
  69.     #endif
  70.     float3 c = tex2D(s0, tex).rgb;
  71.     float HDRavg = (HDR.r + HDR.g + HDR.b) / 3;
  72.     //Extend range (linear)
  73.     c = c * range - rangeoffset;
  74.     //Recompress range with arctangent to fix "burned" colors
  75.     c = 0.33 * (pi*0.5 - atan(-4*c+2));
  76.     //maxInput based on HDRavg
  77.     float maxInput = clamp((HDRavg * brightmult) + (1-brightmult), 0.1, 1.0);
  78.     //Adjust minInput based on HDRavg?
  79.     minInput = minInput*max(minInputAdapt, 1-HDRavg);
  80.     //Adjust gamma based on HDRavg?
  81.     gamma = gamma*max(gammaAdapt, 1-HDRavg);
  82.     #if NIGHTSWITCH == 1
  83.     if ( !isInterior )
  84.     {
  85.         // Day/night interpolation from the sun color, yay!!!
  86.         float daynight = 1 - saturate(suncol.r*3.0);
  87.         gamma *= gamma - daynight*(1.0 - nightGamma);
  88.         #if DEPTHDARK == 1
  89.         // Night depth gamma!
  90.         gamma -= saturate(depth*nightDepthScale)*nightDepthGammaOffset*daynight;
  91.         #endif
  92.     }
  93.     #endif
  94.     #if INTERIORSWITCH == 1
  95.     if ( isInterior )
  96.     {
  97.         gamma *= intGamma;
  98.     }
  99.     #endif
  100.     //correct gamma
  101.     c = pow(c, 1.0/gamma);
  102.     //Adjust saturation
  103.     c.rgb = lerp(c.rgb, dot(coef.xyz, c.rgb), saturation-1 );
  104.     //Adjust with black and white levels
  105.     c = min( max(c - minInput, 0.0) / (maxInput-minInput), 1.0);
  106.     return float4( saturate(c) , 1.0);
  107. }
  108.  
  109. technique T0 < string MGEinterface = "MGE XE 0"; bool requiresHDR = true; >
  110. {
  111.     pass { PixelShader = compile ps_3_0 tone(); }
  112. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top