Light2D-Shape.shader 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. Shader "Hidden/Light2D-Shape"
  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 SPRITE_LIGHT __
  20. #pragma multi_compile_local USE_NORMAL_MAP __
  21. #pragma multi_compile_local USE_ADDITIVE_BLENDING __
  22. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  23. #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/LightingUtility.hlsl"
  24. struct Attributes
  25. {
  26. float3 positionOS : POSITION;
  27. float4 color : COLOR;
  28. float2 uv : TEXCOORD0;
  29. };
  30. struct Varyings
  31. {
  32. float4 positionCS : SV_POSITION;
  33. half4 color : COLOR;
  34. half2 uv : TEXCOORD0;
  35. SHADOW_COORDS(TEXCOORD1)
  36. NORMALS_LIGHTING_COORDS(TEXCOORD2, TEXCOORD3)
  37. };
  38. half _InverseHDREmulationScale;
  39. half4 _LightColor;
  40. half _FalloffDistance;
  41. #ifdef SPRITE_LIGHT
  42. TEXTURE2D(_CookieTex); // This can either be a sprite texture uv or a falloff texture
  43. SAMPLER(sampler_CookieTex);
  44. #else
  45. half _FalloffIntensity;
  46. TEXTURE2D(_FalloffLookup);
  47. SAMPLER(sampler_FalloffLookup);
  48. #endif
  49. NORMALS_LIGHTING_VARIABLES
  50. SHADOW_VARIABLES
  51. Varyings vert(Attributes attributes)
  52. {
  53. Varyings o = (Varyings)0;
  54. float3 positionOS = attributes.positionOS;
  55. positionOS.x = positionOS.x + _FalloffDistance * attributes.color.r;
  56. positionOS.y = positionOS.y + _FalloffDistance * attributes.color.g;
  57. o.positionCS = TransformObjectToHClip(positionOS);
  58. o.color = _LightColor * _InverseHDREmulationScale;
  59. o.color.a = attributes.color.a;
  60. #ifdef SPRITE_LIGHT
  61. o.uv = attributes.uv;
  62. #else
  63. o.uv = float2(o.color.a, _FalloffIntensity);
  64. #endif
  65. float4 worldSpacePos;
  66. worldSpacePos.xyz = TransformObjectToWorld(positionOS);
  67. worldSpacePos.w = 1;
  68. TRANSFER_NORMALS_LIGHTING(o, worldSpacePos)
  69. TRANSFER_SHADOWS(o)
  70. return o;
  71. }
  72. half4 frag(Varyings i) : SV_Target
  73. {
  74. half4 color = i.color;
  75. #if SPRITE_LIGHT
  76. half4 cookie = SAMPLE_TEXTURE2D(_CookieTex, sampler_CookieTex, i.uv);
  77. #if USE_ADDITIVE_BLENDING
  78. color *= cookie * cookie.a;
  79. #else
  80. color *= cookie;
  81. #endif
  82. #else
  83. #if USE_ADDITIVE_BLENDING
  84. color *= SAMPLE_TEXTURE2D(_FalloffLookup, sampler_FalloffLookup, i.uv).r;
  85. #else
  86. color.a = SAMPLE_TEXTURE2D(_FalloffLookup, sampler_FalloffLookup, i.uv).r;
  87. #endif
  88. #endif
  89. APPLY_NORMALS_LIGHTING(i, color);
  90. APPLY_SHADOWS(i, color, _ShadowIntensity);
  91. return color;
  92. }
  93. ENDHLSL
  94. }
  95. }
  96. }