Advertisement
Guest User

SSRLib.cginc

a guest
May 26th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. //The MIT License(MIT)
  2.  
  3. //Copyright(c) 2016 Charles Greivelding Thomas
  4.  
  5. //Permission is hereby granted, free of charge, to any person obtaining a copy
  6. //of this software and associated documentation files (the "Software"), to deal
  7. //in the Software without restriction, including without limitation the rights
  8. //to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. //copies of the Software, and to permit persons to whom the Software is
  10. //furnished to do so, subject to the following conditions:
  11.  
  12. //The above copyright notice and this permission notice shall be included in all
  13. //copies or substantial portions of the Software.
  14.  
  15. //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. //FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. //AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. //LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. //SOFTWARE.
  22.  
  23. #include "UnityStandardBRDF.cginc"
  24.  
  25. // Lux
  26. #include "../../../../../Lux 2.02 Plus/Lux Shaders/Lux Core/Lux Utils/LuxUtilsDeferred.cginc"
  27.  
  28. #define PI 3.141592
  29.  
  30. uniform sampler2D _MainTex,
  31. _ReflectionBuffer,
  32. _PreviousBuffer,
  33. _RayCast,
  34. _RayCastMask;
  35.  
  36. uniform sampler2D _Noise;
  37.  
  38. uniform sampler2D _CameraGBufferTexture0,
  39. _CameraGBufferTexture1,
  40. _CameraGBufferTexture2,
  41. _CameraReflectionsTexture;
  42.  
  43. uniform sampler2D _CameraDepthTexture; // Unity depth
  44. uniform sampler2D _CameraDepthBuffer;
  45. uniform sampler2D_half _CameraMotionVectorsTexture;
  46.  
  47. uniform float4 _MainTex_TexelSize;
  48. uniform float4 _ScreenSize;
  49. uniform float4 _RayCastSize;
  50. uniform float4 _ResolveSize;
  51. uniform float4 _NoiseSize;
  52. uniform float4 _Project;
  53. uniform float4 _GaussianDir;
  54. uniform float4 _JitterSizeAndOffset; // x = jitter width / screen width, y = jitter height / screen height, z = random offset, w = random offset
  55.  
  56. uniform float _EdgeFactor;
  57. uniform float _SmoothnessRange;
  58. uniform float _BRDFBias;
  59.  
  60. uniform float _Thickness;
  61. uniform float _TScale;
  62. uniform float _TMinResponse;
  63. uniform float _TMaxResponse;
  64. uniform float _TResponse;
  65. uniform float _StepSize;
  66. uniform float _Accumulation;
  67. uniform int _NumSteps;
  68. uniform int _RayReuse;
  69. uniform int _MipMapCount;
  70. uniform int _ReflectionVelocity;
  71.  
  72. uniform float4x4 _ProjectionMatrix;
  73. uniform float4x4 _ViewProjectionMatrix;
  74. uniform float4x4 _InverseProjectionMatrix;
  75. uniform float4x4 _InverseViewProjectionMatrix;
  76. uniform float4x4 _WorldToCameraMatrix;
  77. uniform float4x4 _CameraToWorldMatrix;
  78.  
  79. uniform float4x4 _PrevInverseViewProjectionMatrix;
  80. uniform float4x4 _PrevViewProjectionMatrix;
  81.  
  82. //Debug Options
  83. uniform int _UseTemporal;
  84. uniform int _UseFresnel;
  85. uniform int _DebugPass;
  86. uniform int _UseNormalization;
  87. uniform int _Fireflies;
  88. uniform int _MaxMipMap;
  89.  
  90. float sqr(float x)
  91. {
  92. return x*x;
  93. }
  94.  
  95. float fract(float x)
  96. {
  97. return x - floor( x );
  98. }
  99.  
  100. float4 GetSampleColor (sampler2D tex, float2 uv) { return tex2D(tex, uv); }
  101. float4 GetCubeMap (float2 uv) { return tex2D(_CameraReflectionsTexture, uv); }
  102. float4 GetAlbedo (float2 uv) { return tex2D(_CameraGBufferTexture0, uv); }
  103. float4 GetSpecular (float2 uv)
  104. {
  105. // Lux
  106. float4 gbuffer0 = tex2D(_CameraGBufferTexture0, uv);
  107. float4 normSample = tex2D(_CameraGBufferTexture2, uv);
  108. half materialIndex = GetLuxMaterialIndexFull(normSample.a);
  109. float4 specSmoothnessSample = tex2D(_CameraGBufferTexture1, uv);
  110. float3 specColor = GetLuxSpecular(specSmoothnessSample.rgb, gbuffer0, normSample.b, materialIndex);
  111.  
  112. return float4(specColor,specSmoothnessSample.a);
  113. // End Lux
  114.  
  115. //return tex2D(_CameraGBufferTexture1, uv);
  116. }
  117.  
  118. float GetRoughness (float smoothness) { return max(min(_SmoothnessRange, 1 - smoothness), 0.05f); }
  119.  
  120. float4 GetNormal (float2 uv)
  121. {
  122. // Lux
  123. float4 normSample = tex2D(_CameraGBufferTexture2, uv);
  124. half materialIndex = GetLuxMaterialIndexFull(normSample.a);
  125. float3 wsNormal = GetLuxNormal(normSample, materialIndex, uv);
  126.  
  127. return float4(wsNormal,0);
  128. // End Lux
  129.  
  130. //float4 gbuffer2 = tex2D(_CameraGBufferTexture2, uv);
  131. //return gbuffer2*2-1;
  132. }
  133.  
  134. float4 GetVelocity(float2 uv) { return tex2D(_CameraMotionVectorsTexture, uv); }
  135. float4 GetReflection(float2 uv) { return tex2D(_ReflectionBuffer, uv); }
  136.  
  137. float ComputeDepth(float4 clippos)
  138. {
  139. #if defined(SHADER_TARGET_GLSL) || defined(SHADER_API_GLES) || defined(SHADER_API_GLES3)
  140. return (clippos.z / clippos.w) * 0.5 + 0.5;
  141. #else
  142. return clippos.z / clippos.w;
  143. #endif
  144. }
  145.  
  146. float3 GetViewNormal (float3 normal)
  147. {
  148. float3 viewNormal = mul((float3x3)_WorldToCameraMatrix, normal.rgb);
  149. return normalize(viewNormal);
  150. }
  151.  
  152. /*float GetDepth (sampler2D tex, float2 uv)
  153. {
  154. return UNITY_SAMPLE_DEPTH (tex2Dlod(tex, float4(uv, 0, 0)));
  155. }*/
  156.  
  157.  
  158. float GetDepth (sampler2D tex, float2 uv)
  159. {
  160. float z = tex2Dlod(_CameraDepthTexture, float4(uv,0,0));
  161. #if defined(UNITY_REVERSED_Z)
  162. z = 1.0f - z;
  163. #endif
  164. return z;
  165. }
  166.  
  167. float3 GetScreenPos (float2 uv, float depth)
  168. {
  169. return float3(uv.xy * 2 - 1, depth);
  170. }
  171.  
  172. float3 GetViewRayFromUv(float2 uv)
  173. {
  174. float4 _CamScreenDir = float4(1.0 / _ProjectionMatrix[0][0], 1.0 / _ProjectionMatrix[1][1], 1, 1);
  175. float3 ray = float3(uv.x * 2 - 1, uv.y * 2 - 1, 1);
  176. ray *= _CamScreenDir.xyz;
  177. ray = ray * (_ProjectionParams.z / ray.z);
  178. return ray;
  179. }
  180.  
  181. float3 GetWorlPos (float3 screenPos)
  182. {
  183. float4 worldPos = mul(_InverseViewProjectionMatrix, float4(screenPos, 1));
  184. return worldPos.xyz / worldPos.w;
  185. }
  186.  
  187. float3 GetViewPos (float3 screenPos)
  188. {
  189. float4 viewPos = mul(_InverseProjectionMatrix, float4(screenPos, 1));
  190. return viewPos.xyz / viewPos.w;
  191. }
  192.  
  193. float3 GetViewDir (float3 worldPos)
  194. {
  195. return normalize(worldPos - _WorldSpaceCameraPos);
  196. }
  197.  
  198. // Deprecated since 5.4
  199. /*float2 ReprojectUV(float3 clipPosition)
  200. {
  201. float4 previousClipPosition = mul(_PrevInverseViewProjectionMatrix, float4(clipPosition, 1.0f));
  202. previousClipPosition.xyz /= previousClipPosition.w;
  203.  
  204. return float2(previousClipPosition.xy * 0.5f + 0.5f);
  205. }*/
  206.  
  207. static const float2 offset[4] =
  208. {
  209. float2(0, 0),
  210. float2(2, -2),
  211. float2(-2, -2),
  212. float2(0, 2)
  213. };
  214.  
  215. float RayAttenBorder (float2 pos, float value)
  216. {
  217. float borderDist = min(1.0 - max(pos.x, pos.y), min(pos.x, pos.y));
  218. return saturate(borderDist > value ? 1.0 : borderDist / value);
  219. }
  220.  
  221. inline half2 CalculateMotion(float rawDepth, float2 inUV)
  222. {
  223. float3 screenPos = GetScreenPos(inUV, rawDepth);
  224. float4 worldPos = float4(GetWorlPos(screenPos),1);
  225.  
  226. float4 prevClipPos = mul(_PrevViewProjectionMatrix, worldPos);
  227. float4 curClipPos = mul(_ViewProjectionMatrix, worldPos);
  228.  
  229. float2 prevHPos = prevClipPos.xy / prevClipPos.w;
  230. float2 curHPos = curClipPos.xy / curClipPos.w;
  231.  
  232. // V is the viewport position at this pixel in the range 0 to 1.
  233. float2 vPosPrev = (prevHPos.xy + 1.0f) / 2.0f;
  234. float2 vPosCur = (curHPos.xy + 1.0f) / 2.0f;
  235. return vPosCur - vPosPrev;
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement