daily pastebin goal
46%
SHARE
TWEET

RBM 3.0 beta

a guest Sep 27th, 2016 42 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. // ReShade effect file
  3. // visit facebook.com/MartyMcModding for news/updates
  4. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  5. // Reflective Bumpmapping "RBM" 3.0 beta by Marty McFly. For private use.
  6. // Copyright © 2008-2016 Marty McFly
  7. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  8.  
  9. uniform float fRBM_BlurWidthPixels <
  10.     ui_type = "drag";
  11.     ui_min = 0.0; ui_max = 400.00;
  12.     ui_step = 1;
  13.     ui_tooltip = "Controls how far the reflections spread. If you get repeating artifacts, lower this or raise sample count.";
  14. > = 100.0;
  15.  
  16. uniform int iRBM_SampleCount <
  17.     ui_type = "drag";
  18.     ui_min = 16; ui_max = 128;
  19.     ui_tooltip = "Controls how many glossy reflection samples are taken. Raise this if you get repeating artifacts. Performance hit.";
  20. > = 32;
  21.  
  22. uniform float fRBM_ReliefHeight <
  23.     ui_type = "drag";
  24.     ui_min = 0.0; ui_max = 2.00;
  25.     ui_tooltip = "Controls how intensive the relief on surfaces is. 0.0 means mirror-like reflections.";
  26. > = 0.3;
  27.  
  28. uniform float fRBM_FresnelReflectance <
  29.     ui_type = "drag";
  30.     ui_min = 0.0; ui_max = 1.00;
  31.     ui_tooltip = "The lower this value, the lower the view to surface angle has to be to get significant reflection. 1.0 means every surface has 100% gloss.";
  32. > = 0.3;
  33.  
  34. uniform float fRBM_FresnelMult <
  35.     ui_type = "drag";
  36.     ui_min = 0.0; ui_max = 1.00;
  37.     ui_tooltip = "Not physically accurate at all: multiplier of reflection intensity at lowest view-surface angle.";
  38. > = 0.5;
  39.  
  40. uniform float  fRBM_LowerThreshold <
  41.     ui_type = "drag";
  42.     ui_min = 0.0; ui_max = 1.00;
  43.     ui_tooltip = "Anything darker than this does not get reflected at all. Reflection power increases linearly from lower to upper threshold. ";
  44. > = 0.1;
  45.  
  46. uniform float  fRBM_UpperThreshold <
  47.     ui_type = "drag";
  48.     ui_min = 0.0; ui_max = 1.00;
  49.     ui_tooltip = "Anything brighter than this contributes fully to reflection. Reflection power increases linearly from lower to upper threshold. ";
  50. > = 0.2;
  51.  
  52. uniform float  fRBM_ColorMask_Red <
  53.     ui_type = "drag";
  54.     ui_min = 0.0; ui_max = 1.00;
  55.     ui_tooltip = "Reflection mult on red surfaces.Lower this to remove reflections from red surfaces.";
  56. > = 1.0;
  57.  
  58. uniform float  fRBM_ColorMask_Orange <
  59.     ui_type = "drag";
  60.     ui_min = 0.0; ui_max = 1.00;
  61.     ui_tooltip = "Reflection mult on orange surfaces. Lower this to remove reflections from orange surfaces.";
  62. > = 1.0;
  63.  
  64. uniform float  fRBM_ColorMask_Yellow <
  65.     ui_type = "drag";
  66.     ui_min = 0.0; ui_max = 1.00;
  67.     ui_tooltip = "Reflection mult on yellow surfaces. Lower this to remove reflections from yellow surfaces.";
  68. > = 1.0;
  69.  
  70. uniform float  fRBM_ColorMask_Green <
  71.     ui_type = "drag";
  72.     ui_min = 0.0; ui_max = 1.00;
  73.     ui_tooltip = "Reflection mult on green surfaces. Lower this to remove reflections from green surfaces.";
  74. > = 1.0;
  75.  
  76. uniform float  fRBM_ColorMask_Cyan <
  77.     ui_type = "drag";
  78.     ui_min = 0.0; ui_max = 1.00;
  79.     ui_tooltip = "Reflection mult on cyan surfaces. Lower this to remove reflections from cyan surfaces.";
  80. > = 1.0;
  81.  
  82. uniform float  fRBM_ColorMask_Blue <
  83.     ui_type = "drag";
  84.     ui_min = 0.0; ui_max = 1.00;
  85.     ui_tooltip = "Reflection mult on blue surfaces. Lower this to remove reflections from blue surfaces.";
  86. > = 1.0;
  87.  
  88. uniform float  fRBM_ColorMask_Magenta <
  89.     ui_type = "drag";
  90.     ui_min = 0.0; ui_max = 1.00;
  91.     ui_tooltip = "Reflection mult on magenta surfaces. Lower this to remove reflections from magenta surfaces.";
  92. > = 1.0;
  93.  
  94. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  95. //
  96. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  97.  
  98. #include "ReShade.fxh"
  99.  
  100. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  101. //
  102. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  103. float GetLinearDepth(float2 coords)
  104. {
  105.     return ReShade::GetLinearizedDepth(coords);
  106. }
  107.  
  108. float3 GetPosition(float2 coords)
  109. {
  110.     float EyeDepth = GetLinearDepth(coords.xy)*RESHADE_DEPTH_LINEARIZATION_FAR_PLANE;
  111.     return float3((coords.xy * 2.0 - 1.0)*EyeDepth,EyeDepth);
  112. }
  113.  
  114. float3 GetNormalFromDepth(float2 coords)
  115. {
  116.     float3 centerPos = GetPosition(coords.xy);
  117.     float2 offs = ReShade::PixelSize.xy*1.0;
  118.     float3 ddx1 = GetPosition(coords.xy + float2(offs.x, 0)) - centerPos;
  119.     float3 ddx2 = centerPos - GetPosition(coords.xy + float2(-offs.x, 0));
  120.  
  121.     float3 ddy1 = GetPosition(coords.xy + float2(0, offs.y)) - centerPos;
  122.     float3 ddy2 = centerPos - GetPosition(coords.xy + float2(0, -offs.y));
  123.  
  124.     ddx1 = lerp(ddx1, ddx2, abs(ddx1.z) > abs(ddx2.z));
  125.     ddy1 = lerp(ddy1, ddy2, abs(ddy1.z) > abs(ddy2.z));
  126.  
  127.     float3 normal = cross(ddy1, ddx1);
  128.    
  129.     return normalize(normal);
  130. }
  131.  
  132. float3 GetNormalFromColor(float2 coords, float2 offset, float scale, float sharpness)
  133. {
  134.     const float3 lumCoeff = float3(0.299,0.587,0.114);
  135.  
  136.         float hpx = dot(tex2Dlod(ReShade::BackBuffer, float4(coords + float2(offset.x,0.0),0,0)).xyz,lumCoeff) * scale;
  137.         float hmx = dot(tex2Dlod(ReShade::BackBuffer, float4(coords - float2(offset.x,0.0),0,0)).xyz,lumCoeff) * scale;
  138.         float hpy = dot(tex2Dlod(ReShade::BackBuffer, float4(coords + float2(0.0,offset.y),0,0)).xyz,lumCoeff) * scale;
  139.         float hmy = dot(tex2Dlod(ReShade::BackBuffer, float4(coords - float2(0.0,offset.y),0,0)).xyz,lumCoeff) * scale;
  140.  
  141.         float dpx = GetLinearDepth(coords + float2(offset.x,0.0));
  142.         float dmx = GetLinearDepth(coords - float2(offset.x,0.0));
  143.         float dpy = GetLinearDepth(coords + float2(0.0,offset.y));
  144.         float dmy = GetLinearDepth(coords - float2(0.0,offset.y));
  145.  
  146.     float2 xymult = float2(abs(dmx - dpx), abs(dmy - dpy)) * sharpness;
  147.     xymult = max(0.0, 1.0 - xymult);
  148.        
  149.         float ddx = (hmx - hpx) / (2.0 * offset.x) * xymult.x;
  150.         float ddy = (hmy - hpy) / (2.0 * offset.y) * xymult.y;
  151.    
  152.         return normalize(float3(ddx, ddy, 1.0));
  153. }
  154.  
  155. float3 GetBlendedNormals(float3 n1, float3 n2)
  156. {
  157.      return normalize(float3(n1.xy*n2.z + n2.xy*n1.z, n1.z*n2.z));
  158. }
  159.  
  160. float3 RGB2HSV(float3 RGB)
  161. {
  162.         float4 K = float4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
  163.         float4 p = RGB.g < RGB.b ? float4(RGB.bg, K.wz) : float4(RGB.gb, K.xy);
  164.         float4 q = RGB.r < p.x ? float4(p.xyw, RGB.r) : float4(RGB.r, p.yzx);
  165.  
  166.         float d = q.x - min(q.w, q.y);
  167.         float e = 1.0e-10;
  168.         return float3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
  169. }
  170.  
  171. float3 HSV2RGB(float3 HSV)
  172. {
  173.         float4 K = float4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
  174.         float3 p = abs(frac(HSV.xxx + K.xyz) * 6.0 - K.www);
  175.         return HSV.z * lerp(K.xxx, saturate(p - K.xxx), HSV.y); //HDR capable
  176. }
  177.  
  178. float GetHueMask(in float H)   
  179. {
  180.     float SMod = 0.0;
  181.     SMod += fRBM_ColorMask_Red * ( 1.0 - min( 1.0, abs( H / 0.08333333 ) ) );
  182.     SMod += fRBM_ColorMask_Orange * ( 1.0 - min( 1.0, abs( ( 0.08333333 - H ) / ( - 0.08333333 ) ) ) );
  183.     SMod += fRBM_ColorMask_Yellow * ( 1.0 - min( 1.0, abs( ( 0.16666667 - H ) / ( - 0.16666667 ) ) ) );
  184.     SMod += fRBM_ColorMask_Green * ( 1.0 - min( 1.0, abs( ( 0.33333333 - H ) / 0.16666667 ) ) );
  185.     SMod += fRBM_ColorMask_Cyan * ( 1.0 - min( 1.0, abs( ( 0.5 - H ) / 0.16666667 ) ) );
  186.     SMod += fRBM_ColorMask_Blue * ( 1.0 - min( 1.0, abs( ( 0.66666667 - H ) / 0.16666667 ) ) );
  187.     SMod += fRBM_ColorMask_Magenta * ( 1.0 - min( 1.0, abs( ( 0.83333333 - H ) / 0.16666667 ) ) );
  188.     SMod += fRBM_ColorMask_Red * ( 1.0 - min( 1.0, abs( ( 1.0 - H ) / 0.16666667 ) ) );
  189.     return SMod;
  190. }
  191.  
  192. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  193. //
  194. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  195.  
  196. void PS_RBM_Gen(float4 vpos : SV_Position, float2 texcoord : TEXCOORD, out float4 res : SV_Target0)
  197. {
  198.     float scenedepth        = GetLinearDepth(texcoord.xy);
  199.     float3 SurfaceNormals       = GetNormalFromDepth(texcoord.xy).xyz;
  200.     float3 TextureNormals       = GetNormalFromColor(texcoord.xy, 0.01 * ReShade::PixelSize.xy / scenedepth, 0.0002 / scenedepth + 0.1, 1000.0);
  201.     float3 SceneNormals     = GetBlendedNormals(SurfaceNormals, TextureNormals);
  202.     SceneNormals            = normalize(lerp(SurfaceNormals,SceneNormals,fRBM_ReliefHeight));
  203.     float3 ScreenSpacePosition  = GetPosition(texcoord.xy);
  204.     float3 ViewDirection        = normalize(ScreenSpacePosition.xyz);
  205.  
  206.     float4 color = tex2D(ReShade::BackBuffer, texcoord.xy);
  207.     float3 bump = 0.0;
  208.  
  209.     for(float i=1; i<=iRBM_SampleCount; i++)
  210.     {
  211.         float2 currentOffset    = texcoord.xy + SceneNormals.xy * ReShade::PixelSize.xy * i/(float)iRBM_SampleCount * fRBM_BlurWidthPixels;
  212.         float4 texelSample  = tex2Dlod(ReShade::BackBuffer, float4(currentOffset,0,0));
  213.        
  214.         float depthDiff     = smoothstep(0.005,0.0,scenedepth-GetLinearDepth(currentOffset));
  215.         float colorWeight   = smoothstep(fRBM_LowerThreshold,fRBM_UpperThreshold+0.00001,dot(texelSample.xyz,float3(0.299,0.587,0.114)));
  216.         bump += lerp(color.xyz,texelSample.xyz,depthDiff*colorWeight);
  217.     }
  218.  
  219.     bump /= iRBM_SampleCount;
  220.  
  221.     float cosphi = dot(-ViewDirection, SceneNormals);
  222.     //R0 + (1.0 - R0)*(1.0-cosphi)^5;
  223.     float SchlickReflectance = lerp(pow(1.0-cosphi,5.0), 1.0, fRBM_FresnelReflectance);
  224.     SchlickReflectance = saturate(SchlickReflectance)*fRBM_FresnelMult; // *should* be 0~1 but isn't for some pixels.
  225.  
  226.     float3 hsvcol = RGB2HSV(color.xyz);
  227.     float colorMask = GetHueMask(hsvcol.x);
  228.     colorMask = lerp(1.0,colorMask, smoothstep(0.0,0.2,hsvcol.y) * smoothstep(0.0,0.1,hsvcol.z));
  229.     color.xyz = lerp(color.xyz,bump.xyz,SchlickReflectance*colorMask);
  230.  
  231.     res.xyz = color.xyz;
  232.     res.w = 1.0;
  233.  
  234. }
  235.  
  236. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  237. //
  238. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  239.  
  240. technique ReflectiveBumpmapping
  241. {
  242.     pass P1
  243.     {
  244.         VertexShader = PostProcessVS;
  245.         PixelShader  = PS_RBM_Gen;
  246.     }
  247. }
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