You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

819 lines
23 KiB

5 years ago
  1. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
  2. #ifndef FOG_VOLUME_FRAGMENT_INCLUDED
  3. #define FOG_VOLUME_FRAGMENT_INCLUDED
  4. #define HALF_MAX 65504.0
  5. // Clamp HDR value within a safe range
  6. inline half SafeHDR(half c) { return min(c, HALF_MAX); }
  7. inline half2 SafeHDR(half2 c) { return min(c, HALF_MAX); }
  8. inline half3 SafeHDR(half3 c) { return min(c, HALF_MAX); }
  9. inline half4 SafeHDR(half4 c) { return min(c, HALF_MAX); }
  10. float4 frag(v2f i) : SV_Target
  11. {
  12. float3 ViewDir = normalize(i.LocalPos - i.LocalEyePos);
  13. float tmin = 0.0, tmax = 0.0;
  14. bool hit = IntersectBox(i.LocalEyePos, ViewDir, _BoxMin.xyz, _BoxMax.xyz, tmin, tmax);
  15. if (!hit)
  16. discard;
  17. //likely to resolve to a free modifier
  18. if (tmin < 0)
  19. tmin = 0;
  20. float4 ScreenUVs = UNITY_PROJ_COORD(i.ScreenUVs);
  21. float2 screenUV = ScreenUVs.xy / ScreenUVs.w;
  22. float Depth = 0;
  23. #if _FOG_LOWRES_RENDERER && !ExternalDepth
  24. //low res
  25. #ifdef FOG_VOLUME_STEREO_ON
  26. //left eye
  27. if (unity_CameraProjection[0][2] < 0)//lo estaba haciendo con unity_StereoEyeIndex, pero por alg�n motivo, no se entera
  28. {
  29. Depth = tex2D(RT_Depth, screenUV);
  30. }
  31. //right eye
  32. else //if (unity_CameraProjection[0][2] > 0)
  33. {
  34. Depth = tex2Dlod(RT_DepthR, float4(screenUV, 0, 0));
  35. }
  36. #else
  37. Depth = tex2Dlod(RT_Depth, float4(screenUV, 0, 0));
  38. #endif
  39. Depth = 1.0 / Depth;
  40. //#else
  41. //#ifdef ExternalDepth
  42. // //injected from water asset
  43. // float Depth = tex2D(_CustomCameraDepthTexture, screenUV).r;
  44. // Depth = 1.0 / Depth;
  45. //
  46. //#else
  47. //full res or Scene view
  48. // float Depth = tex2D(_CameraDepthTexture, screenUV).r;
  49. // Depth = LinearEyeDepth(Depth);
  50. //#endif
  51. #endif
  52. #ifdef ExternalDepth
  53. //injected from water asset
  54. Depth = tex2D(_CustomCameraDepthTexture, screenUV).r;
  55. Depth = 1.0 / Depth;
  56. #else
  57. #if !_FOG_LOWRES_RENDERER
  58. //full res or Scene view
  59. Depth = tex2D(_CameraDepthTexture, screenUV).r;
  60. Depth = LinearEyeDepth(Depth);
  61. #endif
  62. #endif
  63. //probando
  64. //Depth = tex2D(_CameraDepthTexture, screenUV).r;
  65. //Depth = LinearEyeDepth(Depth);
  66. //return float4( Depth.xxx, 1);
  67. Depth = length(Depth / normalize(i.ViewPos).z);
  68. float thickness = min(max(tmin, tmax), Depth) - min(min(tmin, tmax), Depth);
  69. float Fog = thickness / _Visibility;
  70. Fog = 1.0 - exp(-Fog);
  71. //return Fog;
  72. float4 Final = 0;
  73. float3 Normalized_CameraWorldDir = normalize(i.Wpos - _WorldSpaceCameraPos);
  74. float3 CameraLocalDir = (i.LocalPos - i.LocalEyePos);
  75. half InscatteringDistanceClamp = saturate(Depth / InscatteringTransitionWideness - InscatteringStartDistance);
  76. // half InscatteringDistanceClamp = saturate((InscatteringTransitionWideness -Depth) / (InscatteringTransitionWideness- InscatteringStartDistance));
  77. half4 PointLightAccum = 0;
  78. float4 PointLightsFinal = 0;
  79. #if _FOG_VOLUME_NOISE || _FOG_GRADIENT
  80. half jitter = 1;
  81. #ifdef JITTER
  82. jitter = remap_tri(nrand(ScreenUVs + frac(_Time.x)));
  83. jitter = lerp(1, jitter, _jitter);
  84. #endif
  85. float4 Noise = 1;
  86. float3 ShadowColor = 0;
  87. float3 rayStart = i.LocalEyePos + ViewDir * tmin;
  88. float3 rayStop = i.LocalEyePos + ViewDir * tmax;
  89. float3 rayDir = rayStop - rayStart;
  90. float RayLength = length(rayDir);
  91. Speed *= _Time.x;
  92. float4 FinalNoise = 0;
  93. float4 Gradient = 1;
  94. half Contact = 1;
  95. half3 AmbientColor = 1;
  96. half DistanceFade = 0;
  97. half SphereDistance = 0;
  98. half DetailCascade0 = 0;
  99. half DetailCascade1 = 0;
  100. half DetailCascade2 = 0;
  101. half3 Phase = 0;
  102. /*half*/ PrimitiveAccum = 1;
  103. float3 normal = float3(0, 0, 1);
  104. half LambertTerm = 1;
  105. float3 LightTerms = 0;
  106. half SelfShadows = 1;
  107. float OpacityTerms = 0;
  108. half4 debugOutput = 1;
  109. float DirectLightingShadowStepSize = (1.0 / (float)DirectLightingShadowSteps)*_DirectionalLightingDistance;
  110. float3 LightVector = _LightLocalDirection;
  111. //LightVector.xz = LightVector.zx;
  112. //LightVector.z = -LightVector.z;
  113. //LightVector *= DirectLightingShadowStepSize;
  114. float DirectionalLightingAccum = 1;
  115. float3 DirectionalLighting = 1;
  116. half3 AmbientTerm = 1;
  117. half Lambert = 1;
  118. half absorption = 1;
  119. half LightShafts = 1;
  120. half4 VolumeFog = 0;
  121. half LighsShaftsLightVectorConstrain = VolumeSize.y / VolumeSize.x;
  122. float3 LightShaftsDir = L;
  123. LightShaftsDir.xz = LighsShaftsLightVectorConstrain * LightShaftsDir.zx;
  124. float4 debugIterations = float4(0,0,0,1);
  125. float t = 0, dt = _RayStep;
  126. float3 r0 = rayStart;
  127. float3 rd = normalize(rayDir);
  128. #ifdef SAMPLING_METHOD_ViewAligned
  129. float PlaneNdotRay = dot(rd, i.SliceNormal);
  130. dt = _RayStep / abs(PlaneNdotRay);
  131. t = dt - fmod(dot(r0, i.SliceNormal), _RayStep) / PlaneNdotRay;
  132. #endif
  133. #ifdef JITTER
  134. t *= jitter;
  135. dt *= jitter;
  136. #endif
  137. half VolumeRayDistanceTraveled = 0;
  138. for (int s = 1; s < STEP_COUNT && RayLength>0; s += 1, t += dt, RayLength -= dt)
  139. {
  140. dt *= 1 + s * s * s * _OptimizationFactor;//live fast and die young
  141. float3 pos = r0 + rd * t;
  142. VolumeSpaceCoords = pos;
  143. // added casting for ps4
  144. VolumeSpaceCoordsWorldSpace = mul((float3x3)unity_ObjectToWorld, (float3)VolumeSpaceCoords) + _VolumePosition;
  145. float3 NoiseCoordinates = VolumeSpaceCoords * (_3DNoiseScale * Stretch.rgb);
  146. //DistanceFade = distance(VolumeSpaceCoords, i.LocalEyePos);
  147. DistanceFade = distance(VolumeSpaceCoordsWorldSpace, _WorldSpaceCameraPos);//3.2.1
  148. DetailCascade0 = 1 - saturate(DistanceFade / DetailDistance);
  149. DetailCascade1 = 1 - saturate(DistanceFade / DirectLightingDistance);
  150. #if SHADER_API_GLCORE || SHADER_API_D3D11 || SHADER_API_METAL
  151. #if ATTEN_METHOD_1 || ATTEN_METHOD_2 || ATTEN_METHOD_3
  152. DetailCascade2 = 1 - saturate(DistanceFade * PointLightingDistance2Camera);
  153. #endif
  154. #endif
  155. DistanceFade = saturate(DistanceFade / FadeDistance);
  156. DistanceFade = 1 - DistanceFade;
  157. if (DistanceFade < .001) break;
  158. //#ifdef _COLLISION
  159. if (Collisions != 0)
  160. {
  161. Contact = saturate((Depth - distance(VolumeSpaceCoords, i.LocalEyePos))*_SceneIntersectionSoftness);
  162. if (Contact < .01)
  163. break;
  164. //#endif
  165. }
  166. #ifdef DF
  167. PrimitiveAccum = 0;
  168. half3 p = 0;
  169. //Additive primitives
  170. for (int k = 0; k < _PrimitiveCount; k++)
  171. {
  172. if (_PrimitiveActionType(k) <= 1.0f)
  173. {
  174. p = mul(_PrimitivesTransform[k], VolumeSpaceCoords - _PrimitivePosition[k]);
  175. PrimitiveAccum = max(PrimitiveAccum, 1 - (PrimitiveShape(_PrimitiveShapeType(k), p, _PrimitiveScale[k] * .5) + Constrain));
  176. }
  177. }
  178. //Subtractive primitives
  179. for (int n = 0; n < _PrimitiveCount; n++)
  180. {
  181. if (_PrimitiveActionType(k) > 1.0f)
  182. {
  183. p = mul(_PrimitivesTransform[n], VolumeSpaceCoords - _PrimitivePosition[n]);
  184. PrimitiveAccum = min(PrimitiveAccum, (PrimitiveShape(_PrimitiveShapeType(n), p, _PrimitiveScale[n] * .5) + Constrain));
  185. }
  186. }
  187. //Final adjustments
  188. PrimitiveAccum = ContrastF(PrimitiveAccum * _PrimitiveEdgeSoftener, 1);
  189. #endif
  190. #if defined(_FOG_GRADIENT)
  191. half2 GradientCoords = VolumeSpaceCoords.xy / (_BoxMax.xy - _BoxMin.xy) - .5f;
  192. GradientCoords.y *= 0.95;//correct bottom. must check in the future what's wrong with the uv at the edges
  193. GradientCoords.y -= 0.04;//3.1.1
  194. //if(PrimitiveAccum>.99)
  195. if (gain>0)
  196. Gradient = tex2Dlod(_Gradient, half4(GradientCoords, 0, 0));
  197. #endif
  198. VerticalGrad = (VolumeSpaceCoords.y / (_BoxMax.y - _BoxMin.y) + 0.5);
  199. #ifdef VOLUME_FOG
  200. if (OpacityTerms <1)
  201. VolumeRayDistanceTraveled++;
  202. float VolumeDepth01 = (float)VolumeRayDistanceTraveled / STEP_COUNT;
  203. float DistanceCamera2VolumeWalls = length(CameraLocalDir);
  204. float DistanceCamera2Center = distance(_WorldSpaceCameraPos, _VolumePosition);
  205. float DistanceCamera2VolumePoints = distance(_WorldSpaceCameraPos, VolumeSpaceCoordsWorldSpace);
  206. float VolumeDepth = min(max(tmin, tmax), DistanceCamera2VolumePoints) - min(min(tmin, tmax), DistanceCamera2VolumePoints);
  207. float VolumeDensity = DistanceCamera2VolumePoints - DistanceCamera2VolumeWalls;
  208. VolumeFog = saturate(1 - exp(-VolumeDepth / _Visibility * 5));
  209. //VolumeFog= saturate(1 - exp(-VolumeRayDistanceTraveled / _Visibility));
  210. VolumeFog.a *= Contact/**Gradient.a*/;// aqu� hay que decidir si el gradiente se lo come o no
  211. #endif
  212. NoiseAtten = gain;
  213. NoiseAtten *= DistanceFade;
  214. NoiseAtten *= Contact;
  215. #ifdef HEIGHT_GRAD
  216. heightGradient = half2(HeightGradient(VerticalGrad, GradMin, GradMax),
  217. /*3.1.10: Adding secondary gradient*/HeightGradient(VerticalGrad, GradMin2, GradMax2));
  218. NoiseAtten *= heightGradient.x*heightGradient.y;
  219. #endif
  220. #if SPHERICAL_FADE
  221. SphereDistance = 1 - saturate(length(VolumeSpaceCoords) / SphericalFadeDistance);
  222. NoiseAtten *= SphereDistance;
  223. #endif
  224. //TEXTURE SAMPLERS//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  225. #if _FOG_VOLUME_NOISE || _FOG_GRADIENT
  226. #if Twirl_X || Twirl_Y || Twirl_Z
  227. float3 rotationDegree = length(NoiseCoordinates) * _Vortex + _Rotation + _RotationSpeed * _Time.x;
  228. NoiseCoordinates = rotate(NoiseCoordinates , rotationDegree);
  229. #endif
  230. half4 VerticalCoords = float4(VolumeSpaceCoords.zx / (_BoxMax.zx - _BoxMin.zx) - .5f, 0, 0);
  231. #ifdef COVERAGE
  232. half CoverageTile = 1;
  233. half4 CoverageCoords = CoverageTile * float4(VolumeSpaceCoords.xz / (_BoxMax.xz - _BoxMin.xz) - .5f, 0, 0);
  234. half4 CoverageRGB = tex2Dlod(CoverageTex, CoverageCoords);
  235. #ifdef HEIGHT_GRAD
  236. //CoverageRGB.r *=heightGradient.y * (1+heightGradient.x * heightGradient.x);
  237. CoverageRGB.r *= heightGradient.y;
  238. #endif
  239. half CumulonimbusTop = HeightGradient(VerticalGrad, 1.0, .5);
  240. half Cresta = HeightGradient(VerticalGrad, 0.7, 1)*CumulonimbusTop;
  241. Cresta *= 10 * CoverageRGB.r;
  242. NoiseAtten *= CoverageRGB.r;
  243. NoiseAtten += CoverageRGB.r * 4 * CumulonimbusTop + Cresta;
  244. #endif
  245. if (Contact > 0 && NoiseAtten > 0 && PrimitiveAccum > _PrimitiveCutout)
  246. {
  247. #if _FOG_VOLUME_NOISE && !_FOG_GRADIENT
  248. Noise = noise(NoiseCoordinates, DetailCascade0);
  249. #endif
  250. #if !_FOG_VOLUME_NOISE && _FOG_GRADIENT
  251. Gradient.a *= gain;
  252. Noise = Gradient;
  253. #endif
  254. #if _FOG_VOLUME_NOISE && _FOG_GRADIENT
  255. Noise = noise(NoiseCoordinates, DetailCascade0) * Gradient;
  256. #endif
  257. if (Noise.a>0)
  258. Noise *= DistanceFade;
  259. }
  260. else
  261. {
  262. Noise = 0;
  263. Gradient.a = 0;
  264. }
  265. half absorptionFactor = lerp(1, 200, Absorption);
  266. #ifdef ABSORPTION
  267. half d = Noise.a;//si se multiplica aqu� a�ade cotraste
  268. //half absorptionFactor = lerp(1, 20, Absorption);
  269. half Beers = exp(-d* absorptionFactor)* absorptionFactor;//la �ltima multiplicaci�n da contraste
  270. half Powder = 1 - exp(-d * 2);
  271. absorption = lerp(1, saturate(Beers*Powder), Absorption);
  272. #ifdef HEIGHT_GRAD
  273. half HeightGradientAtten = 1 - heightGradient.x;
  274. HeightGradientAtten = 1.0 - exp(-HeightGradientAtten);
  275. absorption *= lerp(1,HeightGradientAtten, HeightAbsorption);
  276. #endif
  277. // AmbientTerm = absorption;3.2
  278. #else
  279. //AmbientTerm = 1;3.2
  280. #endif
  281. #if _LAMBERT_SHADING
  282. if (LightShafts > 0.1)//si estamos en sombra, pos no hagas n�
  283. {
  284. //Lambert lighting
  285. if (Noise.a > 0 && NoiseAtten > 0 && DetailCascade1 > 0)
  286. {
  287. normal = calcNormal(NoiseCoordinates, DetailCascade0);
  288. //normal = normalize(VolumeSpaceCoordsWorldSpace- _VolumePosition);//sphere normals
  289. LambertTerm = max(0, dot(normal, normalize(-L)));
  290. //kind of half lambert
  291. LambertTerm = LambertTerm*0.5 + LambertianBias;
  292. LambertTerm *= LambertTerm;
  293. Lambert = lerp(1, LambertTerm, Noise.a*DirectLightingAmount*ContrastF(DetailCascade1 * 3, 2));
  294. Lambert = max(0.0, Lambert);
  295. }
  296. }
  297. #endif
  298. AmbientColor = _AmbientColor.rgb;
  299. #ifndef SHADOW_PASS
  300. AmbientTerm.rgb = _AmbientColor.rgb;
  301. #endif
  302. half3 ProxyAmbient = 1;
  303. UNITY_BRANCH
  304. if (PROBES == 1) {
  305. //#ifdef PROBES
  306. ProxyAmbient = ShadeSHPerPixel(i.worldNormal, 0, VolumeSpaceCoordsWorldSpace);
  307. AmbientTerm.rgb *= ProxyAmbient/* * _AmbientColor.rgb*/;
  308. AmbientColor *= ProxyAmbient;
  309. }
  310. //#endif
  311. AmbientTerm *= absorption;//3.2
  312. #endif
  313. #if _SHADE
  314. if (Noise.a > 0)
  315. SelfShadows = Shadow(NoiseCoordinates, i, DetailCascade0, LightVector* ShadowShift, NoiseAtten);
  316. #endif
  317. //3.1.10
  318. HeightAtten = HeightGradient(VerticalGrad, _AmbientHeightAbsorptionMin, _AmbientHeightAbsorptionMax);
  319. HeightAtten = saturate(1.0 - exp(-HeightAtten));
  320. if (_AmbientHeightAbsorptionMin == -1 && _AmbientHeightAbsorptionMax == -1)//just to avoid adding one more shader variant
  321. HeightAtten = 1;
  322. //AmbientTerm *= HeightAtten;3.2 ambient shouldn't be affected by this
  323. SelfShadows *= HeightAtten;
  324. //
  325. #if DIRECTIONAL_LIGHTING
  326. float DirectionalLightingSample = 0;
  327. //TODO if (LightShafts > 0.1)
  328. if (NoiseAtten>0 && Noise.a>0)
  329. {
  330. float3 DirectionalLightingSamplingPosition = NoiseCoordinates;
  331. for (int s = 0; s < DirectLightingShadowSteps; s++)
  332. {
  333. DirectionalLightingSamplingPosition += LightVector*DirectLightingShadowStepSize;
  334. DirectionalLightingSample = noise(DirectionalLightingSamplingPosition, DetailCascade0).r;
  335. DirectionalLightingAccum += DirectionalLightingSample/* / DirectLightingShadowSteps*/;
  336. }
  337. DirectionalLighting = DirectionalLightingAccum;
  338. }
  339. DirectionalLighting *= Noise.a;
  340. #endif
  341. #if defined (_INSCATTERING)
  342. #if ABSORPTION
  343. //lets diffuse LambertTerm according to density/absorption
  344. //remap absorption greyscale to -1 1 range to affect anisotropy according to media density
  345. //////// multiply ranges of [0, 1]
  346. half t = (1 - Noise.a) * (InscatteringShape*0.5 + 0.5);
  347. //get back to [-1, 1]
  348. t = t * 2 - 1;
  349. InscatteringShape = lerp(InscatteringShape, (t), Absorption);
  350. // #if ABSORPTION && VOLUME_FOG
  351. // InscatteringShape = lerp(InscatteringShape*absorption, InscatteringShape, Noise.a);
  352. // #endif
  353. #endif
  354. half HG = Henyey(Normalized_CameraWorldDir, L, InscatteringShape);
  355. HG *= InscatteringDistanceClamp * Contact;
  356. Phase = _InscatteringColor.rgb * _InscatteringIntensity * HG * Gradient.xyz * _LightColor.rgb;//3.2;
  357. Phase *= absorption;
  358. #endif
  359. #ifdef HALO
  360. half LdotV = saturate(dot(L, Normalized_CameraWorldDir));
  361. LdotV = pow(LdotV, _HaloRadius);
  362. LdotV = 1 - exp(-LdotV);
  363. LdotV = ContrastF(LdotV, _HaloWidth);//franja
  364. LdotV = saturate(LdotV);
  365. LdotV -= .5;
  366. //#ifdef ABSORPTION
  367. // half HaloDensityTerm = absorption;
  368. //#else
  369. half HaloDensityTerm = Noise.a;
  370. //#endif
  371. half mip = saturate(Noise.a * 12) * _HaloAbsorption * (1 - HaloDensityTerm);
  372. half4 Halo = 0;
  373. if (LdotV >0)
  374. {
  375. Halo = tex2Dlod(_LightHaloTexture, float4(0, LdotV, 0, mip));
  376. Halo.g = tex2Dlod(_LightHaloTexture, float4(0, LdotV * 1.1, 0, mip)/**_HaloOpticalDispersion*/).r;
  377. Halo.b = tex2Dlod(_LightHaloTexture, float4(0, LdotV * 1.2, 0, mip)/**_HaloOpticalDispersion*/).r;
  378. Halo.rgb *= _HaloIntensity;
  379. Halo.rgb *= Halo.a;
  380. Halo.rgb *= HaloDensityTerm * (1.0 - HaloDensityTerm);
  381. Halo.rgb *= 1 - mip / 12;
  382. Halo.rgb *= LdotV;
  383. }
  384. else
  385. Halo = 0;
  386. #endif
  387. OpacityTerms = Noise.a*Contact;
  388. #if DIRECTIONAL_LIGHTING
  389. //Shadow Color
  390. DirectionalLighting /= LightExtinctionColor.rgb;
  391. DirectionalLighting = DirectionalLighting* DirectLightingShadowDensity;
  392. DirectionalLighting = exp(-DirectionalLighting);
  393. Phase *= DirectionalLighting;
  394. #endif
  395. //half3 LightTerms = exp(-DirectionalLighting) * OpacityTerms;
  396. half3 LightTerms = OpacityTerms * AmbientTerm.rgb * DirectionalLighting;//3.2
  397. //#ifdef VOLUMETRIC_SHADOWS
  398. UNITY_BRANCH
  399. if (VOLUMETRIC_SHADOWS == 1) {
  400. // added casting for ps4
  401. half2 shadowUVs = (mul((float3x3)_ShadowCameraProjection, (float3)(VolumeSpaceCoordsWorldSpace - _ShadowCameraPosition)).xy + _ShadowCameraSize) / (_ShadowCameraSize*2.0f);
  402. #ifdef CONVOLVE_VOLUMETRIC_SHADOWS
  403. #define _GeneralShadowOffset 0.003f
  404. float pointDepth = length(_ShadowCameraPosition - VolumeSpaceCoordsWorldSpace);
  405. //
  406. half4 shadows;
  407. shadows.x = step(pointDepth, tex2Dlod(_ShadowTexture, float4(shadowUVs + float2(-_GeneralShadowOffset, -_GeneralShadowOffset), 0, 0)).r);
  408. shadows.y = step(pointDepth, tex2Dlod(_ShadowTexture, float4(shadowUVs + float2(_GeneralShadowOffset, _GeneralShadowOffset), 0, 0)).r);
  409. shadows.z = step(pointDepth, tex2Dlod(_ShadowTexture, float4(shadowUVs + float2(-_GeneralShadowOffset, _GeneralShadowOffset), 0, 0)).r);
  410. shadows.w = step(pointDepth, tex2Dlod(_ShadowTexture, float4(shadowUVs + float2(_GeneralShadowOffset, -_GeneralShadowOffset), 0, 0)).r);
  411. VolumeShadow = max(dot(shadows, 0.25f), 0.0f);
  412. #else
  413. VolumeShadow = FrameShadow(shadowUVs, VolumeSpaceCoordsWorldSpace);
  414. #endif
  415. //3.2
  416. #if defined (_INSCATTERING)
  417. Phase *= VolumeShadow;
  418. #endif
  419. #ifdef HALO
  420. Halo.rgb *= VolumeShadow;
  421. #endif
  422. //3.2
  423. //LightTerms *= lerp(_AmbientColor.rgb, 1, VolumeShadow);//previous 3.2
  424. //LightTerms *= lerp(AmbientTerm.rgb, 1, VolumeShadow);//3.2 test
  425. }
  426. //#endif
  427. //Phase *= LightTerms;//atten with shadowing//3.2 comment
  428. Phase *= OpacityTerms;//3.2
  429. //LightTerms *= lerp(_AmbientColor.rgb, 1, absorption);//previous 3.2
  430. //LightTerms *= lerp(AmbientTerm.rgb, 1, absorption);//3.2 test
  431. #ifdef VOLUME_SHADOWS
  432. half4 LightMapCoords = VerticalCoords;
  433. LightMapCoords.xy = LightMapCoords.yx;
  434. #ifndef LIGHT_ATTACHED
  435. LightMapCoords.xy += LightShaftsDir.zx*(1 - VerticalGrad);
  436. #endif
  437. LightMapCoords.x = clamp(LightMapCoords.x, -1, 0);
  438. LightMapCoords.y = clamp(LightMapCoords.y, -1, 0);
  439. LightShafts = tex2Dlod(LightshaftTex, LightMapCoords).r;
  440. LightShafts = LightShafts*lerp(50,1,_Cutoff);
  441. LightShafts = 1 - saturate(LightShafts);
  442. //LightShafts *= _LightColor.rgb;3.2
  443. #if defined (_INSCATTERING)
  444. Phase *= LightShafts;
  445. #endif
  446. #ifdef HALO
  447. Halo.rgb *= LightShafts; // changed to rgb for ps4
  448. #endif
  449. #endif
  450. #ifdef VOLUME_SHADOWS
  451. LightTerms *= LightShafts;
  452. #endif
  453. LightTerms *= Lambert;
  454. #ifdef _SHADE
  455. float3 SelfShadowsColor = lerp(_SelfShadowColor.rgb * AmbientTerm.rgb, 1.0, SelfShadows);//3.2 replaced ambient color with AmbientTerm
  456. LightTerms *= SelfShadowsColor;
  457. Phase *= SelfShadowsColor;
  458. #endif
  459. //3.1.10
  460. LightTerms *= HeightAtten;
  461. Phase *= HeightAtten;
  462. //
  463. #ifdef VOLUME_FOG
  464. half3 VolumetricFogVolor = _FogColor.rgb;
  465. //#ifdef AMBIENT_AFFECTS_FOG_COLOR
  466. if (AMBIENT_AFFECTS_FOG_COLOR) VolumetricFogVolor *= AmbientColor;
  467. //#endif
  468. #ifdef VOLUME_SHADOWS
  469. LightTerms = lerp(LightTerms, VolumetricFogVolor * Gradient.rgb * (LightShafts + _AmbientColor.a), VolumeFog.a);
  470. #else
  471. LightTerms = lerp(LightTerms, VolumetricFogVolor * Gradient.rgb, VolumeFog.a);
  472. #endif
  473. #if defined (_VOLUME_FOG_INSCATTERING)
  474. half VolumeFogInscatteringDistanceClamp = saturate((VolumeRayDistanceTraveled - VolumeFogInscatteringStartDistance) / VolumeFogInscatteringTransitionWideness);
  475. half3 VolumeFogPhase = Henyey(Normalized_CameraWorldDir, L, VolumeFogInscatteringAnisotropy);
  476. VolumeFogPhase *= Contact;
  477. VolumeFogPhase *= VolumeFogInscatteringDistanceClamp;
  478. VolumeFogPhase *= VolumeFogInscatteringColor.rgb * VolumeFogInscatteringIntensity * Gradient.xyz;
  479. VolumeFogPhase *= saturate(1 - Noise.a * VolumeFogInscatteringIntensity/*pushin' proportionaly to intensity*/);
  480. half3 FogInscatter = 0;
  481. UNITY_BRANCH
  482. if (FOG_TINTS_INSCATTER == 1)
  483. FogInscatter = VolumeFog.a *VolumetricFogVolor * VolumeFogPhase * _LightColor.rgb;
  484. //3.2
  485. else
  486. FogInscatter = VolumeFog.a *VolumetricFogVolor * VolumeFogPhase * _LightColor.rgb + VolumeFog.a * VolumeFogPhase * _LightColor.rgb;
  487. #ifdef VOLUME_SHADOWS
  488. FogInscatter *= LightShafts;
  489. #endif
  490. UNITY_BRANCH
  491. if (VOLUMETRIC_SHADOWS == 1)
  492. FogInscatter *= VolumeShadow;
  493. LightTerms += FogInscatter;
  494. #endif
  495. OpacityTerms = min(OpacityTerms + VolumeFog.a, 1);
  496. #endif
  497. #if defined(_FOG_GRADIENT)
  498. LightTerms *= Gradient.rgb;
  499. #endif
  500. //#ifdef PROBES
  501. //LightTerms *= ProxyAmbient*5;//maybe not
  502. //#endif
  503. LightTerms += Phase;
  504. //Multiply by LambertTerm and color before additive stuff
  505. LightTerms *= _Color.rgb;
  506. LightTerms *= _LightExposure;//new in 3.1.1
  507. LightTerms += AmbientTerm*Noise.a;//multiplicando para no afectar a la niebla
  508. #if SHADER_API_GLCORE || SHADER_API_D3D11 || SHADER_API_METAL
  509. #if ATTEN_METHOD_1 || ATTEN_METHOD_2 || ATTEN_METHOD_3
  510. if (DetailCascade2>0)
  511. {
  512. for (int k = 0; k < _LightsCount; k++)
  513. {
  514. half PointLightRange = 1 - (length(_LightPositions[k].xyz - VolumeSpaceCoords) * PointLightingDistance);//range clamp
  515. if (PointLightRange > .99) {
  516. if (_LightData[k].z >= 0.0)
  517. {
  518. PointLightAccum +=
  519. (Noise.a + VolumeFog.a)*SpotLight(
  520. _LightPositions[k].xyz,
  521. VolumeSpaceCoords,
  522. _LightRange(k),
  523. _LightColors[k],
  524. _LightIntensity(k),
  525. _LightRotations[k],
  526. _LightSpotAngle(k),
  527. _LightColors[k].w, i) * Contact;
  528. }
  529. else
  530. {
  531. PointLightAccum +=
  532. (Noise.a + VolumeFog.a)*PointLight(
  533. _LightPositions[k].xyz,
  534. VolumeSpaceCoords,
  535. _LightRange(k),
  536. _LightColors[k],
  537. _LightIntensity(k),
  538. //1,
  539. i) * Contact;
  540. }
  541. }
  542. }
  543. half atten = saturate(PointLightAccum.a);
  544. if (atten > 0)
  545. {
  546. PointLightsFinal = PointLightAccum;
  547. }
  548. }
  549. #endif
  550. #endif
  551. #ifdef HALO
  552. //LightTerms += Halo.rgb;
  553. //3.1.10
  554. LightTerms *= (1 + Halo.rgb);
  555. #endif
  556. #ifdef DEBUG
  557. if (_DebugMode == DEBUG_ITERATIONS)
  558. {
  559. debugOutput.rgb = tex2Dlod(_PerformanceLUT, float4(0, (float)s / STEP_COUNT, 0, 0)).rgb;
  560. }
  561. if (_DebugMode == DEBUG_INSCATTERING)
  562. {
  563. LightTerms = Phase;
  564. }
  565. if (_DebugMode == DEBUG_VOLUMETRIC_SHADOWS)
  566. {
  567. LightTerms = LightShafts * .05;
  568. }
  569. #if VOLUME_FOG && _VOLUME_FOG_INSCATTERING
  570. if (_DebugMode == DEBUG_VOLUME_FOG_INSCATTER_CLAMP)
  571. {
  572. LightTerms = OpacityTerms * VolumeFogInscatteringDistanceClamp;
  573. }
  574. if (_DebugMode == DEBUG_VOLUME_FOG_PHASE)
  575. {
  576. LightTerms = OpacityTerms * FogInscatter;
  577. }
  578. #endif
  579. #endif
  580. OpacityTerms *= _PushAlpha;
  581. //FinalNoise.a = saturate(FinalNoise.a);
  582. FinalNoise = FinalNoise + float4(LightTerms, OpacityTerms) * (1.0 - FinalNoise.a);
  583. if (FinalNoise.a > .999)break;//KILL'EM ALL if its already opaque, don't do anything else
  584. }
  585. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////LOOP END
  586. #ifdef DEBUG
  587. if (_DebugMode == DEBUG_INSCATTERING
  588. || _DebugMode == DEBUG_VOLUMETRIC_SHADOWS
  589. || _DebugMode == DEBUG_VOLUME_FOG_INSCATTER_CLAMP
  590. || _DebugMode == DEBUG_VOLUME_FOG_PHASE)
  591. {
  592. debugOutput = FinalNoise;
  593. }
  594. return debugOutput;
  595. #endif
  596. //return FinalNoise;
  597. //return float4(Contact, Contact, Contact,1);
  598. //return float4(NoiseAtten, NoiseAtten, NoiseAtten, 1); ;
  599. //FinalNoise.rgb *= _Color.rgb;
  600. _Color = FinalNoise;
  601. _InscatteringColor *= FinalNoise;
  602. #endif
  603. #if _INSCATTERING && !_FOG_VOLUME_NOISE && !_FOG_GRADIENT
  604. float Inscattering = Henyey(Normalized_CameraWorldDir, L, InscatteringShape);
  605. //_InscatteringIntensity *= .05;
  606. Inscattering *= InscatteringDistanceClamp;
  607. Final = float4(_Color.rgb + _InscatteringColor.rgb * _InscatteringIntensity * Inscattering, _Color.a);
  608. #else
  609. Final = _Color;
  610. #endif
  611. #if ATTEN_METHOD_1 || ATTEN_METHOD_2 || ATTEN_METHOD_3
  612. Final.rgb += PointLightsFinal.rgb;
  613. #endif
  614. #ifdef ColorAdjust
  615. Final.rgb = lerp(Final.rgb, pow(max((Final.rgb + Offset), 0), 1 / Gamma), Final.a);
  616. #if _TONEMAP
  617. Final.rgb = ToneMap(Final.rgb, Exposure);
  618. #endif
  619. #endif
  620. #if !_FOG_VOLUME_NOISE && !_FOG_GRADIENT
  621. Final.a *= (Fog * _Color.a);
  622. #endif
  623. if (IsGammaSpace())
  624. Final.rgb = pow(Final.rgb, 1 / 2.2);
  625. Final.a = saturate(Final.a);
  626. Final.rgb = SafeHDR(Final.rgb);
  627. #ifdef Enviro_Integration
  628. float4 EnviroFog = TransparentFog(Final, i.Wpos, screenUV, Depth);
  629. EnviroFog.a = EnviroFog.a * Final.a;
  630. Final = EnviroFog;
  631. #endif
  632. //Debug the build problem
  633. #if DF && defined(DEBUG_PRIMITIVES)
  634. //Final.rgb = _PrimitiveCount/1.5;
  635. //Final.a = 0;
  636. Final.rgb = float3(0,.5, .5);
  637. #endif
  638. return Final;
  639. }
  640. #endif