Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.67 KB | None | 0 0
  1. Shader "MADFINGER/MechWarz/Universal" {
  2.  
  3. Properties {
  4.  
  5. _MainTex1("Diffuse 1", 2D) = "white" {}
  6. _MainTex2("Diffuse 2", 2D) = "white" {}
  7. _NormalsTex1("Normals 1",2D) = "bump" {}
  8. _NormalsTex2("Normals 2",2D) = "bump" {}
  9. _DiffBlendTex("Diff blend mask",2D) = "white" {}
  10. _EnvMapTex("Env map",Cube) = "black" {}
  11. _ColorTint1("Color tint 1",Color) = (1,1,1,1)
  12. _ColorTint2("Color tint 2",Color) = (1,1,1,1)
  13. _SpecularStrength("Specular strength weights", Vector) = (0,0,0,2)
  14. _ScrollingSpeed("Scrolling speed", Vector) = (0,0,0,0)
  15. _Params("x - c-scaler,y - smooth, z - offset, w - diff UV channel",Vector) = (2,0,0,0)
  16. _Params2("x - mask UV channel, y - shadow specular scale",Vector) = (0,0.25,0,0)
  17. _FresnelParams("x - frensel pow, y - fresnel bias, z - reflection scale",Vector) = (5,0.2,1,0)
  18. _FogParams("x - fog start override, y - fog end override, z - enable color pal",Vector) = (-1,-1,0,0)
  19. _OverlayTexParams("x - up-facing overlay, y - up facing threshold, z - sharpness",Vector) = (0,0.3,8,0)
  20. _SuppressShadowsMask("Shadows suppression: x - level0, y - level1, z - level2",Vector) = (0,0,0,0)
  21.  
  22. [Toggle(USE_LIGHTMAP)] _UseLightmap("Use lightmaps", Float) = 1
  23. [Toggle(USE_OVERLAY_TEX)] _UseOverlayTex("Use overlay tex", Float) = 0
  24. [Toggle(USE_CUSTOM_SHADOWS)] _UseCustomShadows("Use custom shadows", Float) = 1
  25. [Toggle(USE_NATIVE_SHADOWS)] _UseNativeShadows("Use native shadows", Float) = 0
  26. [Toggle(USE_SPECULAR_SHADOW_MASKING_FIXUP)] _UseSpecularShadowMaskingFixup("Use specular shadow masking fixup", Float) = 0
  27. }
  28.  
  29.  
  30. SubShader {
  31.  
  32. Tags
  33. {
  34. "RenderType"="Opaque"
  35. }
  36.  
  37.  
  38. CGINCLUDE
  39.  
  40. #include "UnityCG.cginc"
  41. #include "AutoLight.cginc"
  42. #include "UnityGlobalIllumination.cginc"
  43. #include "../config.cginc"
  44. #include "../globals.cginc"
  45.  
  46. #pragma multi_compile UNITY_SHADER_DETAIL_LOW UNITY_SHADER_DETAIL_MEDIUM UNITY_SHADER_DETAIL_HIGH
  47. #pragma multi_compile DIRLIGHTMAP_OFF DIRLIGHTMAP_COMBINED
  48. #pragma multi_compile_fog
  49. #pragma multi_compile SHADOWS_OFF SHADOWS_SCREEN
  50.  
  51. #pragma shader_feature USE_LIGHTMAP
  52. #pragma shader_feature USE_OVERLAY_TEX
  53. #pragma shader_feature USE_CUSTOM_SHADOWS
  54. #pragma shader_feature USE_NATIVE_SHADOWS
  55. #pragma shader_feature USE_SPECULAR_SHADOW_MASKING_FIXUP
  56.  
  57. sampler2D _MainTex1;
  58. sampler2D _MainTex2;
  59. sampler2D _NormalsTex1;
  60. sampler2D _NormalsTex2;
  61. sampler2D _DiffBlendTex;
  62. samplerCUBE _EnvMapTex;
  63. float4 _EnvMapTex_TexelSize;
  64.  
  65. float4 _ScrollingSpeed;
  66. float4 _SpecularStrength;
  67. float4 _MainTex1_ST;
  68. float4 _MainTex2_ST;
  69. float4 _DiffBlendTex_ST;
  70. float4 _NormalsTex1_ST;
  71. float4 _NormalsTex2_ST;
  72. float4 _Params;
  73. float4 _Params2;
  74. float4 _ColorTint1;
  75. float4 _ColorTint2;
  76. float4 _FogParams;
  77. float4 _OverlayTexParams;
  78. float4 _FresnelParams;
  79. float4 _SuppressShadowsMask;
  80. float _UseLightmap;
  81. float _UseOverlayTex;
  82. float _UseCustomShadows;
  83. float _UseNativeShadows;
  84.  
  85. struct v2f
  86. {
  87. float4 pos : SV_POSITION;
  88. float4 uv : TEXCOORD0;
  89. float4 uv2 : TEXCOORD1;
  90. float4 TNB0 : TEXCOORD2;
  91. float4 TNB1 : TEXCOORD3;
  92. float4 TNB2 : TEXCOORD4;
  93. float4 params : TEXCOORD5;
  94. float4 eyeDir : TEXCOORD6;
  95.  
  96. #if defined(USE_CUSTOM_SHADOWS)
  97. float4 shadowUV : TEXCOORD7;
  98. #elif defined(USE_NATIVE_SHADOWS)
  99. SHADOW_COORDS(7)
  100. #endif
  101.  
  102. float4 col : COLOR;
  103. };
  104.  
  105. v2f vert (appdata_full v)
  106. {
  107. v2f o = (v2f)0;
  108.  
  109. float2 secondUV = _Params.w > 0.0001f ? v.texcoord2.xy : v.texcoord.xy;
  110. float2 maskUV = _Params2.x > 0.0001f ? v.texcoord3.xy : v.texcoord.xy;
  111.  
  112. o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  113. o.uv.xy = MF_TRANSFORM_TEX(v.texcoord.xy,_MainTex1) + frac(_ScrollingSpeed * _Time.y);
  114. o.uv.zw = MF_TRANSFORM_TEX(secondUV,_MainTex2) + frac(_ScrollingSpeed * _Time.y);
  115. o.uv2.zw = MF_TRANSFORM_TEX(maskUV,_DiffBlendTex);
  116. o.uv2.xy = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
  117.  
  118. const float3 T = normalize(mul((float3x3)unity_ObjectToWorld, v.tangent.xyz));
  119. const float3 N = normalize(mul((float3x3)unity_ObjectToWorld, v.normal.xyz));
  120. const float3 B = normalize(cross(N,T) * v.tangent.w);
  121.  
  122. const float3 worldNormal = normalize(mul((float3x3)unity_ObjectToWorld, v.normal));
  123. const float3 wrldPos = mul(unity_ObjectToWorld,v.vertex);
  124. const float3 eyeDir = -WorldSpaceViewDir(v.vertex);
  125.  
  126.  
  127. o.TNB0 = float4(T.x,B.x,N.x,1.0f / (_Params.y + 0.001f));
  128. o.TNB1 = float4(T.y,B.y,N.y,MFCalcFogFactor(o.pos,_FogParams));
  129. o.TNB2 = float4(T.z,B.z,N.z,log2(_EnvMapTex_TexelSize.z));
  130.  
  131. o.eyeDir.xyz = eyeDir;
  132. o.eyeDir.w = 0;
  133.  
  134. o.col.xyz = v.color.xyz;
  135. o.col.w = v.color.a;
  136.  
  137. o.params.xy = MF_TRANSFORM_TEX(v.texcoord.xy,_NormalsTex1);
  138. o.params.zw = MF_TRANSFORM_TEX(secondUV,_NormalsTex2);
  139.  
  140. #if defined(USE_CUSTOM_SHADOWS)
  141. o.shadowUV = MFOutputSMapCoords(wrldPos);
  142. #elif defined(USE_NATIVE_SHADOWS)
  143. TRANSFER_SHADOW(o);
  144. #endif
  145.  
  146. #if !defined(USE_LIGHTMAP)
  147. const float3 ldir = normalize(WorldSpaceLightDir(v.vertex));
  148.  
  149. o.uv2.xy = ldir.xy;
  150. o.eyeDir.w = ldir.z;
  151. #endif
  152.  
  153.  
  154. float normalUpFactor = worldNormal.y > _OverlayTexParams.y;
  155. float useUserDefinedOverlayMask = _OverlayTexParams.x;
  156.  
  157. o.col.a = useUserDefinedOverlayMask > 0 ? 1 - normalUpFactor : o.col.a;
  158.  
  159.  
  160. return o;
  161. }
  162.  
  163. ENDCG
  164.  
  165. Pass
  166. {
  167.  
  168. Tags { "RenderType"="Opaque" "LightMode"="ForwardBase"}
  169.  
  170. LOD 100
  171. ColorMask RGB
  172.  
  173. CGPROGRAM
  174.  
  175. #pragma vertex vert
  176. #pragma fragment frag
  177. #pragma fragmentoption ARB_precision_hint_fastest
  178.  
  179. half3 SampleEnvMap(float3 refl,float envMapCMMip)
  180. {
  181. #if 0
  182. fixed3 env = texCUBElod(_EnvMapTex,float4(refl,envMapCMMip));
  183. #else
  184. // I don't know how to tell Unity to use tri-linear MIP-mapping for cubemap texture, so we must emulate it
  185. half envMapCMMipLo = floor(envMapCMMip);
  186. fixed3 env0 = texCUBElod(_EnvMapTex,float4(refl,envMapCMMipLo));
  187. fixed3 env1 = texCUBElod(_EnvMapTex,float4(refl,envMapCMMipLo + 1));
  188. fixed3 env = lerp(env0,env1,frac(envMapCMMip));
  189. #endif
  190. return env;
  191. }
  192.  
  193. fixed4 frag (v2f i) : COLOR
  194. {
  195. half4 c1 = tex2D (_MainTex1, i.uv.xy) * _ColorTint1;
  196.  
  197. #if defined(USE_OVERLAY_TEX)
  198. half3 nrm1 = tex2D(_NormalsTex1,i.params.xy);
  199. half wnrm1Y = dot(nrm1 * 2 - 1,i.TNB1.xyz);
  200. half wnrmUp = saturate((wnrm1Y - _OverlayTexParams.y) * _OverlayTexParams.z);
  201. half blendCtrl = _OverlayTexParams.x > 0 ? 1 - wnrmUp : i.col.a;
  202.  
  203. half4 c2 = tex2D (_MainTex2, i.uv.zw) * _ColorTint2;
  204. half bt = tex2D (_DiffBlendTex, i.uv2.zw).x;
  205. half bm = saturate(saturate(saturate(bt + _Params.z) - blendCtrl) * i.TNB0.w);
  206. half4 c = lerp(c1,c2,bm);
  207.  
  208. half3 nrm2 = tex2D(_NormalsTex2,i.params.zw);
  209. half3 nrm = normalize(lerp(nrm1,nrm2,bm) * 2 - 1);
  210. #else
  211. half4 c = c1;
  212. half3 nrm = tex2D(_NormalsTex1,i.params.xy) * 2 - 1;
  213. #endif
  214.  
  215. half3 wnrm = normalize(float3(dot(nrm,i.TNB0.xyz),dot(nrm,i.TNB1.xyz),dot(nrm,i.TNB2.xyz)));
  216. half3 refl = normalize(i.eyeDir - 2 * wnrm * dot(wnrm,i.eyeDir));
  217.  
  218. half gloss = saturate(dot(c,_SpecularStrength));
  219. half specStrength = saturate(2 * gloss);
  220. half specPower = MFSpecPowForGlossiness(gloss);
  221.  
  222. half envMapCMMip = i.TNB2.w * saturate(1 - gloss);
  223. half fresnelFacing = saturate(1 + dot(normalize(i.eyeDir.xyz),wnrm));
  224. half fresnel = lerp(1,MFFresnel(fresnelFacing,_FresnelParams.y,_FresnelParams.x),gloss) * _FresnelParams.z;
  225.  
  226. #if defined(USE_CUSTOM_SHADOWS)
  227. fixed shadowMask = MFSampleCustomShadow(i.shadowUV,_SuppressShadowsMask);
  228. #elif defined(USE_NATIVE_SHADOWS)
  229. fixed shadowMask = SHADOW_ATTENUATION(i);
  230. #else
  231. fixed shadowMask = 1;
  232. #endif
  233.  
  234.  
  235. c.rgb *= (i.col.rgb * _Params.x);
  236.  
  237. #if defined(USE_LIGHTMAP)
  238.  
  239. #if defined(DIRLIGHTMAP_COMBINED)
  240. half3 lmCol = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap,i.uv2.xy));
  241. half4 lmDir = UNITY_SAMPLE_TEX2D_SAMPLER (unity_LightmapInd, unity_Lightmap, i.uv2.xy);
  242. half3 dir = normalize(lmDir.xyz * 2 - 1);
  243. half3 lm = MFDecodeDirectionalLightmap(lmCol,lmDir,wnrm);
  244. half RDotL = saturate(dot(dir,refl));
  245. half specIntensity = pow(RDotL,specPower) * (specPower + 2.0) / (2 * PI) * specStrength;
  246.  
  247. c.rgb = c.rgb + specIntensity * lm;
  248. c.rgb *= lm;
  249. #else
  250. c.rgb *= DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap,i.uv2.xy));
  251. #endif
  252. c.rgb *= shadowMask;
  253.  
  254. c.rgb += SampleEnvMap(refl,envMapCMMip) * fresnel;
  255.  
  256. #else // USE_LIGHTMAP
  257.  
  258. const float3 ldir = float3(i.uv2.xy,i.eyeDir.w);
  259. const fixed NDotL = saturate(dot(wnrm,ldir));
  260. const fixed RDotL = saturate(dot(ldir,refl));
  261. const fixed3 lighting = UNITY_LIGHTMODEL_AMBIENT.xyz + NDotL * _LightColor0.xyz * shadowMask;
  262. const half specIntensity = pow(RDotL,specPower) * (specPower + 2.0) / (2 * PI) * specStrength;
  263.  
  264. #if defined(USE_SPECULAR_SHADOW_MASKING_FIXUP)
  265. fixed shadowMaskSoftened = min(shadowMask,NDotL);
  266. #else
  267. fixed shadowMaskSoftened = shadowMask;
  268. #endif
  269. fixed shadowsSpecScaler = lerp(_Params2.y,1,shadowMaskSoftened);
  270.  
  271. c.rgb *= lighting;
  272.  
  273. half3 reflL = specIntensity * _LightColor0.xyz + SampleEnvMap(refl,envMapCMMip) * fresnel;
  274.  
  275. c.rgb += reflL * shadowsSpecScaler;
  276. #endif
  277.  
  278. c.rgb = MFApplyFog(c.rgb,i.TNB1.w);
  279.  
  280. return fixed4(c.xyz,0);
  281. }
  282.  
  283. ENDCG
  284.  
  285. }
  286.  
  287. Pass
  288. {
  289. Name "META"
  290. Tags { "LightMode"="Meta" }
  291. Cull Off
  292.  
  293. CGPROGRAM
  294. #pragma vertex vert_meta
  295. #pragma fragment frag_meta
  296.  
  297. #define UNITY_PASS_META 1
  298.  
  299. #pragma shader_feature _EMISSION
  300. #pragma shader_feature _METALLICGLOSSMAP
  301. #pragma shader_feature ___ _DETAIL_MULX2
  302.  
  303. #include "UnityStandardMeta.cginc"
  304.  
  305. v2f vert_meta(appdata_full v)
  306. {
  307. v2f o = (v2f)0;
  308.  
  309. float2 lmUV = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
  310.  
  311. o.pos = UnityMetaVertexPosition(v.vertex,v.texcoord1.xy,v.texcoord2.xy,unity_LightmapST,unity_DynamicLightmapST);
  312. o.uv.xy = MF_TRANSFORM_TEX(v.texcoord,_MainTex1) + frac(_ScrollingSpeed * _Time.y);
  313. o.uv.zw = MF_TRANSFORM_TEX(v.texcoord,_MainTex2) + frac(_ScrollingSpeed * _Time.y);
  314. o.uv2.zw = MF_TRANSFORM_TEX(v.texcoord,_DiffBlendTex);
  315. o.uv2.xy = lmUV;
  316. o.col.xyz = v.color.xyz * _Params.x;
  317. o.col.w = v.color.a;
  318. o.params = float4(1.0f / _Params.y + 0.001f,0,0,0);
  319.  
  320. return o;
  321. }
  322.  
  323. float4 frag_meta(v2f i) : SV_Target
  324. {
  325. UnityMetaInput o;
  326.  
  327. UNITY_INITIALIZE_OUTPUT(UnityMetaInput, o);
  328.  
  329. float4 c1 = tex2D (_MainTex1, i.uv.xy) * _ColorTint1;
  330. float4 c2 = tex2D (_MainTex2, i.uv.zw) * _ColorTint2;
  331. float bt = tex2D (_DiffBlendTex, i.uv2.zw).x;
  332. float bm = saturate(saturate(saturate(bt + _Params.z) - i.col.a) * i.params.x);
  333. float4 c = lerp(c1,c2,bm);
  334.  
  335. o.Albedo = c;
  336. o.Emission = 0;
  337.  
  338. return UnityMetaFragment(o);
  339. }
  340.  
  341. ENDCG
  342. }
  343.  
  344. }
  345.  
  346. FallBack "VertexLit"
  347. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement