StarSky.shader 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. Shader "Unlit/StarSky"
  2. {
  3. Properties
  4. {
  5. [Header(Sky Setting)]
  6. _Color1 ("Top Color", Color) = (1, 1, 1, 0)
  7. _Color2 ("Horizon Color", Color) = (1, 1, 1, 0)
  8. _Color3 ("Bottom Color", Color) = (1, 1, 1, 0)
  9. _Exponent1 ("Exponent Factor for Top Half", Float) = 1.0
  10. _Exponent2 ("Exponent Factor for Bottom Half", Float) = 1.0
  11. _Intensity ("Intensity Amplifier", Float) = 1.0
  12. [Header(Star Setting)]
  13. [HDR]_StarColor ("Star Color", Color) = (1,1,1,0)
  14. _StarIntensity("Star Intensity", Range(0,1)) = 0.5
  15. _StarSpeed("Star Speed", Range(0,1)) = 0.5
  16. [Header(Cloud Setting)]
  17. [HDR]_CloudColor ("Cloud Color", Color) = (1,1,1,0)
  18. _CloudIntensity("Cloud Intensity", Range(0,1)) = 0.5
  19. _CloudSpeed("CloudSpeed", Range(0,1)) = 0.5
  20. [Header(Aurora Setting)]
  21. [HDR]_AuroraColor ("Aurora Color", Color) = (1,1,1,0)
  22. _AuroraIntensity("Aurora Intensity", Range(0,1)) = 0.5
  23. _AuroraSpeed("AuroraSpeed", Range(0,1)) = 0.5
  24. _SurAuroraColFactor("Sur Aurora Color Factor", Range(0,1)) = 0.5
  25. [Header(Envirment Setting)]
  26. [HDR]_MountainColor ("Mountain Color", Color) = (1,1,1,0)
  27. _MountainFactor("Mountain Factor", Range(0,1)) = 0.5
  28. _MountainHeight("Mountain Height", Range(0,2)) = 0.5
  29. }
  30. CGINCLUDE
  31. #include "UnityCG.cginc"
  32. struct appdata
  33. {
  34. float4 position : POSITION;
  35. float3 texcoord : TEXCOORD0;
  36. float3 normal : NORMAL;
  37. };
  38. struct v2f
  39. {
  40. float4 position : SV_POSITION;
  41. float3 texcoord : TEXCOORD0;
  42. float3 normal : TEXCOORD1;
  43. };
  44. // 环境背景颜色
  45. half4 _Color1;
  46. half4 _Color2;
  47. half4 _Color3;
  48. half _Intensity;
  49. half _Exponent1;
  50. half _Exponent2;
  51. //星星
  52. half4 _StarColor;
  53. half _StarIntensity;
  54. half _StarSpeed;
  55. // 云
  56. half4 _CloudColor;
  57. half _CloudIntensity;
  58. half _CloudSpeed;
  59. // 极光
  60. half4 _AuroraColor;
  61. half _AuroraIntensity;
  62. half _AuroraSpeed;
  63. half _SurAuroraColFactor;
  64. // 远景山
  65. half4 _MountainColor;
  66. float _MountainFactor;
  67. half _MountainHeight;
  68. v2f vert (appdata v)
  69. {
  70. v2f o;
  71. o.position = UnityObjectToClipPos (v.position);
  72. o.texcoord = v.texcoord;
  73. o.normal = v.normal;
  74. return o;
  75. }
  76. // 星空散列哈希
  77. float StarAuroraHash(float3 x) {
  78. float3 p = float3(dot(x,float3(214.1 ,127.7,125.4)),
  79. dot(x,float3(260.5,183.3,954.2)),
  80. dot(x,float3(209.5,571.3,961.2)) );
  81. return -0.001 + _StarIntensity*frac(sin(p)*43758.5453123);
  82. }
  83. // 星空噪声
  84. float StarNoise(float3 st){
  85. // 卷动星空
  86. st += float3(0,_Time.y*_StarSpeed,0);
  87. // fbm
  88. float3 i = floor(st);
  89. float3 f = frac(st);
  90. float3 u = f*f*(3.0-1.0*f);
  91. return lerp(lerp(dot(StarAuroraHash( i + float3(0.0,0.0,0.0)), f - float3(0.0,0.0,0.0) ),
  92. dot(StarAuroraHash( i + float3(1.0,0.0,0.0)), f - float3(1.0,0.0,0.0) ), u.x),
  93. lerp(dot(StarAuroraHash( i + float3(0.0,1.0,0.0)), f - float3(0.0,1.0,0.0) ),
  94. dot(StarAuroraHash( i + float3(1.0,1.0,0.0)), f - float3(1.0,1.0,0.0) ), u.y), u.z) ;
  95. }
  96. // 云散列哈希
  97. float CloudHash (float2 st) {
  98. return frac(sin(dot(st.xy,
  99. float2(12.9898,78.233)))*
  100. 43758.5453123);
  101. }
  102. // 云噪声
  103. float CloudNoise (float2 st,int flow) {
  104. // 云卷动
  105. st += float2(0,_Time.y*_CloudSpeed*flow);
  106. float2 i = floor(st);
  107. float2 f = frac(st);
  108. float a = CloudHash(i);
  109. float b = CloudHash(i + float2(1.0, 0.0));
  110. float c = CloudHash(i + float2(0.0, 1.0));
  111. float d = CloudHash(i + float2(1.0, 1.0));
  112. float2 u = f * f * (3.0 - 2.0 * f);
  113. return lerp(a, b, u.x) +
  114. (c - a)* u.y * (1.0 - u.x) +
  115. (d - b) * u.x * u.y;
  116. }
  117. // 云分型
  118. float Cloudfbm (float2 st,int flow) {
  119. float value = 0.0;
  120. float amplitude = .5;
  121. float frequency = 0.;
  122. for (int i = 0; i < 6; i++) {
  123. value += amplitude * CloudNoise(st,flow);
  124. st *= 2.;
  125. amplitude *= .5;
  126. }
  127. return value;
  128. }
  129. //极光噪声
  130. float AuroraHash(float n ) {
  131. return frac(sin(n)*758.5453);
  132. }
  133. float AuroraNoise(float3 x)
  134. {
  135. float3 p = floor(x);
  136. float3 f = frac(x);
  137. float n = p.x + p.y*57.0 + p.z*800.0;
  138. float res = lerp(lerp(lerp( AuroraHash(n+ 0.0), AuroraHash(n+ 1.0),f.x), lerp( AuroraHash(n+ 57.0), AuroraHash(n+ 58.0),f.x),f.y),
  139. lerp(lerp( AuroraHash(n+800.0), AuroraHash(n+801.0),f.x), lerp( AuroraHash(n+857.0), AuroraHash(n+858.0),f.x),f.y),f.z);
  140. return res;
  141. }
  142. //极光分型
  143. float Aurorafbm(float3 p )
  144. {
  145. float f = 0.50000*AuroraNoise( p );
  146. p *= 2.02;
  147. f += 0.25000*AuroraNoise( p );
  148. p *= 2.03;
  149. f += 0.12500*AuroraNoise( p );
  150. p *= 2.01;
  151. f += 0.06250*AuroraNoise( p );
  152. p *= 2.04;
  153. f += 0.03125*AuroraNoise( p );
  154. return f*1.032258;
  155. }
  156. float GetAurora(float3 p)
  157. {
  158. p+=Aurorafbm(float3(p.x,p.y,0.0)*0.5)*2.25;
  159. float a = smoothstep(.0, .9, Aurorafbm(p*2.)*2.2-1.1);
  160. return a<0.0 ? 0.0 : a;
  161. }
  162. /**************带状极光***************/
  163. // 旋转矩阵
  164. float2x2 RotateMatrix(float a){
  165. float c = cos(a);
  166. float s = sin(a);
  167. return float2x2(c,s,-s,c);
  168. }
  169. float tri(float x){
  170. return clamp(abs(frac(x)-0.5),0.01,0.49);
  171. }
  172. float2 tri2(float2 p){
  173. return float2(tri(p.x)+tri(p.y),tri(p.y+tri(p.x)));
  174. }
  175. // 极光噪声
  176. float SurAuroraNoise(float2 pos)
  177. {
  178. float intensity=1.8;
  179. float size=2.5;
  180. float rz = 0;
  181. pos = mul(RotateMatrix(pos.x*0.06),pos);
  182. float2 bp = pos;
  183. for (int i=0; i<5; i++)
  184. {
  185. float2 dg = tri2(bp*1.85)*.75;
  186. dg = mul(RotateMatrix(_Time.y*_AuroraSpeed),dg);
  187. pos -= dg/size;
  188. bp *= 1.3;
  189. size *= .45;
  190. intensity *= .42;
  191. pos *= 1.21 + (rz-1.0)*.02;
  192. rz += tri(pos.x+tri(pos.y))*intensity;
  193. pos = mul(-float2x2(0.95534, 0.29552, -0.29552, 0.95534),pos);
  194. }
  195. return clamp(1.0/pow(rz*29., 1.3),0,0.55);
  196. }
  197. float SurHash(float2 n){
  198. return frac(sin(dot(n, float2(12.9898, 4.1414))) * 43758.5453);
  199. }
  200. float4 SurAurora(float3 pos,float3 ro)
  201. {
  202. float4 col = float4(0,0,0,0);
  203. float4 avgCol = float4(0,0,0,0);
  204. // 逐层
  205. for(int i=0;i<30;i++)
  206. {
  207. // 坐标
  208. float of = 0.006*SurHash(pos.xy)*smoothstep(0,15, i);
  209. float pt = ((0.8+pow(i,1.4)*0.002)-ro.y)/(pos.y*2.0+0.8);
  210. pt -= of;
  211. float3 bpos = ro + pt*pos;
  212. float2 p = bpos.zx;
  213. // 颜色
  214. float noise = SurAuroraNoise(p);
  215. float4 col2 = float4(0,0,0, noise);
  216. col2.rgb = (sin(1.0-float3(2.15,-.5, 1.2)+i*_SurAuroraColFactor*0.1)*0.8+0.5)*noise;
  217. avgCol = lerp(avgCol, col2, 0.5);
  218. col += avgCol*exp2(-i*0.065 - 2.5)*smoothstep(0.,5., i);
  219. }
  220. col *= (clamp(pos.y*15.+.4,0.,1.));
  221. return col*1.8;
  222. }
  223. half4 frag (v2f i) : COLOR
  224. {
  225. // return fixed4(SurAuroraNoise(i.texcoord.xy),SurAuroraNoise(i.texcoord.xy),SurAuroraNoise(i.texcoord.xy),1);
  226. // 底色
  227. float p = normalize(i.texcoord).y;
  228. float p1 = 1.0f - pow (min (1.0f, 1.0f - p), _Exponent1);
  229. float p3 = 1.0f - pow (min (1.0f, 1.0f + p), _Exponent2);
  230. float p2 = 1.0f - p1 - p3;
  231. int reflection = i.texcoord.y < 0 ? -1 : 1;
  232. //// 云
  233. //float cloud = Cloudfbm(fixed2(i.texcoord.x,i.texcoord.z) * 8,1);
  234. //float4 cloudCol = float4(cloud * _CloudColor.rgb,cloud*0.8) * _CloudIntensity;
  235. // 星星
  236. float star =StarNoise(fixed3(i.texcoord.x,i.texcoord.y * reflection,i.texcoord.z) * 64);
  237. float4 starOriCol = float4(_StarColor.r + 3.25*sin(i.texcoord.x) + 2.45 * (sin(_Time.y * _StarSpeed) + 1)*0.5,
  238. _StarColor.g + 3.85*sin(i.texcoord.y) + 1.45 * (sin(_Time.y * _StarSpeed) + 1)*0.5,
  239. _StarColor.b + 3.45*sin(i.texcoord.z) + 4.45 * (sin(_Time.y * _StarSpeed) + 1)*0.5,
  240. _StarColor.a + 3.85*star);
  241. star = star > 0.8 ? star:smoothstep(0.81,0.98,star);
  242. float4 starCol = fixed4((starOriCol * star).rgb,star);
  243. ////带状极光
  244. //float4 surAuroraCol = smoothstep(0.0,1.5,SurAurora(
  245. // float3(i.texcoord.x,abs(i.texcoord.y),i.texcoord.z),
  246. // float3(0,0,-6.7)
  247. // )) + (reflection-1)*-0.2*0.5;
  248. //极光
  249. float3 aurora = float3 (_AuroraColor.rgb * GetAurora(float3(i.texcoord.xy*float2(1.2,1.0), _Time.y*_AuroraSpeed*0.22)) * 0.9 +
  250. _AuroraColor.rgb * GetAurora(float3(i.texcoord.xy*float2(1.0,0.7) , _Time.y*_AuroraSpeed*0.27)) * 0.9 +
  251. _AuroraColor.rgb * GetAurora(float3(i.texcoord.xy*float2(0.8,0.6) , _Time.y*_AuroraSpeed*0.29)) * 0.5 +
  252. _AuroraColor.rgb * GetAurora(float3(i.texcoord.xy*float2(0.9,0.5) , _Time.y*_AuroraSpeed*0.20)) * 0.57);
  253. float4 auroraCol = float4(aurora,0.1354);
  254. //// 远处山脉
  255. //float mountain = Cloudfbm(fixed2(lerp(0,1,(i.texcoord.x+1)/2),0.412436) * 10,0);
  256. //mountain = (smoothstep(0,(Cloudfbm(fixed2(lerp(0,1,(i.texcoord.x+1)/2),0.412436) * 10,0)),mountain)) * mountain *1.8 - _MountainHeight;
  257. //float4 mountainCol = fixed4(_MountainColor.rgb,(abs(i.texcoord.y) < mountain * 0.15) ? 1 : 0);
  258. //混合
  259. float4 skyCol = (_Color1 * p1 + _Color2 * p2 + _Color3 * p3) * _Intensity;
  260. starCol = reflection==1?starCol:starCol*0.5;
  261. skyCol = skyCol*(1 - starCol.a) + starCol * starCol.a;
  262. //skyCol = skyCol*(1 - cloudCol.a) + cloudCol * cloudCol.a;
  263. //skyCol = skyCol*(1 - surAuroraCol.a) + surAuroraCol * surAuroraCol.a;
  264. skyCol = skyCol*(1 - auroraCol.a) + auroraCol * auroraCol.a;
  265. //skyCol = skyCol*(1 - mountainCol.a) + mountainCol * mountainCol.a;
  266. // 计算水面反射
  267. if(reflection == -1){
  268. // 水面波纹
  269. float c = dot(float3(0,1,0),i.texcoord.xyz);
  270. float3 pos = i.texcoord.xyz * (1.23 / c);
  271. float re = Cloudfbm(pos.xz * 0.5,1);
  272. skyCol.rgb *= (lerp(0.35,1.12,re) - 0.1) ;
  273. float4 reCol = fixed4(skyCol.rgb + re*0.085,re*0.05);
  274. skyCol = skyCol*(1 - reCol.a) + reCol * reCol.a;
  275. skyCol+=_Color2 * p2 * 0.2;
  276. }
  277. return skyCol;
  278. }
  279. ENDCG
  280. SubShader
  281. {
  282. Tags { "RenderType"="Background" "Queue"="Background" }
  283. Pass
  284. {
  285. ZWrite Off
  286. Cull Off
  287. Fog { Mode Off }
  288. CGPROGRAM
  289. #pragma fragmentoption ARB_precision_hint_fastest
  290. #pragma vertex vert
  291. #pragma fragment frag
  292. ENDCG
  293. }
  294. }
  295. }