Advertisement
Guest User

SEGI.shader for Lux

a guest
Sep 13th, 2018
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.36 KB | None | 0 0
  1. Shader "Hidden/SEGI" {
  2. Properties {
  3. _MainTex ("Base (RGB)", 2D) = "white" {}
  4. }
  5.  
  6. CGINCLUDE
  7. #include "UnityCG.cginc"
  8. #include "SEGI.cginc"
  9. // for Lux
  10. #include "../Lux 2.02 Plus/Lux Shaders/Lux Core/Lux Utils/LuxUtilsDeferred.cginc"
  11. #pragma target 5.0
  12.  
  13.  
  14. struct v2f
  15. {
  16. float4 pos : SV_POSITION;
  17. float4 uv : TEXCOORD0;
  18.  
  19. #if UNITY_UV_STARTS_AT_TOP
  20. half4 uv2 : TEXCOORD1;
  21. #endif
  22. };
  23.  
  24. v2f vert(appdata_img v)
  25. {
  26. v2f o;
  27.  
  28. o.pos = UnityObjectToClipPos (v.vertex);
  29. o.uv = float4(v.texcoord.xy, 1, 1);
  30.  
  31. #if UNITY_UV_STARTS_AT_TOP
  32. o.uv2 = float4(v.texcoord.xy, 1, 1);
  33. if (_MainTex_TexelSize.y < 0.0)
  34. o.uv.y = 1.0 - o.uv.y;
  35. #endif
  36.  
  37. return o;
  38. }
  39.  
  40. #define PI 3.147159265
  41.  
  42.  
  43. ENDCG
  44.  
  45.  
  46. SubShader
  47. {
  48. ZTest Off
  49. Cull Off
  50. ZWrite Off
  51. Fog { Mode off }
  52.  
  53. Pass //0
  54. {
  55. CGPROGRAM
  56. #pragma vertex vert
  57. #pragma fragment frag
  58.  
  59. float4x4 CameraToWorld;
  60.  
  61. sampler2D _CameraGBufferTexture2;
  62.  
  63.  
  64. int FrameSwitch;
  65. int HalfResolution;
  66.  
  67.  
  68. sampler3D SEGIVolumeTexture1;
  69.  
  70. sampler2D NoiseTexture;
  71.  
  72.  
  73.  
  74. float4 GetNormal (float2 uv) // texture2
  75. {
  76. // Lux
  77. float4 normSample = tex2D(_CameraGBufferTexture2, uv);
  78. half materialIndex = GetLuxMaterialIndexFull(normSample.a);
  79. float3 wsNormal = GetLuxNormal(normSample, materialIndex, uv);
  80.  
  81. return float4(wsNormal,0);
  82. // End Lux
  83. }
  84.  
  85.  
  86. float4 frag(v2f input) : SV_Target
  87. {
  88. #if UNITY_UV_STARTS_AT_TOP
  89. float2 coord = input.uv2.xy;
  90. #else
  91. float2 coord = input.uv.xy;
  92. #endif
  93.  
  94. //Get view space position and view vector
  95. float4 viewSpacePosition = GetViewSpacePosition(coord);
  96. float3 viewVector = normalize(viewSpacePosition.xyz);
  97.  
  98. //Get voxel space position
  99. float4 voxelSpacePosition = mul(CameraToWorld, viewSpacePosition);
  100. voxelSpacePosition = mul(SEGIWorldToVoxel, voxelSpacePosition);
  101. voxelSpacePosition = mul(SEGIVoxelProjection, voxelSpacePosition);
  102. voxelSpacePosition.xyz = voxelSpacePosition.xyz * 0.5 + 0.5;
  103.  
  104.  
  105. //Prepare for cone trace
  106. float2 dither = rand(coord + (float)FrameSwitch * 0.011734);
  107.  
  108. //float3 worldNormal = normalize(tex2D(_CameraGBufferTexture2, coord).rgb * 2.0 - 1.0);
  109. float3 worldNormal = normalize(GetNormal(coord).rgb); // Lux
  110.  
  111. float3 voxelOrigin = voxelSpacePosition.xyz + worldNormal.xyz * 0.003 * ConeTraceBias * 1.25 / SEGIVoxelScaleFactor;
  112.  
  113. float3 gi = float3(0.0, 0.0, 0.0);
  114. float4 traceResult = float4(0,0,0,0);
  115.  
  116. const float phi = 1.618033988;
  117. const float gAngle = phi * PI * 1.0;
  118.  
  119. //Get blue noise
  120. float2 noiseCoord = (input.uv.xy * _MainTex_TexelSize.zw) / (64.0).xx;
  121. float blueNoise = tex2Dlod(NoiseTexture, float4(noiseCoord, 0.0, 0.0)).x;
  122.  
  123. //Trace GI cones
  124. int numSamples = TraceDirections;
  125. for (int i = 0; i < numSamples; i++)
  126. {
  127. float fi = (float)i + blueNoise * StochasticSampling;
  128. float fiN = fi / numSamples;
  129. float longitude = gAngle * fi;
  130. float latitude = asin(fiN * 2.0 - 1.0);
  131.  
  132. float3 kernel;
  133. kernel.x = cos(latitude) * cos(longitude);
  134. kernel.z = cos(latitude) * sin(longitude);
  135. kernel.y = sin(latitude);
  136.  
  137. kernel = normalize(kernel + worldNormal.xyz * 1.0);
  138.  
  139. traceResult += ConeTrace(voxelOrigin.xyz, kernel.xyz, worldNormal.xyz, coord, dither.y, TraceSteps, ConeSize, 1.0, 1.0);
  140. }
  141.  
  142. traceResult /= numSamples;
  143. gi = traceResult.rgb * 20.0;
  144.  
  145.  
  146. float fadeout = saturate((distance(voxelSpacePosition.xyz, float3(0.5, 0.5, 0.5)) - 0.5f) * 5.0);
  147.  
  148. float3 fakeGI = saturate(dot(worldNormal, float3(0, 1, 0)) * 0.5 + 0.5) * SEGISkyColor.rgb * 5.0;
  149.  
  150. gi.rgb = lerp(gi.rgb, fakeGI, fadeout);
  151.  
  152. gi *= 0.75 + (float)HalfResolution * 0.25;
  153.  
  154.  
  155. return float4(gi, 1.0);
  156. }
  157.  
  158. ENDCG
  159. }
  160.  
  161. Pass //1 Bilateral Blur
  162. {
  163. CGPROGRAM
  164. #pragma vertex vert
  165. #pragma fragment frag
  166.  
  167. float2 Kernel;
  168.  
  169. float DepthTolerance;
  170.  
  171. sampler2D DepthNormalsLow;
  172. sampler2D _CameraGBufferTexture2;
  173. sampler2D DepthLow;
  174. int SourceScale;
  175.  
  176.  
  177.  
  178. float4 GetNormal (float2 uv) // texture2
  179. {
  180. // Lux
  181. float4 normSample = tex2D(_CameraGBufferTexture2, uv);
  182. half materialIndex = GetLuxMaterialIndexFull(normSample.a);
  183. float3 wsNormal = GetLuxNormal(normSample, materialIndex, uv);
  184.  
  185. return float4(wsNormal,0);
  186. // End Lux
  187. }
  188.  
  189. float4 GetNormalTexel (float4 coordlod) // texture2
  190. {
  191. // Lux
  192. float4 normSample = tex2Dlod(_CameraGBufferTexture2, coordlod);
  193. half materialIndex = GetLuxMaterialIndexFull(normSample.a);
  194. float3 wsNormal = GetLuxNormal(normSample, materialIndex, coordlod.xy);
  195.  
  196. return float4(wsNormal,0);
  197. // End Lux
  198. }
  199.  
  200.  
  201. float4 frag(v2f input) : COLOR0
  202. {
  203. float4 blurred = float4(0.0, 0.0, 0.0, 0.0);
  204. float validWeights = 0.0;
  205. float depth = LinearEyeDepth(tex2D(_CameraDepthTexture, input.uv.xy).x);
  206.  
  207. //half3 normal = normalize(tex2D(_CameraGBufferTexture2, input.uv.xy).rgb * 2.0 - 1.0);
  208. half3 normal = normalize(GetNormal(input.uv.xy).rgb); // Lux
  209.  
  210. float thresh = 0.26;
  211.  
  212. float3 viewPosition = GetViewSpacePosition(input.uv.xy).xyz;
  213. float3 viewVector = normalize(viewPosition);
  214.  
  215. float NdotV = 1.0 / (saturate(dot(-viewVector, normal.xyz)) + 0.1);
  216. thresh *= 1.0 + NdotV * 2.0;
  217.  
  218. for (int i = -4; i <= 4; i++)
  219. {
  220. float2 offs = Kernel.xy * (i) * _MainTex_TexelSize.xy * 1.0;
  221. float sampleDepth = LinearEyeDepth(tex2Dlod(_CameraDepthTexture, float4(input.uv.xy + offs.xy * 1, 0, 0)).x);
  222. //half3 sampleNormal = normalize(tex2Dlod(_CameraGBufferTexture2, float4(input.uv.xy + offs.xy * 1, 0, 0)).rgb * 2.0 - 1.0);
  223. half3 sampleNormal = normalize(GetNormalTexel(float4(input.uv.xy + offs.xy * 1, 0, 0)).rgb); // Lux
  224.  
  225. float weight = saturate(1.0 - abs(depth - sampleDepth) / thresh);
  226. weight *= pow(saturate(dot(sampleNormal, normal)), 24.0);
  227.  
  228. float4 blurSample = tex2Dlod(_MainTex, float4(input.uv.xy + offs.xy, 0, 0)).rgba;
  229. blurred += blurSample * weight;
  230. validWeights += weight;
  231. }
  232.  
  233. blurred /= validWeights + 0.001;
  234.  
  235. return blurred;
  236. }
  237.  
  238. ENDCG
  239. }
  240.  
  241. Pass //2 Blend with scene
  242. {
  243. CGPROGRAM
  244. #pragma vertex vert
  245. #pragma fragment frag
  246.  
  247. sampler2D _CameraGBufferTexture2;
  248. sampler2D _CameraGBufferTexture1;
  249.  
  250. sampler2D GITexture;
  251. sampler2D Reflections;
  252.  
  253.  
  254. float4x4 CameraToWorld;
  255.  
  256. int DoReflections;
  257.  
  258. int HalfResolution;
  259.  
  260.  
  261. float4 GetSpecular (float2 uv) // Texture1
  262. {
  263. // Lux
  264. float4 gbuffer0 = tex2D(_CameraGBufferTexture0, uv);
  265. float4 normSample = tex2D(_CameraGBufferTexture2, uv);
  266. half materialIndex = GetLuxMaterialIndexFull(normSample.a);
  267. float4 specSmoothnessSample = tex2D(_CameraGBufferTexture1, uv);
  268. float3 specColor = GetLuxSpecular(specSmoothnessSample.rgb, gbuffer0, normSample.b, materialIndex);
  269.  
  270. return float4(specColor,specSmoothnessSample.a);
  271. // End Lux
  272. }
  273.  
  274. float4 GetNormal (float2 uv) // texture2
  275. {
  276. // Lux
  277. float4 normSample = tex2D(_CameraGBufferTexture2, uv);
  278. half materialIndex = GetLuxMaterialIndexFull(normSample.a);
  279. float3 wsNormal = GetLuxNormal(normSample, materialIndex, uv);
  280.  
  281. return float4(wsNormal,0);
  282. // End Lux
  283. }
  284.  
  285.  
  286. float4 frag(v2f input) : COLOR0
  287. {
  288. #if UNITY_UV_STARTS_AT_TOP
  289. float2 coord = input.uv2.xy;
  290. #else
  291. float2 coord = input.uv.xy;
  292. #endif
  293. float4 albedoTex = tex2D(_CameraGBufferTexture0, input.uv.xy);
  294. float3 albedo = albedoTex.rgb;
  295. float3 gi = tex2D(GITexture, input.uv.xy).rgb;
  296. float3 scene = tex2D(_MainTex, input.uv.xy).rgb;
  297. float3 reflections = tex2D(Reflections, input.uv.xy).rgb;
  298.  
  299. float3 result = scene + gi * albedoTex.a * albedoTex.rgb;
  300.  
  301. if (DoReflections > 0)
  302. {
  303. float4 viewSpacePosition = GetViewSpacePosition(coord);
  304. float3 viewVector = normalize(viewSpacePosition.xyz);
  305. float4 worldViewVector = mul(CameraToWorld, float4(viewVector.xyz, 0.0));
  306.  
  307. //float4 spec = tex2D(_CameraGBufferTexture1, coord);
  308. float4 spec = GetSpecular(coord); // Lux
  309.  
  310. float smoothness = spec.a;
  311. float3 specularColor = spec.rgb;
  312.  
  313. //float3 worldNormal = normalize(tex2D(_CameraGBufferTexture2, coord).rgb * 2.0 - 1.0);
  314. float3 worldNormal = normalize(GetNormal(coord).rgb); // Lux
  315.  
  316. float3 reflectionKernel = reflect(worldViewVector.xyz, worldNormal);
  317.  
  318. float3 fresnel = pow(saturate(dot(worldViewVector.xyz, reflectionKernel.xyz)) * (smoothness * 0.5 + 0.5), 5.0);
  319. fresnel = lerp(fresnel, (1.0).xxx, specularColor.rgb);
  320.  
  321. fresnel *= saturate(smoothness * 4.0);
  322.  
  323. result = lerp(result, reflections, fresnel);
  324. }
  325.  
  326. return float4(result, 1.0);
  327. }
  328.  
  329. ENDCG
  330. }
  331.  
  332. Pass //3 Temporal blend (with unity motion vectors)
  333. {
  334. CGPROGRAM
  335. #pragma vertex vert
  336. #pragma fragment frag
  337.  
  338. sampler2D GITexture;
  339. sampler2D PreviousDepth;
  340. sampler2D CurrentDepth;
  341. sampler2D PreviousLocalWorldPos;
  342.  
  343.  
  344. float4 CameraPosition;
  345. float4 CameraPositionPrev;
  346. float4x4 ProjectionPrev;
  347. float4x4 ProjectionPrevInverse;
  348. float4x4 WorldToCameraPrev;
  349. float4x4 CameraToWorldPrev;
  350. float4x4 CameraToWorld;
  351. float DeltaTime;
  352. float BlendWeight;
  353.  
  354. float4 frag(v2f input) : COLOR0
  355. {
  356. float3 gi = tex2D(_MainTex, input.uv.xy).rgb;
  357.  
  358.  
  359. float depth = GetDepthTexture(input.uv.xy);
  360.  
  361. float4 currentPos = float4(input.uv.x * 2.0 - 1.0, input.uv.y * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0);
  362.  
  363. float4 fragpos = mul(ProjectionMatrixInverse, currentPos);
  364. fragpos = mul(CameraToWorld, fragpos);
  365. fragpos /= fragpos.w;
  366. float4 thisWorldPosition = fragpos;
  367.  
  368.  
  369.  
  370.  
  371. float2 motionVectors = tex2Dlod(_CameraMotionVectorsTexture, float4(input.uv.xy, 0.0, 0.0)).xy;
  372. float2 reprojCoord = input.uv.xy - motionVectors.xy;
  373.  
  374.  
  375.  
  376. float prevDepth = (tex2Dlod(PreviousDepth, float4(reprojCoord + _MainTex_TexelSize.xy * 0.0, 0.0, 0.0)).x);
  377. #if defined(UNITY_REVERSED_Z)
  378. prevDepth = 1.0 - prevDepth;
  379. #endif
  380.  
  381. float4 previousWorldPosition = mul(ProjectionPrevInverse, float4(reprojCoord.xy * 2.0 - 1.0, prevDepth * 2.0 - 1.0, 1.0));
  382. previousWorldPosition = mul(CameraToWorldPrev, previousWorldPosition);
  383. previousWorldPosition /= previousWorldPosition.w;
  384.  
  385.  
  386. float blendWeight = BlendWeight;
  387.  
  388. float posSimilarity = saturate(1.0 - distance(previousWorldPosition.xyz, thisWorldPosition.xyz) * 1.0);
  389. blendWeight = lerp(1.0, blendWeight, posSimilarity);
  390.  
  391.  
  392.  
  393.  
  394. float3 minPrev = float3(10000, 10000, 10000);
  395. float3 maxPrev = float3(0, 0, 0);
  396.  
  397. float3 s0 = tex2Dlod(_MainTex, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(0.5, 0.5), 0, 0)).rgb;
  398. minPrev = s0;
  399. maxPrev = s0;
  400. s0 = tex2Dlod(_MainTex, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(0.5, -0.5), 0, 0)).rgb;
  401. minPrev = min(minPrev, s0);
  402. maxPrev = max(maxPrev, s0);
  403. s0 = tex2Dlod(_MainTex, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(-0.5, 0.5), 0, 0)).rgb;
  404. minPrev = min(minPrev, s0);
  405. maxPrev = max(maxPrev, s0);
  406. s0 = tex2Dlod(_MainTex, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(-0.5, -0.5), 0, 0)).rgb;
  407. minPrev = min(minPrev, s0);
  408. maxPrev = max(maxPrev, s0);
  409.  
  410.  
  411.  
  412. float3 prevGI = tex2Dlod(PreviousGITexture, float4(reprojCoord, 0.0, 0.0)).rgb;
  413. prevGI = lerp(prevGI, clamp(prevGI, minPrev, maxPrev), 0.25);
  414.  
  415. gi = lerp(prevGI, gi, float3(blendWeight, blendWeight, blendWeight));
  416.  
  417. float3 result = gi;
  418. return float4(result, 1.0);
  419. }
  420.  
  421. ENDCG
  422. }
  423.  
  424. Pass //4 Specular/reflections trace
  425. {
  426. ZTest Always
  427.  
  428. CGPROGRAM
  429. #pragma vertex vert
  430. #pragma fragment frag
  431.  
  432. float4x4 CameraToWorld;
  433.  
  434.  
  435. sampler2D _CameraGBufferTexture1;
  436. sampler2D _CameraGBufferTexture2;
  437.  
  438.  
  439. sampler3D SEGIVolumeTexture1;
  440.  
  441. int FrameSwitch;
  442.  
  443.  
  444. float4 GetSpecular (float2 uv) // Texture1
  445. {
  446. // Lux
  447. float4 gbuffer0 = tex2D(_CameraGBufferTexture0, uv);
  448. float4 normSample = tex2D(_CameraGBufferTexture2, uv);
  449. half materialIndex = GetLuxMaterialIndexFull(normSample.a);
  450. float4 specSmoothnessSample = tex2D(_CameraGBufferTexture1, uv);
  451. float3 specColor = GetLuxSpecular(specSmoothnessSample.rgb, gbuffer0, normSample.b, materialIndex);
  452.  
  453. return float4(specColor,specSmoothnessSample.a);
  454. // End Lux
  455. }
  456.  
  457. float4 GetNormal (float2 uv) // texture2
  458. {
  459. // Lux
  460. float4 normSample = tex2D(_CameraGBufferTexture2, uv);
  461. half materialIndex = GetLuxMaterialIndexFull(normSample.a);
  462. float3 wsNormal = GetLuxNormal(normSample, materialIndex, uv);
  463.  
  464. return float4(wsNormal,0);
  465. // End Lux
  466. }
  467.  
  468.  
  469. float4 frag(v2f input) : SV_Target
  470. {
  471. #if UNITY_UV_STARTS_AT_TOP
  472. float2 coord = input.uv2.xy;
  473. #else
  474. float2 coord = input.uv.xy;
  475. #endif
  476.  
  477. //float4 spec = tex2D(_CameraGBufferTexture1, coord);
  478. float4 spec = GetSpecular(coord); //Lux
  479.  
  480. float4 viewSpacePosition = GetViewSpacePosition(coord);
  481. float3 viewVector = normalize(viewSpacePosition.xyz);
  482. float4 worldViewVector = mul(CameraToWorld, float4(viewVector.xyz, 0.0));
  483.  
  484.  
  485. float4 voxelSpacePosition = mul(CameraToWorld, viewSpacePosition);
  486. float3 worldPosition = voxelSpacePosition.xyz;
  487. voxelSpacePosition = mul(SEGIWorldToVoxel, voxelSpacePosition);
  488. voxelSpacePosition = mul(SEGIVoxelProjection, voxelSpacePosition);
  489. voxelSpacePosition.xyz = voxelSpacePosition.xyz * 0.5 + 0.5;
  490.  
  491. //float3 worldNormal = normalize(tex2D(_CameraGBufferTexture2, coord).rgb * 2.0 - 1.0);
  492. float3 worldNormal = normalize(GetNormal(coord).rgb);
  493.  
  494. float3 voxelOrigin = voxelSpacePosition.xyz + worldNormal.xyz * 0.006 * ConeTraceBias * 1.25 / SEGIVoxelScaleFactor;
  495.  
  496. float2 dither = rand(coord + (float)FrameSwitch * 0.11734);
  497.  
  498. float smoothness = spec.a * 0.5;
  499. float3 specularColor = spec.rgb;
  500.  
  501. float4 reflection = (0.0).xxxx;
  502.  
  503. float3 reflectionKernel = reflect(worldViewVector.xyz, worldNormal);
  504.  
  505. float3 fresnel = pow(saturate(dot(worldViewVector.xyz, reflectionKernel.xyz)) * (smoothness * 0.5 + 0.5), 5.0);
  506. fresnel = lerp(fresnel, (1.0).xxx, specularColor.rgb);
  507.  
  508. voxelOrigin += worldNormal.xyz * 0.002 * 1.25 / SEGIVoxelScaleFactor;
  509. reflection = SpecularConeTrace(voxelOrigin.xyz, reflectionKernel.xyz, worldNormal.xyz, smoothness, coord, dither.x);
  510.  
  511. float3 skyReflection = (reflection.a * 1.0 * SEGISkyColor);
  512.  
  513. reflection.rgb = reflection.rgb * 0.7 + skyReflection.rgb * 2.4015 * SkyReflectionIntensity;
  514.  
  515. return float4(reflection.rgb, 1.0);
  516. }
  517.  
  518. ENDCG
  519. }
  520.  
  521. Pass //5 Get camera depth texture
  522. {
  523. CGPROGRAM
  524. #pragma vertex vert
  525. #pragma fragment frag
  526.  
  527. float4 frag(v2f input) : COLOR0
  528. {
  529. float2 coord = input.uv.xy;
  530. float4 tex = tex2D(_CameraDepthTexture, coord);
  531. return tex;
  532. }
  533.  
  534. ENDCG
  535. }
  536.  
  537. Pass //6 Get camera normals texture
  538. {
  539. CGPROGRAM
  540. #pragma vertex vert
  541. #pragma fragment frag
  542.  
  543.  
  544. float4 frag(v2f input) : COLOR0
  545. {
  546. float2 coord = input.uv.xy;
  547. float4 tex = tex2D(_CameraDepthNormalsTexture, coord);
  548. return tex;
  549. }
  550.  
  551. ENDCG
  552. }
  553.  
  554.  
  555. Pass //7 Visualize GI
  556. {
  557. CGPROGRAM
  558. #pragma vertex vert
  559. #pragma fragment frag
  560.  
  561. sampler2D GITexture;
  562.  
  563. float4 frag(v2f input) : COLOR0
  564. {
  565. float4 albedoTex = tex2D(_CameraGBufferTexture0, input.uv.xy);
  566. float3 albedo = albedoTex.rgb;
  567. float3 gi = tex2D(GITexture, input.uv.xy).rgb;
  568. return float4(gi, 1.0);
  569. }
  570.  
  571. ENDCG
  572. }
  573.  
  574.  
  575.  
  576. Pass //8 Write black
  577. {
  578. CGPROGRAM
  579. #pragma vertex vert
  580. #pragma fragment frag
  581.  
  582. float4 frag(v2f input) : COLOR0
  583. {
  584. return float4(0.0, 0.0, 0.0, 1.0);
  585. }
  586.  
  587. ENDCG
  588. }
  589.  
  590. Pass //9 Visualize slice of GI Volume (CURRENTLY UNUSED)
  591. {
  592. CGPROGRAM
  593. #pragma vertex vert
  594. #pragma fragment frag
  595.  
  596. float LayerToVisualize;
  597. int MipLevelToVisualize;
  598.  
  599. sampler3D SEGIVolumeTexture1;
  600.  
  601. float4 frag(v2f input) : COLOR0
  602. {
  603. return float4(tex3D(SEGIVolumeTexture1, float3(input.uv.xy, LayerToVisualize)).rgb, 1.0);
  604. }
  605.  
  606. ENDCG
  607. }
  608.  
  609.  
  610. Pass //10 Visualize voxels (trace through GI volumes)
  611. {
  612. ZTest Always
  613.  
  614. CGPROGRAM
  615. #pragma vertex vert
  616. #pragma fragment frag
  617.  
  618. float4x4 CameraToWorld;
  619.  
  620. //sampler2D _CameraGBufferTexture2;
  621.  
  622. float4 CameraPosition;
  623.  
  624. float4 frag(v2f input) : SV_Target
  625. {
  626. #if UNITY_UV_STARTS_AT_TOP
  627. float2 coord = input.uv2.xy;
  628. #else
  629. float2 coord = input.uv.xy;
  630. #endif
  631.  
  632. float4 viewSpacePosition = GetViewSpacePosition(coord);
  633. float3 viewVector = normalize(viewSpacePosition.xyz);
  634. float4 worldViewVector = mul(CameraToWorld, float4(viewVector.xyz, 0.0));
  635.  
  636. float4 voxelCameraPosition = mul(SEGIWorldToVoxel, float4(CameraPosition.xyz, 1.0));
  637. voxelCameraPosition = mul(SEGIVoxelProjection, voxelCameraPosition);
  638. voxelCameraPosition.xyz = voxelCameraPosition.xyz * 0.5 + 0.5;
  639.  
  640. float4 result = VisualConeTrace(voxelCameraPosition.xyz, worldViewVector.xyz);
  641.  
  642. return float4(result.rgb, 1.0);
  643. }
  644.  
  645. ENDCG
  646. }
  647.  
  648. Pass //11 Bilateral upsample
  649. {
  650. CGPROGRAM
  651. #pragma vertex vert
  652. #pragma fragment frag
  653.  
  654. float2 Kernel;
  655.  
  656. float DepthTolerance;
  657.  
  658. sampler2D DepthNormalsLow;
  659. sampler2D DepthLow;
  660. int SourceScale;
  661. sampler2D CurrentDepth;
  662. sampler2D CurrentNormal;
  663.  
  664.  
  665. float4 frag(v2f input) : COLOR0
  666. {
  667. float4 blurred = float4(0.0, 0.0, 0.0, 0.0);
  668. float4 blurredDumb = float4(0.0, 0.0, 0.0, 0.0);
  669. float validWeights = 0.0;
  670. float depth = LinearEyeDepth(tex2D(_CameraDepthTexture, input.uv.xy).x);
  671. half3 normal = DecodeViewNormalStereo(tex2D(_CameraDepthNormalsTexture, input.uv.xy));
  672. float thresh = 0.26;
  673.  
  674. float3 viewPosition = GetViewSpacePosition(input.uv.xy).xyz;
  675. float3 viewVector = normalize(viewPosition);
  676.  
  677. float NdotV = 1.0 / (saturate(dot(-viewVector, normal.xyz)) + 0.1);
  678. thresh *= 1.0 + NdotV * 2.0;
  679.  
  680. float4 sample00 = tex2Dlod(_MainTex, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(0.0, 0.0) * 1.0, 0.0, 0.0));
  681. float4 sample10 = tex2Dlod(_MainTex, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(1.0, 0.0) * 1.0, 0.0, 0.0));
  682. float4 sample11 = tex2Dlod(_MainTex, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(1.0, 1.0) * 1.0, 0.0, 0.0));
  683. float4 sample01 = tex2Dlod(_MainTex, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(0.0, 1.0) * 1.0, 0.0, 0.0));
  684.  
  685. float4 depthSamples = float4(0,0,0,0);
  686. depthSamples.x = LinearEyeDepth(tex2Dlod(CurrentDepth, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(0.0, 0.0), 0, 0)).x);
  687. depthSamples.y = LinearEyeDepth(tex2Dlod(CurrentDepth, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(1.0, 0.0), 0, 0)).x);
  688. depthSamples.z = LinearEyeDepth(tex2Dlod(CurrentDepth, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(1.0, 1.0), 0, 0)).x);
  689. depthSamples.w = LinearEyeDepth(tex2Dlod(CurrentDepth, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(0.0, 1.0), 0, 0)).x);
  690.  
  691. half3 normal00 = DecodeViewNormalStereo(tex2D(CurrentNormal, input.uv.xy + _MainTex_TexelSize.xy * float2(0.0, 0.0)));
  692. half3 normal10 = DecodeViewNormalStereo(tex2D(CurrentNormal, input.uv.xy + _MainTex_TexelSize.xy * float2(1.0, 0.0)));
  693. half3 normal11 = DecodeViewNormalStereo(tex2D(CurrentNormal, input.uv.xy + _MainTex_TexelSize.xy * float2(1.0, 1.0)));
  694. half3 normal01 = DecodeViewNormalStereo(tex2D(CurrentNormal, input.uv.xy + _MainTex_TexelSize.xy * float2(0.0, 1.0)));
  695.  
  696. float4 depthWeights = saturate(1.0 - abs(depthSamples - depth.xxxx) / thresh);
  697.  
  698. float4 normalWeights = float4(0,0,0,0);
  699. normalWeights.x = pow(saturate(dot(normal00, normal)), 24.0);
  700. normalWeights.y = pow(saturate(dot(normal10, normal)), 24.0);
  701. normalWeights.z = pow(saturate(dot(normal11, normal)), 24.0);
  702. normalWeights.w = pow(saturate(dot(normal01, normal)), 24.0);
  703.  
  704. float4 weights = depthWeights * normalWeights;
  705.  
  706. float weightSum = dot(weights, float4(1.0, 1.0, 1.0, 1.0));
  707.  
  708. if (weightSum < 0.01)
  709. {
  710. weightSum = 4.0;
  711. weights = (1.0).xxxx;
  712. }
  713.  
  714. weights /= weightSum;
  715.  
  716. float2 fractCoord = frac(input.uv.xy * _MainTex_TexelSize.zw * 1.0);
  717.  
  718. float4 filteredX0 = lerp(sample00 * weights.x, sample10 * weights.y, fractCoord.x);
  719. float4 filteredX1 = lerp(sample01 * weights.w, sample11 * weights.z, fractCoord.x);
  720.  
  721. float4 filtered = lerp(filteredX0, filteredX1, fractCoord.y);
  722.  
  723.  
  724. return filtered * 3.0;
  725.  
  726. return blurred;
  727. }
  728.  
  729. ENDCG
  730. }
  731.  
  732. Pass //12 Temporal blending without motion vectors (for legacy support)
  733. {
  734. CGPROGRAM
  735. #pragma vertex vert
  736. #pragma fragment frag
  737.  
  738. sampler2D GITexture;
  739. sampler2D PreviousDepth;
  740. sampler2D CurrentDepth;
  741. sampler2D PreviousLocalWorldPos;
  742.  
  743.  
  744. float4 CameraPosition;
  745. float4 CameraPositionPrev;
  746. float4x4 ProjectionPrev;
  747. float4x4 ProjectionPrevInverse;
  748. float4x4 WorldToCameraPrev;
  749. float4x4 CameraToWorldPrev;
  750. float4x4 CameraToWorld;
  751. float DeltaTime;
  752. float BlendWeight;
  753.  
  754. float4 frag(v2f input) : COLOR0
  755. {
  756. float3 gi = tex2D(_MainTex, input.uv.xy).rgb;
  757.  
  758. float2 depthLookupCoord = round(input.uv.xy * _MainTex_TexelSize.zw) * _MainTex_TexelSize.xy;
  759. depthLookupCoord = input.uv.xy;
  760. //float depth = tex2Dlod(_CameraDepthTexture, float4(depthLookupCoord, 0.0, 0.0)).x;
  761. float depth = GetDepthTexture(depthLookupCoord);
  762.  
  763. float4 currentPos = float4(input.uv.x * 2.0 - 1.0, input.uv.y * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0);
  764.  
  765. float4 fragpos = mul(ProjectionMatrixInverse, currentPos);
  766. float4 thisViewPos = fragpos;
  767. fragpos = mul(CameraToWorld, fragpos);
  768. fragpos /= fragpos.w;
  769. float4 thisWorldPosition = fragpos;
  770. fragpos.xyz += CameraPosition.xyz * DeltaTime;
  771.  
  772. float4 prevPos = fragpos;
  773. prevPos.xyz -= CameraPositionPrev.xyz * DeltaTime;
  774. prevPos = mul(WorldToCameraPrev, prevPos);
  775. prevPos = mul(ProjectionPrev, prevPos);
  776. prevPos /= prevPos.w;
  777.  
  778. float2 diff = currentPos.xy - prevPos.xy;
  779.  
  780. float2 reprojCoord = input.uv.xy - diff.xy * 0.5;
  781. float2 previousTexcoord = input.uv.xy + diff.xy * 0.5;
  782.  
  783.  
  784. float blendWeight = BlendWeight;
  785.  
  786. float prevDepth = (tex2Dlod(PreviousDepth, float4(reprojCoord + _MainTex_TexelSize.xy * 0.0, 0.0, 0.0)).x);
  787.  
  788. float4 previousWorldPosition = mul(ProjectionPrevInverse, float4(reprojCoord.xy * 2.0 - 1.0, prevDepth * 2.0 - 1.0, 1.0));
  789. previousWorldPosition = mul(CameraToWorldPrev, previousWorldPosition);
  790. previousWorldPosition /= previousWorldPosition.w;
  791.  
  792. if (distance(previousWorldPosition.xyz, thisWorldPosition.xyz) > 0.1 || reprojCoord.x > 1.0 || reprojCoord.x < 0.0 || reprojCoord.y > 1.0 || reprojCoord.y < 0.0)
  793. {
  794. blendWeight = 1.0;
  795. }
  796.  
  797. float3 prevGI = tex2D(PreviousGITexture, reprojCoord).rgb;
  798.  
  799. gi = lerp(prevGI, gi, float3(blendWeight, blendWeight, blendWeight));
  800.  
  801. float3 result = gi;
  802. return float4(result, 1.0);
  803. }
  804.  
  805. ENDCG
  806. }
  807.  
  808.  
  809. }
  810.  
  811. Fallback off
  812.  
  813. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement