Lux URP FX SphereVolume.shader 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. Shader "Lux URP/FX/Sphere Volume"
  2. {
  3. Properties
  4. {
  5. [HeaderHelpLuxURP_URL(t98mzd66fi0m)]
  6. [Header(Surface Options)]
  7. [Space(8)]
  8. [Enum(UnityEngine.Rendering.CompareFunction)]
  9. _ZTest ("ZTest", Int) = 8
  10. [Enum(UnityEngine.Rendering.CullMode)]
  11. _Cull ("Culling", Float) = 1
  12. [Toggle(ORTHO_SUPPORT)]
  13. _OrthoSpport ("Enable Orthographic Support", Float) = 0
  14. [Header(Surface Inputs)]
  15. [Space(8)]
  16. _Color ("Color", Color) = (1, 1, 1, 1)
  17. [Toggle(_ENABLEGRADIENT)]
  18. _EnableGradient ("Enable Gradient", Float) = 0
  19. [NoScaleOffset]
  20. _MainTex (" Thickness Gradient", 2D) = "white" {}
  21. [Header(Thickness Remap)]
  22. [Space(8)]
  23. _Lower (" Lower", Range(0,1)) = 0
  24. _Upper (" Upper", Range(0,4)) = 1
  25. //[Space(5)]
  26. //_SoftEdge (" Soft Edge Factor", Float) = 2.0
  27. [Space(5)]
  28. [Toggle(_APPLYFOG)]
  29. _ApplyFog ("Enable Fog", Float) = 0.0
  30. [Toggle(_HQFOG)]
  31. _HQFog (" HQ Fog", Float) = 0.0
  32. }
  33. SubShader
  34. {
  35. Tags
  36. {
  37. "RenderPipeline" = "UniversalPipeline"
  38. "RenderType"="Opaque"
  39. "Queue"= "Transparent+50"
  40. }
  41. Pass
  42. {
  43. Name "StandardUnlit"
  44. Tags{"LightMode" = "UniversalForward"}
  45. Blend SrcAlpha OneMinusSrcAlpha
  46. // As we want to be able to enter the volume we have to draw the back faces
  47. Cull [_Cull]
  48. // We fully rely on the depth texture sample!
  49. ZTest [_ZTest]
  50. ZWrite Off
  51. ColorMask RGB
  52. HLSLPROGRAM
  53. // Required to compile gles 2.0 with standard srp library
  54. #pragma prefer_hlslcc gles
  55. #pragma exclude_renderers d3d11_9x
  56. #pragma target 2.0
  57. #pragma shader_feature_local _ENABLEGRADIENT
  58. #pragma shader_feature_local _APPLYFOG
  59. #pragma shader_feature_local ORTHO_SUPPORT
  60. // -------------------------------------
  61. // Unity defined keywords
  62. #if defined(_APPLYFOG)
  63. #pragma multi_compile_fog
  64. #pragma shader_feature_local _HQFOG
  65. #endif
  66. //--------------------------------------
  67. // GPU Instancing
  68. #pragma multi_compile_instancing
  69. #pragma vertex vert
  70. #pragma fragment frag
  71. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  72. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
  73. #include "Packages/com.unity.render-pipelines.universal/Shaders/UnlitInput.hlsl"
  74. CBUFFER_START(UnityPerMaterial)
  75. half4 _Color;
  76. half _Lower;
  77. half _Upper;
  78. //half _SoftEdge;
  79. CBUFFER_END
  80. // Stereo-related bits - backported to LWRP
  81. #if defined(UNITY_STEREO_INSTANCING_ENABLED) || defined(UNITY_STEREO_MULTIVIEW_ENABLED)
  82. #define LUX_SLICE_ARRAY_INDEX unity_StereoEyeIndex
  83. #define LUX_TEXTURE2D_X TEXTURE2D_ARRAY
  84. #define LUX_TEXTURE2D_X_FLOAT TEXTURE2D_ARRAY_FLOAT
  85. #define LUX_LOAD_TEXTURE2D_X(textureName, unCoord2) LOAD_TEXTURE2D_ARRAY(textureName, unCoord2, LUX_SLICE_ARRAY_INDEX)
  86. #define LUX_SAMPLE_TEXTURE2D_X(textureName, samplerName, coord2) SAMPLE_TEXTURE2D_ARRAY(textureName, samplerName, coord2, LUX_SLICE_ARRAY_INDEX)
  87. #else
  88. #define LUX_SLICE_ARRAY_INDEX 0
  89. #define LUX_TEXTURE2D_X TEXTURE2D
  90. #define LUX_TEXTURE2D_X_FLOAT TEXTURE2D_FLOAT
  91. #define LUX_LOAD_TEXTURE2D_X LOAD_TEXTURE2D
  92. #define LUX_SAMPLE_TEXTURE2D_X SAMPLE_TEXTURE2D
  93. #endif
  94. #if defined(_ENABLEGRADIENT)
  95. TEXTURE2D(_MainTex); SAMPLER(sampler_MainTex);
  96. #endif
  97. #if defined(SHADER_API_GLES)
  98. TEXTURE2D(_CameraDepthTexture); SAMPLER(sampler_CameraDepthTexture);
  99. #else
  100. LUX_TEXTURE2D_X_FLOAT(_CameraDepthTexture);
  101. #endif
  102. float4 _CameraDepthTexture_TexelSize;
  103. struct VertexInput
  104. {
  105. float4 vertex : POSITION;
  106. UNITY_VERTEX_INPUT_INSTANCE_ID
  107. };
  108. struct VertexOutput
  109. {
  110. float4 positionCS : SV_POSITION;
  111. float3 positionWS : TEXCOORD1;
  112. float2 projectedPosition : TEXCOORD2;
  113. float3 cameraPositionOS : TEXCOORD4;
  114. float scale : TEXCOORD5;
  115. #if defined(_APPLYFOG)
  116. half fogCoord : TEXCOORD0;
  117. #endif
  118. UNITY_VERTEX_INPUT_INSTANCE_ID
  119. UNITY_VERTEX_OUTPUT_STEREO
  120. };
  121. VertexOutput vert (VertexInput input)
  122. {
  123. VertexOutput o = (VertexOutput)0;
  124. UNITY_SETUP_INSTANCE_ID(input);
  125. UNITY_TRANSFER_INSTANCE_ID(input, o);
  126. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  127. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.vertex.xyz);
  128. o.positionCS = vertexInput.positionCS;
  129. o.positionWS = vertexInput.positionWS;
  130. o.projectedPosition = vertexInput.positionNDC.xy;
  131. o.cameraPositionOS = mul(GetWorldToObjectMatrix(), float4(_WorldSpaceCameraPos, 1)).xyz;
  132. float4x4 ObjectToWorldMatrix = GetObjectToWorldMatrix();
  133. float3 worldScale = float3(
  134. length(ObjectToWorldMatrix._m00_m10_m20), // scale x axis
  135. length(ObjectToWorldMatrix._m01_m11_m21), // scale y axis
  136. length(ObjectToWorldMatrix._m02_m12_m22) // scale z axis
  137. );
  138. o.scale = 1.0f / max(worldScale.x, max(worldScale.y, worldScale.z));
  139. #if defined(_APPLYFOG)
  140. o.fogCoord = ComputeFogFactor(o.positionCS.z);
  141. #endif
  142. return o;
  143. }
  144. // Ray-sphere intersection.
  145. // Returns the distance to the first and second intersection.
  146. bool IntersectRaySphere (float3 rayStart, float3 rayDir, float3 sc, float radius, out float2 intersections)
  147. {
  148. rayStart -= sc;
  149. float a = dot(rayDir, rayDir);
  150. float b = dot(rayStart, rayDir) * 2.0f;
  151. float c = dot(rayStart, rayStart) - radius * radius; // radius is fixed: 0.5, should be optimized by the compiler
  152. float discriminant = b * b - 4.0f * a * c;
  153. if (discriminant < 0.0f) {
  154. return false;
  155. }
  156. else {
  157. discriminant = sqrt(discriminant);
  158. intersections = float2(-b - discriminant, -b + discriminant) / (2.0f * a);
  159. // When the camera is inside the volume we may get negative values so the sphere from behind the camera gets "mirrored" into the view.
  160. intersections.x = max(intersections.x, 0);
  161. return true;
  162. }
  163. }
  164. real LuxComputeFogFactor(float z)
  165. {
  166. float clipZ_01 = UNITY_Z_0_FAR_FROM_CLIPSPACE(z);
  167. #if defined(FOG_LINEAR)
  168. // factor = (end-z)/(end-start) = z * (-1/(end-start)) + (end/(end-start))
  169. float fogFactor = saturate(clipZ_01 * unity_FogParams.z + unity_FogParams.w);
  170. return real(fogFactor);
  171. #elif defined(FOG_EXP) || defined(FOG_EXP2)
  172. // factor = exp(-(density*z)^2)
  173. // -density * z computed at vertex
  174. return real(unity_FogParams.x * clipZ_01);
  175. #else
  176. return 0.0h;
  177. #endif
  178. }
  179. // ------------------------------------------------------------------
  180. // Helper functions to handle orthographic / perspective projection
  181. inline float GetOrthoDepthFromZBuffer (float rawDepth) {
  182. #if defined(UNITY_REVERSED_Z)
  183. // Needed to handle openGL
  184. #if UNITY_REVERSED_Z == 1
  185. rawDepth = 1.0f - rawDepth;
  186. #endif
  187. #endif
  188. return lerp(_ProjectionParams.y, _ProjectionParams.z, rawDepth);
  189. }
  190. inline float GetProperEyeDepth (float rawDepth) {
  191. #if defined(ORTHO_SUPPORT)
  192. float perspectiveSceneDepth = LinearEyeDepth(rawDepth, _ZBufferParams);
  193. float orthoSceneDepth = GetOrthoDepthFromZBuffer(rawDepth);
  194. return lerp(perspectiveSceneDepth, orthoSceneDepth, unity_OrthoParams.w);
  195. #else
  196. return LinearEyeDepth(rawDepth, _ZBufferParams);
  197. #endif
  198. }
  199. half4 frag (VertexOutput input) : SV_Target
  200. {
  201. UNITY_SETUP_INSTANCE_ID(input);
  202. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  203. half4 color = half4(1,1,1,0);
  204. #if defined(ORTHO_SUPPORT)
  205. input.positionCS.w = lerp(input.positionCS.w, 1.0f, unity_OrthoParams.w);
  206. #endif
  207. float2 screenUV = input.projectedPosition.xy / input.positionCS.w;
  208. // Fix screenUV for Single Pass Stereo Rendering
  209. #if defined(UNITY_SINGLE_PASS_STEREO)
  210. screenUV.x = screenUV.x * 0.5f + (float)unity_StereoEyeIndex * 0.5f;
  211. #endif
  212. float3 viewDirWS = normalize(input.positionWS - _WorldSpaceCameraPos);
  213. // Scene depth as linear eye depth
  214. #if defined(SHADER_API_GLES)
  215. float sceneZ = SAMPLE_DEPTH_TEXTURE_LOD(_CameraDepthTexture, sampler_CameraDepthTexture, screenUV, 0);
  216. #else
  217. float sceneZ = LUX_LOAD_TEXTURE2D_X(_CameraDepthTexture, _CameraDepthTexture_TexelSize.zw * screenUV).x;
  218. #endif
  219. sceneZ = GetProperEyeDepth(sceneZ);
  220. // Convert linear eye depth to distance in world space
  221. float3 camForward = UNITY_MATRIX_V[2].xyz;
  222. float sceneDistance = sceneZ / dot(-viewDirWS, camForward);
  223. float3 rayDir = mul(GetWorldToObjectMatrix(), float4(viewDirWS, 0)).xyz;
  224. float3 rayStart = input.cameraPositionOS;
  225. float2 intersections = 0;
  226. bool intersect = IntersectRaySphere(rayStart , rayDir, float3(0, 0, 0), 0.5, intersections);
  227. // Not needed if we use a sphere.
  228. // UNITY_BRANCH
  229. // if (intersect) {
  230. // Entry point in world space
  231. float3 entry = mul(GetObjectToWorldMatrix(), float4(rayStart + rayDir * intersections.x, 1)).xyz;
  232. float distanceToEntry = length(entry - _WorldSpaceCameraPos);
  233. float sceneToEntry = sceneDistance - distanceToEntry;
  234. // Nothing to do if the scene is in front of the entry point
  235. clip(sceneToEntry);
  236. // Exit point in world space
  237. float3 exit = mul(GetObjectToWorldMatrix(), float4(rayStart + rayDir * intersections.y, 1)).xyz;
  238. float maxTravel = distance(exit, entry);
  239. float denom = min(sceneToEntry, maxTravel);
  240. float percentage = maxTravel / denom;
  241. percentage = rcp(percentage);
  242. // This only attenuates alpha in object space :(
  243. float3 mid = rayStart + rayDir * (intersections.x + intersections.y) * 0.5;
  244. float alpha = 1 - length(mid) * 2.0;
  245. // Smooth falloff - only the object space falloff
  246. alpha = smoothstep(_Lower, _Upper, alpha);
  247. // In order to factor in object scale and dimensions we multiply alpha by maxTravel. / Not really correct
  248. alpha *= maxTravel * input.scale * percentage;
  249. // Smooth falloff
  250. //alpha = smoothstep(_Lower, _Upper, alpha);
  251. // Scene blending
  252. //alpha *= saturate(sceneToEntry / _SoftEdge);
  253. // saturate eliminates artifacts at grazing angles
  254. color.a = saturate(alpha);
  255. #if defined(_ENABLEGRADIENT)
  256. color.rgb = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, float2(alpha, 0.5)).rgb;
  257. #endif
  258. // }
  259. color *= _Color;
  260. #if defined(_APPLYFOG)
  261. #if defined(_HQFOG)
  262. float3 exitFog = mul(GetObjectToWorldMatrix(), float4(rayStart + rayDir * intersections.y * sqrt(percentage), 1)).xyz;
  263. float4 FogClipSpace = TransformWorldToHClip(exitFog);
  264. float fogFactor = LuxComputeFogFactor( FogClipSpace.z);
  265. color.rgb = MixFog(color.rgb, fogFactor);
  266. #else
  267. color.rgb = MixFog(color.rgb, input.fogCoord);
  268. #endif
  269. #endif
  270. return color;
  271. }
  272. ENDHLSL
  273. }
  274. }
  275. FallBack "Hidden/InternalErrorShader"
  276. }