SunCorona.shader 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
  2. // Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
  3. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  4. Shader "FORGE3D/Planets/Sun Corona"
  5. {
  6. Properties
  7. {
  8. _CoronaNoise("Corona Noise Map", 2D) = "black" {}
  9. _DischargeTileX("Discharge Tile X", float) = 0
  10. _DischargeTileY("Discharge Tile Y", float) = 0
  11. _DischargePanSpeed("Discharge Pan Speed", float) = 0
  12. _CoronaFluidTile("Corona Fluid Tile", float) = 0
  13. _CoronaFluidInfluence("Corona Fluid Influence", float) = 0
  14. _SolarStormFalloff("Solar Storm Falloff", float) = 0
  15. _SolarStormPower("Solar Storm Power", float) = 0
  16. _CoronaTileX("Corona Tile X", float) = 0
  17. _CoronaTileY("Corona Tile Y", float) = 0
  18. _CoronaSpeed("Corona Speed", float) = 0
  19. _CoronaAmp("Corona Amp", float) = 0
  20. _CoronaExp("Corona Exp", float) = 0
  21. _CoronaBoost("Corona Boost", float) = 0
  22. _CoronaFalloff("Corona Falloff", float) = 0
  23. _CoronaColor("Corona Color", Color) = (0.5,0.5,0.5,0.5)
  24. _EdgeMaskFalloff("Edge Mask Falloff", float) = 0
  25. _EdgeMaskPower("Edge Mask Power", float) = 0
  26. _InvFade ("Fade Factor", float) = 1.0
  27. }
  28. Category
  29. {
  30. Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
  31. Blend One One
  32. AlphaTest Greater .01
  33. ColorMask RGB
  34. Cull Off Lighting Off ZWrite Off Fog { Mode Off }
  35. // Tags { "RenderType"="Opaque" }
  36. SubShader {
  37. // Disable Dynamic Batching
  38. //Pass{}
  39. Pass {
  40. CGPROGRAM
  41. #pragma vertex vert
  42. #pragma fragment frag
  43. // #pragma only_renderers opengl d3d9 d3d11
  44. #pragma glsl
  45. #pragma target 3.0
  46. #pragma multi_compile_particles
  47. #include "UnityCG.cginc"
  48. float _DischargeTileX, _DischargeTileY, _DischargePanSpeed;
  49. float _CoronaFluidTile, _CoronaFluidInfluence;
  50. float _SolarStormFalloff, _SolarStormPower;
  51. float _CoronaTileX, _CoronaTileY, _CoronaSpeed, _CoronaAmp, _CoronaExp, _CoronaBoost, _CoronaFalloff;
  52. float4 _CoronaColor;
  53. float _EdgeMaskFalloff, _EdgeMaskPower;
  54. sampler2D _CoronaNoise;
  55. sampler2D _CameraDepthTexture;
  56. uniform float4 _CameraDepthTexture_ST;
  57. float _InvFade;
  58. struct VertexInput
  59. {
  60. float4 vertex : POSITION;
  61. float3 normal : NORMAL;
  62. float4 color : COLOR;
  63. float2 texcoord0 : TEXCOORD0;
  64. };
  65. struct VertexOutput
  66. {
  67. float4 color : COLOR;
  68. float4 pos : POSITION;
  69. float4 posWorld : TEXCOORD0;
  70. float3 normalDir : TEXCOORD1;
  71. float2 uv : TEXCOORD2;
  72. #ifdef SOFTPARTICLES_ON
  73. float4 projPos : TEXCOORD3;
  74. #endif
  75. };
  76. VertexOutput vert (VertexInput v)
  77. {
  78. VertexOutput o;
  79. o.normalDir = mul(float4(v.normal,0), unity_WorldToObject).xyz;
  80. o.posWorld = mul(unity_ObjectToWorld, v.vertex);
  81. o.pos = UnityObjectToClipPos(v.vertex);
  82. o.uv = v.texcoord0;
  83. #ifdef SOFTPARTICLES_ON
  84. o.projPos = ComputeScreenPos (o.pos);
  85. // o.projPos = clamp(o.projPos, 0, 1);
  86. // o.projPos.y = 1 - o.projPos.y;
  87. COMPUTE_EYEDEPTH(o.projPos.z);
  88. #endif
  89. o.color = v.color;
  90. return o;
  91. }
  92. float2 PolarCood(float2 uv, float xTile, float yTile)
  93. {
  94. float2 uvScaled = uv - 1;
  95. float2 uvMult = pow(uvScaled, 2);
  96. float a1 = sqrt(uvMult.x + uvMult.y);
  97. float a2 = -atan2(uvScaled.y, uvScaled.x);
  98. float twoPI = 3.142 * 2;
  99. float2 uvOut;
  100. if(a2 >= 0.0) uvOut.x = a2 / twoPI;
  101. else uvOut.x = (a2 + twoPI) / twoPI;
  102. uvOut.x *= xTile;
  103. uvOut.y = a1 * yTile;
  104. return uvOut;
  105. }
  106. float2 Panner(float2 uv, float speedX, float speedY, float t)
  107. {
  108. return float2(uv.x + speedX * t , uv.y + speedY * t);
  109. }
  110. float2 Rotator(float2 uv, float speed)
  111. {
  112. uv -=0.5;
  113. float s = sin ( speed );
  114. float c = cos ( speed );
  115. float2x2 rotationMatrix = float2x2( c, -s, s, c);
  116. rotationMatrix *=0.5;
  117. rotationMatrix +=0.5;
  118. rotationMatrix = rotationMatrix * 2-1;
  119. uv = mul ( uv, rotationMatrix );
  120. uv += 0.5;
  121. return uv;
  122. }
  123. float CoronaMask(float2 _uv, float sizeX, float sizeY, float power, float falloff)
  124. {
  125. float2 uv= pow(_uv - 0.5, 2);
  126. uv.x *= sizeX;
  127. uv.y *= sizeY;
  128. float mask = 1 - pow(sqrt(uv.x + uv.y) * power, falloff);
  129. return clamp(mask, 0 ,1);
  130. }
  131. float4 frag(VertexOutput i) : COLOR
  132. {
  133. float2 uvA = PolarCood(i.uv * 2, _DischargeTileX * 0.5, _DischargeTileY * 0.5);
  134. float2 uvB = PolarCood(i.uv * 2, _DischargeTileX, _DischargeTileY);
  135. float2 uvC = PolarCood(i.uv * 2, _CoronaTileX, _CoronaTileY);
  136. uvA = Panner(uvA, 0, -1, _DischargePanSpeed * _Time.x);
  137. uvB = Panner(uvB, 0, -1, _DischargePanSpeed * _Time.x);
  138. uvC = Panner(uvC, 0, -1, _CoronaSpeed * _Time.x);
  139. float cNoiseA = tex2D(_CoronaNoise, uvA).g;
  140. float cNoiseC = tex2D(_CoronaNoise, Rotator(i.uv * _CoronaFluidTile * 4, 0.2 * _Time.x)).r * _CoronaFluidInfluence;
  141. float cNoiseB = tex2D(_CoronaNoise, uvB + cNoiseC).r;
  142. float cNoiseD = tex2D(_CoronaNoise, uvC).g;
  143. float sStorm = pow(cNoiseA * cNoiseB, _SolarStormFalloff) * _SolarStormPower;
  144. float corona = pow(cNoiseD * _CoronaAmp, _CoronaExp);
  145. float cMaskA = (1 - CoronaMask(i.uv, 1, 1, 4, 3)) * 3.5;
  146. float cMaskB = CoronaMask(i.uv, 1, 1, 2.25, 0.01) * cMaskA * _CoronaBoost;
  147. cMaskB = clamp(pow(cMaskB, _CoronaFalloff), 0, 1);
  148. corona += cMaskB;
  149. float cMaskC = clamp(CoronaMask(i.uv, 0.65, 0.65, 3.75, 3), 0, 1);
  150. float cNoiseE = pow(tex2D(_CoronaNoise, Rotator(i.uv * 2, 4)).r, 1.25) * 2 * cMaskC;
  151. float3 finalColor = float3(corona.xxx + sStorm * cNoiseE) * _CoronaColor * 5;
  152. float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
  153. // float viewDot = dot(viewDirection, mul(_Object2World, float4(1, 0, 0, 0)).rgb);
  154. float viewDot = abs(dot(viewDirection, normalize(i.normalDir)));
  155. viewDot = pow(viewDot, _EdgeMaskFalloff) * _EdgeMaskPower;
  156. viewDot = saturate(viewDot);
  157. #ifdef SOFTPARTICLES_ON
  158. float sceneZ = LinearEyeDepth (UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos))));
  159. float partZ = i.projPos.z;
  160. float fade = clamp((_InvFade * (sceneZ-partZ)), 0, 1);
  161. i.color.a = fade;
  162. #endif
  163. return float4(finalColor * viewDot * cMaskB * i.color.a, 1);
  164. // return float4(viewDot.xxx, 1);
  165. }
  166. ENDCG
  167. }
  168. }
  169. }
  170. }