Light2D-Point-Volumetric.shader 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. Shader "Hidden/Light2d-Point-Volumetric"
  2. {
  3. SubShader
  4. {
  5. Tags { "Queue" = "Transparent" "RenderType" = "Transparent" "RenderPipeline" = "UniversalPipeline" }
  6. Pass
  7. {
  8. Blend One One
  9. ZWrite Off
  10. Cull Off
  11. HLSLPROGRAM
  12. #pragma vertex vert
  13. #pragma fragment frag
  14. #pragma multi_compile_local USE_POINT_LIGHT_COOKIES __
  15. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  16. #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/LightingUtility.hlsl"
  17. struct Attributes
  18. {
  19. float3 positionOS : POSITION;
  20. float2 texcoord : TEXCOORD0;
  21. };
  22. struct Varyings
  23. {
  24. float4 positionCS : SV_POSITION;
  25. half2 uv : TEXCOORD0;
  26. half2 lookupUV : TEXCOORD2; // This is used for light relative direction
  27. SHADOW_COORDS(TEXCOORD5)
  28. };
  29. #if USE_POINT_LIGHT_COOKIES
  30. TEXTURE2D(_PointLightCookieTex);
  31. SAMPLER(sampler_PointLightCookieTex);
  32. #endif
  33. TEXTURE2D(_FalloffLookup);
  34. SAMPLER(sampler_FalloffLookup);
  35. half _FalloffIntensity;
  36. TEXTURE2D(_LightLookup);
  37. SAMPLER(sampler_LightLookup);
  38. half4 _LightLookup_TexelSize;
  39. half4 _LightColor;
  40. half _VolumeOpacity;
  41. float4 _LightPosition;
  42. float4x4 _LightInvMatrix;
  43. float4x4 _LightNoRotInvMatrix;
  44. half _LightZDistance;
  45. half _OuterAngle; // 1-0 where 1 is the value at 0 degrees and 1 is the value at 180 degrees
  46. half _InnerAngleMult; // 1-0 where 1 is the value at 0 degrees and 1 is the value at 180 degrees
  47. half _InnerRadiusMult; // 1-0 where 1 is the value at the center and 0 is the value at the outer radius
  48. half _InverseHDREmulationScale;
  49. half _IsFullSpotlight;
  50. SHADOW_VARIABLES
  51. Varyings vert(Attributes input)
  52. {
  53. Varyings output = (Varyings)0;
  54. output.positionCS = TransformObjectToHClip(input.positionOS);
  55. output.uv = input.texcoord;
  56. float4 worldSpacePos;
  57. worldSpacePos.xyz = TransformObjectToWorld(input.positionOS);
  58. worldSpacePos.w = 1;
  59. float4 lightSpacePos = mul(_LightInvMatrix, worldSpacePos);
  60. float4 lightSpaceNoRotPos = mul(_LightNoRotInvMatrix, worldSpacePos);
  61. float halfTexelOffset = 0.5 * _LightLookup_TexelSize.x;
  62. output.lookupUV = 0.5 * (lightSpacePos.xy + 1) + halfTexelOffset;
  63. TRANSFER_SHADOWS(output)
  64. return output;
  65. }
  66. half4 frag(Varyings input) : SV_Target
  67. {
  68. half4 lookupValue = SAMPLE_TEXTURE2D(_LightLookup, sampler_LightLookup, input.lookupUV); // r = distance, g = angle, b = x direction, a = y direction
  69. // Inner Radius
  70. half attenuation = saturate(_InnerRadiusMult * lookupValue.r); // This is the code to take care of our inner radius
  71. // Spotlight
  72. half spotAttenuation = saturate((_OuterAngle - lookupValue.g + _IsFullSpotlight) * _InnerAngleMult);
  73. attenuation = attenuation * spotAttenuation;
  74. half2 mappedUV;
  75. mappedUV.x = attenuation;
  76. mappedUV.y = _FalloffIntensity;
  77. attenuation = SAMPLE_TEXTURE2D(_FalloffLookup, sampler_FalloffLookup, mappedUV).r;
  78. #if USE_POINT_LIGHT_COOKIES
  79. half4 cookieColor = SAMPLE_TEXTURE2D(_PointLightCookieTex, sampler_PointLightCookieTex, input.lookupUV);
  80. half4 lightColor = cookieColor * _LightColor * attenuation;
  81. #else
  82. half4 lightColor = _LightColor * attenuation;
  83. #endif
  84. APPLY_SHADOWS(input, lightColor, _ShadowVolumeIntensity);
  85. return _VolumeOpacity * lightColor * _InverseHDREmulationScale;
  86. }
  87. ENDHLSL
  88. }
  89. }
  90. }