Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Shader "MADFINGER/MechWarz/Universal" {
- Properties {
- _MainTex1("Diffuse 1", 2D) = "white" {}
- _MainTex2("Diffuse 2", 2D) = "white" {}
- _NormalsTex1("Normals 1",2D) = "bump" {}
- _NormalsTex2("Normals 2",2D) = "bump" {}
- _DiffBlendTex("Diff blend mask",2D) = "white" {}
- _EnvMapTex("Env map",Cube) = "black" {}
- _ColorTint1("Color tint 1",Color) = (1,1,1,1)
- _ColorTint2("Color tint 2",Color) = (1,1,1,1)
- _SpecularStrength("Specular strength weights", Vector) = (0,0,0,2)
- _ScrollingSpeed("Scrolling speed", Vector) = (0,0,0,0)
- _Params("x - c-scaler,y - smooth, z - offset, w - diff UV channel",Vector) = (2,0,0,0)
- _Params2("x - mask UV channel, y - shadow specular scale",Vector) = (0,0.25,0,0)
- _FresnelParams("x - frensel pow, y - fresnel bias, z - reflection scale",Vector) = (5,0.2,1,0)
- _FogParams("x - fog start override, y - fog end override, z - enable color pal",Vector) = (-1,-1,0,0)
- _OverlayTexParams("x - up-facing overlay, y - up facing threshold, z - sharpness",Vector) = (0,0.3,8,0)
- _SuppressShadowsMask("Shadows suppression: x - level0, y - level1, z - level2",Vector) = (0,0,0,0)
- [Toggle(USE_LIGHTMAP)] _UseLightmap("Use lightmaps", Float) = 1
- [Toggle(USE_OVERLAY_TEX)] _UseOverlayTex("Use overlay tex", Float) = 0
- [Toggle(USE_CUSTOM_SHADOWS)] _UseCustomShadows("Use custom shadows", Float) = 1
- [Toggle(USE_NATIVE_SHADOWS)] _UseNativeShadows("Use native shadows", Float) = 0
- [Toggle(USE_SPECULAR_SHADOW_MASKING_FIXUP)] _UseSpecularShadowMaskingFixup("Use specular shadow masking fixup", Float) = 0
- }
- SubShader {
- Tags
- {
- "RenderType"="Opaque"
- }
- CGINCLUDE
- #include "UnityCG.cginc"
- #include "AutoLight.cginc"
- #include "UnityGlobalIllumination.cginc"
- #include "../config.cginc"
- #include "../globals.cginc"
- #pragma multi_compile UNITY_SHADER_DETAIL_LOW UNITY_SHADER_DETAIL_MEDIUM UNITY_SHADER_DETAIL_HIGH
- #pragma multi_compile DIRLIGHTMAP_OFF DIRLIGHTMAP_COMBINED
- #pragma multi_compile_fog
- #pragma multi_compile SHADOWS_OFF SHADOWS_SCREEN
- #pragma shader_feature USE_LIGHTMAP
- #pragma shader_feature USE_OVERLAY_TEX
- #pragma shader_feature USE_CUSTOM_SHADOWS
- #pragma shader_feature USE_NATIVE_SHADOWS
- #pragma shader_feature USE_SPECULAR_SHADOW_MASKING_FIXUP
- sampler2D _MainTex1;
- sampler2D _MainTex2;
- sampler2D _NormalsTex1;
- sampler2D _NormalsTex2;
- sampler2D _DiffBlendTex;
- samplerCUBE _EnvMapTex;
- float4 _EnvMapTex_TexelSize;
- float4 _ScrollingSpeed;
- float4 _SpecularStrength;
- float4 _MainTex1_ST;
- float4 _MainTex2_ST;
- float4 _DiffBlendTex_ST;
- float4 _NormalsTex1_ST;
- float4 _NormalsTex2_ST;
- float4 _Params;
- float4 _Params2;
- float4 _ColorTint1;
- float4 _ColorTint2;
- float4 _FogParams;
- float4 _OverlayTexParams;
- float4 _FresnelParams;
- float4 _SuppressShadowsMask;
- float _UseLightmap;
- float _UseOverlayTex;
- float _UseCustomShadows;
- float _UseNativeShadows;
- struct v2f
- {
- float4 pos : SV_POSITION;
- float4 uv : TEXCOORD0;
- float4 uv2 : TEXCOORD1;
- float4 TNB0 : TEXCOORD2;
- float4 TNB1 : TEXCOORD3;
- float4 TNB2 : TEXCOORD4;
- float4 params : TEXCOORD5;
- float4 eyeDir : TEXCOORD6;
- #if defined(USE_CUSTOM_SHADOWS)
- float4 shadowUV : TEXCOORD7;
- #elif defined(USE_NATIVE_SHADOWS)
- SHADOW_COORDS(7)
- #endif
- float4 col : COLOR;
- };
- v2f vert (appdata_full v)
- {
- v2f o = (v2f)0;
- float2 secondUV = _Params.w > 0.0001f ? v.texcoord2.xy : v.texcoord.xy;
- float2 maskUV = _Params2.x > 0.0001f ? v.texcoord3.xy : v.texcoord.xy;
- o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
- o.uv.xy = MF_TRANSFORM_TEX(v.texcoord.xy,_MainTex1) + frac(_ScrollingSpeed * _Time.y);
- o.uv.zw = MF_TRANSFORM_TEX(secondUV,_MainTex2) + frac(_ScrollingSpeed * _Time.y);
- o.uv2.zw = MF_TRANSFORM_TEX(maskUV,_DiffBlendTex);
- o.uv2.xy = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
- const float3 T = normalize(mul((float3x3)unity_ObjectToWorld, v.tangent.xyz));
- const float3 N = normalize(mul((float3x3)unity_ObjectToWorld, v.normal.xyz));
- const float3 B = normalize(cross(N,T) * v.tangent.w);
- const float3 worldNormal = normalize(mul((float3x3)unity_ObjectToWorld, v.normal));
- const float3 wrldPos = mul(unity_ObjectToWorld,v.vertex);
- const float3 eyeDir = -WorldSpaceViewDir(v.vertex);
- o.TNB0 = float4(T.x,B.x,N.x,1.0f / (_Params.y + 0.001f));
- o.TNB1 = float4(T.y,B.y,N.y,MFCalcFogFactor(o.pos,_FogParams));
- o.TNB2 = float4(T.z,B.z,N.z,log2(_EnvMapTex_TexelSize.z));
- o.eyeDir.xyz = eyeDir;
- o.eyeDir.w = 0;
- o.col.xyz = v.color.xyz;
- o.col.w = v.color.a;
- o.params.xy = MF_TRANSFORM_TEX(v.texcoord.xy,_NormalsTex1);
- o.params.zw = MF_TRANSFORM_TEX(secondUV,_NormalsTex2);
- #if defined(USE_CUSTOM_SHADOWS)
- o.shadowUV = MFOutputSMapCoords(wrldPos);
- #elif defined(USE_NATIVE_SHADOWS)
- TRANSFER_SHADOW(o);
- #endif
- #if !defined(USE_LIGHTMAP)
- const float3 ldir = normalize(WorldSpaceLightDir(v.vertex));
- o.uv2.xy = ldir.xy;
- o.eyeDir.w = ldir.z;
- #endif
- float normalUpFactor = worldNormal.y > _OverlayTexParams.y;
- float useUserDefinedOverlayMask = _OverlayTexParams.x;
- o.col.a = useUserDefinedOverlayMask > 0 ? 1 - normalUpFactor : o.col.a;
- return o;
- }
- ENDCG
- Pass
- {
- Tags { "RenderType"="Opaque" "LightMode"="ForwardBase"}
- LOD 100
- ColorMask RGB
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- #pragma fragmentoption ARB_precision_hint_fastest
- half3 SampleEnvMap(float3 refl,float envMapCMMip)
- {
- #if 0
- fixed3 env = texCUBElod(_EnvMapTex,float4(refl,envMapCMMip));
- #else
- // I don't know how to tell Unity to use tri-linear MIP-mapping for cubemap texture, so we must emulate it
- half envMapCMMipLo = floor(envMapCMMip);
- fixed3 env0 = texCUBElod(_EnvMapTex,float4(refl,envMapCMMipLo));
- fixed3 env1 = texCUBElod(_EnvMapTex,float4(refl,envMapCMMipLo + 1));
- fixed3 env = lerp(env0,env1,frac(envMapCMMip));
- #endif
- return env;
- }
- fixed4 frag (v2f i) : COLOR
- {
- half4 c1 = tex2D (_MainTex1, i.uv.xy) * _ColorTint1;
- #if defined(USE_OVERLAY_TEX)
- half3 nrm1 = tex2D(_NormalsTex1,i.params.xy);
- half wnrm1Y = dot(nrm1 * 2 - 1,i.TNB1.xyz);
- half wnrmUp = saturate((wnrm1Y - _OverlayTexParams.y) * _OverlayTexParams.z);
- half blendCtrl = _OverlayTexParams.x > 0 ? 1 - wnrmUp : i.col.a;
- half4 c2 = tex2D (_MainTex2, i.uv.zw) * _ColorTint2;
- half bt = tex2D (_DiffBlendTex, i.uv2.zw).x;
- half bm = saturate(saturate(saturate(bt + _Params.z) - blendCtrl) * i.TNB0.w);
- half4 c = lerp(c1,c2,bm);
- half3 nrm2 = tex2D(_NormalsTex2,i.params.zw);
- half3 nrm = normalize(lerp(nrm1,nrm2,bm) * 2 - 1);
- #else
- half4 c = c1;
- half3 nrm = tex2D(_NormalsTex1,i.params.xy) * 2 - 1;
- #endif
- half3 wnrm = normalize(float3(dot(nrm,i.TNB0.xyz),dot(nrm,i.TNB1.xyz),dot(nrm,i.TNB2.xyz)));
- half3 refl = normalize(i.eyeDir - 2 * wnrm * dot(wnrm,i.eyeDir));
- half gloss = saturate(dot(c,_SpecularStrength));
- half specStrength = saturate(2 * gloss);
- half specPower = MFSpecPowForGlossiness(gloss);
- half envMapCMMip = i.TNB2.w * saturate(1 - gloss);
- half fresnelFacing = saturate(1 + dot(normalize(i.eyeDir.xyz),wnrm));
- half fresnel = lerp(1,MFFresnel(fresnelFacing,_FresnelParams.y,_FresnelParams.x),gloss) * _FresnelParams.z;
- #if defined(USE_CUSTOM_SHADOWS)
- fixed shadowMask = MFSampleCustomShadow(i.shadowUV,_SuppressShadowsMask);
- #elif defined(USE_NATIVE_SHADOWS)
- fixed shadowMask = SHADOW_ATTENUATION(i);
- #else
- fixed shadowMask = 1;
- #endif
- c.rgb *= (i.col.rgb * _Params.x);
- #if defined(USE_LIGHTMAP)
- #if defined(DIRLIGHTMAP_COMBINED)
- half3 lmCol = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap,i.uv2.xy));
- half4 lmDir = UNITY_SAMPLE_TEX2D_SAMPLER (unity_LightmapInd, unity_Lightmap, i.uv2.xy);
- half3 dir = normalize(lmDir.xyz * 2 - 1);
- half3 lm = MFDecodeDirectionalLightmap(lmCol,lmDir,wnrm);
- half RDotL = saturate(dot(dir,refl));
- half specIntensity = pow(RDotL,specPower) * (specPower + 2.0) / (2 * PI) * specStrength;
- c.rgb = c.rgb + specIntensity * lm;
- c.rgb *= lm;
- #else
- c.rgb *= DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap,i.uv2.xy));
- #endif
- c.rgb *= shadowMask;
- c.rgb += SampleEnvMap(refl,envMapCMMip) * fresnel;
- #else // USE_LIGHTMAP
- const float3 ldir = float3(i.uv2.xy,i.eyeDir.w);
- const fixed NDotL = saturate(dot(wnrm,ldir));
- const fixed RDotL = saturate(dot(ldir,refl));
- const fixed3 lighting = UNITY_LIGHTMODEL_AMBIENT.xyz + NDotL * _LightColor0.xyz * shadowMask;
- const half specIntensity = pow(RDotL,specPower) * (specPower + 2.0) / (2 * PI) * specStrength;
- #if defined(USE_SPECULAR_SHADOW_MASKING_FIXUP)
- fixed shadowMaskSoftened = min(shadowMask,NDotL);
- #else
- fixed shadowMaskSoftened = shadowMask;
- #endif
- fixed shadowsSpecScaler = lerp(_Params2.y,1,shadowMaskSoftened);
- c.rgb *= lighting;
- half3 reflL = specIntensity * _LightColor0.xyz + SampleEnvMap(refl,envMapCMMip) * fresnel;
- c.rgb += reflL * shadowsSpecScaler;
- #endif
- c.rgb = MFApplyFog(c.rgb,i.TNB1.w);
- return fixed4(c.xyz,0);
- }
- ENDCG
- }
- Pass
- {
- Name "META"
- Tags { "LightMode"="Meta" }
- Cull Off
- CGPROGRAM
- #pragma vertex vert_meta
- #pragma fragment frag_meta
- #define UNITY_PASS_META 1
- #pragma shader_feature _EMISSION
- #pragma shader_feature _METALLICGLOSSMAP
- #pragma shader_feature ___ _DETAIL_MULX2
- #include "UnityStandardMeta.cginc"
- v2f vert_meta(appdata_full v)
- {
- v2f o = (v2f)0;
- float2 lmUV = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
- o.pos = UnityMetaVertexPosition(v.vertex,v.texcoord1.xy,v.texcoord2.xy,unity_LightmapST,unity_DynamicLightmapST);
- o.uv.xy = MF_TRANSFORM_TEX(v.texcoord,_MainTex1) + frac(_ScrollingSpeed * _Time.y);
- o.uv.zw = MF_TRANSFORM_TEX(v.texcoord,_MainTex2) + frac(_ScrollingSpeed * _Time.y);
- o.uv2.zw = MF_TRANSFORM_TEX(v.texcoord,_DiffBlendTex);
- o.uv2.xy = lmUV;
- o.col.xyz = v.color.xyz * _Params.x;
- o.col.w = v.color.a;
- o.params = float4(1.0f / _Params.y + 0.001f,0,0,0);
- return o;
- }
- float4 frag_meta(v2f i) : SV_Target
- {
- UnityMetaInput o;
- UNITY_INITIALIZE_OUTPUT(UnityMetaInput, o);
- float4 c1 = tex2D (_MainTex1, i.uv.xy) * _ColorTint1;
- float4 c2 = tex2D (_MainTex2, i.uv.zw) * _ColorTint2;
- float bt = tex2D (_DiffBlendTex, i.uv2.zw).x;
- float bm = saturate(saturate(saturate(bt + _Params.z) - i.col.a) * i.params.x);
- float4 c = lerp(c1,c2,bm);
- o.Albedo = c;
- o.Emission = 0;
- return UnityMetaFragment(o);
- }
- ENDCG
- }
- }
- FallBack "VertexLit"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement