SkyStudio3DStandard.shader 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  1. // Copyright(c) 2017 Funly LLC
  2. //
  3. // Author: Jason Ederle
  4. // Description: Renders a customizable sky for a 3D skybox sphere.
  5. // Contact: jason@funly.io
  6. Shader "Funly/Sky Studio/Skybox/3D Standard" {
  7. Properties {
  8. // Gradient Sky.
  9. _GradientSkyUpperColor("Sky Top Color", Color) = (.47, .45, .75, 1) // Color of sky.
  10. _GradientSkyMiddleColor("Sky Middle Color", Color) = (1, 1, 1, 1) // Color in the middle of sky 3 way gradient.
  11. _GradientSkyLowerColor("Sky Lower Color", Color) = (.7, .53, .69, 1) // Color of horizon.
  12. _GradientFadeBegin("Horizon Fade Begin", Range(-1, 1)) = -.179 // Position to begin horizon fade into sky.
  13. _GradientFadeEnd("Horizon Fade End", Range(-1, 1)) = .302 // Position to end horizon fade into sky.
  14. _GradientFadeMiddlePosition("Horizon Fade Middle Position", Range(0, 1)) = .5 // Position of the middle gradient color.
  15. // Shrink stars closer to horizon.
  16. _HorizonScaleFactor("Star Horizon Scale Factor", Range(0, 1)) = .7
  17. // Cubemap background.
  18. [NoScaleOffset]_MainTex("Background Cubemap", CUBE) = "white" {} // Cubemap for custom background behind stars.
  19. // Star fading.
  20. _StarFadeBegin("Star Fade Begin", Range(-1, 1)) = .067 // Height to begin star fade in.
  21. _StarFadeEnd("Star Fade End", Range(-1, 1)) = .36 // Height where all stars are faded in at.
  22. // Star (Basic).
  23. [NoScaleOffset]_StarBasicCubemap("Star Basic Cubemap", CUBE) = "black" {}
  24. _StarBasicTwinkleSpeed("Star Basic Twinkle Speed", Range(0, 10)) = 5
  25. _StarBasicTwinkleAmount("Star Basic Twinkle Amount", Range(0, 1)) = .75
  26. _StarBasicOpacity("Star Basic Opacity", Range(0, 1)) = 1
  27. _StarBasicTintColor("Star Basic Tint Color", Color) = (1, 1, 1, 1)
  28. _StarBasicExponent("Star Basic Exponent", Range(.5, 5)) = 1.2
  29. // Star Layer 1.
  30. [NoScaleOffset]_StarLayer1Tex("Star 1 Texture", 2D) = "white" {}
  31. _StarLayer1Color("Star Layer 1 - Color", Color) = (1, 1, 1, 1) // Color tint for stars.
  32. _StarLayer1Density("Star Layer 1 - Star Density", Range(0, .05)) = .01 // Space between stars.
  33. _StarLayer1MaxRadius("Star Layer 1 - Star Size", Range(0, .1)) = .007 // Max radius of stars.
  34. _StarLayer1TwinkleAmount("Star Layer 1 - Twinkle Amount", Range(0, 1)) = .775 // Percent of star twinkle amount.
  35. _StarLayer1TwinkleSpeed("Star Layer 1 - Twinkle Speed", float) = 2.0 // Twinkle speed.
  36. _StarLayer1RotationSpeed("Star Layer 1 - Rotation Speed", float) = 2 // Rotation speed of stars.
  37. _StarLayer1EdgeFade("Star Layer 1 - Edge Feathering", Range(0.0001, .9999)) = .2 // Softness of star blending with background.
  38. _StarLayer1HDRBoost("Star Layer 1 - HDR Bloom Boost", Range(1, 10)) = 1.0 // Boost star colors so they glow with bloom filters.
  39. _StarLayer1SpriteDimensions("Star Layer 1 Sprite Dimensions", Vector) = (0, 0, 0, 0) // Dimensions of columns (x), and rows (y) in sprite sheet.
  40. _StarLayer1SpriteItemCount("Star Layer 1 Sprite Total Items", int) = 1 // Total number of items in sprite sheet.
  41. _StarLayer1SpriteAnimationSpeed("Star Layer 1 Sprite Speed", int) = 1 // Speed of the sprite sheet animation.
  42. [NoScaleOffset]_StarLayer1DataTex("Star Layer 1 - Data Image", 2D) = "black" {} // Data image with star positions.
  43. // Star Layer 2. - See property descriptions from star layer 1.
  44. [NoScaleOffset]_StarLayer2Tex("Star 2 Texture", 2D) = "white" {}
  45. _StarLayer2Color("Star Layer 2 - Color", Color) = (1, .5, .96, 1)
  46. _StarLayer2Density("Star Layer 2 - Star Density", Range(0, .05)) = .01
  47. _StarLayer2MaxRadius("Star Layer 2 - Star Size", Range(0, .4)) = .014
  48. _StarLayer2TwinkleAmount("Star Layer 2 - Twinkle Amount", Range(0, 1)) = .875
  49. _StarLayer2TwinkleSpeed("Star Layer 2 - Twinkle Speed", float) = 3.0
  50. _StarLayer2RotationSpeed("Star Layer 2 - Rotation Speed", float) = 2
  51. _StarLayer2EdgeFade("Star Layer 2 - Edge Feathering", Range(0.0001, .9999)) = .2
  52. _StarLayer2HDRBoost("Star Layer 2 - HDR Bloom Boost", Range(1, 10)) = 1.0
  53. _StarLayer2SpriteDimensions("Star Layer 2 Sprite Dimensions", Vector) = (0, 0, 0, 0)
  54. _StarLayer2SpriteItemCount("Star Layer 2 Sprite Total Items", int) = 1
  55. _StarLayer2SpriteAnimationSpeed("Star Layer 2 Sprite Speed", int) = 1
  56. [NoScaleOffset]_StarLayer2DataTex("Star Layer 2 - Data Image", 2D) = "black" {}
  57. // Star Layer 3. - See property descriptions from star layer 1.
  58. [NoScaleOffset]_StarLayer3Tex("Star 3 Texture", 2D) = "white" {}
  59. _StarLayer3Color("Star Layer 3 - Color", Color) = (.22, 1, .55, 1)
  60. _StarLayer3Density("Star Layer 3 - Star Density", Range(0, .05)) = .01
  61. _StarLayer3MaxRadius("Star Layer 3 - Star Size", Range(0, .4)) = .01
  62. _StarLayer3TwinkleAmount("Star Layer 3 - Twinkle Amount", Range(0, 1)) = .7
  63. _StarLayer3TwinkleSpeed("Star Layer 3 - Twinkle Speed", float) = 1.0
  64. _StarLayer3RotationSpeed("Star Layer 3 - Rotation Speed", float) = 2
  65. _StarLayer3EdgeFade("Star Layer 3 - Edge Feathering", Range(0.0001, .9999)) = .2
  66. _StarLayer3HDRBoost("Star Layer 3 - HDR Bloom Boost", Range(1, 10)) = 1.0
  67. _StarLayer3SpriteDimensions("Star Layer 3 Sprite Dimensions", Vector) = (0, 0, 0, 0)
  68. _StarLayer3SpriteItemCount("Star Layer 3 Sprite Total Items", int) = 1
  69. _StarLayer3SpriteAnimationSpeed("Star Layer 3 Sprite Speed", int) = 1
  70. [NoScaleOffset]_StarLayer3DataTex("Star Layer 3 - Data Image", 2D) = "black" {}
  71. // Moon properties.
  72. [NoScaleOffset]_MoonTex("Moon Texture", 2D) = "white" {} // Moon image.
  73. _MoonColor("Moon Color", Color) = (.66, .65, .55, 1) // Moon tint color.
  74. _MoonRadius("Moon Size", Range(0, 1)) = .1 // Radius of the moon.
  75. _MoonEdgeFade("Moon Edge Feathering", Range(0.0001, .9999)) = .3 // Soften edges of moon texture.
  76. _MoonHDRBoost("Moon HDR Bloom Boost", Range(1, 10)) = 1 // Control brightness for HDR bloom filter.
  77. _MoonSpriteDimensions("Moon Sprite Dimensions", Vector) = (0, 0, 0, 0) // Dimensions of columns (x), and rows (y) in sprite sheet.
  78. _MoonSpriteItemCount("Moon Sprite Total Items", int) = 1 // Total number of items in sprite sheet.
  79. _MoonSpriteAnimationSpeed("Moon Sprite Speed", int) = 1 // Speed of the sprite sheet animation.
  80. _MoonPosition("Moon Position" , Vector) = (0, 0, 0, 0) // Moon Position.
  81. // Sun properties.
  82. [NoScaleOffset]_SunTex("Sun Texture", 2D) = "white" {} // Sun image.
  83. _SunColor("Sun Color", Color) = (.66, .65, .55, 1) // Sun tint color.
  84. _SunRadius("Sun Size", Range(0, 1)) = .1 // Radius of the Sun.
  85. _SunEdgeFade("Sun Edge Feathering", Range(0.0001, .9999)) = .3 // Soften edges of Sun texture.
  86. _SunHDRBoost("Sun HDR Bloom Boost", Range(1, 10)) = 1 // Control brightness for HDR bloom filter.
  87. _SunSpriteDimensions("Sun Sprite Dimensions", Vector) = (0, 0, 0, 0) // Dimensions of columns (x), and rows (y) in sprite sheet.
  88. _SunSpriteItemCount("Sun Sprite Total Items", int) = 1 // Total number of items in sprite sheet.
  89. _SunSpriteAnimationSpeed("Sun Sprite Speed", int) = 1 // Speed of the sprite sheet animation.
  90. _SunPosition("Sun Position Data" , Vector) = (0, 0, 0, 0) // Sun position.
  91. // Noise Cloud properties.
  92. [NoScaleOffset]_CloudNoiseTexture("Cloud Texture", 2D) = "black" {} // Cloud noise texture.
  93. _CloudFadePosition("Cloud Fade Position", Range(0, .97)) = .74 // Position that the clouds will begin fading out at.
  94. _CloudFadeAmount("Cloud Fade Amount", Range(0, 1)) = .5 // Amount of fade to clouds.
  95. _CloudDensity("Cloud Density", Range(0, 1)) = .25 // Cloud density.
  96. _CloudSpeed("Cloud Speed", Range(0, 1)) = .1 // Cloud speed.
  97. _CloudDirection("Cloud Direction", Range(0, 6.283)) = 1.0 // Cloud direction.
  98. _CloudHeight("Cloud Height", Range(0, 1)) = .5 // Cloud height, by scaling up/down texture.
  99. _CloudTextureTiling("Cloud Tiling", Range(.01, 10)) = 2 // Cloud tiling which changes visible resolution.
  100. _CloudColor1("Cloud 1 Color", Color) = (1, 1, 1, 1) // Cloud color 1.
  101. _CloudColor2("Cloud 2 Color", Color) = (.6, .6, .6, 1) // Cloud color 2.
  102. // Cubemap Clouds.
  103. [NoScaleOffset]_CloudCubemapTexture("Cloud Cubemap", CUBE) = "clear" {} // Cloud custom texture.
  104. _CloudCubemapRotationSpeed("Cloud Cubemap Rotation Speed", Range(-1, 1)) = .01 // Rotation speed and direction.
  105. _CloudCubemapTintColor("Cloud Cubemap Tint Color", Color) = (1, 1, 1, 1) // Tint color.
  106. _CloudCubemapHeight("Cloud Cubemap Height", Range(-1, 1)) = 0 // Cloud height
  107. [NoScaleOffset]_CloudCubemapDoubleTexture("Cloud Double Cubemap", CUBE) = "clear" {} // Cloud custom texture.
  108. _CloudCubemapDoubleLayerHeight("Cloud Cubemap Double Layer Offset", float) = 0 // Offset of the duplicate cloud layer.
  109. _CloudCubemapDoubleLayerRotationSpeed("Cloud Cubemap Double Layer Rotation Speed", Range(-1, 1)) = .02
  110. _CloudCubemapDoubleLayerTintColor("Cloud Cubemap Double Tint Color", Color) = (1, 1, 1, 1)
  111. // Cubemap Normal Clouds.
  112. [NoScaleOffset]_CloudCubemapNormalTexture("Cloud Cubemap Normal Texture", CUBE) = "clear" {} // Cubemap texture with normals.
  113. _CloudCubemapNormalAmbientIntensity("Cloud Ambient Light Intensity", Range(0, 1)) = .2 // Ambient light intensity.
  114. _CloudCubemapNormalRotationSpeed("Cloud Cubemap Normal Rotation Speed", Range(-1, 1)) = .01 // Rotation speed and direction.
  115. _CloudCubemapNormalLitColor("Cloud Cubemap Normal Lit Color", Color) = (1, 1, 1, 1) // Lit color.
  116. _CloudCubemapNormalShadowColor("Cloud Cubemap Normal Shadow Color", Color) = (0, 0, 0, 1) // Shadow color.
  117. _CloudCubemapNormalHeight("Cloud Cubemap Normal Height", Range(-1, 1)) = 0 // Cloud height
  118. _CloudCubemapNormalToLight("Cloud Cubemap Light Direction", Vector) = (0, 1, 0, 0) // Direction to light.
  119. [NoScaleOffset]_CloudCubemapNormalDoubleTexture("Cloud Cubemap Normal Double Cubemap", CUBE) = "clear" {} // Cloud custom texture.
  120. _CloudCubemapNormalDoubleLayerHeight("Cloud Cubemap Normal Double Layer Offset", float) = 0 // Offset of the duplicate cloud layer.
  121. _CloudCubemapNormalDoubleLayerRotationSpeed("Cloud Cubemap Normal Double Layer Rotation Speed", Range(-1, 1)) = .02
  122. _CloudCubemapNormalDoubleLitColor("Cloud Cubemap Normal Double Lit Color", Color) = (1, 1, 1, 1) // Double layer lit color.
  123. _CloudCubemapNormalDoubleShadowColor("Cloud Cubemap Normal Double Shadow Color", Color) = (0, 0, 0, 1) // Double layer shadow color.
  124. _HorizonFogColor("Fog Color", Color) = (1, 1, 1, 1) // Fog color.
  125. _HorizonFogDensity("Fog Density", Range(0, 1)) = .12 // Density and visibility of the fog.
  126. _HorizonFogLength("Fog Height", Range(.03, 1)) = .1 // Height the fog reaches up into the skybox.
  127. _DebugPointsCount("Debug Points Count", Range(0, 100)) = 0 // Used for visualizing orbit paths in editor only.
  128. _DebugPointRadius("Debug Point Radius", Range(0, .1)) = .03 // Size of sphere point dots when visualized.
  129. }
  130. SubShader {
  131. Tags { "RenderType"="Opaque" "Queue"="Background" "IgnoreProjector"="true" "PreviewType" = "Skybox" }
  132. LOD 100
  133. ZWrite Off
  134. Cull Off
  135. Pass {
  136. CGPROGRAM
  137. #pragma target 2.0
  138. #pragma multi_compile_fog
  139. #pragma shader_feature_local GRADIENT_BACKGROUND
  140. #pragma shader_feature_local VERTEX_GRADIENT_BACKGROUND
  141. #pragma shader_feature_local STARS_BASIC
  142. #pragma shader_feature_local STAR_LAYER_1
  143. #pragma shader_feature_local STAR_LAYER_2
  144. #pragma shader_feature_local STAR_LAYER_3
  145. #pragma shader_feature_local STAR_LAYER_1_CUSTOM_TEXTURE
  146. #pragma shader_feature_local STAR_LAYER_2_CUSTOM_TEXTURE
  147. #pragma shader_feature_local STAR_LAYER_3_CUSTOM_TEXTURE
  148. #pragma shader_feature_local STAR_LAYER_1_SPRITE_SHEET
  149. #pragma shader_feature_local STAR_LAYER_2_SPRITE_SHEET
  150. #pragma shader_feature_local STAR_LAYER_3_SPRITE_SHEET
  151. #pragma shader_feature_local MOON
  152. #pragma shader_feature_local MOON_CUSTOM_TEXTURE
  153. #pragma shader_feature_local MOON_SPRITE_SHEET
  154. #pragma shader_feature_local SUN
  155. #pragma shader_feature_local SUN_CUSTOM_TEXTURE
  156. #pragma shader_feature_local SUN_SPRITE_SHEET
  157. #pragma shader_feature_local HORIZON_FOG
  158. #pragma shader_feature_local CLOUDS
  159. #pragma shader_feature_local NOISE_CLOUDS
  160. #pragma shader_feature_local CUBEMAP_CLOUDS
  161. #pragma shader_feature_local CUBEMAP_NORMAL_CLOUDS
  162. #pragma shader_feature_local CUBEMAP_CLOUD_DOUBLE_LAYER
  163. #pragma shader_feature_local CUBEMAP_NORMAL_CLOUD_DOUBLE_LAYER
  164. #pragma shader_feature_local CUBEMAP_CLOUD_FORMAT_RGB
  165. #pragma shader_feature_local CUBEMAP_CLOUD_FORMAT_RGBA
  166. #pragma shader_feature_local CUBEMAP_CLOUD_DOUBLE_LAYER_CUSTOM_TEXTURE
  167. #pragma shader_feature_local CUBEMAP_NORMAL_CLOUD_DOUBLE_LAYER_CUSTOM_TEXTURE
  168. #pragma shader_feature_local SUN_ALPHA_BLEND
  169. #pragma shader_feature_local MOON_ALPHA_BLEND
  170. #pragma shader_feature_local MOON_ROTATION
  171. #pragma shader_feature_local RENDER_DEBUG_POINTS
  172. #pragma vertex vert
  173. #pragma fragment frag
  174. #include "UnityCG.cginc"
  175. #include "Utility/SkyMathUtilities.cginc"
  176. #if defined(STAR_LAYER_1) || defined(STAR_LAYER_2) || defined(STAR_LAYER_3)
  177. #define STARS_ADVANCED 1
  178. #endif
  179. struct appdata {
  180. float4 vertex : POSITION;
  181. float3 normal : NORMAL;
  182. };
  183. struct v2f {
  184. float4 vertex : SV_POSITION;
  185. float3 smoothVertex : TEXCOORD1;
  186. #if CLOUDS && NOISE_CLOUDS
  187. float4 cloudUVs : TEXCOORD2;
  188. #endif
  189. #if GRADIENT_BACKGROUND && VERTEX_GRADIENT_BACKGROUND
  190. fixed4 backgroundColor : TEXCOORD3;
  191. #endif
  192. #if SUN
  193. float3 sunLocalPosition : TEXCOORD4;
  194. #endif
  195. #if MOON
  196. float3 moonLocalPosition : TEXCOORD5;
  197. #endif
  198. #if STARS_ADVANCED
  199. float2 starDataTexUV : TEXCOORD6;
  200. #endif
  201. };
  202. // Cubemap.
  203. samplerCUBE _MainTex;
  204. // Gradient sky.
  205. float _UseGradientSky;
  206. fixed4 _GradientSkyUpperColor;
  207. fixed4 _GradientSkyMiddleColor;
  208. fixed4 _GradientSkyLowerColor;
  209. float _GradientFadeMiddlePosition;
  210. float _GradientFadeBegin;
  211. float _GradientFadeEnd;
  212. float _StarFadeBegin;
  213. float _StarFadeEnd;
  214. #if STARS_BASIC
  215. samplerCUBE _StarBasicCubemap;
  216. float _StarBasicTwinkleAmount;
  217. float _StarBasicTwinkleSpeed;
  218. float _StarBasicOpacity;
  219. fixed4 _StarBasicTintColor;
  220. float _StarBasicHDRBoost;
  221. float _StarBasicExponent;
  222. #endif
  223. #ifdef STAR_LAYER_1
  224. // Star Layer 1
  225. fixed4 _StarLayer1Color;
  226. float _StarLayer1MaxRadius;
  227. float _StarLayer1Density;
  228. float _StarLayer1TwinkleAmount;
  229. float _StarLayer1TwinkleSpeed;
  230. float _StarLayer1RotationSpeed;
  231. float _StarLayer1EdgeFade;
  232. sampler2D _StarLayer1DataTex;
  233. float4 _StarLayer1DataTex_ST;;
  234. fixed _StarLayer1HDRBoost;
  235. #ifdef STAR_LAYER_1_CUSTOM_TEXTURE
  236. sampler2D _StarLayer1Tex;
  237. #ifdef STAR_LAYER_1_SPRITE_SHEET
  238. float2 _StarLayer1SpriteDimensions;
  239. int _StarLayer1SpriteItemCount;
  240. int _StarLayer1SpriteAnimationSpeed;
  241. #endif
  242. #endif
  243. #endif
  244. #ifdef STAR_LAYER_2
  245. // Star Layer 2
  246. fixed4 _StarLayer2Color;
  247. float _StarLayer2MaxRadius;
  248. float _StarLayer2Density;
  249. float _StarLayer2TwinkleAmount;
  250. float _StarLayer2TwinkleSpeed;
  251. float _StarLayer2RotationSpeed;
  252. float _StarLayer2EdgeFade;
  253. sampler2D _StarLayer2DataTex;
  254. float4 _StarLayer2DataTex_ST;;
  255. fixed _StarLayer2HDRBoost;
  256. #ifdef STAR_LAYER_2_CUSTOM_TEXTURE
  257. sampler2D _StarLayer2Tex;
  258. #ifdef STAR_LAYER_2_SPRITE_SHEET
  259. float2 _StarLayer2SpriteDimensions;
  260. int _StarLayer2SpriteItemCount;
  261. int _StarLayer2SpriteAnimationSpeed;
  262. #endif
  263. #endif
  264. #endif
  265. #ifdef STAR_LAYER_3
  266. // Star Layer 3
  267. fixed4 _StarLayer3Color;
  268. float _StarLayer3MaxRadius;
  269. float _StarLayer3Density;
  270. float _StarLayer3TwinkleAmount;
  271. float _StarLayer3TwinkleSpeed;
  272. float _StarLayer3RotationSpeed;
  273. float _StarLayer3EdgeFade;
  274. sampler2D _StarLayer3DataTex;
  275. float4 _StarLayer3DataTex_ST;;
  276. fixed _StarLayer3HDRBoost;
  277. #ifdef STAR_LAYER_3_CUSTOM_TEXTURE
  278. sampler2D _StarLayer3Tex;
  279. #ifdef STAR_LAYER_3_SPRITE_SHEET
  280. float2 _StarLayer3SpriteDimensions;
  281. int _StarLayer3SpriteItemCount;
  282. int _StarLayer3SpriteAnimationSpeed;
  283. #endif
  284. #endif
  285. #endif
  286. float _HorizonScaleFactor;
  287. #ifdef MOON
  288. // Moon
  289. float4x4 _MoonWorldToLocalMat;
  290. #ifdef MOON_CUSTOM_TEXTURE
  291. sampler2D _MoonTex;
  292. #ifdef MOON_SPRITE_SHEET
  293. float2 _MoonSpriteDimensions;
  294. int _MoonSpriteItemCount;
  295. int _MoonSpriteAnimationSpeed;
  296. #endif
  297. #endif
  298. fixed4 _MoonColor;
  299. float _MoonRadius;
  300. float _MoonEdgeFade;
  301. fixed _MoonHDRBoost;
  302. float4 _MoonPosition;
  303. #endif
  304. #ifdef SUN
  305. // Sun
  306. float4x4 _SunWorldToLocalMat;
  307. #ifdef SUN_CUSTOM_TEXTURE
  308. sampler2D _SunTex;
  309. #ifdef SUN_SPRITE_SHEET
  310. float2 _SunSpriteDimensions;
  311. int _SunSpriteItemCount;
  312. int _SunSpriteAnimationSpeed;
  313. #endif
  314. #endif
  315. fixed4 _SunColor;
  316. float _SunRadius;
  317. float _SunEdgeFade;
  318. fixed _SunHDRBoost;
  319. float4 _SunPosition;
  320. #endif
  321. #ifdef CLOUDS
  322. // Generic cloud uniforms.
  323. float _CloudSpeed;
  324. float _CloudHeight;
  325. #if NOISE_CLOUDS
  326. sampler2D _CloudNoiseTexture;
  327. float _CloudDensity;
  328. float _CloudDirection;
  329. float _CloudFadePosition;
  330. float _CloudFadeAmount;
  331. float _CloudTextureTiling;
  332. fixed4 _CloudColor1;
  333. fixed4 _CloudColor2;
  334. #endif
  335. #if CUBEMAP_CLOUDS
  336. samplerCUBE _CloudCubemapTexture;
  337. float _CloudCubemapRotationSpeed;
  338. fixed4 _CloudCubemapTintColor;
  339. float _CloudCubemapHeight;
  340. #if CUBEMAP_CLOUD_DOUBLE_LAYER
  341. float _CloudCubemapDoubleLayerHeight;
  342. float _CloudCubemapDoubleLayerRotationSpeed;
  343. fixed4 _CloudCubemapDoubleLayerTintColor;
  344. #if CUBEMAP_CLOUD_DOUBLE_LAYER_CUSTOM_TEXTURE
  345. samplerCUBE _CloudCubemapDoubleTexture;
  346. #endif
  347. #endif // CUBEMAP_CLOUD_DOUBLE_LAYER
  348. #endif
  349. #if CUBEMAP_NORMAL_CLOUDS
  350. samplerCUBE _CloudCubemapNormalTexture;
  351. float _CloudCubemapNormalAmbientIntensity;
  352. float _CloudCubemapNormalRotationSpeed;
  353. float _CloudCubemapNormalHeight;
  354. fixed4 _CloudCubemapNormalLitColor;
  355. fixed4 _CloudCubemapNormalShadowColor;
  356. float3 _CloudCubemapNormalToLight;
  357. #if CUBEMAP_NORMAL_CLOUD_DOUBLE_LAYER
  358. float _CloudCubemapNormalDoubleLayerHeight;
  359. float _CloudCubemapNormalDoubleLayerRotationSpeed;
  360. fixed4 _CloudCubemapNormalDoubleLitColor;
  361. fixed4 _CloudCubemapNormalDoubleShadowColor;
  362. #if CUBEMAP_NORMAL_CLOUD_DOUBLE_LAYER_CUSTOM_TEXTURE
  363. samplerCUBE _CloudCubemapNormalDoubleTexture;
  364. #endif
  365. #endif // CUBEMAP_NORMAL_CLOUD_DOUBLE_LAYER
  366. #endif
  367. #endif
  368. #if HORIZON_FOG
  369. fixed4 _HorizonFogColor;
  370. float _HorizonFogDensity;
  371. float _HorizonFogLength;
  372. #endif
  373. #if RENDER_DEBUG_POINTS
  374. // This is only used in the editor for debugging and will get compiled out.
  375. float4 _DebugPoints[100];
  376. int _DebugPointsCount;
  377. float _DebugPointRadius;
  378. #endif
  379. #define _MAX_CLOUD_COVERAGE 7
  380. #define _CLOUD_HEIGHT_LIMITS float2(30, 100)
  381. fixed4 AlphaBlendPartial(fixed4 top, fixed4 bottom) {
  382. fixed outAlpha = top.a + bottom.a * (1.0f - top.a);
  383. fixed3 outColor = (top.rgb * top.a + bottom.rgb * bottom.a * (1.0f - top.a)) / outAlpha;
  384. return fixed4(outColor, outAlpha);
  385. }
  386. // Does an over alpha blend, assumes bottom color is opaque.
  387. fixed4 AlphaBlend(fixed4 top, fixed4 bottom) {
  388. fixed3 ca = top.xyz;
  389. fixed aa = top.w;
  390. fixed3 cb = bottom.xyz;
  391. fixed ab = bottom.w;
  392. fixed3 color = (ca * aa + cb * ab * (1 - aa)) / (aa + ab * (1 - aa));
  393. return fixed4(color, 1.0f);
  394. }
  395. float2 CalculateStarRotation(float3 star)
  396. {
  397. float3 starPos = float3(star.x, star.y, star.z);
  398. float yRotationAngle = AngleToReachTarget(starPos.xz, UNITY_HALF_PI);
  399. starPos = RotateAroundYAxis(starPos, yRotationAngle);
  400. float xRotationAngle = AngleToReachTarget(starPos.zy, 0.0f);
  401. return float2(xRotationAngle, yRotationAngle);
  402. }
  403. float2 GetUVsForSpherePoint(float3 fragPos, float radius, float3 targetPoint) {
  404. float2 bodyRotations = CalculateStarRotation(targetPoint);
  405. float3 projectedPosition = RotatePoint(fragPos, bodyRotations.x, bodyRotations.y);
  406. // Find our UV position.
  407. return clamp(float2(
  408. (projectedPosition.x + radius) / (2.0 * radius),
  409. (projectedPosition.y + radius) / (2.0 * radius)), 0, 1);
  410. }
  411. float4 GetDataFromTexture(sampler2D tex, float2 uv) {
  412. float4 col = tex2Dlod(tex, float4(uv.x, uv.y, 0.0f, 0.0f));
  413. #if defined(UNITY_COLORSPACE_GAMMA) == false
  414. col.xyz = LinearToGammaSpace(col.xyz);
  415. #endif
  416. return col;
  417. }
  418. inline float4 GetStarDataFromTexture(sampler2D nearbyStarTexture, float2 uv) {
  419. float4 percentData = GetDataFromTexture(nearbyStarTexture, uv);
  420. float2 sphericalCoord = ConvertPercentToSphericalCoordinate(percentData.xy);
  421. return float4(sphericalCoord.x, sphericalCoord.y, percentData.z, 1.0f);
  422. }
  423. float2 AnimateStarRotation(float2 starUV, float rotationSpeed, float scale, float2 pivot) {
  424. return Rotate2d(starUV - pivot, rotationSpeed * _Time.y * scale) + pivot;
  425. }
  426. float GetStarRadius(float noise, float maxRadius, float twinkleAmount) {
  427. float noisePercent = noise;
  428. float minRadius = clamp((1 - twinkleAmount) * maxRadius, 0, maxRadius);
  429. return clamp(maxRadius * noise, minRadius, maxRadius) * _HorizonScaleFactor;
  430. }
  431. uint GetSpriteTargetIndex(int itemCount, int animationSpeed, float seed) {
  432. float delta = _Time.y + (10.0f * seed);
  433. float timePerFrame = 1.0f / (float)animationSpeed;
  434. int frameIndex = (int)(delta / timePerFrame);
  435. return (uint)abs(frameIndex % itemCount);
  436. }
  437. float2 GetSpriteItemSize(float2 dimensions) {
  438. return float2(1.0f / dimensions.x, (1.0f / dimensions.x) * (dimensions.x / dimensions.y));
  439. }
  440. float2 GetSpriteRotationOrigin(uint targetFrameIndex, float2 dimensions, float2 itemSize) {
  441. uint rows = (uint)dimensions.y;
  442. uint columns = (uint)dimensions.x;
  443. return float2(((float)(targetFrameIndex % columns) * itemSize.x + (itemSize.x / 2.0f)),
  444. (float)((rows - 1) - (targetFrameIndex / columns)) * itemSize.y + (itemSize.y / 2.0f));
  445. }
  446. float2 GetSpriteSheetCoords(float2 uv, float2 dimensions, uint targetFrameIndex, float2 itemSize, uint numItems) {
  447. uint rows = (uint)dimensions.y;
  448. uint columns = (uint)dimensions.x;
  449. float2 scaledUV = float2(uv.x * itemSize.x, uv.y * itemSize.y);
  450. float2 offset = float2(
  451. targetFrameIndex % columns * itemSize.x,
  452. ((rows - 1) - (targetFrameIndex / columns)) * itemSize.y);
  453. return scaledUV + offset;
  454. }
  455. float2 ConvertLocalPointToUV(float2 localPoint, float radius) {
  456. float2 shiftedPoint = localPoint.xy + float2(radius / 2.0f, radius / 2.0f);
  457. return abs(shiftedPoint) / radius;
  458. }
  459. #if STARS_ADVANCED
  460. fixed4 StarColorWithTexture(
  461. float3 pos,
  462. float2 starCoords,
  463. float2 starUV,
  464. sampler2D starTexture,
  465. fixed4 starColorTint,
  466. float starDensity,
  467. float radius,
  468. float twinkleAmount,
  469. float twinkleSpeed,
  470. float rotationSpeed,
  471. float edgeFade,
  472. sampler2D nearbyStarsTexture,
  473. float4 gridPointWithNoise) {
  474. float3 gridPoint = gridPointWithNoise.xyz;
  475. float distanceToCenter = distance(pos, gridPoint);
  476. fixed4 outputColor = tex2D(starTexture, starUV) * starColorTint;
  477. // Animate alpha with twinkle wave.
  478. half twinkleWavePercent = smoothstep(-1, 1, cos(gridPointWithNoise.w * (100 + _Time.y) * twinkleSpeed));
  479. outputColor *= clamp(twinkleWavePercent, (1 - twinkleAmount), 1);
  480. // If it's outside the radius, zero is multiplied to clear the color values.
  481. return outputColor * smoothstep(radius, radius * (1 - edgeFade), distanceToCenter);
  482. }
  483. fixed4 StarColorNoTexture(
  484. float3 pos,
  485. fixed4 starColorTint,
  486. float starDensity,
  487. float radius,
  488. float twinkleAmount,
  489. float twinkleSpeed,
  490. float edgeFade,
  491. sampler2D nearbyStarsTexture,
  492. float4 gridPointWithNoise) {
  493. float3 gridPoint = gridPointWithNoise.xyz;
  494. float distanceToCenter = distance(pos, gridPoint);
  495. // Apply a horizon scale so stars are less visible with distance.
  496. radius *= _HorizonScaleFactor;
  497. fixed4 outputColor = starColorTint;
  498. // Animate alpha with twinkle wave.
  499. half twinkleWavePercent = smoothstep(-1, 1, cos(gridPointWithNoise.w * (100 + _Time.y) * twinkleSpeed));
  500. outputColor *= clamp(twinkleWavePercent, (1 - twinkleAmount), 1);
  501. // If it's outside the radius, zero is multiplied to clear the color values.
  502. return outputColor * smoothstep(radius, radius * (1 - edgeFade), distanceToCenter);
  503. }
  504. fixed4 StarColorFromAllGrids(float3 pos, float2 starTextureUV) {
  505. float4 nearbyStar = float4(0, 0, 0, 0);
  506. fixed4 allStarColors = fixed4(0, 0, 0, 0);
  507. float4 nearbySphericalStar = float4(0, 0, 0, 0);
  508. float3 nearbyStarDirection = float3(0, 0, 0);
  509. #ifdef STAR_LAYER_3
  510. nearbySphericalStar = GetStarDataFromTexture(_StarLayer3DataTex, starTextureUV);
  511. nearbyStarDirection = SphericalCoordinateToDirection(nearbySphericalStar.xy);
  512. nearbyStar = float4(nearbyStarDirection.x, nearbyStarDirection.y, nearbyStarDirection.z, nearbySphericalStar.z);
  513. if (distance(pos, nearbyStar) <= _StarLayer3MaxRadius) {
  514. float radius = GetStarRadius(nearbyStar.w, _StarLayer3MaxRadius, _StarLayer3TwinkleAmount);
  515. #ifdef STAR_LAYER_3_CUSTOM_TEXTURE
  516. float2 texUV = GetUVsForSpherePoint(pos, radius, nearbyStar.xyz);
  517. float2 pivot = float2(.5f, .5f);
  518. #if STAR_LAYER_3_SPRITE_SHEET
  519. uint spriteFrameIndex = GetSpriteTargetIndex(_StarLayer3SpriteItemCount, _StarLayer3SpriteAnimationSpeed, nearbyStar.w);
  520. float2 spriteItemSize = GetSpriteItemSize(_StarLayer3SpriteDimensions);
  521. texUV = GetSpriteSheetCoords(texUV, _StarLayer3SpriteDimensions, spriteFrameIndex, spriteItemSize, _StarLayer3SpriteItemCount);
  522. pivot = GetSpriteRotationOrigin(spriteFrameIndex, _StarLayer3SpriteDimensions, spriteItemSize);
  523. #endif
  524. texUV = AnimateStarRotation(texUV, _StarLayer3RotationSpeed * nearbyStar.w, 1, pivot);
  525. allStarColors += StarColorWithTexture(
  526. pos,
  527. starTextureUV,
  528. texUV,
  529. _StarLayer3Tex,
  530. _StarLayer3Color,
  531. _StarLayer3Density,
  532. radius,
  533. _StarLayer3TwinkleAmount,
  534. _StarLayer3TwinkleSpeed,
  535. _StarLayer3RotationSpeed,
  536. _StarLayer3EdgeFade,
  537. _StarLayer3DataTex,
  538. nearbyStar) * _StarLayer3HDRBoost;
  539. #else
  540. allStarColors += StarColorNoTexture(
  541. pos,
  542. _StarLayer3Color,
  543. _StarLayer3Density,
  544. radius,
  545. _StarLayer3TwinkleAmount,
  546. _StarLayer3TwinkleSpeed,
  547. _StarLayer3EdgeFade,
  548. _StarLayer3DataTex,
  549. nearbyStar) * _StarLayer3HDRBoost;
  550. #endif
  551. }
  552. #endif
  553. #ifdef STAR_LAYER_2
  554. nearbySphericalStar = GetStarDataFromTexture(_StarLayer2DataTex, starTextureUV);
  555. nearbyStarDirection = SphericalCoordinateToDirection(nearbySphericalStar.xy);
  556. nearbyStar = float4(nearbyStarDirection.x, nearbyStarDirection.y, nearbyStarDirection.z, nearbySphericalStar.z);
  557. if (distance(pos, nearbyStar) <= _StarLayer2MaxRadius) {
  558. float radius = GetStarRadius(nearbyStar.w, _StarLayer2MaxRadius, _StarLayer2TwinkleAmount);
  559. #ifdef STAR_LAYER_2_CUSTOM_TEXTURE
  560. float2 texUV = GetUVsForSpherePoint(pos, radius, nearbyStar.xyz);
  561. float2 pivot = float2(.5f, .5f);
  562. #if STAR_LAYER_2_SPRITE_SHEET
  563. uint spriteFrameIndex = GetSpriteTargetIndex(_StarLayer2SpriteItemCount, _StarLayer2SpriteAnimationSpeed, nearbyStar.w);
  564. float2 spriteItemSize = GetSpriteItemSize(_StarLayer2SpriteDimensions);
  565. texUV = GetSpriteSheetCoords(texUV, _StarLayer2SpriteDimensions, spriteFrameIndex, spriteItemSize, _StarLayer2SpriteItemCount);
  566. pivot = GetSpriteRotationOrigin(spriteFrameIndex, _StarLayer2SpriteDimensions, spriteItemSize);
  567. #endif
  568. texUV = AnimateStarRotation(texUV, _StarLayer2RotationSpeed * nearbyStar.w, 1, pivot);
  569. allStarColors += StarColorWithTexture(
  570. pos,
  571. starTextureUV,
  572. texUV,
  573. _StarLayer2Tex,
  574. _StarLayer2Color,
  575. _StarLayer2Density,
  576. radius,
  577. _StarLayer2TwinkleAmount,
  578. _StarLayer2TwinkleSpeed,
  579. _StarLayer2RotationSpeed,
  580. _StarLayer2EdgeFade,
  581. _StarLayer2DataTex,
  582. nearbyStar) * _StarLayer2HDRBoost;
  583. #else
  584. allStarColors += StarColorNoTexture(
  585. pos,
  586. _StarLayer2Color,
  587. _StarLayer2Density,
  588. radius,
  589. _StarLayer2TwinkleAmount,
  590. _StarLayer2TwinkleSpeed,
  591. _StarLayer2EdgeFade,
  592. _StarLayer2DataTex,
  593. nearbyStar) * _StarLayer2HDRBoost;
  594. #endif
  595. }
  596. #endif
  597. #ifdef STAR_LAYER_1
  598. nearbySphericalStar = GetStarDataFromTexture(_StarLayer1DataTex, starTextureUV);
  599. nearbyStarDirection = SphericalCoordinateToDirection(nearbySphericalStar.xy);
  600. nearbyStar = float4(nearbyStarDirection.x, nearbyStarDirection.y, nearbyStarDirection.z, nearbySphericalStar.z);
  601. if (distance(pos, nearbyStar) <= _StarLayer1MaxRadius) {
  602. float radius = GetStarRadius(nearbyStar.w, _StarLayer1MaxRadius, _StarLayer1TwinkleAmount);
  603. #ifdef STAR_LAYER_1_CUSTOM_TEXTURE
  604. float2 texUV = GetUVsForSpherePoint(pos, radius, nearbyStar.xyz);
  605. float2 pivot = float2(.5f, .5f);
  606. #if STAR_LAYER_1_SPRITE_SHEET
  607. uint spriteFrameIndex = GetSpriteTargetIndex(_StarLayer1SpriteItemCount, _StarLayer1SpriteAnimationSpeed, nearbyStar.w);
  608. float2 spriteItemSize = GetSpriteItemSize(_StarLayer1SpriteDimensions);
  609. texUV = GetSpriteSheetCoords(texUV, _StarLayer1SpriteDimensions, spriteFrameIndex, spriteItemSize, _StarLayer1SpriteItemCount);
  610. pivot = GetSpriteRotationOrigin(spriteFrameIndex, _StarLayer1SpriteDimensions, spriteItemSize);
  611. #endif
  612. texUV = AnimateStarRotation(texUV, _StarLayer1RotationSpeed * nearbyStar.w, 1, pivot);
  613. allStarColors += StarColorWithTexture(
  614. pos,
  615. starTextureUV,
  616. texUV,
  617. _StarLayer1Tex,
  618. _StarLayer1Color,
  619. _StarLayer1Density,
  620. radius,
  621. _StarLayer1TwinkleAmount,
  622. _StarLayer1TwinkleSpeed,
  623. _StarLayer1RotationSpeed,
  624. _StarLayer1EdgeFade,
  625. _StarLayer1DataTex,
  626. nearbyStar) * _StarLayer1HDRBoost;
  627. #else
  628. allStarColors += StarColorNoTexture(
  629. pos,
  630. _StarLayer1Color,
  631. _StarLayer1Density,
  632. radius,
  633. _StarLayer1TwinkleAmount,
  634. _StarLayer1TwinkleSpeed,
  635. _StarLayer1EdgeFade,
  636. _StarLayer1DataTex,
  637. nearbyStar) * _StarLayer1HDRBoost;
  638. #endif
  639. }
  640. #endif
  641. return allStarColors;
  642. }
  643. #endif // STARS_ADVANCED
  644. fixed4 FadeStarsColor(float verticalPosition, fixed4 starColor) {
  645. float fadeAmount = smoothstep(_StarFadeBegin, _StarFadeEnd, verticalPosition);
  646. return fixed4(starColor.xyz * fadeAmount, 1.0f);
  647. }
  648. #if STARS_BASIC
  649. fixed4 BasicStarColorAtFragment(float3 pos) {
  650. fixed3 starData = texCUBE(_StarBasicCubemap, pos);
  651. float twinklePercent = sin(_Time.y * _StarBasicTwinkleSpeed + (starData.b * 3.0f) ) * .5f + .5f;
  652. float twinkleValue = lerp(1.0f - _StarBasicTwinkleAmount, _StarBasicHDRBoost, twinklePercent);
  653. fixed starIntensity = pow(starData.g + starData.r, _StarBasicExponent) * twinkleValue * _StarBasicOpacity;
  654. return _StarBasicTintColor * starIntensity;
  655. }
  656. #endif
  657. fixed4 Calculate3WayGradientBackgroundAtPosition(float3 pos) {
  658. float2 sphereFragCoord = DirectionToSphericalCoordinate(pos);
  659. float verticalPosition = pos.y;
  660. // 3 way gradient.
  661. float middleGradientPosition = _GradientFadeBegin
  662. + ((_GradientFadeEnd - _GradientFadeBegin) * _GradientFadeMiddlePosition);
  663. fixed4 lowerColor = _GradientSkyLowerColor;
  664. fixed4 middleColor = _GradientSkyMiddleColor;
  665. fixed4 upperColor = _GradientSkyUpperColor;
  666. float bottomColorPercent = smoothstep(_GradientFadeBegin, middleGradientPosition, verticalPosition);
  667. fixed4 bottomMixedColor = lerp(lowerColor, middleColor, bottomColorPercent);
  668. bottomMixedColor *= !step(middleGradientPosition, verticalPosition);
  669. float topColorPercent = smoothstep(middleGradientPosition, _GradientFadeEnd, verticalPosition);
  670. fixed4 topMixedColor = lerp(middleColor, upperColor, topColorPercent);
  671. topMixedColor *= step(middleGradientPosition, verticalPosition);
  672. return bottomMixedColor + topMixedColor;
  673. }
  674. v2f vert(appdata v) {
  675. v2f o;
  676. o.vertex = UnityObjectToClipPos(v.vertex);
  677. float3 normalizedVertex = normalize(v.vertex.xyz);
  678. float3 worldVertexPosition = mul(unity_ObjectToWorld, float4(v.vertex.xyz, 1.0f));
  679. o.smoothVertex = v.vertex;
  680. #ifdef CLOUDS
  681. #if NOISE_CLOUDS
  682. float3 cloudWorldVertex = normalize(mul((float3x3)unity_ObjectToWorld, v.vertex.xyz));
  683. float computedHeight = lerp(_CLOUD_HEIGHT_LIMITS.x, _CLOUD_HEIGHT_LIMITS.y, 1 - _CloudHeight);
  684. cloudWorldVertex.y *= computedHeight * .2f;
  685. float cloudSpeed = _CloudSpeed * _Time;
  686. cloudWorldVertex.xz = Rotate2d(cloudWorldVertex.xz, _CloudDirection);
  687. cloudWorldVertex = normalize(cloudWorldVertex);
  688. o.cloudUVs.xy = (cloudWorldVertex.xz * _CloudTextureTiling) + float2(cloudSpeed, cloudSpeed);
  689. o.cloudUVs.zw = (cloudWorldVertex.xz * _CloudTextureTiling) + float2(cloudSpeed / 10.0f, cloudSpeed / 11.0f);
  690. #endif
  691. #endif
  692. #if GRADIENT_BACKGROUND && VERTEX_GRADIENT_BACKGROUND
  693. o.backgroundColor = Calculate3WayGradientBackgroundAtPosition(normalizedVertex);
  694. #endif
  695. #if SUN
  696. o.sunLocalPosition = mul(_SunWorldToLocalMat, float4(normalizedVertex, 1.0f));
  697. #endif
  698. #if MOON
  699. o.moonLocalPosition = mul(_MoonWorldToLocalMat, float4(normalizedVertex, 1.0f));
  700. #endif
  701. #if STARS_ADVANCED
  702. o.starDataTexUV = ConvertSphericalCoordateToUV(DirectionToSphericalCoordinate(normalizedVertex));
  703. #endif
  704. return o;
  705. }
  706. fixed4 OrbitBodyColorWithTextureUV(float3 pos, float3 orbitBodyPosition,
  707. fixed4 orbitBodyTintColor, float orbitBodyRadius, float orbitBodyEdgeFade, sampler2D orbitBodyTex, float2 bodyUVs) {
  708. fixed4 color = tex2D(orbitBodyTex, bodyUVs) * orbitBodyTintColor;
  709. float fragDistance = distance(orbitBodyPosition, pos);
  710. float fadeEnd = orbitBodyRadius * (1 - orbitBodyEdgeFade);
  711. return smoothstep(orbitBodyRadius, fadeEnd, fragDistance) * color;
  712. }
  713. // Alpha premultiplied into color.
  714. fixed4 OrbitBodyColorNoTexture(float3 pos, float3 orbitBodyPosition,
  715. fixed4 orbitBodyColor, float orbitBodyRadius, float orbitBodyEdgeFade) {
  716. float fragDistance = distance(orbitBodyPosition, pos);
  717. float fadeEnd = orbitBodyRadius * (1 - orbitBodyEdgeFade);
  718. return orbitBodyColor * smoothstep(orbitBodyRadius, fadeEnd, fragDistance);
  719. }
  720. #ifdef CLOUDS
  721. #if CUBEMAP_NORMAL_CLOUDS
  722. float4 SampleNormalCloudCubemap(float3 vertexPos, float rotationSpeed, float heightOffset, float rotationOffset, float3 shadowColor, float3 litColor) {
  723. float rotationAngle = (_Time.y * rotationSpeed) + rotationOffset;
  724. float3 rotatedDirection = RotateAroundYAxis(vertexPos, -rotationAngle);
  725. rotatedDirection.y += (-1.0f * heightOffset);
  726. float4 cloudColor = texCUBE(_CloudCubemapNormalTexture, rotatedDirection);
  727. float3 cloudFragWorldNormal = cloudColor.xyz * 2.0f - 1.0f;
  728. float3 cloudRotatedWorldNormal = RotateAroundYAxis(cloudFragWorldNormal, rotationAngle);
  729. float3 lightPosition = _CloudCubemapNormalToLight * 10.0f;
  730. float3 toLight = normalize(lightPosition - vertexPos.xyz);
  731. float lightDotProduct = dot(cloudRotatedWorldNormal, toLight);
  732. float lightPercent = (lightDotProduct + 1.0f) / 2.0f;
  733. float3 processedColor = lerp(shadowColor, litColor, saturate(_CloudCubemapNormalAmbientIntensity + lightPercent));
  734. return float4(processedColor, cloudColor.w);
  735. }
  736. // Clouds coming from a cubemap with normals.
  737. half4 RenderCubemapNormalClouds(float3 vertexPos, float4 backgroundColor)
  738. {
  739. float4 cloudColor = SampleNormalCloudCubemap(vertexPos, _CloudCubemapNormalRotationSpeed, _CloudCubemapNormalHeight, 0,
  740. _CloudCubemapNormalShadowColor, _CloudCubemapNormalLitColor);
  741. #if CUBEMAP_NORMAL_CLOUD_DOUBLE_LAYER
  742. float4 cloudColor2 = SampleNormalCloudCubemap(vertexPos, _CloudCubemapNormalDoubleLayerRotationSpeed,
  743. _CloudCubemapNormalDoubleLayerHeight, UNITY_HALF_PI, _CloudCubemapNormalDoubleShadowColor, _CloudCubemapNormalDoubleLitColor);
  744. float origAlpha = max(cloudColor.w, cloudColor2.w);
  745. cloudColor = saturate(AlphaBlend(cloudColor, cloudColor2));
  746. cloudColor.w = origAlpha;
  747. #endif
  748. return AlphaBlend(cloudColor, backgroundColor);
  749. }
  750. #endif
  751. #if CUBEMAP_CLOUDS
  752. fixed4 SampleCloudCubemap(float3 vertexPos, float rotationSpeed, float heightOffset, float rotationOffset, samplerCUBE tex, float4 tintColor) {
  753. float rotationAngle = (_Time.y * rotationSpeed) + rotationOffset;
  754. float3 rotatedDirection = RotateAroundYAxis(vertexPos, -rotationAngle);
  755. rotatedDirection.y += (-1.0f * heightOffset);
  756. fixed4 outColor = texCUBE(tex, rotatedDirection) * tintColor;
  757. outColor.a = pow(outColor.a, 2);
  758. return outColor;
  759. }
  760. // Clouds coming from a cubemap, no normals, unlit
  761. fixed4 RenderCubemapClouds(float3 vertexPos, fixed4 backgroundColor) {
  762. float rotationAngle = _Time.y * _CloudCubemapRotationSpeed;
  763. float3 rotatedDirection = RotateAroundYAxis(vertexPos, -rotationAngle);
  764. rotatedDirection.y += (-1.0f * _CloudCubemapHeight);
  765. fixed4 cloudColor = SampleCloudCubemap(vertexPos, _CloudCubemapRotationSpeed, _CloudCubemapHeight,
  766. 0, _CloudCubemapTexture, _CloudCubemapTintColor);
  767. #if CUBEMAP_CLOUD_FORMAT_RGB
  768. // Premultiply alpha before additive blending for opacity control.
  769. cloudColor.xyz *= cloudColor.a;
  770. #if CUBEMAP_CLOUD_DOUBLE_LAYER
  771. #if CUBEMAP_CLOUD_DOUBLE_LAYER_CUSTOM_TEXTURE
  772. fixed4 cloudColor2 = SampleCloudCubemap(vertexPos, _CloudCubemapDoubleLayerRotationSpeed, _CloudCubemapDoubleLayerHeight,
  773. UNITY_HALF_PI, _CloudCubemapDoubleTexture, _CloudCubemapDoubleLayerTintColor);
  774. #else // Else no custom texture.
  775. fixed4 cloudColor2 = SampleCloudCubemap(vertexPos, _CloudCubemapDoubleLayerRotationSpeed, _CloudCubemapDoubleLayerHeight,
  776. UNITY_HALF_PI, _CloudCubemapTexture, _CloudCubemapDoubleLayerTintColor);
  777. #endif // CUBEMAP_CLOUD_DOUBLE_LAYER_CUSTOM_TEXTURE
  778. // Premultiply alpha before additive blending for opacity control.
  779. cloudColor2.xyz *= cloudColor2.a;
  780. return fixed4(cloudColor.xyz + cloudColor2.xyz + backgroundColor.xyz, 1.0f);
  781. #else // Else, no double layer.
  782. return fixed4(cloudColor.xyz + backgroundColor.xyz, 1.0f);
  783. #endif // CUBEMAP_CLOUD_DOUBLE_LAYER
  784. #else // CUBEMAP RGBA
  785. #if CUBEMAP_CLOUD_DOUBLE_LAYER
  786. #if CUBEMAP_CLOUD_DOUBLE_LAYER_CUSTOM_TEXTURE
  787. fixed4 cloudColor2 = SampleCloudCubemap(vertexPos, _CloudCubemapDoubleLayerRotationSpeed, _CloudCubemapDoubleLayerHeight,
  788. UNITY_HALF_PI, _CloudCubemapDoubleTexture, _CloudCubemapDoubleLayerTintColor);
  789. #else
  790. fixed4 cloudColor2 = SampleCloudCubemap(vertexPos, _CloudCubemapDoubleLayerRotationSpeed, _CloudCubemapDoubleLayerHeight,
  791. UNITY_HALF_PI, _CloudCubemapTexture, _CloudCubemapDoubleLayerTintColor);
  792. #endif
  793. cloudColor = AlphaBlendPartial(cloudColor, cloudColor2);
  794. #endif // CUBEMAP_CLOUD_DOUBLE_LAYER
  795. backgroundColor.a = 1.0f;
  796. return AlphaBlend(cloudColor, backgroundColor);
  797. #endif
  798. }
  799. #endif
  800. #if NOISE_CLOUDS
  801. half4 RenderNoiseClouds(float4 cloudUVs, float3 vertexPos, float4 backgroundColor) {
  802. // Cloud noise.
  803. float4 tex1 = GetDataFromTexture(_CloudNoiseTexture, cloudUVs.xy);
  804. float4 tex2 = GetDataFromTexture(_CloudNoiseTexture, cloudUVs.zw);
  805. float noise1 = pow(tex1.g + tex2.g, 0.25f);
  806. float noise2 = pow(tex2.b * tex1.r, 0.5f);
  807. // Percent in the fadeout (0 means no fadeout, 1 means full fadeout - no clouds)
  808. float fadeOutPercent = smoothstep(_CloudFadePosition, 1, length(vertexPos.xz));
  809. _CloudColor1.rgb = pow(_CloudColor1.rgb, 2.2f);
  810. _CloudColor2.rgb = pow(_CloudColor2.rgb, 2.2f);
  811. float3 cloud1 = lerp(float3(0, 0, 0), _CloudColor2.rgb, noise1);
  812. float3 cloud2 = lerp(float3(0, 0, 0), _CloudColor1.rgb, noise2) * 1.5f;
  813. float3 cloud = lerp(cloud1, cloud2, noise1 * noise2);
  814. // Cloud alpha.
  815. float outColorAlpha = 1.0f;
  816. float expandedDensity = _MAX_CLOUD_COVERAGE * (1.0f - _CloudDensity);
  817. float cloudAlpha = saturate(pow(noise1 * noise2, expandedDensity)) * pow(outColorAlpha, 0.35f);
  818. cloudAlpha *= 1.0f - fadeOutPercent * _CloudFadeAmount;
  819. cloudAlpha *= step(0.0f, vertexPos.y);
  820. float3 outColor = lerp(backgroundColor, cloud, cloudAlpha);
  821. return half4(outColor, 1.0f);
  822. }
  823. #endif
  824. #endif // CLOUDS
  825. #ifdef RENDER_DEBUG_POINTS
  826. // Debug points are used for visualized spherical point keyframes in the editor only.
  827. fixed4 RenderDebugPoints(float3 pos) {
  828. fixed4 pointColor = fixed4(1, 0, 0, 1);
  829. fixed4 selectedPointColor = fixed4(0, 1, 0, 1);
  830. for (int i = 0; i < _DebugPointsCount; i++) {
  831. float4 debugPoint = _DebugPoints[i];
  832. float radius = debugPoint.w;
  833. if (distance(debugPoint.xyz, pos) <= _DebugPointRadius) {
  834. half4 color = pointColor;
  835. if (debugPoint.w > 0) {
  836. return selectedPointColor;
  837. } else {
  838. return pointColor;
  839. }
  840. }
  841. }
  842. return fixed4(0, 0, 0, 0);
  843. }
  844. #endif
  845. #ifdef HORIZON_FOG
  846. fixed4 ApplyHorizonFog(fixed4 skyColor, float3 vertexPos) {
  847. float fadePercent = smoothstep(1 - _HorizonFogLength, 1, length(vertexPos.xz));
  848. fadePercent *= _HorizonFogDensity;
  849. return lerp(skyColor, _HorizonFogColor, fadePercent);
  850. }
  851. #endif
  852. fixed4 frag(v2f i) : SV_Target {
  853. #ifdef GRADIENT_BACKGROUND
  854. #if VERTEX_GRADIENT_BACKGROUND
  855. fixed4 background = i.backgroundColor;
  856. #else
  857. fixed4 background = Calculate3WayGradientBackgroundAtPosition(i.smoothVertex);
  858. #endif
  859. #else
  860. fixed4 background = texCUBE(_MainTex, i.smoothVertex);
  861. #endif
  862. float3 normalizedSmoothVertex = normalize(i.smoothVertex);
  863. bool isMoonPixel = 0;
  864. bool isSunPixel = 0;
  865. fixed4 sunColor = fixed4(0, 0, 0, 0);
  866. fixed4 moonColor = fixed4(0, 0, 0, 0);
  867. #ifdef MOON
  868. isMoonPixel = step(distance(normalizedSmoothVertex, _MoonPosition.xyz), _MoonRadius);
  869. float2 moonTexUV = ConvertLocalPointToUV(i.moonLocalPosition.xyz, _MoonRadius * 2.0f);
  870. #if MOON_CUSTOM_TEXTURE
  871. #if MOON_SPRITE_SHEET
  872. uint spriteFrameIndex = GetSpriteTargetIndex(_MoonSpriteItemCount, _MoonSpriteAnimationSpeed, 0.0f);
  873. float2 spriteItemSize = GetSpriteItemSize(_MoonSpriteDimensions.xy);
  874. moonTexUV = GetSpriteSheetCoords(moonTexUV, _MoonSpriteDimensions, spriteFrameIndex, spriteItemSize, _MoonSpriteItemCount);
  875. #endif
  876. moonColor = OrbitBodyColorWithTextureUV(
  877. normalizedSmoothVertex,
  878. _MoonPosition.xyz,
  879. _MoonColor,
  880. _MoonRadius,
  881. _MoonEdgeFade,
  882. _MoonTex,
  883. moonTexUV) * _MoonHDRBoost * isMoonPixel;
  884. #else
  885. moonColor = OrbitBodyColorNoTexture(
  886. normalizedSmoothVertex,
  887. _MoonPosition.xyz,
  888. _MoonColor,
  889. _MoonRadius,
  890. _MoonEdgeFade) * _MoonHDRBoost * isMoonPixel;
  891. #endif
  892. #endif
  893. #ifdef SUN
  894. isSunPixel = step(distance(normalizedSmoothVertex, _SunPosition.xyz), _SunRadius);
  895. float2 sunTexUV = ConvertLocalPointToUV(i.sunLocalPosition.xyz, _SunRadius * 2.0f);
  896. #if SUN_CUSTOM_TEXTURE
  897. #if SUN_SPRITE_SHEET
  898. uint spriteFrameIndex = GetSpriteTargetIndex(_SunSpriteItemCount, _SunSpriteAnimationSpeed, 0.0f);
  899. float2 spriteItemSize = GetSpriteItemSize(_SunSpriteDimensions.xy);
  900. sunTexUV = GetSpriteSheetCoords(sunTexUV, _SunSpriteDimensions, spriteFrameIndex, spriteItemSize, _SunSpriteItemCount);
  901. #endif
  902. sunColor = OrbitBodyColorWithTextureUV(
  903. normalizedSmoothVertex,
  904. _SunPosition.xyz,
  905. _SunColor,
  906. _SunRadius,
  907. _SunEdgeFade,
  908. _SunTex,
  909. sunTexUV) * _SunHDRBoost * isSunPixel;
  910. #else
  911. sunColor = OrbitBodyColorNoTexture(
  912. normalizedSmoothVertex,
  913. _SunPosition.xyz,
  914. _SunColor,
  915. _SunRadius,
  916. _SunEdgeFade) * _SunHDRBoost * isSunPixel;
  917. #endif
  918. #endif
  919. #if STARS_BASIC
  920. fixed4 starColor = BasicStarColorAtFragment(normalizedSmoothVertex);
  921. starColor = FadeStarsColor(i.smoothVertex.y, starColor);
  922. // FIXME - Create generic macro for if any star type is active.
  923. #if MOON && !defined(MOON_ALPHA_BLEND)
  924. starColor *= (fixed)!isMoonPixel;
  925. #endif
  926. #if SUN && !defined(SUN_ALPHA_BLEND)
  927. starColor *= (fixed)!isSunPixel;
  928. #endif
  929. background.xyz += starColor.xyz;
  930. #elif STARS_ADVANCED
  931. fixed4 starColor = StarColorFromAllGrids(normalizedSmoothVertex, i.starDataTexUV);
  932. // Fade stars over the horizon.
  933. starColor = FadeStarsColor(i.smoothVertex.y, starColor);
  934. #if MOON && !defined(MOON_ALPHA_BLEND)
  935. starColor *= (fixed)!isMoonPixel;
  936. #endif
  937. #if SUN && !defined(SUN_ALPHA_BLEND)
  938. starColor *= (fixed)!isSunPixel;
  939. #endif
  940. background.xyz += starColor.xyz;
  941. #endif
  942. #ifdef RENDER_DEBUG_POINTS
  943. fixed4 debugPointColor = RenderDebugPoints(normalize(i.smoothVertex.xyz));
  944. bool useDebugColor = step(.1f, length(debugPointColor));
  945. debugPointColor *= useDebugColor;
  946. background *= (fixed)!useDebugColor;
  947. background = background + debugPointColor;
  948. #endif
  949. // Merge the stars over the background color.
  950. fixed4 upperSkyColor = background;
  951. fixed4 finalColor = fixed4(0, 0, 0, 1);
  952. #ifdef SUN_ALPHA_BLEND
  953. finalColor = AlphaBlend(sunColor, upperSkyColor);
  954. #else
  955. finalColor = upperSkyColor + sunColor;
  956. #endif
  957. #ifdef MOON_ALPHA_BLEND
  958. finalColor = AlphaBlend(moonColor, finalColor);
  959. #else
  960. finalColor += moonColor;
  961. #endif
  962. #ifdef CLOUDS
  963. #if NOISE_CLOUDS
  964. finalColor = RenderNoiseClouds(i.cloudUVs, i.smoothVertex, finalColor);
  965. #elif CUBEMAP_CLOUDS
  966. finalColor = RenderCubemapClouds(i.smoothVertex, finalColor);
  967. #elif CUBEMAP_NORMAL_CLOUDS
  968. finalColor = RenderCubemapNormalClouds(i.smoothVertex, finalColor);
  969. #endif
  970. #endif
  971. #ifdef HORIZON_FOG
  972. finalColor = ApplyHorizonFog(finalColor, i.smoothVertex);
  973. #endif
  974. return finalColor;
  975. }
  976. ENDCG
  977. }
  978. }
  979. CustomEditor "DoNotModifyShaderEditor"
  980. Fallback "Unlit/Color"
  981. }