설명 없음
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.shader 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. Shader "Hidden/Light2D"
  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) = 0
  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 USE_ADDITIVE_BLENDING __
  23. #pragma multi_compile_local USE_VOLUMETRIC __
  24. #pragma multi_compile_local USE_POINT_LIGHT_COOKIES __
  25. #pragma multi_compile_local LIGHT_QUALITY_FAST __
  26. #include_with_pragmas "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/ShapeLightShared.hlsl"
  27. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  28. #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/LightingUtility.hlsl"
  29. struct Attributes
  30. {
  31. float3 positionOS : POSITION;
  32. float4 color : COLOR;
  33. float2 uv : TEXCOORD0;
  34. };
  35. struct Varyings
  36. {
  37. float4 positionCS : SV_POSITION;
  38. half4 color : COLOR;
  39. half2 uv : TEXCOORD0;
  40. half2 lookupUV : TEXCOORD1; // This is used for light relative direction
  41. SHADOW_COORDS(TEXCOORD2)
  42. NORMALS_LIGHTING_COORDS(TEXCOORD3, TEXCOORD4)
  43. LIGHT_OFFSET(TEXCOORD5)
  44. };
  45. TEXTURE2D(_CookieTex); // This can either be a sprite texture uv or a falloff texture
  46. SAMPLER(sampler_CookieTex);
  47. TEXTURE2D(_FalloffLookup);
  48. SAMPLER(sampler_FalloffLookup);
  49. TEXTURE2D(_LightLookup);
  50. SAMPLER(sampler_LightLookup);
  51. half4 _LightLookup_TexelSize;
  52. #if USE_POINT_LIGHT_COOKIES
  53. TEXTURE2D(_PointLightCookieTex);
  54. SAMPLER(sampler_PointLightCookieTex);
  55. #endif
  56. NORMALS_LIGHTING_VARIABLES
  57. SHADOW_VARIABLES
  58. UNITY_LIGHT2D_DATA
  59. half _InverseHDREmulationScale;
  60. Varyings vert_shape(Attributes a, PerLight2D light)
  61. {
  62. Varyings o = (Varyings)0;
  63. float3 positionOS = a.positionOS;
  64. positionOS.x = positionOS.x + _L2D_FALLOFF_DISTANCE * a.color.r;
  65. positionOS.y = positionOS.y + _L2D_FALLOFF_DISTANCE * a.color.g;
  66. o.positionCS = TransformObjectToHClip(positionOS);
  67. o.color = _L2D_COLOR * _InverseHDREmulationScale;
  68. o.color.a = a.color.a;
  69. #if USE_VOLUMETRIC
  70. o.color.a = _L2D_COLOR.a * _L2D_VOLUME_OPACITY;
  71. #endif
  72. // If Sprite use UV.
  73. o.uv = (_L2D_LIGHT_TYPE == 2) ? a.uv : float2(a.color.a, _L2D_FALLOFF_INTENSITY);
  74. float4 worldSpacePos;
  75. worldSpacePos.xyz = TransformObjectToWorld(positionOS);
  76. worldSpacePos.w = 1;
  77. TRANSFER_NORMALS_LIGHTING(o, worldSpacePos, _L2D_POSITION.xyz, _L2D_POSITION.w)
  78. TRANSFER_SHADOWS(o)
  79. return o;
  80. }
  81. Varyings vert_point(Attributes a, PerLight2D light)
  82. {
  83. Varyings output = (Varyings)0;
  84. output.positionCS = TransformObjectToHClip(a.positionOS);
  85. output.uv = a.uv;
  86. float4 worldSpacePos;
  87. worldSpacePos.xyz = TransformObjectToWorld(a.positionOS);
  88. worldSpacePos.w = 1;
  89. float4 lightSpacePos = mul(_L2D_INVMATRIX, worldSpacePos);
  90. float halfTexelOffset = 0.5 * _LightLookup_TexelSize.x;
  91. output.lookupUV = 0.5 * (lightSpacePos.xy + 1) + halfTexelOffset;
  92. TRANSFER_NORMALS_LIGHTING(output, worldSpacePos, _L2D_POSITION.xyz, _L2D_POSITION.w)
  93. TRANSFER_SHADOWS(output)
  94. return output;
  95. }
  96. Varyings vert(Attributes attributes)
  97. {
  98. PerLight2D light;
  99. #if USE_STRUCTURED_BUFFER_FOR_LIGHT2D_DATA
  100. light = GetPerLight2D(attributes.color);
  101. #endif
  102. switch (_L2D_LIGHT_TYPE)
  103. {
  104. case 0:
  105. case 1:
  106. case 2:
  107. {
  108. Varyings v = vert_shape(attributes, light);
  109. v.lightOffset = attributes.color;
  110. return v;
  111. }
  112. break;
  113. case 3:
  114. {
  115. Varyings v = vert_point(attributes, light);
  116. v.lightOffset = attributes.color;
  117. return v;
  118. }
  119. }
  120. Varyings v = (Varyings)0;
  121. return v;
  122. }
  123. FragmentOutput frag_shape(Varyings i, PerLight2D light)
  124. {
  125. half4 lightColor = i.color;
  126. if (_L2D_LIGHT_TYPE == 2)
  127. {
  128. half4 cookie = SAMPLE_TEXTURE2D(_CookieTex, sampler_CookieTex, i.uv);
  129. #if USE_ADDITIVE_BLENDING
  130. lightColor *= cookie * cookie.a;
  131. #else
  132. lightColor *= cookie;
  133. #endif
  134. }
  135. else
  136. {
  137. #if USE_ADDITIVE_BLENDING
  138. lightColor *= SAMPLE_TEXTURE2D(_FalloffLookup, sampler_FalloffLookup, i.uv).r;
  139. #elif USE_VOLUMETRIC
  140. lightColor.a = i.color.a * SAMPLE_TEXTURE2D(_FalloffLookup, sampler_FalloffLookup, i.uv).r;
  141. #else
  142. lightColor.a = SAMPLE_TEXTURE2D(_FalloffLookup, sampler_FalloffLookup, i.uv).r;
  143. #endif
  144. }
  145. #if !USE_VOLUMETRIC
  146. APPLY_NORMALS_LIGHTING(i, lightColor, _L2D_POSITION.xyz, _L2D_POSITION.w);
  147. #endif
  148. APPLY_SHADOWS(i, lightColor, _L2D_SHADOW_INTENSITY);
  149. return ToFragmentOutput(lightColor);
  150. }
  151. FragmentOutput frag_point(Varyings i, PerLight2D light)
  152. {
  153. half4 lookupValue = SAMPLE_TEXTURE2D(_LightLookup, sampler_LightLookup, i.lookupUV); // r = distance, g = angle, b = x direction, a = y direction
  154. // Inner Radius
  155. half attenuation = saturate(_L2D_INNER_RADIUS_MULT * lookupValue.r); // This is the code to take care of our inner radius
  156. // Spotlight
  157. half isFullSpotlight = _L2D_INNER_ANGLE == 1.0f;
  158. half spotAttenuation = saturate((_L2D_OUTER_ANGLE - lookupValue.g + isFullSpotlight) * (1.0f / (_L2D_OUTER_ANGLE - _L2D_INNER_ANGLE)));
  159. attenuation = attenuation * spotAttenuation;
  160. half2 mappedUV;
  161. mappedUV.x = attenuation;
  162. mappedUV.y = _L2D_FALLOFF_INTENSITY;
  163. attenuation = SAMPLE_TEXTURE2D(_FalloffLookup, sampler_FalloffLookup, mappedUV).r;
  164. half4 lightColor = _L2D_COLOR;
  165. #if USE_POINT_LIGHT_COOKIES
  166. half4 cookieColor = SAMPLE_TEXTURE2D(_PointLightCookieTex, sampler_PointLightCookieTex, i.lookupUV);
  167. lightColor = cookieColor * _L2D_COLOR;
  168. #endif
  169. #if USE_ADDITIVE_BLENDING || USE_VOLUMETRIC
  170. lightColor *= attenuation;
  171. #else
  172. lightColor.a = attenuation;
  173. #endif
  174. #if !USE_VOLUMETRIC
  175. APPLY_NORMALS_LIGHTING(i, lightColor, _L2D_POSITION.xyz, _L2D_POSITION.w);
  176. #endif
  177. APPLY_SHADOWS(i, lightColor, _L2D_SHADOW_INTENSITY);
  178. #if USE_VOLUMETRIC
  179. lightColor *= _L2D_VOLUME_OPACITY;
  180. #endif
  181. return ToFragmentOutput(lightColor * _InverseHDREmulationScale);
  182. }
  183. FragmentOutput frag(Varyings i)
  184. {
  185. PerLight2D light;
  186. #if USE_STRUCTURED_BUFFER_FOR_LIGHT2D_DATA
  187. light = GetPerLight2D(i.lightOffset);
  188. #endif
  189. switch (_L2D_LIGHT_TYPE)
  190. {
  191. case 0:
  192. case 1:
  193. case 2:
  194. {
  195. FragmentOutput output = frag_shape(i, light);
  196. return output;
  197. }
  198. break;
  199. case 3:
  200. {
  201. FragmentOutput output = frag_point(i, light);
  202. return output;
  203. }
  204. }
  205. half4 color = i.color;
  206. return ToFragmentOutput(color);
  207. }
  208. ENDHLSL
  209. }
  210. }
  211. }