图片是从网上抠下来的质量有点低,思路是用模型右方向判断当前光源朝向
float Remap(half value, half4 remap) {
return remap.z + (value - remap.x) * (remap.w - remap.z) / (remap.y - remap.x);
}
//unity自带方法,invert 默认float2(0,1)代表从图片左上角开始播放
void Flipbook(float2 UV, float Width, float Height, float Tile, float2 Invert, out float2 Out)
{
Tile = floor(fmod(Tile + 0.000001, Width*Height));
float2 tileCount = float2(1.0, 1.0) / float2(Width, Height);
float tileX = (Tile - Width * floor(Tile * tileCount.x));
float tileY = (Invert.y * Height - (floor(Tile * tileCount.x) + Invert.y * 1));
Out = (UV + float2(tileX, tileY)) * tileCount;
}
half GetIrisSSS(float2 uv)
{
float3 lightDir = _MainLightPosition.xyz;
float3 front = unity_ObjectToWorld._12_22_32;
float3 right = unity_ObjectToWorld._13_23_33;
float FrontL = dot(front.xyz, lightDir.xyz);
float RightL = dot(right.xz, lightDir.xz);
half f = Remap(saturate(1 - abs(RightL)), half4(0,1,0,5));//0-1映射到0-5驱动图的6帧
float fps = fmod(f, 6);
int tile = floor(fps);
uv = RightL < 0 ? uv : float2(1 - uv.x, uv.y);//左右翻转uv
Flipbook(uv, 3, 2, tile, float2(0,1), uv);
half s = SAMPLE_TEXTURE2D(_EnvMap, sampler_EnvMap, uv).a * saturate(FrontL);//过滤背部
return s;
}