Advertisement
Guest User

Untitled

a guest
May 29th, 2018
1,232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.60 KB | None | 0 0
  1. /*
  2. MMJ's Cel Shader - v1.03
  3. ----------------------------------------------------------------
  4. -- 180403 --
  5. This is a port of my old shader from around 2006 for Pete's OGL2
  6. plugin for ePSXe. It started out as a shader based on the
  7. "CComic" shader by Maruke. I liked his concept, but I was
  8. looking for something a little different in the output.
  9.  
  10. Since the last release, I've seen some test screenshots from MAME
  11. using a port of my original shader and have also seen another
  12. port to get it working with the PCSX2 emulator. Having recently
  13. seen some Kingdom Hearts II and Soul Calibur 3 YouTube videos with
  14. my ported shader inspired me to revisit it and get it working in
  15. RetroArch.
  16.  
  17. As for this version (1.03), I've made a few small modifications
  18. (such as to remove the OGL2Param references, which were specific
  19. to Pete's plugin) and I added some RetroArch Parameter support,
  20. so some values can now be changed in real time.
  21.  
  22. Keep in mind, that this was originally developed for PS1, using
  23. various 3D games as a test. In general, it will look better in
  24. games with less detailed textures, as "busy" textures will lead
  25. to more outlining / messy appearance. Increasing "Outline
  26. Brightness" can help mitigate this some by lessening the
  27. "strength" of the outlines.
  28.  
  29. Also (in regards to PS1 - I haven't really tested other systems
  30. too much yet), 1x internal resolution will look terrible. 2x
  31. will also probably be fairly blurry/messy-looking. For best
  32. results, you should probably stick to 4x or higher internal
  33. resolution with this shader.
  34.  
  35. Parameters:
  36. -----------
  37. White Level Cutoff = Anything above this luminance value will be
  38.     forced to pure white.
  39.  
  40. Black Level Cutoff = Anything below this luminance value will be
  41.     forced to pure black.
  42.  
  43. Shading Levels = Determines how many color "slices" there should
  44.     be (not counting black/white cutoffs, which are always
  45.     applied).
  46.  
  47. Saturation Modifier = Increase or decrease color saturation.
  48.     Default value boosts saturation a little for a more
  49.     cartoonish look. Set to 0.00 for grayscale.
  50.  
  51. Outline Brightness = Adjusts darkness of the outlines. At a
  52.     setting of 1, outlines should be disabled.
  53.  
  54. Shader Strength = Adjusts the weight of the color banding
  55.     portion of the shader from 0% (0.00) to 100% (1.00). At a
  56.     setting of 0.00, you can turn off the color banding effect
  57.     altogether, but still keep outlines enabled.
  58. -----------
  59. MMJuno
  60. */
  61.  
  62. #include "ReShade.fxh"
  63.  
  64. uniform float WhtCutoff <
  65.     ui_type = "drag";
  66.     ui_min = 0.50;
  67.     ui_max = 1.0;
  68.     ui_step = 0.01;
  69.     ui_label = "White Level Cutoff [MMJCelShader]";
  70. > = 0.97;
  71.  
  72. uniform float BlkCutoff <
  73.     ui_type = "drag";
  74.     ui_min = 0.0;
  75.     ui_max = 0.5;
  76.     ui_step = 0.01;
  77.     ui_label = "Black Level Cutoff [MMJCelShader]";
  78. > = 0.03;
  79.  
  80. uniform float ShdLevels <
  81.     ui_type = "drag";
  82.     ui_min = 1.0;
  83.     ui_max = 16.0;
  84.     ui_step = 1.0;
  85.     ui_label = "Shading Levels [MMJCelShader]";
  86. > = 9.0;
  87.  
  88. uniform float SatModify <
  89.     ui_type = "drag";
  90.     ui_min = 0.0;
  91.     ui_max = 2.0;
  92.     ui_step = 0.01;
  93.     ui_label = "Saturation Modifier [MMJCelShader]";
  94. > = 1.15;
  95.  
  96. uniform float OtlModify <
  97.     ui_type = "drag";
  98.     ui_min = 0.0;
  99.     ui_max = 1.0;
  100.     ui_step = 0.01;
  101.     ui_label = "Outline Brightness [MMJCelShader]";
  102. > = 0.20;
  103.  
  104. uniform float ShdWeight <
  105.     ui_type = "drag";
  106.     ui_min = 0.0;
  107.     ui_max = 1.0;
  108.     ui_step = 0.01;
  109.     ui_label = "Shader Strength [MMJCelShader]";
  110. > = 0.50;
  111.  
  112. sampler RetroArchSRGB { Texture = ReShade::BackBufferTex; MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = LINEAR; SRGBTexture = true;};
  113.  
  114. #define mod(x,y) (x-y*floor(x/y))
  115.  
  116. float3 RGB2HCV(in float3 RGB)
  117. {
  118.     RGB = saturate(RGB);
  119.     float Epsilon = 1e-10;
  120.         // Based on work by Sam Hocevar and Emil Persson
  121.         float4 P = (RGB.g < RGB.b) ? float4(RGB.bg, -1.0, 2.0/3.0) : float4(RGB.gb, 0.0, -1.0/3.0);
  122.         float4 Q = (RGB.r < P.x) ? float4(P.xyw, RGB.r) : float4(RGB.r, P.yzx);
  123.         float C = Q.x - min(Q.w, Q.y);
  124.         float H = abs((Q.w - Q.y) / (6 * C + Epsilon) + Q.z);
  125.         return float3(H, C, Q.x);
  126. }
  127.  
  128. float3 RGB2HSL(in float3 RGB)
  129. {
  130.         float3 HCV = RGB2HCV(RGB);
  131.         float L = HCV.z - HCV.y * 0.5;
  132.         float S = HCV.y / (1.0000001 - abs(L * 2 - 1));
  133.         return float3(HCV.x, S, L);
  134. }
  135.  
  136. float3 HSL2RGB(in float3 HSL)
  137. {
  138.     HSL = saturate(HSL);
  139.     //HSL.z *= 0.99;
  140.         float3 RGB = saturate(float3(abs(HSL.x * 6.0 - 3.0) - 1.0,2.0 - abs(HSL.x * 6.0 - 2.0),2.0 - abs(HSL.x * 6.0 - 4.0)));
  141.         float C = (1 - abs(2 * HSL.z - 1)) * HSL.y;
  142.         return (RGB - 0.5) * C + HSL.z;
  143. }
  144.  
  145. float3 colorAdjust(float3 cRGB)
  146. {
  147.     float3 cHSL = RGB2HSL(cRGB);
  148.  
  149.     float cr = 1.0 / ShdLevels;
  150.  
  151.     // brightness modifier
  152.     float BrtModify = mod(cHSL.z, cr);
  153.  
  154.     if     (cHSL.z > WhtCutoff) { cHSL.y  = 1.0; cHSL.z = 1.0; }
  155.     else if(cHSL.z > BlkCutoff) { cHSL.y *= SatModify; cHSL.z += (cHSL.z * cr - BrtModify); }
  156.     else                         { cHSL.y  = 0.0; cHSL.z = 0.0; }
  157.     cRGB = 1.2 * HSL2RGB(cHSL);
  158.  
  159.     return cRGB;
  160. }
  161.  
  162. void PS_MMJCel(in float4 pos : SV_POSITION, in float2 texcoord : TEXCOORD0, out float4 fragColor : COLOR0)
  163. {
  164.  
  165.     float2 offset = float2(0.0,0.0);
  166.     float2 offset_inv = float2(0.0,0.0);
  167.     float2 TEX0 = texcoord.xy;
  168.     float2 TEX1 = float2(0.0,0.0);
  169.     float2 TEX1_INV = float2(0.0,0.0);
  170.     float2 TEX2 = float2(0.0,0.0);
  171.     float2 TEX2_INV = float2(0.0,0.0);
  172.     float2 TEX3 = float2(0.0,0.0);
  173.     float2 TEX3_INV = float2(0.0,0.0);
  174.    
  175.     offset = -(float2(1.0 / ReShade::ScreenSize.x, 0.0)); //XY
  176.     offset_inv = float2(1.0 / ReShade::ScreenSize.x,0.0); //ZW
  177.     TEX1 = TEX0 + offset;
  178.     TEX1_INV = TEX0 + offset_inv;
  179.    
  180.     offset = -(float2(0.0,(1.0 / ReShade::ScreenSize.y))); //XY
  181.     offset_inv = float2(0.0, (1.0 / ReShade::ScreenSize.y)); //ZW
  182.    
  183.     TEX2 = TEX0.xy + offset;
  184.     TEX2_INV = TEX0.xy + offset_inv;
  185.     TEX3 = TEX1.xy + offset;
  186.     TEX3_INV = TEX1.xy + offset_inv;
  187.    
  188.     float3 c0 = tex2D(RetroArchSRGB, TEX3.xy).rgb;
  189.     float3 c1 = tex2D(RetroArchSRGB, TEX2.xy).rgb;
  190.     float3 c2 = tex2D(RetroArchSRGB, float2(TEX3_INV.x,TEX3.y)).rgb;
  191.     float3 c3 = tex2D(RetroArchSRGB, TEX1.xy).rgb;
  192.     float3 c4 = tex2D(RetroArchSRGB, TEX0.xy).rgb;
  193.     float3 c5 = tex2D(RetroArchSRGB, TEX1_INV.xy).rgb;
  194.     float3 c6 = tex2D(RetroArchSRGB, float2(TEX3.x,TEX3_INV.y)).rgb;
  195.     float3 c7 = tex2D(RetroArchSRGB, TEX2_INV).rgb;
  196.     float3 c8 = tex2D(RetroArchSRGB, TEX3_INV).rgb;
  197.     float3 c9 = ((c0 + c2 + c6 + c8) * 0.15 + (c1 + c3 + c5 + c7) * 0.25 + c4) / 2.6;
  198.  
  199.     float3 o = float3(1.0,1.0,1.0);
  200.     float3 h = float3(0.05,0.05,0.05);
  201.     float3 hz = h;
  202.     float k = 0.005;
  203.     float kz = 0.007;
  204.     float i = 0.0;
  205.  
  206.     float3 cz = (c9 + h) / (dot(o, c9) + k);
  207.     float3 col = float3(0.0,0.0,0.0);
  208.  
  209.     hz = (cz - ((c0 + h) / (dot(o, c0) + k))); i  = kz / (dot(hz, hz) + kz);
  210.     hz = (cz - ((c1 + h) / (dot(o, c1) + k))); i += kz / (dot(hz, hz) + kz);
  211.     hz = (cz - ((c2 + h) / (dot(o, c2) + k))); i += kz / (dot(hz, hz) + kz);
  212.     hz = (cz - ((c3 + h) / (dot(o, c3) + k))); i += kz / (dot(hz, hz) + kz);
  213.     hz = (cz - ((c5 + h) / (dot(o, c5) + k))); i += kz / (dot(hz, hz) + kz);
  214.     hz = (cz - ((c6 + h) / (dot(o, c6) + k))); i += kz / (dot(hz, hz) + kz);
  215.     hz = (cz - ((c7 + h) / (dot(o, c7) + k))); i += kz / (dot(hz, hz) + kz);
  216.     hz = (cz - ((c8 + h) / (dot(o, c8) + k))); i += kz / (dot(hz, hz) + kz);
  217.  
  218.     i /= 8.0;
  219.     i = pow(i, 0.75);
  220.  
  221.     if(i < OtlModify) { i = OtlModify; }
  222.     c9 = min(o, min(c9, c9 + dot(o, c9)));
  223.     col = lerp(c4 * i, colorAdjust(c9 * i), ShdWeight);
  224.     fragColor = float4(col,1.0);
  225. }
  226.  
  227. technique MMJCelShader {
  228.     pass MMJCelShader {
  229.         VertexShader=PostProcessVS;
  230.         PixelShader=PS_MMJCel;
  231.         SRGBWriteEnable = true;
  232.     }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement