Advertisement
Guest User

Untitled

a guest
Feb 14th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. float3 ViewDir = normalize(Parameters.AbsoluteWorldPosition - ResolvedView.WorldCameraOrigin);
  2.  
  3.  
  4. float3 BoxMin = float3(CloudsPosition.x - CloudsScale.x / 2.0, CloudsPosition.y - CloudsScale.y / 2.0, CloudsPosition.z);
  5. float3 BoxMax = float3(CloudsPosition.x + CloudsScale.x / 2.0, CloudsPosition.y + CloudsScale.y / 2.0, CloudsPosition.z + CloudsScale.z);
  6.  
  7.  
  8. float3 tMin = (BoxMin - ResolvedView.WorldCameraOrigin) / ViewDir;
  9. float3 tMax = (BoxMax - ResolvedView.WorldCameraOrigin) / ViewDir;
  10.  
  11. float3 t1 = min(tMin, tMax);
  12. float3 t2 = max(tMin, tMax);
  13.  
  14. float NearL = max(max(t1.x, t1.y), t1.z);
  15. float FarL = min(min(t2.x, t2.y), t2.z);
  16. float RayLength = FarL - NearL;
  17.  
  18.  
  19. float DensitySamples = MaxSamples;
  20. float RayOffset = CloudsScale.z / MinSamples;
  21.  
  22. if(bStaticOffset == 0)
  23. {
  24. DensitySamples = lerp(MinSamples, MaxSamples, (RayLength - CloudsScale.z) / (CloudsScale.x / 2.0));
  25. RayOffset = RayLength / DensitySamples;
  26. }
  27.  
  28.  
  29. float3 RayDir = ViewDir * RayOffset;
  30.  
  31. if(ResolvedView.WorldCameraOrigin.z < BoxMax.z && ResolvedView.WorldCameraOrigin.z > BoxMin.z && ResolvedView.WorldCameraOrigin.x < BoxMax.x && ResolvedView.WorldCameraOrigin.x > BoxMin.x && ResolvedView.WorldCameraOrigin.y < BoxMax.y && ResolvedView.WorldCameraOrigin.y > BoxMin.y)
  32. {
  33. NearL = 0;
  34. }
  35.  
  36.  
  37.  
  38.  
  39.  
  40. float3 RayPos = ResolvedView.WorldCameraOrigin + ViewDir * NearL;
  41.  
  42. float Jitter =float(Rand3DPCG16(int3(Parameters.SvPosition.xy, View.StateFrameIndexMod8)).x) / 0xffff;
  43. RayPos += RayDir * Jitter;
  44.  
  45.  
  46.  
  47. static const float3 RandomVectors[] =
  48. {
  49.     float3( 0.38051305f,  0.92453449f, -0.02111345f),
  50.     float3(-0.50625799f, -0.03590792f, -0.86163418f),
  51.     float3(-0.32509218f, -0.94557439f,  0.01428793f),
  52.     float3( 0.09026238f, -0.27376545f,  0.95755165f),
  53.     float3( 0.28128598f,  0.42443639f, -0.86065785f),
  54.     float3(-0.16852403f,  0.14748697f,  0.97460106f)
  55. };
  56.  
  57.  
  58.  
  59.  
  60. float Transmittance = 1.0;
  61. float3 LightEnergy = 0.0;
  62.  
  63. ///////////////////////////////////////////////////////////////////////// Ray Basis
  64. float ShadowRayOffset = (CloudsScale.z / 2.0) / ShadowSamples;
  65.  
  66.  
  67. /////////////////////////
  68. ///////////////////////// FUNCTIONS
  69. /////////////////////////
  70. struct Functions
  71. {
  72. float WMSize;
  73. float2 WMOffset;
  74. Texture2D WeatherMap;
  75.  
  76. float BNSize;
  77. float3 NoiseOffset;
  78.  
  79. Texture3D BaseNoise;
  80.  
  81. float DistortionMapSize;
  82. Texture2D DistortionMap;
  83. float DistortionMapIntensity;
  84.  
  85. float DNSize;
  86. float DetailNoiseOffsetScale;
  87.  
  88. Texture3D DetailNoise;
  89.  
  90. float ShadowSamples;
  91.  
  92. float3 LightVector;
  93. float ShadowRayOffset;
  94. float ConeRadius;
  95.  
  96. float CloudsPos;
  97. float CloudsHeight;
  98.  
  99. float ConeStepScale;
  100.  
  101. float InScatter;
  102. float SilverLightIntensity;
  103. float SilverLightExponent;
  104. float OutScatter;
  105. float InOutScatterLerp;
  106.  
  107. float3 AmbientLightTop;
  108. float3 AmbientLightBottom;
  109.  
  110. float3 SDensity;
  111. float3 SClamp;
  112.  
  113. float CoverageShift;
  114.  
  115. ///////////////////////// Remapping function.
  116. float remap(float V, float L0, float H0, float Ln, float Hn)
  117. {
  118. return Ln + (V - L0) * (Hn - Ln) / (H0 - L0);
  119. }
  120. float3 remap3(float3 V, float3 L0, float3 H0, float3 Ln, float3 Hn)
  121. {
  122. return Ln + (V - L0) * (Hn - Ln) / (H0 - L0);
  123. }
  124.  
  125. ///////////////////////// Height gradient.
  126. float GetHeightGradient(float3 LocalRayPos)
  127. {
  128. return (LocalRayPos.z - CloudsPos) / CloudsHeight;
  129. }
  130.  
  131.  
  132. /////////////////////////
  133. ///////////////////////// DENSITY
  134. /////////////////////////
  135. float3 GetSimpleDensity(float HeightGradient, float3 LocalRayPos, float Mipmap, SamplerState WeatherMapSampler, SamplerState BaseNoiseSampler)
  136. {
  137.  
  138. //Weather texture.
  139. float2 WMUV = (LocalRayPos.xy - (WMSize / 2.0)) / (WMSize) + WMOffset;
  140. float4 WM = WeatherMap.SampleLevel(WeatherMapSampler, WMUV, Mipmap);
  141.  
  142. float WMc = WM.r;
  143.  
  144. float CloudType = WM.g;
  145.  
  146. float CloudMaxHeight = saturate(0.1 + CloudType * 2.0);
  147.  
  148.  
  149. float GlobalCoverage = saturate(0.5 + CloudType * 2.0);
  150. GlobalCoverage = CoverageShift;
  151. float CloudAnvilAmount = saturate((CloudType - 0.5) * 2.0);
  152.  
  153.  
  154. float CloudMaxDensity = 0.5;
  155.  
  156. //float SA = HeightAlter(HeightGradient, WM);
  157. float SA = pow(saturate(remap(HeightGradient , 0.0, 0.07, 0.0, 1.0)) * saturate(remap(HeightGradient, saturate(CloudMaxHeight + 0.12) * 0.2, saturate(CloudMaxHeight + 0.12), 1.0, 0.0)), saturate(remap(HeightGradient, 0.65, 0.95, 1.0, (1 - CloudAnvilAmount  *  GlobalCoverage))));
  158.  
  159. float DA = HeightGradient * saturate(remap(HeightGradient, 0.0, 0.2, 0.0, 1.0)) * CloudMaxDensity * 2.0 * lerp(1.0, saturate(remap(pow(HeightGradient, 0.5), 0.4, 0.95, 1.0, 0.2)), CloudAnvilAmount) * saturate(remap(HeightGradient, 0.9, 1.0, 1.0, 0.0));
  160.  
  161.  
  162.  
  163.  
  164. //Base noise.
  165. float3 BNUVW = (LocalRayPos / BNSize + NoiseOffset);
  166. float4 BN = BaseNoise.SampleLevel(BaseNoiseSampler, BNUVW, Mipmap).xyzw;
  167.  
  168.  
  169. //float SNs = ShapeNoise(BN);
  170. float SNs = remap(BN.r, -(1 - (BN.g  *  0.625 + BN.b  *  0.25 + BN.a  * 0.125)), 1.0, 0.0, 1.0);
  171. //SNs = 1.0;
  172. float SN = saturate(remap(SNs * SA, 1.0 - GlobalCoverage * WMc, 1.0, 0.0, 1.0));
  173.  
  174. return float3(SN, DA, GlobalCoverage);
  175. }
  176.  
  177. float GetDetailedDensity(float HeightGradient, float3 LocalRayPos, float Mipmap, float SN, float DA, float GlobalCoverage, SamplerState DistortionMapSampler, SamplerState DetailNoiseSampler)
  178. {
  179. //Distortion.
  180.  
  181. float2 DMUV = (LocalRayPos.xy) / (DistortionMapSize);
  182. float3 DM = DistortionMap.SampleLevel(DistortionMapSampler, DMUV, Mipmap).xyz;
  183.  
  184. DM = ((DM - 0.5) * DistortionMapIntensity);
  185.  
  186. DM *= (1.0 - HeightGradient * 0.5);
  187.  
  188. //Detail noise.
  189. float3 DNUVW = (LocalRayPos / DNSize + NoiseOffset * DetailNoiseOffsetScale) + DM;
  190. float4 DN = DetailNoise.SampleLevel(DetailNoiseSampler, DNUVW, Mipmap);
  191.  
  192. //float SNd = DetailNoise(HeightGradient, DN, SN);
  193. float DNs = DN.r * 0.625 + DN.g * 0.25 + DN.b *  0.125;
  194.  
  195. float SNd = saturate(remap(SN, lerp(DNs, 1.0 - DNs, saturate(HeightGradient * 5.0 + 0.25)) * 0.35 * exp(-GlobalCoverage * 0.75), 1.0, 0.0, 1.0));
  196.  
  197. return SNd * DA;
  198. }
  199.  
  200. ///////////////////////// Density to sun.
  201. float GetDTS(float3 LocalRayPos, int MipmapStart, SamplerState WeatherMapSampler, SamplerState BaseNoiseSampler, SamplerState DistortionMapSampler, SamplerState DetailNoiseSampler)
  202. {
  203. float DTS = 0.0;
  204. float3 ShadowRayPos = 0.0;
  205.  
  206. for(int srs = 0; srs < ShadowSamples; srs++)
  207. {
  208. int ResultMipmap = MipmapStart + int(srs * 0.5);
  209.  
  210. LocalRayPos += LightVector * ShadowRayOffset;
  211.  
  212. ShadowRayPos = LocalRayPos + ConeRadius * RandomVectors[floor(frac(srs / 6) * 6)];
  213.  
  214. //Height gradient.
  215. float HeightGradient = GetHeightGradient(ShadowRayPos);
  216.  
  217. if(HeightGradient > 1.0 || HeightGradient < 0.0)
  218. break;
  219.  
  220. //Sample simple density.
  221. float3 SimpleDS = GetSimpleDensity(HeightGradient, ShadowRayPos, ResultMipmap, WeatherMapSampler, BaseNoiseSampler);
  222.  
  223. if(SimpleDS.x * SimpleDS.y > 0.0)
  224. {
  225. DTS += GetDetailedDensity(HeightGradient, ShadowRayPos, ResultMipmap, SimpleDS.x, SimpleDS.y, SimpleDS.z, DistortionMapSampler, DetailNoiseSampler);
  226. }
  227.  
  228. ConeRadius += ConeStepScale;
  229. }
  230.  
  231. //Last disntaced step.
  232. LocalRayPos += LightVector * ShadowRayOffset * ShadowSamples * 0.5;
  233.  
  234. float HeightGradient = GetHeightGradient(LocalRayPos);
  235. float3 SimpleDS = GetSimpleDensity(HeightGradient, LocalRayPos, 6, WeatherMapSampler, BaseNoiseSampler);
  236. if(SimpleDS.x * SimpleDS.y > 0.0)
  237. {
  238. DTS += GetDetailedDensity(HeightGradient, LocalRayPos, 6, SimpleDS.x, SimpleDS.y, SimpleDS.z, DistortionMapSampler, DetailNoiseSampler);
  239. }
  240.  
  241. return DTS;
  242. }
  243.  
  244. ///////////////////////// Henyey-Greenstein.
  245. float HenyeyGreenstein(float Angle, float G)
  246. {
  247. return ((1.0 - G*G) / pow(abs(1.0 + G*G - 2.0 * G * Angle), 1.5)) / (4 * 3.1415);
  248. }
  249.  
  250. ///////////////////////// Scattering.
  251. float GetInOutScatter(float cos_angle)
  252. {
  253. float first_hg = HenyeyGreenstein(cos_angle , InScatter);
  254. float second_hg =  SilverLightIntensity * pow(abs(saturate(cos_angle)), SilverLightExponent);
  255.  
  256. float in_scatter_hg = max(first_hg , second_hg);
  257. float out_scatter_hg = HenyeyGreenstein(cos_angle , -OutScatter);
  258.  
  259. return lerp(in_scatter_hg ,out_scatter_hg, InOutScatterLerp);
  260. }
  261.  
  262. ///////////////////////// Exponential integral.
  263. float IntE( float z )
  264. {
  265. return 0.5772156649015328606065 + log( 1e-4 + abs(z) ) + z * (1.0 + z * (0.25 + z * ( (1.0/18.0) + z * ( (1.0/96.0) + z * (1.0/600.0) ) ) ) ); // For x!=0
  266. }
  267.  
  268. ///////////////////////// Exponential ambient color.
  269. float3 GetIsotropicScattering(float HeightGradient, float ExtinctionCoeff)
  270. {
  271. float Sample = -ExtinctionCoeff * HeightGradient;
  272. float3 IsotropicScatteringTop = AmbientLightTop * max(0.0, exp(Sample) - Sample * IntE(Sample));
  273.  
  274. Sample = -ExtinctionCoeff * (1.0 - HeightGradient);
  275. float3 IsotropicScatteringBottom = AmbientLightBottom* max(0.0, exp(Sample) - Sample * IntE(Sample));
  276.  
  277. return IsotropicScatteringTop + IsotropicScatteringBottom;
  278. }
  279.  
  280. ///////////////////////// Attenuation.
  281. float3 GetAttenuation( float DTS, float Angle)
  282. {
  283. float3 prim = exp(-SDensity * DTS);
  284. float3 scnd = exp(-SDensity * SClamp) * 0.7;
  285.  
  286. float3 checkval = remap3(Angle, 0.0, 1.0, scnd, scnd * 0.5);
  287.  
  288. return max(checkval, prim);
  289. }
  290.  
  291. };
  292. /////////////////////////
  293. /////////////////////////FUNCTIONS END
  294. /////////////////////////
  295. Functions F;
  296.  
  297.  
  298. /** Rework it, way too slow and bad solution. */
  299. F.WMSize = WMSize;
  300. F.WMOffset = WMOffset;
  301. F.WeatherMap = WeatherMap;
  302.  
  303. F.BNSize = BNSize;
  304. F.NoiseOffset = NoiseOffset;
  305.  
  306. F.BaseNoise = BaseNoise;
  307.  
  308. F.DistortionMapSize = DistortionMapSize;
  309. F.DistortionMap = DistortionMap;
  310. F.DistortionMapIntensity = DistortionMapIntensity;
  311.  
  312. F.DNSize = DNSize;
  313. F.DetailNoiseOffsetScale = DetailNoiseOffsetScale;
  314.  
  315. F.DetailNoise = DetailNoise;
  316.  
  317. F.ShadowSamples = ShadowSamples;
  318.  
  319. F.LightVector = LightVector;
  320. F.ShadowRayOffset = ShadowRayOffset;
  321. F.ConeRadius = ConeRadius;
  322.  
  323. F.CloudsPos = CloudsPosition.z;
  324. F.CloudsHeight = CloudsScale.z;
  325.  
  326. F.ConeStepScale = ConeStepScale;
  327.  
  328. F.InScatter = InScatter;
  329. F.SilverLightIntensity = SilverLightIntensity;
  330. F.SilverLightExponent = SilverLightExponent;
  331. F.OutScatter = OutScatter;
  332. F.InOutScatterLerp = InOutScatterLerp;
  333.  
  334. F.AmbientLightTop = AmbientLightTop;
  335. F.AmbientLightBottom = AmbientLightBottom;
  336.  
  337. F.SDensity = SDensity;
  338. F.SClamp = SClamp;
  339.  
  340. F.CoverageShift = CoverageShift;
  341.  
  342.  
  343. float3 SDSample = 0.0;
  344. float DSample = 0.0;
  345. float3 DensitySum = 0.0;
  346.  
  347. float DensityToSun = 0.0;
  348.  
  349. float3 Attenuation = 0.0;
  350. float3 Powder = 0.0;
  351. float InOutScatter = 0.0;
  352. float3 IsotropicScatter = 0.0;
  353.  
  354. float AngleVdL = dot(normalize(RayDir), normalize(LightVector));
  355.  
  356. float BaseDensityMip = 0.0;
  357. int DensityMip = 0;
  358.  
  359. float SkyBlendSum = ((NearL * SkyBlendIntensity * Transmittance) / SkyBlendDistance);
  360.  
  361. for(int rs = 0; rs<DensitySamples; rs++)
  362. {
  363. if(length(RayPos - ResolvedView.WorldCameraOrigin) >= DepthBehind)
  364. break;
  365.  
  366. if(Transmittance > 0.001)
  367. {
  368.  
  369. //Calculate density mip,ap parameter based on distance and density itself.
  370. BaseDensityMip = length(RayPos.xy - ResolvedView.WorldCameraOrigin.xy) / MipDistance;
  371. DensityMip = floor(BaseDensityMip);
  372.  
  373. if(1 - Transmittance >= 0.3)
  374. {
  375. if(1 - Transmittance >= 0.6)
  376. {
  377. DensityMip += 2;
  378. }
  379. else
  380. {
  381. DensityMip += 1;
  382. }
  383. }
  384.  
  385.  
  386.  
  387. //HeightGradient.
  388. float HeightGradient = F.GetHeightGradient(RayPos);
  389.  
  390. if(HeightGradient < 0.0 || HeightGradient > 1.0)
  391. break;
  392.  
  393. SDSample = F.GetSimpleDensity(HeightGradient, RayPos, DensityMip, WeatherMapSampler, BaseNoiseSampler);
  394.  
  395. if(SDSample.x * SDSample.y > 0.0)
  396. {
  397.  
  398. DSample = F.GetDetailedDensity(HeightGradient, RayPos, DensityMip, SDSample.x, SDSample.y, SDSample.z, DistortionMapSampler, DetailNoiseSampler);
  399.  
  400. if(DSample > 0.001)
  401. {
  402. DensitySum =  (DSample * CloudsDensity);
  403. DensityToSun = F.GetDTS(RayPos, DensityMip, WeatherMapSampler, BaseNoiseSampler, DistortionMapSampler, DetailNoiseSampler);
  404.  
  405. Powder = 1.0 - exp( -DSample);
  406. Attenuation = F.GetAttenuation(DensityToSun , AngleVdL) * LightColor;
  407. InOutScatter = F.GetInOutScatter(AngleVdL);
  408. IsotropicScatter = F.GetIsotropicScattering(HeightGradient,  DSample);
  409.  
  410. float3 LightSample = (Attenuation * IsotropicScatter * InOutScatter);
  411. LightSample = lerp(PColor, 1.0, LightSample);
  412.  
  413. LightEnergy += LightSample * DensitySum * Transmittance;
  414.  
  415. Transmittance *= exp(-DensitySum.x);
  416.  
  417. }//end:if(DSample > 0.001)
  418.  
  419. }//end:if(SDSample.x * SDSample.y > 0.0)
  420.  
  421. RayPos += RayDir + RayDir * BaseDensityMip;
  422.  
  423. if(RayPos.z > BoxMax.z && RayPos.z < BoxMin.z
  424. && RayPos.x > BoxMax.x && RayPos.x < BoxMin.x
  425. && RayPos.y > BoxMax.y && RayPos.y < BoxMin.y)
  426. {
  427. break;
  428. }
  429.  
  430.  
  431. SkyBlendSum += (((RayOffset + RayOffset * BaseDensityMip) * SkyBlendIntensity * Transmittance) / SkyBlendDistance) * (1.0 - saturate(RayPos.z / SkyBlendHeight));
  432.  
  433.  
  434. }//end:if(Transmittance > 0.01)
  435. else
  436. {
  437. break;
  438. }
  439.  
  440.  
  441.  
  442.  
  443.  
  444. }//end: for(int rs = 0; rs<DensitySamples; rs++)
  445.  
  446. //LightEnergy = lerp(AmbientLightBottom, AmbientLightTop, LightEnergy) * (1.0 - Transmittance);
  447.  
  448.  
  449. float SkyBlendLerp = saturate(SkyBlendSum - SkyBlendStartDistance);
  450. float SkyTransmittance = lerp(0.0, 1.0, SkyBlendLerp);
  451.  
  452. LightEnergy = lerp(LightEnergy, 0.0, SkyBlendLerp);
  453. LightEnergy += SkyColor * SkyTransmittance * (1.0 - Transmittance);
  454.  
  455. return float4(LightEnergy, saturate(Transmittance - 0.01));
  456.  
  457.  
  458.  
  459. return 1.0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement