目录
前言
漫反射-Diffuse
直接采样maintex
阴影-RampMap
ndotl获取阴影阈值作为rampmap的uv采样
float NdotL = saturate(dot(nDirWS,lDirWS));
float halfLambertRamp = smoothstep(0.0, _RampSmoothScale, NdotL * 0.5 + 0.5);
float3 var_rampTex = tex2D(_RampTex, float2(halfLambertRamp,_RampSide));
边缘光-屏幕空间深度偏移
取当前屏幕像素UV,向某一方向进行偏移后采样深度,与当前像素差异大于一阈值为边缘
float3 nDirVS = normalize(mul((float3x3)UNITY_MATRIX_V, nDirWS));
float2 screenParams01 = float2(i.pos.x/_ScreenParams.x,i.pos.y/_ScreenParams.y);
float2 offectSamplePos = screenParams01+nDirVS*_RimOffect/i.pos.w/_ScreenParams.x;
float offcetDepth = tex2D(_CameraDepthTexture, offectSamplePos);
float trueDepth = tex2D(_CameraDepthTexture, screenParams01);
float linearEyeOffectDepth = LinearEyeDepth(offcetDepth);
float linearEyeTrueDepth = LinearEyeDepth(trueDepth);
float depthDiffer = linearEyeOffectDepth-linearEyeTrueDepth;
float rimIntensity = step(_Threshold,depthDiffer);
描边-视图空间法线外扩
float4 pos = UnityObjectToClipPos(v.vertex);
float3 viewNormal = mul(UNITY_MATRIX_IT_MV, v.normal);
float3 proNormal = normalize(TransformViewToProjection(viewNormal)) * pos.w;
float4 nearUpperRight = mul(unity_CameraInvProjection, _ProjectionParams.y * float4(1, 1, -1, 1));
proNormal.x *= abs(nearUpperRight.y / nearUpperRight.x);
pos.xy += proNormal.xy * _OutlineWidth * 0.1f;
头发
高光-AnisoSpecular
采样高光贴图作为各向异性高光遮罩,计算菲涅尔
float AnisoMask = tex2D(_MaskTexture, i.uv0).r;
float anisoHairFresnel = pow((1.0 - saturate(dot(nDirWS, vDirWS))),_AnisoHairFresnelPow) *_AnisoHairFresnelIntensity;
float anisoHair = saturate(1-anisoHairFresnel) * saturate(AnisoMask*NdotL);
面部
阴影-SDF阴影
首先拿一张距离场贴图,还分别需要向前、向右、光照的向量,距离场贴图记录了单项光的方向,计算另一边光照是需要反转采样
float3 qianDir = normalize(UnityObjectToWorldDir(float3(0.0,0.0,1.0)));
float3 rightDir = normalize(UnityObjectToWorldDir(float3(1.0,0.0,0.0)));
float FLambert = dot(lDirWS.xz,qianDir.xz)*0.5+0.5;
float FLambert2 = dot(lDirWS.xz,rightDir.xz);
float ver_Faceshadow = tex2D(_ShadowTex,i.uv0);
float ver_Faceshadow2 = tex2D(_ShadowTex,float2(-i.uv0.x,i.uv0.y));
float Faceshadow = lerp(ver_Faceshadow,ver_Faceshadow2,step(FLambert2,0));
float face = FLambert>Faceshadow?1.0:0.0;
P2
用unity后处理里的bloom,和colorgrad自定义调色
toonmap
none
neutral
aces