Light2D-Point.shader 4.7 KB

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