Shader "MyShader/PBRTer3"
|
|
{
|
|
Properties {
|
|
_Splat0 ("Layer 1", 2D) = "white" {}
|
|
_Splat1 ("Layer 2", 2D) = "white" {}
|
|
_Splat2 ("Layer 3", 2D) = "white" {}
|
|
_BumpSplat0 ("Layer1Normalmap", 2D) = "bump" {}
|
|
_BumpSplat1 ("Layer2Normalmap", 2D) = "bump" {}
|
|
_BumpSplat2 ("Layer3Normalmap", 2D) = "bump" {}
|
|
_Control ("Control (RGBA)", 2D) = "white" {}
|
|
|
|
_Color("MainColor", Color) = (0.5,0.5,0.5,1)
|
|
_NormalScale("Normal Scale", Range(0, 3)) = 1.2
|
|
_LightAdjustMin("light_adjust_min", Range(0, 2)) = 0
|
|
_LightAdjustMax("light_adjust_max", Range(0, 2)) = 1
|
|
}
|
|
|
|
CGINCLUDE
|
|
|
|
#include "UnityCG.cginc"
|
|
|
|
sampler2D _Splat0, _Splat1, _Splat2, _BumpSplat0, _BumpSplat1, _BumpSplat2;
|
|
sampler2D _Control;
|
|
|
|
half4 _Splat0_ST, _Splat1_ST, _Splat2_ST;
|
|
half _NormalScale, _LightAdjustMin, _LightAdjustMax;
|
|
half4 _Color;
|
|
|
|
half4 _GlobalLightColor;
|
|
half4 _GlobalLightDir;
|
|
|
|
inline float GGX2(float nh, float hl, float r)
|
|
{
|
|
hl = max (hl, 0.01f);
|
|
float a = max(0.01f, r * r * 0.49f);
|
|
float aa = a * a;
|
|
float M = ((nh * aa) - nh) * nh + 1.0;
|
|
float t = M * hl;
|
|
t *= t;
|
|
t *= (r + 0.5f);
|
|
return clamp(aa / (4.0f * t), 0, 100);
|
|
}
|
|
|
|
ENDCG
|
|
|
|
SubShader
|
|
{
|
|
Tags { "RenderType"="Opaque" }
|
|
|
|
Pass
|
|
{
|
|
CGPROGRAM
|
|
#pragma vertex vert
|
|
#pragma fragment frag
|
|
#pragma multi_compile_fwdbase
|
|
#pragma multi_compile_fog
|
|
#pragma target 3.0
|
|
#pragma fragmentoption ARB_precision_hint_fastest
|
|
|
|
struct a2v
|
|
{
|
|
float4 vertex : POSITION;
|
|
float3 normal : NORMAL;
|
|
float4 tangent : TANGENT;
|
|
float2 texcoord : TEXCOORD0;
|
|
float2 texcoord1 : TEXCOORD1;
|
|
};
|
|
|
|
struct v2f
|
|
{
|
|
float4 pos : SV_POSITION;
|
|
float4 uv : TEXCOORD0;
|
|
float4 posWorld : TEXCOORD1;
|
|
float3 normalDir : TEXCOORD2;
|
|
float3 tangentDir : TEXCOORD3;
|
|
float3 bitangentDir : TEXCOORD4;
|
|
float4 splatUv0 : TEXCOORD5;
|
|
float4 splatUv1 : TEXCOORD6;
|
|
UNITY_FOG_COORDS(7)
|
|
};
|
|
|
|
v2f vert(a2v v)
|
|
{
|
|
v2f o;
|
|
o.posWorld = mul(unity_ObjectToWorld, v.vertex);
|
|
o.pos = mul(UNITY_MATRIX_VP, o.posWorld);
|
|
o.uv.xy = v.texcoord;
|
|
o.uv.zw = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
|
|
|
|
o.normalDir = mul((float3x3)unity_ObjectToWorld, v.normal);
|
|
o.tangentDir = normalize(mul((float3x3)unity_ObjectToWorld, v.tangent.xyz));
|
|
o.bitangentDir = normalize(cross(o.normalDir, o.tangentDir) * v.tangent.w);
|
|
|
|
o.splatUv0.xy = TRANSFORM_TEX(v.texcoord, _Splat0);
|
|
o.splatUv0.zw = TRANSFORM_TEX(v.texcoord, _Splat1);
|
|
o.splatUv1.xy = TRANSFORM_TEX(v.texcoord, _Splat2);
|
|
|
|
UNITY_TRANSFER_FOG(o, o.pos);
|
|
return o;
|
|
}
|
|
|
|
half4 frag (v2f i) : SV_Target
|
|
{
|
|
half4 splat = tex2D (_Control, i.uv.xy);
|
|
half4 cBase = tex2D(_Splat0, i.splatUv0.xy) * splat.r;
|
|
half4 texBump = tex2D(_BumpSplat0, i.splatUv0.xy) * splat.r;
|
|
|
|
cBase += tex2D(_Splat1, i.splatUv0.zw) * splat.g;
|
|
texBump += tex2D(_BumpSplat1, i.splatUv0.zw) * splat.g;
|
|
|
|
cBase += tex2D(_Splat2, i.splatUv1.xy) * splat.b;
|
|
texBump += tex2D(_BumpSplat2, i.splatUv1.xy) * splat.b;
|
|
|
|
cBase.rgb *= cBase.rgb;
|
|
half roughness = cBase.a;
|
|
half metallic = texBump.a;
|
|
|
|
float3x3 tangentTransform = float3x3( i.tangentDir, i.bitangentDir, i.normalDir);
|
|
half3 normal = texBump.xyz * 2.0 - 1.0f;
|
|
normal.xy *= _NormalScale;
|
|
normal = normalize(mul(normal, tangentTransform));
|
|
|
|
half3 litDir = _GlobalLightDir.xyz;
|
|
half3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
|
|
half3 halfDir = normalize(litDir + viewDir);
|
|
|
|
half ndotl = dot(normal, litDir);
|
|
half nl = max(0, ndotl);
|
|
half nh = clamp(dot(normal, halfDir), 0, 1);
|
|
half lh = clamp(dot(litDir, halfDir), 0, 1);
|
|
|
|
half3 cLit = _GlobalLightColor.rgb * nl;
|
|
half3 cDiffuse = (1.0f - metallic) * cBase.rgb;
|
|
|
|
float specularTerm = GGX2(nh, lh, roughness);
|
|
half3 finalColor = 0;
|
|
|
|
half3 diffTerm = 0;
|
|
half3 lmCol = DecodeLightmap (UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv.zw)) ;
|
|
finalColor += lmCol * cBase.rgb;
|
|
cLit *= smoothstep (_LightAdjustMin, _LightAdjustMax, Luminance(lmCol.rgb));
|
|
|
|
diffTerm += cLit;
|
|
|
|
finalColor += (specularTerm * metallic * cLit + cDiffuse * diffTerm);
|
|
finalColor = sqrt(finalColor);
|
|
finalColor *= _Color * 2.0f;
|
|
|
|
UNITY_APPLY_FOG(i.fogCoord, finalColor);
|
|
return half4(finalColor, 1);
|
|
}
|
|
ENDCG
|
|
}
|
|
|
|
}
|
|
}
|