Shader "Unlit/ModelSkin"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_MainNormal("MainNormal",2D)="bump"{}
_SSSTex("SSSTex",2D)="white" {}
_Shiness("Shiness",float)=1
_ReflectHDR("ReflectHDR",Cube)="white" {}
_IndirectReflect("IndirectReflect",Range(0,1))=0
_SkinExpose("SkinExpose",Range(0,2))=0
_SkinLUT("SkinLUT",2D) = "white" {}
_LutHoffset("LutHoffset",Range(0,1))=0
_LutVoffset("LutVoffset",Range(0,1))=0
_roughAdjust("roughAdjust",Range(-1,1))=0
_metalAdjust("metalAdjust",Range(-1,1))=0
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
Tags{"LightMode"="ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase
#include "UnityCG.cginc"
#include "AutoLight.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float3 normal:NORMAL;
float4 tangent:TANGENT;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 pos : SV_POSITION;
float3 world_pos:TEXCOORD1;
float3 world_normal:TEXCOORD2;
float3 world_binormal:TEXCOORD3;
float3 world_tangent:TEXCOORD4;
LIGHTING_COORDS(5,6)
};
sampler2D _MainTex;
sampler2D _MainNormal;
sampler2D _SSSTex;
sampler2D _SkinLUT;
samplerCUBE _ReflectHDR;
float4 _ReflectHDR_HDR;
float4 _MainTex_ST;
float _Shiness;
float _IndirectReflect;
float _SkinExpose;
float _LutHoffset;
float _LutVoffset;
fixed4 _LightColor0;
float _roughAdjust;
float _metalAdjust;
inline float3 ACES_ToneMapping(float3 x)
{
float a=2.51f;
float b=0.03f;
float c=2.43f;
float d=0.59f;
float e=0.14f;
float3 encode_color=saturate((x*(a*x+b))/(x*(c*x+d)+e));
return encode_color;
}
v2f vert (appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.texcoord*_MainTex_ST.xy+_MainTex_ST.zw;
o.world_pos=mul(unity_ObjectToWorld,v.vertex);
o.world_normal=normalize(mul(float4(v.normal,0.0),unity_WorldToObject)).xyz;
o.world_tangent=normalize(mul(unity_ObjectToWorld,float4(v.tangent.xyz,0.0f))).xyz;
o.world_binormal=normalize(cross(o.world_normal,o.world_tangent).xyz/v.tangent.w).xyz;
TRANSFER_VERTEX_TO_FRAGMENT(o);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
half atten=LIGHT_ATTENUATION(i);
// sample the texture
fixed4 base_color_gamma = tex2D(_MainTex, i.uv);
fixed3 base_color_linner= pow(base_color_gamma,2.2).rgb;
fixed3 SSS_Message= tex2D(_SSSTex, i.uv).rgb;
float roughness=saturate(SSS_Message.r+_roughAdjust);
float metal=saturate(SSS_Message.g+_metalAdjust);
float skin_area=1-SSS_Message.b;
float3 base_color=(1-metal)*base_color_linner;
float3 sepc_color=lerp(0.02,base_color_linner,metal);
half3 normal_data=UnpackNormal(tex2D(_MainNormal,i.uv));
half3 world_normal=normalize(i.world_normal);
half3 world_binoraml=normalize(i.world_binormal);
half3 world_tangent=normalize(i.world_tangent);
float3x3 TBN=float3x3(world_tangent,world_binoraml,world_normal);
normal_data=normalize(mul(normal_data,TBN));
float3 view_dir=normalize(_WorldSpaceCameraPos.xyz-i.world_pos);
float3 light_dir=normalize(_WorldSpaceLightPos0.xyz);
//light_direct_diffuse
fixed lamert_light=max(0.0,dot(normal_data,light_dir));
float half_lamert= lamert_light*0.5+0.5;
fixed3 direct_diffuse=lamert_light*_LightColor0.xyz*base_color*atten;
half2 uv_lut=half2(lamert_light+_LutHoffset,_LutVoffset);
fixed4 tex_lut=tex2D(_SkinLUT,uv_lut);
float3 tex_diffuse=tex_lut*_LightColor0.xyz*base_color*atten;
direct_diffuse=lerp(direct_diffuse,tex_diffuse,skin_area);
//light_direct_reflect
half3 h_dir=normalize(view_dir+light_dir);
float HDotV=max(0.0,dot(normal_data,h_dir));
float smoothness=(1-roughness);
float shiness_direct=lerp(1,_Shiness,smoothness);
float shiness=pow(HDotV,shiness_direct);
float skin_reflect=lerp(sepc_color,_SkinExpose,skin_area);
fixed3 direct_reflect=shiness*_LightColor0.xyz*skin_reflect*atten;
float3 amibent=ShadeSH9(float4(normal_data,1.0));
float3 env_diffuse=amibent*base_color*half_lamert*_LightColor0.xyz;
//reflect
float3 reflect_dir=reflect(-view_dir,normal_data);
float roughness_HDR=(1.7-0.7*roughness)*roughness;
float min_mip=roughness_HDR*6;
float4 cubeMap=texCUBElod(_ReflectHDR,float4(reflect_dir,min_mip));
float3 decodeCube=DecodeHDR(cubeMap,_ReflectHDR_HDR);
fixed3 indirect_reflect=decodeCube*_LightColor0.xyz*sepc_color*half_lamert;
fixed3 final_color=direct_diffuse+direct_reflect+env_diffuse*_IndirectReflect+indirect_reflect;
final_color=ACES_ToneMapping(final_color);
final_color=pow(final_color,1/2.2);
return fixed4(final_color,1.0);
}
ENDCG
}
}
FallBack "Diffuse"
}
unity手机人物基础渲染shader
最新推荐文章于 2023-04-23 17:15:43 发布