Shader "MyShader/PBRDiffuse2"
|
|
{
|
|
Properties {
|
|
_Color("MainColor", Color) = (0.5,0.5,0.5,1)
|
|
_MainTex ("Base (RGB)", 2D) = "white" {}
|
|
[NoScaleOffset]_NmlMap("Normal Map", 2D) = "bump" {}
|
|
_NormalScale("Normal Scale", Range(0, 3)) = 1
|
|
[NoScaleOffset]_RMmap("Roughness(r)Metallic(g)map", 2D) = "black"{}
|
|
_RoughnessScale("Roughness Scale", Range(0, 1)) = 1
|
|
_MetallicScale("Metallic Scale", Range(0, 1)) = 1
|
|
_LightAdjustMin("light_adjust_min", Range(0, 2)) = 0
|
|
_LightAdjustMax("light_adjust_max", Range(0, 2)) = 1
|
|
}
|
|
|
|
CGINCLUDE
|
|
#include "UnityCG.cginc"
|
|
|
|
#pragma multi_compile_fwdbase
|
|
#pragma multi_compile_fog
|
|
#pragma fragmentoption ARB_precision_hint_fastest
|
|
|
|
sampler2D _MainTex;
|
|
sampler2D _NmlMap;
|
|
sampler2D _RMmap;
|
|
half _NormalScale, _RoughnessScale, _MetallicScale, _LightAdjustMin, _LightAdjustMax;
|
|
|
|
half4 _MainTex_ST, _Color;
|
|
half4 _GlobalLightColor;
|
|
half4 _GlobalLightDir;
|
|
|
|
inline float BRDFSpec(float noh, float loh, float r4, float r)
|
|
{
|
|
float a = (noh * noh) * (r4 - 1) + 1;
|
|
a *= loh;
|
|
float a2 = a * a;
|
|
return min(32, r4 / (4 * UNITY_PI * a2 * (r + 0.5)));
|
|
}
|
|
|
|
ENDCG
|
|
|
|
SubShader
|
|
{
|
|
Tags { "RenderType"="Opaque" }
|
|
|
|
Pass
|
|
{
|
|
CGPROGRAM
|
|
#pragma vertex vert
|
|
#pragma fragment frag
|
|
#pragma target 3.0
|
|
#pragma fragmentoption ARB_precision_hint_fastest
|
|
|
|
struct a2v
|
|
{
|
|
float4 vertex : POSITION;
|
|
half3 normal : NORMAL;
|
|
half4 tangent : TANGENT;
|
|
half2 texcoord : TEXCOORD0;
|
|
half2 texcoord1 : TEXCOORD1;
|
|
};
|
|
|
|
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)
|
|
};
|
|
|
|
v2f vert(a2v v)
|
|
{
|
|
v2f o;
|
|
o.posWorld = mul(unity_ObjectToWorld, v.vertex);
|
|
o.pos = mul(UNITY_MATRIX_VP, o.posWorld);
|
|
o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
|
|
o.uv.zw = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
|
|
|
|
o.normalDir = normalize(mul((float3x3)unity_WorldToObject, 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;
|
|
}
|
|
|
|
half4 frag (v2f i) : SV_Target
|
|
{
|
|
half4 cBase = tex2D(_MainTex, i.uv.xy);
|
|
|
|
float3x3 tangentTransform = float3x3(i.tangentDir, i.bitangentDir, i.normalDir);
|
|
half3 normal = UnpackNormal(tex2D(_NmlMap, i.uv.xy));
|
|
normal.xy *= _NormalScale;
|
|
normal = normalize(mul(normal, tangentTransform));
|
|
|
|
half4 texRM = tex2D(_RMmap, i.uv.xy);
|
|
half roughness = max(texRM.r, 0.008) * _RoughnessScale;
|
|
half metallic = texRM.g * _MetallicScale;
|
|
|
|
half3 L = _GlobalLightDir.xyz;
|
|
half3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
|
|
half3 halfDir = normalize(L + viewDir);
|
|
|
|
half nl = max(dot(normal, L), 0.001);
|
|
half nh = max(dot(normal, halfDir), 0.01);
|
|
half lh = max(dot(L, halfDir), 0.01);
|
|
|
|
half3 cLit = _GlobalLightColor * nl;
|
|
cBase.rgb *= cBase.rgb;
|
|
|
|
half3 cDiffuse = (1.0f - metallic) * cBase.rgb;
|
|
half3 SpecularColor = (0.04 - 0.04 * metallic) + cBase.rgb * metallic;
|
|
|
|
half4 r4 = roughness * roughness;
|
|
r4 *= r4;
|
|
|
|
|
|
float3 specularTerm = BRDFSpec(nh, lh, r4, roughness) * SpecularColor;
|
|
half3 lmCol = DecodeLightmap (UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv.zw)) ;
|
|
half3 finalColor = lmCol.rgb * lmCol.rgb * cDiffuse.rgb;
|
|
cLit *= smoothstep(_LightAdjustMin, _LightAdjustMax, Luminance(lmCol.rgb));
|
|
|
|
finalColor += (specularTerm + cDiffuse)* cLit;
|
|
finalColor = sqrt(finalColor);
|
|
|
|
finalColor.rgb *= (2.0 * _Color);
|
|
UNITY_APPLY_FOG(i.fogCoord, finalColor);
|
|
return half4(finalColor, 1);
|
|
}
|
|
ENDCG
|
|
}
|
|
|
|
}
|
|
}
|