ShadowStandard.shader 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License.
  3. Shader "ShadowSDK/Standard"
  4. {
  5. Properties
  6. {
  7. // Main maps.
  8. _Color("Color", Color) = (1.0, 1.0, 1.0, 1.0)
  9. _MainTex("Albedo", 2D) = "white" {}
  10. [Enum(AlbedoAlphaMode)] _AlbedoAlphaMode("Albedo Alpha Mode", Float) = 0 // "Transparency"
  11. [Toggle] _AlbedoAssignedAtRuntime("Albedo Assigned at Runtime", Float) = 0.0
  12. _Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
  13. _Metallic("Metallic", Range(0.0, 1.0)) = 0.0
  14. _Smoothness("Smoothness", Range(0.0, 1.0)) = 0.5
  15. [Toggle(_CHANNEL_MAP)] _EnableChannelMap("Enable Channel Map", Float) = 0.0
  16. [NoScaleOffset] _ChannelMap("Channel Map", 2D) = "white" {}
  17. [Toggle(_NORMAL_MAP)] _EnableNormalMap("Enable Normal Map", Float) = 0.0
  18. [NoScaleOffset] _NormalMap("Normal Map", 2D) = "bump" {}
  19. _NormalMapScale("Scale", Float) = 1.0
  20. [Toggle(_EMISSION)] _EnableEmission("Enable Emission", Float) = 0.0
  21. [HDR]_EmissiveColor("Emissive Color", Color) = (0.0, 0.0, 0.0, 1.0)
  22. [Toggle(_TRIPLANAR_MAPPING)] _EnableTriplanarMapping("Triplanar Mapping", Float) = 0.0
  23. [Toggle(_LOCAL_SPACE_TRIPLANAR_MAPPING)] _EnableLocalSpaceTriplanarMapping("Local Space", Float) = 0.0
  24. _TriplanarMappingBlendSharpness("Blend Sharpness", Range(1.0, 16.0)) = 4.0
  25. // Rendering options.
  26. [Toggle(_DIRECTIONAL_LIGHT)] _DirectionalLight("Directional Light", Float) = 1.0
  27. [Toggle(_SPECULAR_HIGHLIGHTS)] _SpecularHighlights("Specular Highlights", Float) = 1.0
  28. [Toggle(_SPHERICAL_HARMONICS)] _SphericalHarmonics("Spherical Harmonics", Float) = 0.0
  29. [Toggle(_REFLECTIONS)] _Reflections("Reflections", Float) = 0.0
  30. [Toggle(_REFRACTION)] _Refraction("Refraction", Float) = 0.0
  31. _RefractiveIndex("Refractive Index", Range(0.0, 3.0)) = 0.0
  32. [Toggle(_RIM_LIGHT)] _RimLight("Rim Light", Float) = 0.0
  33. _RimColor("Rim Color", Color) = (0.5, 0.5, 0.5, 1.0)
  34. _RimPower("Rim Power", Range(0.0, 8.0)) = 0.25
  35. [Toggle(_VERTEX_COLORS)] _VertexColors("Vertex Colors", Float) = 0.0
  36. [Toggle(_VERTEX_EXTRUSION)] _VertexExtrusion("Vertex Extrusion", Float) = 0.0
  37. _VertexExtrusionValue("Vertex Extrusion Value", Float) = 0.0
  38. [Toggle(_VERTEX_EXTRUSION_SMOOTH_NORMALS)] _VertexExtrusionSmoothNormals("Vertex Extrusion Smooth Normals", Float) = 0.0
  39. _BlendedClippingWidth("Blended Clipping With", Range(0.0, 10.0)) = 1.0
  40. [Toggle(_CLIPPING_BORDER)] _ClippingBorder("Clipping Border", Float) = 0.0
  41. _ClippingBorderWidth("Clipping Border Width", Range(0.0, 1.0)) = 0.025
  42. _ClippingBorderColor("Clipping Border Color", Color) = (1.0, 0.2, 0.0, 1.0)
  43. [Toggle(_NEAR_PLANE_FADE)] _NearPlaneFade("Near Plane Fade", Float) = 0.0
  44. [Toggle(_NEAR_LIGHT_FADE)] _NearLightFade("Near Light Fade", Float) = 0.0
  45. _FadeBeginDistance("Fade Begin Distance", Range(0.0, 10.0)) = 0.85
  46. _FadeCompleteDistance("Fade Complete Distance", Range(0.0, 10.0)) = 0.5
  47. _FadeMinValue("Fade Min Value", Range(0.0, 1.0)) = 0.0
  48. // Fluent options.
  49. [Toggle(_HOVER_LIGHT)] _HoverLight("Hover Light", Float) = 1.0
  50. [Toggle(_HOVER_COLOR_OVERRIDE)] _EnableHoverColorOverride("Hover Color Override", Float) = 0.0
  51. _HoverColorOverride("Hover Color Override", Color) = (1.0, 1.0, 1.0, 1.0)
  52. [Toggle(_PROXIMITY_LIGHT)] _ProximityLight("Proximity Light", Float) = 0.0
  53. [Toggle(_PROXIMITY_LIGHT_COLOR_OVERRIDE)] _EnableProximityLightColorOverride("Proximity Light Color Override", Float) = 0.0
  54. [HDR]_ProximityLightCenterColorOverride("Proximity Light Center Color Override", Color) = (1.0, 0.0, 0.0, 0.0)
  55. [HDR]_ProximityLightMiddleColorOverride("Proximity Light Middle Color Override", Color) = (0.0, 1.0, 0.0, 0.5)
  56. [HDR]_ProximityLightOuterColorOverride("Proximity Light Outer Color Override", Color) = (0.0, 0.0, 1.0, 1.0)
  57. [Toggle(_PROXIMITY_LIGHT_SUBTRACTIVE)] _ProximityLightSubtractive("Proximity Light Subtractive", Float) = 0.0
  58. [Toggle(_PROXIMITY_LIGHT_TWO_SIDED)] _ProximityLightTwoSided("Proximity Light Two Sided", Float) = 0.0
  59. _FluentLightIntensity("Fluent Light Intensity", Range(0.0, 1.0)) = 1.0
  60. [Toggle(_ROUND_CORNERS)] _RoundCorners("Round Corners", Float) = 0.0
  61. _RoundCornerRadius("Round Corner Radius", Range(0.0, 0.5)) = 0.25
  62. _RoundCornerMargin("Round Corner Margin", Range(0.0, 0.5)) = 0.01
  63. [Toggle(_INDEPENDENT_CORNERS)] _IndependentCorners("Independent Corners", Float) = 0.0
  64. _RoundCornersRadius("Round Corners Radius", Vector) = (0.5 ,0.5, 0.5, 0.5)
  65. [Toggle(_BORDER_LIGHT)] _BorderLight("Border Light", Float) = 0.0
  66. [Toggle(_BORDER_LIGHT_USES_HOVER_COLOR)] _BorderLightUsesHoverColor("Border Light Uses Hover Color", Float) = 0.0
  67. [Toggle(_BORDER_LIGHT_REPLACES_ALBEDO)] _BorderLightReplacesAlbedo("Border Light Replaces Albedo", Float) = 0.0
  68. [Toggle(_BORDER_LIGHT_OPAQUE)] _BorderLightOpaque("Border Light Opaque", Float) = 0.0
  69. _BorderWidth("Border Width", Range(0.0, 1.0)) = 0.1
  70. _BorderMinValue("Border Min Value", Range(0.0, 1.0)) = 0.1
  71. _EdgeSmoothingValue("Edge Smoothing Value", Range(0.0, 0.2)) = 0.002
  72. _BorderLightOpaqueAlpha("Border Light Opaque Alpha", Range(0.0, 1.0)) = 1.0
  73. [Toggle(_INNER_GLOW)] _InnerGlow("Inner Glow", Float) = 0.0
  74. _InnerGlowColor("Inner Glow Color (RGB) and Intensity (A)", Color) = (1.0, 1.0, 1.0, 0.75)
  75. _InnerGlowPower("Inner Glow Power", Range(2.0, 32.0)) = 4.0
  76. [Toggle(_IRIDESCENCE)] _Iridescence("Iridescence", Float) = 0.0
  77. [NoScaleOffset] _IridescentSpectrumMap("Iridescent Spectrum Map", 2D) = "white" {}
  78. _IridescenceIntensity("Iridescence Intensity", Range(0.0, 1.0)) = 0.5
  79. _IridescenceThreshold("Iridescence Threshold", Range(0.0, 1.0)) = 0.05
  80. _IridescenceAngle("Iridescence Angle", Range(-0.78, 0.78)) = -0.78
  81. [Toggle(_ENVIRONMENT_COLORING)] _EnvironmentColoring("Environment Coloring", Float) = 0.0
  82. _EnvironmentColorThreshold("Environment Color Threshold", Range(0.0, 3.0)) = 1.5
  83. _EnvironmentColorIntensity("Environment Color Intensity", Range(0.0, 1.0)) = 0.5
  84. _EnvironmentColorX("Environment Color X (RGB)", Color) = (1.0, 0.0, 0.0, 1.0)
  85. _EnvironmentColorY("Environment Color Y (RGB)", Color) = (0.0, 1.0, 0.0, 1.0)
  86. _EnvironmentColorZ("Environment Color Z (RGB)", Color) = (0.0, 0.0, 1.0, 1.0)
  87. // Advanced options.
  88. [Enum(RenderingMode)] _Mode("Rendering Mode", Float) = 0 // "Opaque"
  89. [Enum(CustomRenderingMode)] _CustomMode("Mode", Float) = 0 // "Opaque"
  90. [Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend("Source Blend", Float) = 1 // "One"
  91. [Enum(UnityEngine.Rendering.BlendMode)] _DstBlend("Destination Blend", Float) = 0 // "Zero"
  92. [Enum(UnityEngine.Rendering.BlendOp)] _BlendOp("Blend Operation", Float) = 0 // "Add"
  93. [Enum(UnityEngine.Rendering.CompareFunction)] _ZTest("Depth Test", Float) = 4 // "LessEqual"
  94. [Enum(DepthWrite)] _ZWrite("Depth Write", Float) = 1 // "On"
  95. _ZOffsetFactor("Depth Offset Factor", Float) = 0 // "Zero"
  96. _ZOffsetUnits("Depth Offset Units", Float) = 0 // "Zero"
  97. [Enum(UnityEngine.Rendering.ColorWriteMask)] _ColorWriteMask("Color Write Mask", Float) = 15 // "All"
  98. [Enum(UnityEngine.Rendering.CullMode)] _CullMode("Cull Mode", Float) = 2 // "Back"
  99. _RenderQueueOverride("Render Queue Override", Range(-1.0, 5000)) = -1
  100. [Toggle(_INSTANCED_COLOR)] _InstancedColor("Instanced Color", Float) = 0.0
  101. [Toggle(_IGNORE_Z_SCALE)] _IgnoreZScale("Ignore Z Scale", Float) = 0.0
  102. [Toggle(_STENCIL)] _Stencil("Enable Stencil Testing", Float) = 0.0
  103. _StencilReference("Stencil Reference", Range(0, 255)) = 0
  104. [Enum(UnityEngine.Rendering.CompareFunction)]_StencilComparison("Stencil Comparison", Int) = 0
  105. [Enum(UnityEngine.Rendering.StencilOp)]_StencilOperation("Stencil Operation", Int) = 0
  106. }
  107. SubShader
  108. {
  109. Pass
  110. {
  111. Name "Main"
  112. Tags{ "RenderType" = "Opaque" "LightMode" = "ForwardBase" }
  113. LOD 100
  114. Blend[_SrcBlend][_DstBlend]
  115. BlendOp[_BlendOp]
  116. ZTest[_ZTest]
  117. ZWrite[_ZWrite]
  118. Cull[_CullMode]
  119. Offset[_ZOffsetFactor],[_ZOffsetUnits]
  120. ColorMask[_ColorWriteMask]
  121. Stencil
  122. {
  123. Ref[_StencilReference]
  124. Comp[_StencilComparison]
  125. Pass[_StencilOperation]
  126. }
  127. CGPROGRAM
  128. #pragma vertex vert
  129. #pragma fragment frag
  130. #pragma multi_compile_instancing
  131. #pragma multi_compile _ LIGHTMAP_ON
  132. #pragma multi_compile _ _CLIPPING_PLANE _CLIPPING_SPHERE _CLIPPING_BOX
  133. #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON
  134. #pragma shader_feature _DISABLE_ALBEDO_MAP
  135. #pragma shader_feature _ _METALLIC_TEXTURE_ALBEDO_CHANNEL_A _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
  136. #pragma shader_feature _CHANNEL_MAP
  137. #pragma shader_feature _NORMAL_MAP
  138. #pragma shader_feature _EMISSION
  139. #pragma shader_feature _TRIPLANAR_MAPPING
  140. #pragma shader_feature _LOCAL_SPACE_TRIPLANAR_MAPPING
  141. #pragma shader_feature _DIRECTIONAL_LIGHT
  142. #pragma shader_feature _SPECULAR_HIGHLIGHTS
  143. #pragma shader_feature _SPHERICAL_HARMONICS
  144. #pragma shader_feature _REFLECTIONS
  145. #pragma shader_feature _REFRACTION
  146. #pragma shader_feature _RIM_LIGHT
  147. #pragma shader_feature _VERTEX_COLORS
  148. #pragma shader_feature _VERTEX_EXTRUSION
  149. #pragma shader_feature _VERTEX_EXTRUSION_SMOOTH_NORMALS
  150. #pragma shader_feature _CLIPPING_BORDER
  151. #pragma shader_feature _NEAR_PLANE_FADE
  152. #pragma shader_feature _NEAR_LIGHT_FADE
  153. #pragma shader_feature _HOVER_LIGHT
  154. #pragma shader_feature _HOVER_COLOR_OVERRIDE
  155. #pragma shader_feature _PROXIMITY_LIGHT
  156. #pragma shader_feature _PROXIMITY_LIGHT_COLOR_OVERRIDE
  157. #pragma shader_feature _PROXIMITY_LIGHT_SUBTRACTIVE
  158. #pragma shader_feature _PROXIMITY_LIGHT_TWO_SIDED
  159. #pragma shader_feature _ROUND_CORNERS
  160. #pragma shader_feature _INDEPENDENT_CORNERS
  161. #pragma shader_feature _BORDER_LIGHT
  162. #pragma shader_feature _BORDER_LIGHT_USES_HOVER_COLOR
  163. #pragma shader_feature _BORDER_LIGHT_REPLACES_ALBEDO
  164. #pragma shader_feature _BORDER_LIGHT_OPAQUE
  165. #pragma shader_feature _INNER_GLOW
  166. #pragma shader_feature _IRIDESCENCE
  167. #pragma shader_feature _ENVIRONMENT_COLORING
  168. #pragma shader_feature _INSTANCED_COLOR
  169. #pragma shader_feature _IGNORE_Z_SCALE
  170. #define IF(a, b, c) lerp(b, c, step((fixed) (a), 0.0));
  171. #include "UnityCG.cginc"
  172. #include "UnityStandardConfig.cginc"
  173. #include "UnityStandardUtils.cginc"
  174. #include "ShadowShaderUtils.cginc"
  175. // This define will get commented in by the UpgradeShaderForLightweightRenderPipeline method.
  176. //#define _LIGHTWEIGHT_RENDER_PIPELINE
  177. #if defined(_TRIPLANAR_MAPPING) || defined(_DIRECTIONAL_LIGHT) || defined(_SPHERICAL_HARMONICS) || defined(_REFLECTIONS) || defined(_RIM_LIGHT) || defined(_PROXIMITY_LIGHT) || defined(_ENVIRONMENT_COLORING)
  178. #define _NORMAL
  179. #else
  180. #undef _NORMAL
  181. #endif
  182. #if defined(_CLIPPING_PLANE) || defined(_CLIPPING_SPHERE) || defined(_CLIPPING_BOX)
  183. #define _CLIPPING_PRIMITIVE
  184. #else
  185. #undef _CLIPPING_PRIMITIVE
  186. #endif
  187. #if defined(_NORMAL) || defined(_CLIPPING_PRIMITIVE) || defined(_NEAR_PLANE_FADE) || defined(_HOVER_LIGHT) || defined(_PROXIMITY_LIGHT)
  188. #define _WORLD_POSITION
  189. #else
  190. #undef _WORLD_POSITION
  191. #endif
  192. #if defined(_ALPHATEST_ON) || defined(_CLIPPING_PRIMITIVE) || defined(_ROUND_CORNERS)
  193. #define _ALPHA_CLIP
  194. #else
  195. #undef _ALPHA_CLIP
  196. #endif
  197. #if defined(_ALPHABLEND_ON)
  198. #define _TRANSPARENT
  199. #undef _ALPHA_CLIP
  200. #else
  201. #undef _TRANSPARENT
  202. #endif
  203. #if defined(_VERTEX_EXTRUSION) || defined(_ROUND_CORNERS) || defined(_BORDER_LIGHT)
  204. #define _SCALE
  205. #else
  206. #undef _SCALE
  207. #endif
  208. #if defined(_DIRECTIONAL_LIGHT) || defined(_RIM_LIGHT)
  209. #define _FRESNEL
  210. #else
  211. #undef _FRESNEL
  212. #endif
  213. #if defined(_ROUND_CORNERS) || defined(_BORDER_LIGHT) || defined(_INNER_GLOW)
  214. #define _DISTANCE_TO_EDGE
  215. #else
  216. #undef _DISTANCE_TO_EDGE
  217. #endif
  218. #if !defined(_DISABLE_ALBEDO_MAP) || defined(_TRIPLANAR_MAPPING) || defined(_CHANNEL_MAP) || defined(_NORMAL_MAP) || defined(_DISTANCE_TO_EDGE) || defined(_IRIDESCENCE)
  219. #define _UV
  220. #else
  221. #undef _UV
  222. #endif
  223. struct appdata_t
  224. {
  225. float4 vertex : POSITION;
  226. // The default UV channel used for texturing.
  227. float2 uv : TEXCOORD0;
  228. #if defined(LIGHTMAP_ON)
  229. // Reserved for Unity's light map UVs.
  230. float2 uv1 : TEXCOORD1;
  231. #endif
  232. // Used for smooth normal data (or UGUI scaling data).
  233. float4 uv2 : TEXCOORD2;
  234. // Used for UGUI scaling data.
  235. float2 uv3 : TEXCOORD3;
  236. #if defined(_VERTEX_COLORS)
  237. fixed4 color : COLOR0;
  238. #endif
  239. fixed3 normal : NORMAL;
  240. #if defined(_NORMAL_MAP)
  241. fixed4 tangent : TANGENT;
  242. #endif
  243. UNITY_VERTEX_INPUT_INSTANCE_ID
  244. };
  245. struct v2f
  246. {
  247. float4 position : SV_POSITION;
  248. #if defined(_BORDER_LIGHT)
  249. float4 uv : TEXCOORD0;
  250. #elif defined(_UV)
  251. float2 uv : TEXCOORD0;
  252. #endif
  253. #if defined(LIGHTMAP_ON)
  254. float2 lightMapUV : TEXCOORD1;
  255. #endif
  256. #if defined(_VERTEX_COLORS)
  257. fixed4 color : COLOR0;
  258. #endif
  259. #if defined(_SPHERICAL_HARMONICS)
  260. fixed3 ambient : COLOR1;
  261. #endif
  262. #if defined(_IRIDESCENCE)
  263. fixed3 iridescentColor : COLOR2;
  264. #endif
  265. #if defined(_WORLD_POSITION)
  266. #if defined(_NEAR_PLANE_FADE)
  267. float4 worldPosition : TEXCOORD2;
  268. #else
  269. float3 worldPosition : TEXCOORD2;
  270. #endif
  271. #endif
  272. #if defined(_SCALE)
  273. float3 scale : TEXCOORD3;
  274. #endif
  275. #if defined(_NORMAL)
  276. #if defined(_TRIPLANAR_MAPPING)
  277. fixed3 worldNormal : COLOR3;
  278. fixed3 triplanarNormal : COLOR4;
  279. float3 triplanarPosition : TEXCOORD6;
  280. #elif defined(_NORMAL_MAP)
  281. fixed3 tangentX : COLOR3;
  282. fixed3 tangentY : COLOR4;
  283. fixed3 tangentZ : COLOR5;
  284. #else
  285. fixed3 worldNormal : COLOR3;
  286. #endif
  287. #endif
  288. UNITY_VERTEX_OUTPUT_STEREO
  289. #if defined(_INSTANCED_COLOR)
  290. UNITY_VERTEX_INPUT_INSTANCE_ID
  291. #endif
  292. };
  293. #if defined(_INSTANCED_COLOR)
  294. UNITY_INSTANCING_BUFFER_START(Props)
  295. UNITY_DEFINE_INSTANCED_PROP(float4, _Color)
  296. UNITY_INSTANCING_BUFFER_END(Props)
  297. #else
  298. fixed4 _Color;
  299. #endif
  300. sampler2D _MainTex;
  301. fixed4 _MainTex_ST;
  302. #if defined(_ALPHA_CLIP)
  303. fixed _Cutoff;
  304. #endif
  305. fixed _Metallic;
  306. fixed _Smoothness;
  307. #if defined(_CHANNEL_MAP)
  308. sampler2D _ChannelMap;
  309. #endif
  310. #if defined(_NORMAL_MAP)
  311. sampler2D _NormalMap;
  312. float _NormalMapScale;
  313. #endif
  314. #if defined(_EMISSION)
  315. fixed4 _EmissiveColor;
  316. #endif
  317. #if defined(_TRIPLANAR_MAPPING)
  318. float _TriplanarMappingBlendSharpness;
  319. #endif
  320. #if defined(_DIRECTIONAL_LIGHT)
  321. #if defined(_LIGHTWEIGHT_RENDER_PIPELINE)
  322. CBUFFER_START(_LightBuffer)
  323. float4 _MainLightPosition;
  324. half4 _MainLightColor;
  325. CBUFFER_END
  326. #else
  327. fixed4 _LightColor0;
  328. #endif
  329. #endif
  330. #if defined(_REFRACTION)
  331. fixed _RefractiveIndex;
  332. #endif
  333. #if defined(_RIM_LIGHT)
  334. fixed3 _RimColor;
  335. fixed _RimPower;
  336. #endif
  337. #if defined(_VERTEX_EXTRUSION)
  338. float _VertexExtrusionValue;
  339. #endif
  340. #if defined(_CLIPPING_PLANE)
  341. fixed _ClipPlaneSide;
  342. float4 _ClipPlane;
  343. #endif
  344. #if defined(_CLIPPING_SPHERE)
  345. fixed _ClipSphereSide;
  346. float4 _ClipSphere;
  347. #endif
  348. #if defined(_CLIPPING_BOX)
  349. fixed _ClipBoxSide;
  350. float4 _ClipBoxSize;
  351. float4x4 _ClipBoxInverseTransform;
  352. #endif
  353. #if defined(_CLIPPING_PRIMITIVE)
  354. float _BlendedClippingWidth;
  355. #endif
  356. #if defined(_CLIPPING_BORDER)
  357. fixed _ClippingBorderWidth;
  358. fixed3 _ClippingBorderColor;
  359. #endif
  360. #if defined(_NEAR_PLANE_FADE)
  361. float _FadeBeginDistance;
  362. float _FadeCompleteDistance;
  363. fixed _FadeMinValue;
  364. #endif
  365. #if defined(_HOVER_LIGHT) || defined(_NEAR_LIGHT_FADE)
  366. #define HOVER_LIGHT_COUNT 2
  367. #define HOVER_LIGHT_DATA_SIZE 2
  368. float4 _HoverLightData[HOVER_LIGHT_COUNT * HOVER_LIGHT_DATA_SIZE];
  369. #if defined(_HOVER_COLOR_OVERRIDE)
  370. fixed3 _HoverColorOverride;
  371. #endif
  372. #endif
  373. #if defined(_PROXIMITY_LIGHT) || defined(_NEAR_LIGHT_FADE)
  374. #define PROXIMITY_LIGHT_COUNT 2
  375. #define PROXIMITY_LIGHT_DATA_SIZE 6
  376. float4 _ProximityLightData[PROXIMITY_LIGHT_COUNT * PROXIMITY_LIGHT_DATA_SIZE];
  377. #if defined(_PROXIMITY_LIGHT_COLOR_OVERRIDE)
  378. float4 _ProximityLightCenterColorOverride;
  379. float4 _ProximityLightMiddleColorOverride;
  380. float4 _ProximityLightOuterColorOverride;
  381. #endif
  382. #endif
  383. #if defined(_HOVER_LIGHT) || defined(_PROXIMITY_LIGHT) || defined(_BORDER_LIGHT)
  384. fixed _FluentLightIntensity;
  385. #endif
  386. #if defined(_ROUND_CORNERS)
  387. #if defined(_INDEPENDENT_CORNERS)
  388. float4 _RoundCornersRadius;
  389. #else
  390. fixed _RoundCornerRadius;
  391. #endif
  392. fixed _RoundCornerMargin;
  393. #endif
  394. #if defined(_BORDER_LIGHT)
  395. fixed _BorderWidth;
  396. fixed _BorderMinValue;
  397. #endif
  398. #if defined(_BORDER_LIGHT_OPAQUE)
  399. fixed _BorderLightOpaqueAlpha;
  400. #endif
  401. #if defined(_ROUND_CORNERS) || defined(_BORDER_LIGHT)
  402. fixed _EdgeSmoothingValue;
  403. #endif
  404. #if defined(_INNER_GLOW)
  405. fixed4 _InnerGlowColor;
  406. fixed _InnerGlowPower;
  407. #endif
  408. #if defined(_IRIDESCENCE)
  409. sampler2D _IridescentSpectrumMap;
  410. fixed _IridescenceIntensity;
  411. fixed _IridescenceThreshold;
  412. fixed _IridescenceAngle;
  413. #endif
  414. #if defined(_ENVIRONMENT_COLORING)
  415. fixed _EnvironmentColorThreshold;
  416. fixed _EnvironmentColorIntensity;
  417. fixed3 _EnvironmentColorX;
  418. fixed3 _EnvironmentColorY;
  419. fixed3 _EnvironmentColorZ;
  420. #endif
  421. #if defined(_DIRECTIONAL_LIGHT)
  422. static const fixed _MinMetallicLightContribution = 0.7;
  423. static const fixed _IblContribution = 0.1;
  424. #endif
  425. #if defined(_SPECULAR_HIGHLIGHTS)
  426. static const float _Shininess = 800.0;
  427. #endif
  428. #if defined(_FRESNEL)
  429. static const float _FresnelPower = 8.0;
  430. #endif
  431. #if defined(_NEAR_LIGHT_FADE)
  432. static const float _MaxNearLightDistance = 10.0;
  433. inline float NearLightDistance(float4 light, float3 worldPosition)
  434. {
  435. return distance(worldPosition, light.xyz) + ((1.0 - light.w) * _MaxNearLightDistance);
  436. }
  437. #endif
  438. #if defined(_HOVER_LIGHT)
  439. inline float HoverLight(float4 hoverLight, float inverseRadius, float3 worldPosition)
  440. {
  441. return (1.0 - saturate(length(hoverLight.xyz - worldPosition) * inverseRadius)) * hoverLight.w;
  442. }
  443. #endif
  444. #if defined(_PROXIMITY_LIGHT)
  445. inline float ProximityLight(float4 proximityLight, float4 proximityLightParams, float4 proximityLightPulseParams, float3 worldPosition, float3 worldNormal, out fixed colorValue)
  446. {
  447. float proximityLightDistance = dot(proximityLight.xyz - worldPosition, worldNormal);
  448. #if defined(_PROXIMITY_LIGHT_TWO_SIDED)
  449. worldNormal = IF(proximityLightDistance < 0.0, -worldNormal, worldNormal);
  450. proximityLightDistance = abs(proximityLightDistance);
  451. #endif
  452. float normalizedProximityLightDistance = saturate(proximityLightDistance * proximityLightParams.y);
  453. float3 projectedProximityLight = proximityLight.xyz - (worldNormal * abs(proximityLightDistance));
  454. float projectedProximityLightDistance = length(projectedProximityLight - worldPosition);
  455. float attenuation = (1.0 - normalizedProximityLightDistance) * proximityLight.w;
  456. colorValue = saturate(projectedProximityLightDistance * proximityLightParams.z);
  457. float pulse = step(proximityLightPulseParams.x, projectedProximityLightDistance) * proximityLightPulseParams.y;
  458. return smoothstep(1.0, 0.0, projectedProximityLightDistance / (proximityLightParams.x * max(pow(normalizedProximityLightDistance, 0.25), proximityLightParams.w))) * pulse * attenuation;
  459. }
  460. inline fixed3 MixProximityLightColor(fixed4 centerColor, fixed4 middleColor, fixed4 outerColor, fixed t)
  461. {
  462. fixed3 color = lerp(centerColor.rgb, middleColor.rgb, smoothstep(centerColor.a, middleColor.a, t));
  463. return lerp(color, outerColor, smoothstep(middleColor.a, outerColor.a, t));
  464. }
  465. #endif
  466. #if defined(_ROUND_CORNERS)
  467. inline float PointVsRoundedBox(float2 position, float2 cornerCircleDistance, float cornerCircleRadius)
  468. {
  469. return length(max(abs(position) - cornerCircleDistance, 0.0)) - cornerCircleRadius;
  470. }
  471. inline fixed RoundCornersSmooth(float2 position, float2 cornerCircleDistance, float cornerCircleRadius)
  472. {
  473. return smoothstep(1.0, 0.0, PointVsRoundedBox(position, cornerCircleDistance, cornerCircleRadius) / _EdgeSmoothingValue);
  474. }
  475. inline fixed RoundCorners(float2 position, float2 cornerCircleDistance, float cornerCircleRadius)
  476. {
  477. #if defined(_TRANSPARENT)
  478. return RoundCornersSmooth(position, cornerCircleDistance, cornerCircleRadius);
  479. #else
  480. return (PointVsRoundedBox(position, cornerCircleDistance, cornerCircleRadius) < 0.0);
  481. #endif
  482. }
  483. #endif
  484. #if defined(_IRIDESCENCE)
  485. fixed3 Iridescence(float tangentDotIncident, sampler2D spectrumMap, float threshold, float2 uv, float angle, float intensity)
  486. {
  487. float k = tangentDotIncident * 0.5 + 0.5;
  488. float4 left = tex2D(spectrumMap, float2(lerp(0.0, 1.0 - threshold, k), 0.5), float2(0.0, 0.0), float2(0.0, 0.0));
  489. float4 right = tex2D(spectrumMap, float2(lerp(threshold, 1.0, k), 0.5), float2(0.0, 0.0), float2(0.0, 0.0));
  490. float2 XY = uv - float2(0.5, 0.5);
  491. float s = (cos(angle) * XY.x - sin(angle) * XY.y) / cos(angle);
  492. return (left.rgb + s * (right.rgb - left.rgb)) * intensity;
  493. }
  494. #endif
  495. v2f vert(appdata_t v)
  496. {
  497. v2f o;
  498. UNITY_SETUP_INSTANCE_ID(v);
  499. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  500. #if defined(_INSTANCED_COLOR)
  501. UNITY_TRANSFER_INSTANCE_ID(v, o);
  502. #endif
  503. float4 vertexPosition = v.vertex;
  504. #if defined(_WORLD_POSITION) || defined(_VERTEX_EXTRUSION)
  505. float3 worldVertexPosition = mul(unity_ObjectToWorld, vertexPosition).xyz;
  506. #endif
  507. #if defined(_SCALE)
  508. o.scale.x = length(mul(unity_ObjectToWorld, float4(1.0, 0.0, 0.0, 0.0)));
  509. o.scale.y = length(mul(unity_ObjectToWorld, float4(0.0, 1.0, 0.0, 0.0)));
  510. #if defined(_IGNORE_Z_SCALE)
  511. o.scale.z = o.scale.x;
  512. #else
  513. o.scale.z = length(mul(unity_ObjectToWorld, float4(0.0, 0.0, 1.0, 0.0)));
  514. #endif
  515. #if !defined(_VERTEX_EXTRUSION_SMOOTH_NORMALS)
  516. // uv3.y will contain a negative value when rendered by a UGUI and ScaleMeshEffect.
  517. if (v.uv3.y < 0.0)
  518. {
  519. o.scale.x *= v.uv2.x;
  520. o.scale.y *= v.uv2.y;
  521. o.scale.z *= v.uv3.x;
  522. }
  523. #endif
  524. #endif
  525. fixed3 localNormal = v.normal;
  526. #if defined(_NORMAL) || defined(_VERTEX_EXTRUSION)
  527. fixed3 worldNormal = UnityObjectToWorldNormal(localNormal);
  528. #endif
  529. #if defined(_VERTEX_EXTRUSION)
  530. #if defined(_VERTEX_EXTRUSION_SMOOTH_NORMALS)
  531. worldVertexPosition += UnityObjectToWorldNormal(v.uv2 * o.scale) * _VertexExtrusionValue;
  532. #else
  533. worldVertexPosition += worldNormal * _VertexExtrusionValue;
  534. #endif
  535. vertexPosition = mul(unity_WorldToObject, float4(worldVertexPosition, 1.0));
  536. #endif
  537. o.position = UnityObjectToClipPos(vertexPosition);
  538. #if defined(_WORLD_POSITION)
  539. o.worldPosition.xyz = worldVertexPosition;
  540. #endif
  541. #if defined(_NEAR_PLANE_FADE)
  542. float rangeInverse = 1.0 / (_FadeBeginDistance - _FadeCompleteDistance);
  543. #if defined(_NEAR_LIGHT_FADE)
  544. float fadeDistance = _MaxNearLightDistance;
  545. [unroll]
  546. for (int hoverLightIndex = 0; hoverLightIndex < HOVER_LIGHT_COUNT; ++hoverLightIndex)
  547. {
  548. int dataIndex = hoverLightIndex * HOVER_LIGHT_DATA_SIZE;
  549. fadeDistance = min(fadeDistance, NearLightDistance(_HoverLightData[dataIndex], o.worldPosition));
  550. }
  551. [unroll]
  552. for (int proximityLightIndex = 0; proximityLightIndex < PROXIMITY_LIGHT_COUNT; ++proximityLightIndex)
  553. {
  554. int dataIndex = proximityLightIndex * PROXIMITY_LIGHT_DATA_SIZE;
  555. fadeDistance = min(fadeDistance, NearLightDistance(_ProximityLightData[dataIndex], o.worldPosition));
  556. }
  557. #else
  558. float fadeDistance = -UnityObjectToViewPos(vertexPosition).z;
  559. #endif
  560. o.worldPosition.w = max(saturate(mad(fadeDistance, rangeInverse, -_FadeCompleteDistance * rangeInverse)), _FadeMinValue);
  561. #endif
  562. #if defined(_BORDER_LIGHT) || defined(_ROUND_CORNERS)
  563. o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex);
  564. float minScale = min(min(o.scale.x, o.scale.y), o.scale.z);
  565. #if defined(_BORDER_LIGHT)
  566. float maxScale = max(max(o.scale.x, o.scale.y), o.scale.z);
  567. float minOverMiddleScale = minScale / (o.scale.x + o.scale.y + o.scale.z - minScale - maxScale);
  568. float areaYZ = o.scale.y * o.scale.z;
  569. float areaXZ = o.scale.z * o.scale.x;
  570. float areaXY = o.scale.x * o.scale.y;
  571. float borderWidth = _BorderWidth;
  572. #endif
  573. if (abs(localNormal.x) == 1.0) // Y,Z plane.
  574. {
  575. o.scale.x = o.scale.z;
  576. o.scale.y = o.scale.y;
  577. #if defined(_BORDER_LIGHT)
  578. if (areaYZ > areaXZ && areaYZ > areaXY)
  579. {
  580. borderWidth *= minOverMiddleScale;
  581. }
  582. #endif
  583. }
  584. else if (abs(localNormal.y) == 1.0) // X,Z plane.
  585. {
  586. o.scale.x = o.scale.x;
  587. o.scale.y = o.scale.z;
  588. #if defined(_BORDER_LIGHT)
  589. if (areaXZ > areaXY && areaXZ > areaYZ)
  590. {
  591. borderWidth *= minOverMiddleScale;
  592. }
  593. #endif
  594. }
  595. else // X,Y plane.
  596. {
  597. o.scale.x = o.scale.x;
  598. o.scale.y = o.scale.y;
  599. #if defined(_BORDER_LIGHT)
  600. if (areaXY > areaYZ && areaXY > areaXZ)
  601. {
  602. borderWidth *= minOverMiddleScale;
  603. }
  604. #endif
  605. }
  606. o.scale.z = minScale;
  607. #if defined(_BORDER_LIGHT)
  608. float scaleRatio = min(o.scale.x, o.scale.y) / max(o.scale.x, o.scale.y);
  609. o.uv.z = IF(o.scale.x > o.scale.y, 1.0 - (borderWidth * scaleRatio), 1.0 - borderWidth);
  610. o.uv.w = IF(o.scale.x > o.scale.y, 1.0 - borderWidth, 1.0 - (borderWidth * scaleRatio));
  611. #endif
  612. #elif defined(_UV)
  613. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  614. #endif
  615. #if defined(LIGHTMAP_ON)
  616. o.lightMapUV.xy = v.uv1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
  617. #endif
  618. #if defined(_VERTEX_COLORS)
  619. o.color = v.color;
  620. #endif
  621. #if defined(_SPHERICAL_HARMONICS)
  622. o.ambient = ShadeSH9(float4(worldNormal, 1.0));
  623. #endif
  624. #if defined(_IRIDESCENCE)
  625. float3 rightTangent = normalize(mul((float3x3)unity_ObjectToWorld, float3(1.0, 0.0, 0.0)));
  626. float3 incidentWithCenter = normalize(mul(unity_ObjectToWorld, float4(0.0, 0.0, 0.0, 1.0)) - _WorldSpaceCameraPos);
  627. float tangentDotIncident = dot(rightTangent, incidentWithCenter);
  628. o.iridescentColor = Iridescence(tangentDotIncident, _IridescentSpectrumMap, _IridescenceThreshold, v.uv, _IridescenceAngle, _IridescenceIntensity);
  629. #endif
  630. #if defined(_NORMAL)
  631. #if defined(_TRIPLANAR_MAPPING)
  632. o.worldNormal = worldNormal;
  633. #if defined(_LOCAL_SPACE_TRIPLANAR_MAPPING)
  634. o.triplanarNormal = localNormal;
  635. o.triplanarPosition = vertexPosition;
  636. #else
  637. o.triplanarNormal = worldNormal;
  638. o.triplanarPosition = o.worldPosition;
  639. #endif
  640. #elif defined(_NORMAL_MAP)
  641. fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
  642. fixed tangentSign = v.tangent.w * unity_WorldTransformParams.w;
  643. fixed3 worldBitangent = cross(worldNormal, worldTangent) * tangentSign;
  644. o.tangentX = fixed3(worldTangent.x, worldBitangent.x, worldNormal.x);
  645. o.tangentY = fixed3(worldTangent.y, worldBitangent.y, worldNormal.y);
  646. o.tangentZ = fixed3(worldTangent.z, worldBitangent.z, worldNormal.z);
  647. #else
  648. o.worldNormal = worldNormal;
  649. #endif
  650. #endif
  651. return o;
  652. }
  653. fixed4 frag(v2f i, fixed facing : VFACE) : SV_Target
  654. {
  655. #if defined(_INSTANCED_COLOR)
  656. UNITY_SETUP_INSTANCE_ID(i);
  657. #endif
  658. #if defined(_TRIPLANAR_MAPPING)
  659. // Calculate triplanar uvs and apply texture scale and offset values like TRANSFORM_TEX.
  660. fixed3 triplanarBlend = pow(abs(i.triplanarNormal), _TriplanarMappingBlendSharpness);
  661. triplanarBlend /= dot(triplanarBlend, fixed3(1.0, 1.0, 1.0));
  662. float2 uvX = i.triplanarPosition.zy * _MainTex_ST.xy + _MainTex_ST.zw;
  663. float2 uvY = i.triplanarPosition.xz * _MainTex_ST.xy + _MainTex_ST.zw;
  664. float2 uvZ = i.triplanarPosition.xy * _MainTex_ST.xy + _MainTex_ST.zw;
  665. // Ternary operator is 2 instructions faster than sign() when we don't care about zero returning a zero sign.
  666. float3 axisSign = i.triplanarNormal < 0 ? -1 : 1;
  667. uvX.x *= axisSign.x;
  668. uvY.x *= axisSign.y;
  669. uvZ.x *= -axisSign.z;
  670. #endif
  671. // Texturing.
  672. #if defined(_DISABLE_ALBEDO_MAP)
  673. fixed4 albedo = fixed4(1.0, 1.0, 1.0, 1.0);
  674. #else
  675. #if defined(_TRIPLANAR_MAPPING)
  676. fixed4 albedo = tex2D(_MainTex, uvX) * triplanarBlend.x +
  677. tex2D(_MainTex, uvY) * triplanarBlend.y +
  678. tex2D(_MainTex, uvZ) * triplanarBlend.z;
  679. #else
  680. fixed4 albedo = tex2D(_MainTex, i.uv);
  681. #endif
  682. #endif
  683. #ifdef LIGHTMAP_ON
  684. albedo.rgb *= DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.lightMapUV));
  685. #endif
  686. #if defined(_CHANNEL_MAP)
  687. fixed4 channel = tex2D(_ChannelMap, i.uv);
  688. _Metallic = channel.r;
  689. albedo.rgb *= channel.g;
  690. _Smoothness = channel.a;
  691. #else
  692. #if defined(_METALLIC_TEXTURE_ALBEDO_CHANNEL_A)
  693. _Metallic = albedo.a;
  694. albedo.a = 1.0;
  695. #elif defined(_SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A)
  696. _Smoothness = albedo.a;
  697. albedo.a = 1.0;
  698. #endif
  699. #endif
  700. // Primitive clipping.
  701. #if defined(_CLIPPING_PRIMITIVE)
  702. float primitiveDistance = 1.0;
  703. #if defined(_CLIPPING_PLANE)
  704. primitiveDistance = min(primitiveDistance, PointVsPlane(i.worldPosition.xyz, _ClipPlane) * _ClipPlaneSide);
  705. #endif
  706. #if defined(_CLIPPING_SPHERE)
  707. primitiveDistance = min(primitiveDistance, PointVsSphere(i.worldPosition.xyz, _ClipSphere) * _ClipSphereSide);
  708. #endif
  709. #if defined(_CLIPPING_BOX)
  710. primitiveDistance = min(primitiveDistance, PointVsBox(i.worldPosition.xyz, _ClipBoxSize.xyz, _ClipBoxInverseTransform) * _ClipBoxSide);
  711. #endif
  712. #if defined(_CLIPPING_BORDER)
  713. fixed3 primitiveBorderColor = lerp(_ClippingBorderColor, fixed3(0.0, 0.0, 0.0), primitiveDistance / _ClippingBorderWidth);
  714. albedo.rgb += primitiveBorderColor * IF((primitiveDistance < _ClippingBorderWidth), 1.0, 0.0);
  715. #endif
  716. #endif
  717. #if defined(_DISTANCE_TO_EDGE)
  718. fixed2 distanceToEdge;
  719. distanceToEdge.x = abs(i.uv.x - 0.5) * 2.0;
  720. distanceToEdge.y = abs(i.uv.y - 0.5) * 2.0;
  721. #endif
  722. // Rounded corner clipping.
  723. #if defined(_ROUND_CORNERS)
  724. float2 halfScale = i.scale.xy * 0.5;
  725. float2 roundCornerPosition = distanceToEdge * halfScale;
  726. fixed currentCornerRadius;
  727. #if defined(_INDEPENDENT_CORNERS)
  728. _RoundCornersRadius = clamp(_RoundCornersRadius, 0, 0.5);
  729. if (i.uv.x < 0.5)
  730. {
  731. if (i.uv.y > 0.5)
  732. {
  733. currentCornerRadius = _RoundCornersRadius.x;
  734. }
  735. else
  736. {
  737. currentCornerRadius = _RoundCornersRadius.w;
  738. }
  739. }
  740. else
  741. {
  742. if (i.uv.y > 0.5)
  743. {
  744. currentCornerRadius = _RoundCornersRadius.y;
  745. }
  746. else
  747. {
  748. currentCornerRadius = _RoundCornersRadius.z;
  749. }
  750. }
  751. #else
  752. currentCornerRadius = _RoundCornerRadius;
  753. #endif
  754. float cornerCircleRadius = saturate(max(currentCornerRadius - _RoundCornerMargin, 0.01)) * i.scale.z;
  755. float2 cornerCircleDistance = halfScale - (_RoundCornerMargin * i.scale.z) - cornerCircleRadius;
  756. float roundCornerClip = RoundCorners(roundCornerPosition, cornerCircleDistance, cornerCircleRadius);
  757. #endif
  758. #if defined(_INSTANCED_COLOR)
  759. albedo *= UNITY_ACCESS_INSTANCED_PROP(Props, _Color);
  760. #else
  761. albedo *= _Color;
  762. #endif
  763. #if defined(_VERTEX_COLORS)
  764. albedo *= i.color;
  765. #endif
  766. #if defined(_IRIDESCENCE)
  767. albedo.rgb += i.iridescentColor;
  768. #endif
  769. // Normal calculation.
  770. #if defined(_NORMAL)
  771. fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(i.worldPosition.xyz));
  772. #if defined(_REFLECTIONS) || defined(_ENVIRONMENT_COLORING)
  773. fixed3 incident = -worldViewDir;
  774. #endif
  775. fixed3 worldNormal;
  776. #if defined(_NORMAL_MAP)
  777. #if defined(_TRIPLANAR_MAPPING)
  778. fixed3 tangentNormalX = UnpackScaleNormal(tex2D(_NormalMap, uvX), _NormalMapScale);
  779. fixed3 tangentNormalY = UnpackScaleNormal(tex2D(_NormalMap, uvY), _NormalMapScale);
  780. fixed3 tangentNormalZ = UnpackScaleNormal(tex2D(_NormalMap, uvZ), _NormalMapScale);
  781. tangentNormalX.x *= axisSign.x;
  782. tangentNormalY.x *= axisSign.y;
  783. tangentNormalZ.x *= -axisSign.z;
  784. // Swizzle world normals to match tangent space and apply Whiteout normal blend.
  785. tangentNormalX = fixed3(tangentNormalX.xy + i.worldNormal.zy, tangentNormalX.z * i.worldNormal.x);
  786. tangentNormalY = fixed3(tangentNormalY.xy + i.worldNormal.xz, tangentNormalY.z * i.worldNormal.y);
  787. tangentNormalZ = fixed3(tangentNormalZ.xy + i.worldNormal.xy, tangentNormalZ.z * i.worldNormal.z);
  788. // Swizzle tangent normals to match world normal and blend together.
  789. worldNormal = normalize(tangentNormalX.zyx * triplanarBlend.x +
  790. tangentNormalY.xzy * triplanarBlend.y +
  791. tangentNormalZ.xyz * triplanarBlend.z);
  792. #else
  793. fixed3 tangentNormal = UnpackScaleNormal(tex2D(_NormalMap, i.uv), _NormalMapScale);
  794. worldNormal.x = dot(i.tangentX, tangentNormal);
  795. worldNormal.y = dot(i.tangentY, tangentNormal);
  796. worldNormal.z = dot(i.tangentZ, tangentNormal);
  797. worldNormal = normalize(worldNormal) * facing;
  798. #endif
  799. #else
  800. worldNormal = normalize(i.worldNormal) * facing;
  801. #endif
  802. #endif
  803. fixed pointToLight = 1.0;
  804. fixed3 fluentLightColor = fixed3(0.0, 0.0, 0.0);
  805. // Hover light.
  806. #if defined(_HOVER_LIGHT)
  807. pointToLight = 0.0;
  808. [unroll]
  809. for (int hoverLightIndex = 0; hoverLightIndex < HOVER_LIGHT_COUNT; ++hoverLightIndex)
  810. {
  811. int dataIndex = hoverLightIndex * HOVER_LIGHT_DATA_SIZE;
  812. fixed hoverValue = HoverLight(_HoverLightData[dataIndex], _HoverLightData[dataIndex + 1].w, i.worldPosition.xyz);
  813. pointToLight += hoverValue;
  814. #if !defined(_HOVER_COLOR_OVERRIDE)
  815. fluentLightColor += lerp(fixed3(0.0, 0.0, 0.0), _HoverLightData[dataIndex + 1].rgb, hoverValue);
  816. #endif
  817. }
  818. #if defined(_HOVER_COLOR_OVERRIDE)
  819. fluentLightColor = _HoverColorOverride.rgb * pointToLight;
  820. #endif
  821. #endif
  822. // Proximity light.
  823. #if defined(_PROXIMITY_LIGHT)
  824. #if !defined(_HOVER_LIGHT)
  825. pointToLight = 0.0;
  826. #endif
  827. [unroll]
  828. for (int proximityLightIndex = 0; proximityLightIndex < PROXIMITY_LIGHT_COUNT; ++proximityLightIndex)
  829. {
  830. int dataIndex = proximityLightIndex * PROXIMITY_LIGHT_DATA_SIZE;
  831. fixed colorValue;
  832. fixed proximityValue = ProximityLight(_ProximityLightData[dataIndex], _ProximityLightData[dataIndex + 1], _ProximityLightData[dataIndex + 2], i.worldPosition.xyz, worldNormal, colorValue);
  833. pointToLight += proximityValue;
  834. #if defined(_PROXIMITY_LIGHT_COLOR_OVERRIDE)
  835. fixed3 proximityColor = MixProximityLightColor(_ProximityLightCenterColorOverride, _ProximityLightMiddleColorOverride, _ProximityLightOuterColorOverride, colorValue);
  836. #else
  837. fixed3 proximityColor = MixProximityLightColor(_ProximityLightData[dataIndex + 3], _ProximityLightData[dataIndex + 4], _ProximityLightData[dataIndex + 5], colorValue);
  838. #endif
  839. #if defined(_PROXIMITY_LIGHT_SUBTRACTIVE)
  840. fluentLightColor -= lerp(fixed3(0.0, 0.0, 0.0), proximityColor, proximityValue);
  841. #else
  842. fluentLightColor += lerp(fixed3(0.0, 0.0, 0.0), proximityColor, proximityValue);
  843. #endif
  844. }
  845. #endif
  846. // Border light.
  847. #if defined(_BORDER_LIGHT)
  848. fixed borderValue;
  849. #if defined(_ROUND_CORNERS)
  850. fixed borderMargin = _RoundCornerMargin + _BorderWidth * 0.5;
  851. cornerCircleRadius = saturate(max(currentCornerRadius - borderMargin, 0.01)) * i.scale.z;
  852. cornerCircleDistance = halfScale - (borderMargin * i.scale.z) - cornerCircleRadius;
  853. borderValue = 1.0 - RoundCornersSmooth(roundCornerPosition, cornerCircleDistance, cornerCircleRadius);
  854. #else
  855. borderValue = max(smoothstep(i.uv.z - _EdgeSmoothingValue, i.uv.z + _EdgeSmoothingValue, distanceToEdge.x),
  856. smoothstep(i.uv.w - _EdgeSmoothingValue, i.uv.w + _EdgeSmoothingValue, distanceToEdge.y));
  857. #endif
  858. #if defined(_HOVER_LIGHT) && defined(_BORDER_LIGHT_USES_HOVER_COLOR) && defined(_HOVER_COLOR_OVERRIDE)
  859. fixed3 borderColor = _HoverColorOverride.rgb;
  860. #else
  861. fixed3 borderColor = fixed3(1.0, 1.0, 1.0);
  862. #endif
  863. fixed3 borderContribution = borderColor * borderValue * _BorderMinValue * _FluentLightIntensity;
  864. #if defined(_BORDER_LIGHT_REPLACES_ALBEDO)
  865. albedo.rgb = lerp(albedo.rgb, borderContribution, borderValue);
  866. #else
  867. albedo.rgb += borderContribution;
  868. #endif
  869. #if defined(_HOVER_LIGHT) || defined(_PROXIMITY_LIGHT)
  870. albedo.rgb += (fluentLightColor * borderValue * pointToLight * _FluentLightIntensity) * 2.0;
  871. #endif
  872. #if defined(_BORDER_LIGHT_OPAQUE)
  873. albedo.a = max(albedo.a, borderValue * _BorderLightOpaqueAlpha);
  874. #endif
  875. #endif
  876. #if defined(_ROUND_CORNERS)
  877. albedo *= roundCornerClip;
  878. pointToLight *= roundCornerClip;
  879. #endif
  880. #if defined(_ALPHA_CLIP)
  881. #if !defined(_ALPHATEST_ON)
  882. _Cutoff = 0.5;
  883. #endif
  884. #if defined(_CLIPPING_PRIMITIVE)
  885. albedo *= (primitiveDistance > 0.0);
  886. #endif
  887. clip(albedo.a - _Cutoff);
  888. albedo.a = 1.0;
  889. #endif
  890. // Blinn phong lighting.
  891. #if defined(_DIRECTIONAL_LIGHT)
  892. #if defined(_LIGHTWEIGHT_RENDER_PIPELINE)
  893. float4 directionalLightDirection = _MainLightPosition;
  894. #else
  895. float4 directionalLightDirection = _WorldSpaceLightPos0;
  896. #endif
  897. fixed diffuse = max(0.0, dot(worldNormal, directionalLightDirection));
  898. #if defined(_SPECULAR_HIGHLIGHTS)
  899. fixed halfVector = max(0.0, dot(worldNormal, normalize(directionalLightDirection + worldViewDir)));
  900. fixed specular = saturate(pow(halfVector, _Shininess * pow(_Smoothness, 4.0)) * (_Smoothness * 2.0) * _Metallic);
  901. #else
  902. fixed specular = 0.0;
  903. #endif
  904. #endif
  905. // Image based lighting (attempt to mimic the Standard shader).
  906. #if defined(_REFLECTIONS)
  907. fixed3 worldReflection = reflect(incident, worldNormal);
  908. fixed4 iblData = UNITY_SAMPLE_TEXCUBE_LOD(unity_SpecCube0, worldReflection, (1.0 - _Smoothness) * UNITY_SPECCUBE_LOD_STEPS);
  909. fixed3 ibl = DecodeHDR(iblData, unity_SpecCube0_HDR);
  910. #if defined(_REFRACTION)
  911. fixed4 refractColor = UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, refract(incident, worldNormal, _RefractiveIndex));
  912. ibl *= DecodeHDR(refractColor, unity_SpecCube0_HDR);
  913. #endif
  914. #else
  915. fixed3 ibl = unity_IndirectSpecColor.rgb;
  916. #endif
  917. // Fresnel lighting.
  918. #if defined(_FRESNEL)
  919. fixed fresnel = 1.0 - saturate(abs(dot(worldViewDir, worldNormal)));
  920. #if defined(_RIM_LIGHT)
  921. fixed3 fresnelColor = _RimColor * pow(fresnel, _RimPower);
  922. #else
  923. fixed3 fresnelColor = unity_IndirectSpecColor.rgb * (pow(fresnel, _FresnelPower) * max(_Smoothness, 0.5));
  924. #endif
  925. #endif
  926. // Final lighting mix.
  927. fixed4 output = albedo;
  928. #if defined(_SPHERICAL_HARMONICS)
  929. fixed3 ambient = i.ambient;
  930. #else
  931. fixed3 ambient = glstate_lightmodel_ambient + fixed3(0.25, 0.25, 0.25);
  932. #endif
  933. fixed minProperty = min(_Smoothness, _Metallic);
  934. #if defined(_DIRECTIONAL_LIGHT)
  935. fixed oneMinusMetallic = (1.0 - _Metallic);
  936. output.rgb = lerp(output.rgb, ibl, minProperty);
  937. #if defined(_LIGHTWEIGHT_RENDER_PIPELINE)
  938. fixed3 directionalLightColor = _MainLightColor.rgb;
  939. #else
  940. fixed3 directionalLightColor = _LightColor0.rgb;
  941. #endif
  942. output.rgb *= lerp((ambient + directionalLightColor * diffuse + directionalLightColor * specular) * max(oneMinusMetallic, _MinMetallicLightContribution), albedo, minProperty);
  943. output.rgb += (directionalLightColor * albedo * specular) + (directionalLightColor * specular * _Smoothness);
  944. output.rgb += ibl * oneMinusMetallic * _IblContribution;
  945. #elif defined(_REFLECTIONS)
  946. output.rgb = lerp(output.rgb, ibl, minProperty);
  947. output.rgb *= lerp(ambient, albedo, minProperty);
  948. #elif defined(_SPHERICAL_HARMONICS)
  949. output.rgb *= ambient;
  950. #endif
  951. #if defined(_FRESNEL)
  952. #if defined(_RIM_LIGHT) || !defined(_REFLECTIONS)
  953. output.rgb += fresnelColor;
  954. #else
  955. output.rgb += fresnelColor * (1.0 - minProperty);
  956. #endif
  957. #endif
  958. #if defined(_EMISSION)
  959. #if defined(_CHANNEL_MAP)
  960. output.rgb += _EmissiveColor * channel.b;
  961. #else
  962. output.rgb += _EmissiveColor;
  963. #endif
  964. #endif
  965. // Inner glow.
  966. #if defined(_INNER_GLOW)
  967. fixed2 uvGlow = pow(distanceToEdge * _InnerGlowColor.a, _InnerGlowPower);
  968. output.rgb += lerp(fixed3(0.0, 0.0, 0.0), _InnerGlowColor.rgb, uvGlow.x + uvGlow.y);
  969. #endif
  970. // Environment coloring.
  971. #if defined(_ENVIRONMENT_COLORING)
  972. fixed3 environmentColor = incident.x * incident.x * _EnvironmentColorX +
  973. incident.y * incident.y * _EnvironmentColorY +
  974. incident.z * incident.z * _EnvironmentColorZ;
  975. output.rgb += environmentColor * max(0.0, dot(incident, worldNormal) + _EnvironmentColorThreshold) * _EnvironmentColorIntensity;
  976. #endif
  977. #if defined(_NEAR_PLANE_FADE)
  978. output *= i.worldPosition.w;
  979. #endif
  980. // Hover and proximity lighting should occur after near plane fading.
  981. #if defined(_HOVER_LIGHT) || defined(_PROXIMITY_LIGHT)
  982. output.rgb += fluentLightColor * _FluentLightIntensity * pointToLight;
  983. #endif
  984. // Perform non-alpha clipped primitive clipping on the final output.
  985. #if defined(_CLIPPING_PRIMITIVE) && !defined(_ALPHA_CLIP)
  986. output *= saturate(primitiveDistance * (1.0f / _BlendedClippingWidth));
  987. #endif
  988. return output;
  989. }
  990. ENDCG
  991. }
  992. // Extracts information for lightmapping, GI (emission, albedo, ...)
  993. // This pass it not used during regular rendering.
  994. Pass
  995. {
  996. Name "Meta"
  997. Tags { "LightMode" = "Meta" }
  998. CGPROGRAM
  999. #pragma vertex vert
  1000. #pragma fragment frag
  1001. #pragma shader_feature EDITOR_VISUALIZATION
  1002. #pragma shader_feature _EMISSION
  1003. #pragma shader_feature _CHANNEL_MAP
  1004. #include "UnityCG.cginc"
  1005. #include "UnityMetaPass.cginc"
  1006. // This define will get commented in by the UpgradeShaderForLightweightRenderPipeline method.
  1007. //#define _LIGHTWEIGHT_RENDER_PIPELINE
  1008. struct v2f
  1009. {
  1010. float4 vertex : SV_POSITION;
  1011. float2 uv : TEXCOORD0;
  1012. };
  1013. float4 _MainTex_ST;
  1014. v2f vert(appdata_full v)
  1015. {
  1016. v2f o;
  1017. o.vertex = UnityMetaVertexPosition(v.vertex, v.texcoord1.xy, v.texcoord2.xy, unity_LightmapST, unity_DynamicLightmapST);
  1018. o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
  1019. return o;
  1020. }
  1021. sampler2D _MainTex;
  1022. sampler2D _ChannelMap;
  1023. fixed4 _Color;
  1024. fixed4 _EmissiveColor;
  1025. #if defined(_LIGHTWEIGHT_RENDER_PIPELINE)
  1026. CBUFFER_START(_LightBuffer)
  1027. float4 _MainLightPosition;
  1028. half4 _MainLightColor;
  1029. CBUFFER_END
  1030. #else
  1031. fixed4 _LightColor0;
  1032. #endif
  1033. half4 frag(v2f i) : SV_Target
  1034. {
  1035. UnityMetaInput output;
  1036. UNITY_INITIALIZE_OUTPUT(UnityMetaInput, output);
  1037. output.Albedo = tex2D(_MainTex, i.uv) * _Color;
  1038. #if defined(_EMISSION)
  1039. #if defined(_CHANNEL_MAP)
  1040. output.Emission += tex2D(_ChannelMap, i.uv).b * _EmissiveColor;
  1041. #else
  1042. output.Emission += _EmissiveColor;
  1043. #endif
  1044. #endif
  1045. #if defined(_LIGHTWEIGHT_RENDER_PIPELINE)
  1046. output.SpecularColor = _MainLightColor.rgb;
  1047. #else
  1048. output.SpecularColor = _LightColor0.rgb;
  1049. #endif
  1050. return UnityMetaFragment(output);
  1051. }
  1052. ENDCG
  1053. }
  1054. }
  1055. Fallback "Hidden/InternalErrorShader"
  1056. }