Ei kuvausta
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.

Light2D-Shape.shader 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. Shader "Hidden/Light2D-Shape"
  2. {
  3. Properties
  4. {
  5. [HideInInspector] _SrcBlend("__src", Float) = 1.0
  6. [HideInInspector] _DstBlend("__dst", Float) = 0.0
  7. [Enum(UnityEngine.Rendering.CompareFunction)] _HandleZTest ("_HandleZTest", Int) = 4
  8. }
  9. SubShader
  10. {
  11. Tags { "Queue" = "Transparent" "RenderType" = "Transparent" "RenderPipeline" = "UniversalPipeline" }
  12. Pass
  13. {
  14. Blend [_SrcBlend][_DstBlend]
  15. ZWrite Off
  16. ZTest [_HandleZTest]
  17. Cull Off
  18. HLSLPROGRAM
  19. #pragma vertex vert
  20. #pragma fragment frag
  21. #pragma multi_compile_local USE_NORMAL_MAP __
  22. #pragma multi_compile_local LIGHT_QUALITY_FAST __
  23. #pragma multi_compile_local USE_ADDITIVE_BLENDING __
  24. #pragma multi_compile_local USE_VOLUMETRIC __
  25. #pragma multi_compile USE_SHAPE_LIGHT_TYPE_0 __
  26. #pragma multi_compile USE_SHAPE_LIGHT_TYPE_1 __
  27. #pragma multi_compile USE_SHAPE_LIGHT_TYPE_2 __
  28. #pragma multi_compile USE_SHAPE_LIGHT_TYPE_3 __
  29. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  30. #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/LightingUtility.hlsl"
  31. struct Attributes
  32. {
  33. float3 positionOS : POSITION;
  34. float4 color : COLOR;
  35. float2 uv : TEXCOORD0;
  36. };
  37. struct Varyings
  38. {
  39. float4 positionCS : SV_POSITION;
  40. half4 color : COLOR;
  41. half2 uv : TEXCOORD0;
  42. SHADOW_COORDS(TEXCOORD1)
  43. NORMALS_LIGHTING_COORDS(TEXCOORD2, TEXCOORD3)
  44. LIGHT_OFFSET(TEXCOORD4)
  45. };
  46. UNITY_LIGHT2D_DATA
  47. half _InverseHDREmulationScale;
  48. TEXTURE2D(_CookieTex); // This can either be a sprite texture uv or a falloff texture
  49. SAMPLER(sampler_CookieTex);
  50. TEXTURE2D(_FalloffLookup);
  51. SAMPLER(sampler_FalloffLookup);
  52. NORMALS_LIGHTING_VARIABLES
  53. SHADOW_VARIABLES
  54. Varyings vert(Attributes a)
  55. {
  56. Varyings o = (Varyings)0;
  57. o.lightOffset = a.color;
  58. #if USE_STRUCTURED_BUFFER_FOR_LIGHT2D_DATA
  59. PerLight2D light = GetPerLight2D(o.lightOffset);
  60. #endif
  61. float3 positionOS = a.positionOS;
  62. positionOS.x = positionOS.x + _L2D_FALLOFF_DISTANCE * a.color.r;
  63. positionOS.y = positionOS.y + _L2D_FALLOFF_DISTANCE * a.color.g;
  64. o.positionCS = TransformObjectToHClip(positionOS);
  65. o.color = _L2D_COLOR * _InverseHDREmulationScale;
  66. o.color.a = a.color.a;
  67. #if USE_VOLUMETRIC
  68. o.color.a = _L2D_COLOR.a * _L2D_VOLUME_OPACITY;
  69. #endif
  70. // If Sprite use UV.
  71. o.uv = (_L2D_LIGHT_TYPE == 2) ? a.uv : float2(a.color.a, _L2D_FALLOFF_INTENSITY);
  72. float4 worldSpacePos;
  73. worldSpacePos.xyz = TransformObjectToWorld(positionOS);
  74. worldSpacePos.w = 1;
  75. TRANSFER_NORMALS_LIGHTING(o, worldSpacePos, _L2D_POSITION.xyz, _L2D_POSITION.w)
  76. TRANSFER_SHADOWS(o)
  77. return o;
  78. }
  79. FragmentOutput frag(Varyings i) : SV_Target
  80. {
  81. #if USE_STRUCTURED_BUFFER_FOR_LIGHT2D_DATA
  82. PerLight2D light = GetPerLight2D(i.lightOffset);
  83. #endif
  84. half4 lightColor = i.color;
  85. if (_L2D_LIGHT_TYPE == 2)
  86. {
  87. half4 cookie = SAMPLE_TEXTURE2D(_CookieTex, sampler_CookieTex, i.uv);
  88. #if USE_ADDITIVE_BLENDING
  89. lightColor *= cookie * cookie.a;
  90. #else
  91. lightColor *= cookie;
  92. #endif
  93. }
  94. else
  95. {
  96. #if USE_ADDITIVE_BLENDING
  97. lightColor *= SAMPLE_TEXTURE2D(_FalloffLookup, sampler_FalloffLookup, i.uv).r;
  98. #elif USE_VOLUMETRIC
  99. lightColor.a = i.color.a * SAMPLE_TEXTURE2D(_FalloffLookup, sampler_FalloffLookup, i.uv).r;
  100. #else
  101. lightColor.a = SAMPLE_TEXTURE2D(_FalloffLookup, sampler_FalloffLookup, i.uv).r;
  102. #endif
  103. }
  104. APPLY_NORMALS_LIGHTING(i, lightColor, _L2D_POSITION.xyz, _L2D_POSITION.w);
  105. APPLY_SHADOWS(i, lightColor, _L2D_SHADOW_INTENSITY);
  106. return ToFragmentOutput(lightColor);
  107. }
  108. ENDHLSL
  109. }
  110. }
  111. }