Sun.shader 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. Shader "Unlit/Sun"
  2. {
  3. Properties
  4. {
  5. _MainTex("Texture", 2D) = "white" {}
  6. _SecondaryTex("Secondary texture", 2D) = "white" {}
  7. _NormalTex("Normals", 2D) = "white" {}
  8. _ColorEmission("Colors emission", Color) = (1,1,1,1)
  9. _FactorEmission("Factor emission", Range(0.1,10)) = 1
  10. }
  11. SubShader
  12. {
  13. Tags{ "RenderType" = "Opaque""LightMode" = "Deferred" }
  14. Pass
  15. {
  16. Stencil{
  17. Ref 148
  18. Comp Always
  19. Pass replace
  20. }
  21. CGPROGRAM
  22. #pragma vertex vert
  23. #pragma fragment frag
  24. #pragma target 4.0
  25. #pragma multi_compile ___ UNITY_HDR_ON
  26. #include "UnityCG.cginc"
  27. struct appdata
  28. {
  29. float4 vertex : POSITION;
  30. float2 uv : TEXCOORD0;
  31. float2 uv2 : TEXCOORD1;
  32. };
  33. struct v2f
  34. {
  35. float2 uv : TEXCOORD0;
  36. float2 uv2 : TEXCOORD1;
  37. float4 vertex : SV_POSITION;
  38. };
  39. // 2D Random
  40. float random(in float2 st) {
  41. return frac(sin(dot(st.xy,
  42. float2(12.9898, 78.233)))
  43. * 43758.5453123);
  44. }
  45. // 2D Noise based on Morgan McGuire @morgan3d
  46. // https://www.shadertoy.com/view/4dS3Wd
  47. float noise(in float2 st) {
  48. float2 i = floor(st);
  49. float2 f = frac(st);
  50. // Four corners in 2D of a tile
  51. float a = random(i);
  52. float b = random(i + float2(1.0, 0.0));
  53. float c = random(i + float2(0.0, 1.0));
  54. float d = random(i + float2(1.0, 1.0));
  55. // Smooth Interpolation
  56. // Cubic Hermine Curve. Same as SmoothStep()
  57. float2 u = f*f*(3.0 - 2.0*f);
  58. // u = smoothstep(0.,1.,f);
  59. // Mix 4 coorners porcentages
  60. return lerp(a, b, u.x) +
  61. (c - a)* u.y * (1.0 - u.x) +
  62. (d - b) * u.x * u.y;
  63. }
  64. sampler2D _MainTex;
  65. sampler2D _NormalTex;
  66. sampler2D _SecondaryTex;
  67. float4 _MainTex_ST;
  68. float4 _SecondaryTex_ST;
  69. float4 _ColorEmission;
  70. float _FactorEmission;
  71. v2f vert(appdata v)
  72. {
  73. v2f o;
  74. float4 ve = v.vertex;
  75. ve.xyz += noise(v.vertex.xy*float2(_Time.y * 5, _Time.y * 5)) / 100.0f;
  76. o.vertex = UnityObjectToClipPos(ve);
  77. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  78. o.uv2 = TRANSFORM_TEX(v.uv2, _SecondaryTex);
  79. return o;
  80. }
  81. void frag(v2f i,
  82. out half4 outColor : SV_Target0,
  83. out half4 outNormal : SV_Target2,
  84. out half4 outEmission : SV_Target3
  85. )
  86. {
  87. UNITY_INITIALIZE_OUTPUT(half4,outColor);
  88. UNITY_INITIALIZE_OUTPUT(half4,outNormal);
  89. UNITY_INITIALIZE_OUTPUT(half4,outEmission);
  90. float2 uv = i.uv;
  91. // sample the texture
  92. float4 col = tex2D(_MainTex, uv + noise(uv*float2(_Time.y, _Time.y)) / 30.0f);
  93. float4 col2 = tex2D(_SecondaryTex, i.uv - noise(uv*float2(_Time.y, _Time.y)) / 30.0f);
  94. outColor = normalize(col2*col2*0.6);
  95. #if UNITY_HDR_ON
  96. outEmission = _ColorEmission*outColor*_FactorEmission;
  97. #else
  98. outEmission = float4(1, 1, 1, 0);
  99. #endif
  100. outNormal.a = 0.33;
  101. }
  102. ENDCG
  103. }
  104. }
  105. SubShader
  106. {
  107. Tags{ "RenderType" = "Opaque""LightMode" = "ForwardBase" }
  108. Pass
  109. {
  110. Stencil{
  111. Ref 148
  112. Comp Always
  113. Pass replace
  114. }
  115. CGPROGRAM
  116. #pragma vertex vert
  117. #pragma fragment frag
  118. #pragma multi_compile ___ UNITY_HDR_ON
  119. #pragma target 4.0
  120. #include "UnityCG.cginc"
  121. struct appdata
  122. {
  123. float4 vertex : POSITION;
  124. float2 uv : TEXCOORD0;
  125. float2 uv2 : TEXCOORD1;
  126. };
  127. struct v2f
  128. {
  129. float2 uv : TEXCOORD0;
  130. float2 uv2 : TEXCOORD1;
  131. float4 vertex : SV_POSITION;
  132. };
  133. // 2D Random
  134. float random(in float2 st) {
  135. return frac(sin(dot(st.xy,
  136. float2(12.9898, 78.233)))
  137. * 43758.5453123);
  138. }
  139. // 2D Noise based on Morgan McGuire @morgan3d
  140. // https://www.shadertoy.com/view/4dS3Wd
  141. float noise(in float2 st) {
  142. float2 i = floor(st);
  143. float2 f = frac(st);
  144. // Four corners in 2D of a tile
  145. float a = random(i);
  146. float b = random(i + float2(1.0, 0.0));
  147. float c = random(i + float2(0.0, 1.0));
  148. float d = random(i + float2(1.0, 1.0));
  149. // Smooth Interpolation
  150. // Cubic Hermine Curve. Same as SmoothStep()
  151. float2 u = f*f*(3.0 - 2.0*f);
  152. // u = smoothstep(0.,1.,f);
  153. // Mix 4 coorners porcentages
  154. return lerp(a, b, u.x) +
  155. (c - a)* u.y * (1.0 - u.x) +
  156. (d - b) * u.x * u.y;
  157. }
  158. sampler2D _MainTex;
  159. sampler2D _NormalTex;
  160. sampler2D _SecondaryTex;
  161. float4 _MainTex_ST;
  162. float4 _SecondaryTex_ST;
  163. float4 _ColorEmission;
  164. float _FactorEmission;
  165. v2f vert(appdata v)
  166. {
  167. v2f o;
  168. float4 ve = v.vertex;
  169. //ve.xyz += noise(v.vertex.xy*float2(_Time.y * 5, _Time.y * 5)) / 200.0f;
  170. o.vertex = UnityObjectToClipPos(ve);
  171. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  172. o.uv2 = TRANSFORM_TEX(v.uv2, _SecondaryTex);
  173. return o;
  174. }
  175. void frag(v2f i,
  176. out half4 outColor : SV_Target0
  177. )
  178. {
  179. float2 uv = i.uv;
  180. // sample the texture
  181. float4 col = tex2D(_MainTex, uv + noise(uv*float2(_Time.y, _Time.y)) / 30.0f);
  182. float4 col2 = tex2D(_SecondaryTex, i.uv - noise(uv*float2(_Time.y, _Time.y)) / 30.0f);
  183. outColor = normalize(col*col2);
  184. outColor = _ColorEmission*outColor*_FactorEmission;
  185. }
  186. ENDCG
  187. }
  188. }
  189. }