Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2017
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5. #include "ReShade.fxh"
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12. texture2D texColor : COLOR;
  13. texture texHDR { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; MipLevels = 5; Format = RGBA16;};
  14. texture texNormals { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; MipLevels = 2; Format = RGBA16;};
  15. texture2D texDepth : DEPTH;
  16. sampler2D SamplerColor
  17. {
  18. Texture = texColor;
  19. MinFilter = LINEAR;
  20. MagFilter = LINEAR;
  21. MipFilter = LINEAR;
  22. AddressU = Clamp;
  23. AddressV = Clamp;
  24. };
  25. sampler2D SamplerHDR
  26. {
  27. Texture = texHDR;
  28. MinFilter = LINEAR;
  29. MagFilter = LINEAR;
  30. MipFilter = LINEAR;
  31. AddressU = Clamp;
  32. AddressV = Clamp;
  33. };
  34. sampler2D SamplerNormals
  35. {
  36. Texture = texNormals;
  37. MinFilter = LINEAR;
  38. MagFilter = LINEAR;
  39. MipFilter = LINEAR;
  40. AddressU = Clamp;
  41. AddressV = Clamp;
  42. };
  43.  
  44. sampler2D SamplerDepth
  45. {
  46. Texture = texDepth;
  47. MinFilter = LINEAR;
  48. MagFilter = LINEAR;
  49. MipFilter = LINEAR;
  50. AddressU = Clamp;
  51. AddressV = Clamp;
  52. };
  53.  
  54.  
  55. uniform float Timer < source = "timer"; >;
  56. #define ScreenSize float4(BUFFER_WIDTH, BUFFER_RCP_WIDTH, float(BUFFER_WIDTH) / float(BUFFER_HEIGHT), float(BUFFER_HEIGHT) / float(BUFFER_WIDTH)) //x=Width, y=1/Width, z=ScreenScaleY, w=1/ScreenScaleY
  57. //#define PixelSize float2(BUFFER_RCP_WIDTH, BUFFER_RCP_HEIGHT)
  58.  
  59. struct VS_OUTPUT_POST
  60. {
  61. float4 vpos : POSITION;
  62. float2 txcoord : TEXCOORD0;
  63. };
  64.  
  65. VS_OUTPUT_POST VS_PostProcess(in uint id : SV_VertexID)
  66. {
  67. VS_OUTPUT_POST OUT;
  68. OUT.txcoord.x = (id == 2) ? 2.0 : 0.0;
  69. OUT.txcoord.y = (id == 1) ? 2.0 : 0.0;
  70. OUT.vpos = float4(OUT.txcoord * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);
  71. return OUT;
  72. }
  73.  
  74. float GetLinearDepth(float2 coords)
  75. {
  76. return ReShade::GetLinearizedDepth(coords.xy);
  77. }
  78.  
  79. float3 GetPosition(float2 coords)
  80. {
  81. return float3(coords.xy*2.0-1.0,1.0)*GetLinearDepth(coords.xy)*RESHADE_DEPTH_LINEARIZATION_FAR_PLANE;
  82. }
  83.  
  84. float3 GetNormalFromDepth(float2 coords)
  85. {
  86. float3 offs = float3(ReShade::PixelSize.xy,0);
  87.  
  88. float3 f = GetPosition(coords.xy);
  89. float3 d_dx1 = - f + GetPosition(coords.xy + offs.xz);
  90. float3 d_dx2 = f - GetPosition(coords.xy - offs.xz);
  91. float3 d_dy1 = - f + GetPosition(coords.xy + offs.zy);
  92. float3 d_dy2 = f - GetPosition(coords.xy - offs.zy);
  93.  
  94. d_dx1 = lerp(d_dx1, d_dx2, abs(d_dx1.z) > abs(d_dx2.z));
  95. d_dy1 = lerp(d_dy1, d_dy2, abs(d_dy1.z) > abs(d_dy2.z));
  96.  
  97. return -normalize(cross(d_dy1,d_dx1));
  98. }
  99.  
  100.  
  101. float4 PS_Init(VS_OUTPUT_POST IN) : COLOR
  102. {
  103. float4 res = tex2D(SamplerColor, IN.txcoord.xy);
  104. float lineardepth = GetLinearDepth(IN.txcoord.xy);
  105.  
  106. float resgray = dot(res.xyz,0.333);
  107.  
  108. res.a = resgray;
  109.  
  110. return res;
  111.  
  112. }
  113.  
  114. float2 ParallaxOcclusionMap(in float2 baseTC, in float3 viewDirNrm, in int numSteps, in float displacement) {
  115. float step = 1.0 / (float)numSteps;
  116. float bumpScale = displacement;
  117.  
  118. float2 delta = -float2(viewDirNrm.x, viewDirNrm.y) * bumpScale / ( numSteps);
  119.  
  120. float NB0 = tex2D(SamplerHDR, baseTC).w;
  121.  
  122. float height = 1 - step;
  123. float4 offset = float4(baseTC + delta, 0, 1);
  124. float NB1 = tex2D(SamplerHDR, offset.xy).w;
  125.  
  126. int i;
  127.  
  128. for (i = 0; i < numSteps; i++) {
  129. [flatten]
  130. if (NB1 >= height)
  131. break;
  132.  
  133. NB0 = NB1;
  134.  
  135. height -= step;
  136. offset.xy += delta;
  137.  
  138. NB1 = tex2Dlod(SamplerHDR, offset).w;
  139. }
  140.  
  141. float4 offsetBest = offset;
  142. float error = 1.0;
  143.  
  144. float t1 = height;
  145. float t0 = t1 + step;
  146.  
  147. float delta1 = t1 - NB1;
  148. float delta0 = t0 - NB0;
  149.  
  150. float4 intersect = float4(delta * numSteps, delta * numSteps + baseTC);
  151.  
  152. for (i = 0; i < 10; i++) {
  153. [flatten]
  154. if (abs(error) <= 0.01)
  155. break;
  156.  
  157. float denom = delta1 - delta0;
  158. float t = (t0 * delta1 - t1 * delta0) / denom;
  159. offsetBest.xy = -t * intersect.xy + intersect.zw;
  160.  
  161. float NB = tex2Dlod(SamplerHDR, offsetBest).w;
  162.  
  163. error = t - NB;
  164. if (error < 0) {
  165. delta1 = error;
  166. t1 = t;
  167. }
  168. else {
  169. delta0 = error;
  170. t0 = t;
  171. }
  172. }
  173.  
  174. return offsetBest.xy;
  175. }
  176.  
  177. float4 PS_Normals(VS_OUTPUT_POST IN) : COLOR
  178. {
  179. float3 screenNormals = GetNormalFromDepth(IN.txcoord.xy);
  180. /* screenNormals.y = -screenNormals.y;*/
  181. screenNormals.xyz = screenNormals.xyz*0.5+0.5;
  182.  
  183. return float4(screenNormals.xyz , 1.0);
  184. }
  185.  
  186. float4 PS_Emboss(VS_OUTPUT_POST IN) : COLOR
  187. {
  188. float4 color = tex2D(SamplerHDR, IN.txcoord.xy);
  189.  
  190. float2 texcoord = IN.txcoord.xy;
  191. float3 position = GetPosition(texcoord.xy);
  192. float3 scenenormals = tex2D(SamplerNormals, IN.txcoord.xy).xyz*2.0-1.0;
  193. float2 offsetBest = ParallaxOcclusionMap(IN.txcoord.xy, scenenormals.xyz, 20, 0.4/position.z);
  194.  
  195.  
  196. float3 offsetPosition = GetPosition(offsetBest);
  197.  
  198.  
  199. //offsetBest = lerp(offsetBest,texcoord.xy,length(offsetPosition-position) > 0.4);
  200.  
  201.  
  202.  
  203. color = tex2D(SamplerHDR, offsetBest.xy);
  204.  
  205. return color;
  206. }
  207.  
  208. technique Emboss < bool enabled = 1; toggle = 0x20; >
  209. {
  210. pass Init
  211. {
  212. VertexShader = VS_PostProcess;
  213. PixelShader = PS_Init;
  214. RenderTarget = texHDR;
  215. }
  216. pass Normals
  217. {
  218. VertexShader = VS_PostProcess;
  219. PixelShader = PS_Normals;
  220. RenderTarget = texNormals;
  221. }
  222. pass Emboss
  223. {
  224. VertexShader = VS_PostProcess;
  225. PixelShader = PS_Emboss;
  226. }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement