|
|
-
- Shader "MyShader/PDRDiffuse4"
- {
- Properties
- {
- _Color("MainColor", Color) = (0.5,0.5,0.5,1)
- _MainTex("Base 2D", 2D) = "white"{}
- _MaskTex("Mask 2D", 2D) = "white"{}
- _Normal("Normal", 2D) = "white"{}
- _EmissionRatio("EmissionRatio", Range(0, 30)) = 1
- _RoughnessScale("Roughness Scale", Range(0, 2)) = 1
- _MetallicScale("Metallic Scale", Range(0, 5)) = 1
- _LMcolScale("LMcol Scale", Range(1, 5)) = 1
- _MCScale("MCScale Scale", Range(0, 2)) = 0.5
- }
-
- SubShader
- {
- LOD 300
- Tags{ "RenderType" = "Opaque" }
-
- Pass
- {
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- #pragma multi_compile_fog
- #include "UnityCG.cginc"
-
- struct appdataInput
- {
- float4 vertex : POSITION;
- float2 uv : TEXCOORD0;
- float2 uv1 : TEXCOORD1;
- float3 normal:NORMAL;
- float4 tangent : TANGENT;
- };
-
- struct v2f
- {
- float4 pos : SV_POSITION;
- float4 uv : TEXCOORD0;
- float4 posWorld : TEXCOORD1;
- float3 normalDir : TEXCOORD2;
- float3 tangentDir : TEXCOORD3;
- float3 bitangentDir : TEXCOORD4;
- UNITY_FOG_COORDS(5)
- };
-
- sampler2D _MainTex, _MaskTex, _Normal;
- float _EmissionRatio;
- float4 _Color;
- float4 _GlobalLightColor, _GlobalLightDir;
- half _RoughnessScale, _MetallicScale, _LMcolScale, _MCScale;
-
- half GGX_Mobile(half Roughness, half NoH)
- {
- float OneMinusNoHSqr = 1.0 - NoH * NoH;
-
- half a = Roughness * Roughness;
- float n = NoH * a;
- float p = a / (OneMinusNoHSqr + n * n);
- float d = min(32, p * p);
- return d;
- }
-
- half3 EnvBRDFApprox(half3 SpecularColor, half Roughness, half NoV)
- {
- const half4 c0 = { -1, -0.0275, -0.572, 0.022 };
- const half4 c1 = { 1, 0.0425, 1.04, -0.04 };
- half4 r = Roughness * c0 + c1;
- half a004 = min(r.x * r.x, exp2(-9.28 * NoV)) * r.x + r.y;
- half2 AB = half2(-1.04, 1.04) * a004 + r.zw;
-
- AB.y *= saturate(50.0 * SpecularColor.g);
- return SpecularColor * AB.x + AB.y;
- }
-
- v2f vert(appdataInput v)
- {
- v2f o;
- o.posWorld = mul(unity_ObjectToWorld, v.vertex);
- o.pos = mul(UNITY_MATRIX_VP, o.posWorld);
- o.uv.xy = v.uv;
- o.uv.zw = v.uv1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
-
- o.normalDir = UnityObjectToWorldNormal(v.normal);
- o.tangentDir = normalize(mul((float3x3)unity_ObjectToWorld, v.tangent.xyz));
- o.bitangentDir = cross(o.normalDir, o.tangentDir) * v.tangent.w;
-
- UNITY_TRANSFER_FOG(o, o.pos);
- return o;
- }
-
- float4 frag(v2f i) : SV_Target
- {
- float3 mainColor = tex2D(_MainTex, i.uv.xy).rgb;
- mainColor *= mainColor;
- float4 maskColor = tex2D(_MaskTex, i.uv.xy);
-
- float3 V = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
-
- float3x3 tangentTransform = float3x3(i.tangentDir, i.bitangentDir, i.normalDir);
- float3 N = UnpackNormal(tex2D(_Normal, i.uv.xy));
- N = normalize(mul(N, tangentTransform));
-
- //float3 R = reflect(-V, i.normalDir);
-
- float Metallic = maskColor.r * _MetallicScale;
- float Roughness = max(maskColor.g, 0.008) * _RoughnessScale;
-
- float3 DiffuseColor = mainColor - mainColor * Metallic;
- float3 SpecularColor0 = (0.04 - 0.04 * Metallic) + mainColor * Metallic;
-
- float3 L = _GlobalLightDir.xyz; //;
- float3 H = normalize(L + V);
-
- float4 resultColor = 0;
- float NdotL = max(dot(N, L), 0.001);
- float NdotV = max(dot(N, V), 0.001);
- float NdotH = max(dot(N, H), 0.01);
-
- float3 SpecularColor = SpecularColor0 *0.9;//EnvBRDFApprox(SpecularColor0, Roughness, NdotV);
- float3 specular = SpecularColor * (Roughness * 0.25 + 0.25) * GGX_Mobile(Roughness, NdotH);
- resultColor.rgb = (DiffuseColor + specular) * _GlobalLightColor * NdotL;
-
- float3 lmCol = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv.zw));
- float3 lmGray = Luminance(lmCol.rgb) * _LMcolScale;
- resultColor.rgb += lmCol.rgb * lmCol.rgb * mainColor.rgb * lmGray * lmGray;
-
- resultColor.rgb += maskColor.b * mainColor * _EmissionRatio;
- resultColor.rgb = sqrt(resultColor.rgb);
-
- resultColor.rgb *= (_MCScale* _Color.rgb);
- //resultColor = clamp(resultColor, 0.0, 10.0);
- UNITY_APPLY_FOG(i.fogCoord, resultColor);
- resultColor.a = 1;
- return resultColor;
- }
- ENDCG
- }
- }
-
- Fallback "Diffuse"
- }
|