您的位置:首页 > 移动开发 > Unity3D

从一个shader剖析unity混合光照和pbr

2018-01-31 09:44 555 查看
从unity5.6自带的代码来看。

下面是standard.shader的面板



shader结构图:



subShader的区别,先看两个pass的区别:





然后我们先看第一个forward pass,“UnityStandardCoreForward.cginc”里查看,vertBase和fragBase又根据是否定义simple而执行不同的分支:



定义了simple的分支,数据结构和函数,我们去“UnityStandardCoreForwardSimple.cginc”文件里查看,vert函数的输入结构定义:



vert函数输出结构定义



vert函数定义



里面涉及的函数,获取切线空间光照和view方向的函数



获取lightmap或环境光的uv的函数:



grazing参数的获取:



再看fragment函数fragForwardBaseSimpleInternal,在UnityStandardCoreForwardSimple.cginc文件中:



里面第一句又从输入的结构得到FragmentCommonData,函数如下:



fragmentCommonData结构如下:



与前面的结构比较:



转换函数FragmentSetupSimple也在SimpleForward这个文件里



其中的UNITY_SETUP_BRDF_INPUT函数(UnityStandardCore.cginc中)定义如下,这个函数里获取FragmentCommonData的difColor,SpecColor,oneMinusReflectivity和smoothness。



其中a通过Alpha函数得到,如果定义了_SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A则通过_color.a得到,否则:


。该宏的定义通过editor得到,在builtin_shaders-5.6\Editor\StandardShaderGUI.cs文件中:



其中smoothness通过SpecularGloss函数得到(在UnityStandardInput.cginc中定义),如果有speculargloss贴图,则对贴图采样,如果用主贴图的a通道存储gloss值,则sg的a通道通过采样主贴图得到,如果a通道在specularGLoss贴图,则通过采样specGLoss贴图得到。如果没有定义高光贴图,则高光颜色通过_SpecColor得到,如果gloss在主贴图的a通道,则采样主贴图获取gloss,否则,gloss通过_Glossiness得到:



diffColor,SpecColor,oneMinusReflectivity又通过EnergyConservationBetweenDiffuseAndSpecular得到:



diffColor用preMultiplyAlpha函数,里面将blend模式加入:



看完FragmentCommonData后,我们再继续看fragForwardBaseSimpleInternal函数,mainLight包含颜色和方向,nDotL,然后是shadowMask衰减,实时阴影衰减并将两个衰减混合。从遮挡贴图采样得到遮挡因子。rl好像是反射光线和主光源方向的点乘,然后是FragmentGI求出GI。FragmentGI函数如下:





fragmentGI函数里首先构建了一个UnityGIInput数据结构,然后调用UnityGlobalIllumination求出gi,有Unity_GlossyEnvironmentData的话,GI里会加入该参数。



主要包含UnityGIbase和UnityGI_IndirectSpecular,前者计算了阴影的衰减(烘焙阴影和实时阴影混合),SH光照,烘焙的lightmap,平行光与非平行光lightmap,动态lightmap。最终返回UnityGI结构,该结构包含light,color,indirect.diffuse参数。

UnityGI_IndirectSpecular(UnityGLobalIllumination.cginc)计算间接高光,用probe相关的属性计算。





用求得的GI,计算衰减的光颜色。

然后就是BRDF计算间接光照和直接光照了



simple版好像brdf不多,下面看看复杂版



输入结构:



复杂的比simple版多了些东西,但原理是一样的。

后面我发现BRDF有3个版本。上面的是brdf3.

根据冯乐乐的数,第18章,在disney使用的brdf2中,它的漫反射项为:



float attenuation = 1;
float3 attenColor = attenuation * _LightColor0.xyz;

float Pi = 3.141592654;
float InvPi = 0.31830988618;
float gloss = _Gloss;
half fd90 = 0.5 + 2 * LdotH * LdotH * (1-gloss);
float3 directDiffuse = ((1 +(fd90 - 1)*pow((1.00001-NdotL), 5)) * (1 + (fd90 - 1)*pow((1.00001-NdotV), 5)) * NdotL) * attenColor;


pbs高光项:



函数实现:



代码实例:



整个fragment shader代码(包含了GI部分,调用unity内置函数实现):

float4 frag(VertexOutput i) : COLOR {
i.normalDir = normalize(i.normalDir);
// UNITY_SAMPLE_DEPTH对深度贴图进行采样
// _ProjectionParams的x为1,如果投影翻转则x为-1,y是摄像机近裁剪平面,z是摄像机远裁剪平面,w是1/z.
float sceneZ = max(0,LinearEyeDepth (UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)))) - _ProjectionParams.g); //减去近裁剪平面
float partZ = max(0,i.projPos.z - _ProjectionParams.g); //物体之深度
float3x3 tangentTransform = float3x3( i.tangentDir, i.bitangentDir, i.normalDir); // 转移矩阵
float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
float3 _WetNormal_var = UnpackNormal(tex2D(_WetNormal,TRANSFORM_TEX(i.uv0, _WetNormal)));
float3 _Ripple_var = UnpackNormal(tex2D(_Ripple,TRANSFORM_TEX(i.uv0, _Ripple)));
//float3 node_4532_nrm_base = _WetNormal_var.rgb + float3(0,0,1);
//float3 node_4532_nrm_detail = _Ripple_var.rgb * float3(-1,-1,1);   //wet和下雨混合
//float3 node_4532_nrm_combined = node_4532_nrm_base*dot(node_4532_nrm_base, node_4532_nrm_detail)/node_4532_nrm_base.z - node_4532_nrm_detail;
float3 node_4532_nrm_combined = _WetNormal_var.rgb +  _Ripple_var.rgb;
float3 node_4532 = node_4532_nrm_combined;
float3 normalLocal = node_4532;
float3 normalDirection = normalize(mul( normalLocal, tangentTransform ));
float3 viewReflectDirection = reflect( -viewDirection, normalDirection );
float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
float3 lightColor = _LightColor0.rgb;
float3 halfDirection = normalize(viewDirection+lightDirection);
float attenuation = 1;
float3 attenColor = attenuation * _LightColor0.xyz;
float Pi = 3.141592654;
float InvPi = 0.31830988618;
float gloss = _Gloss;
float specPow = exp2( gloss * 10.0+1.0);

UnityLight light;
#ifdef LIGHTMAP_OFF
light.color = lightColor;
light.dir = lightDirection;
light.ndotl = LambertTerm (normalDirection, light.dir);
#else
light.color = half3(0.f, 0.f, 0.f);
light.ndotl = 0.0f;
light.dir = half3(0.f, 0.f, 0.f);
#endif
UnityGIInput d;
d.light = light;
d.worldPos = i.posWorld.xyz;
d.worldViewDir = viewDirection;
d.atten = attenuation;
#if defined(LIGHTMAP_ON) || defined(DYNAMICLIGHTMAP_ON)
d.ambient = 0;
d.lightmapUV = i.ambientOrLightmapUV;
#else
d.ambient = i.ambientOrLightmapUV;
#endif
d.boxMax[0] = unity_SpecCube0_BoxMax;
d.boxMin[0] = unity_SpecCube0_BoxMin;
d.probePosition[0] = unity_SpecCube0_ProbePosition;
d.probeHDR[0] = unity_SpecCube0_HDR;
d.boxMax[1] = unity_SpecCube1_BoxMax;
d.boxMin[1] = unity_SpecCube1_BoxMin;
d.probePosition[1] = unity_SpecCube1_ProbePosition;
d.probeHDR[1] = unity_SpecCube1_HDR;
Unity_GlossyEnvironmentData ugls_en_data;
ugls_en_data.roughness = 1.0 - gloss;
ugls_en_data.reflUVW = viewReflectDirection;
UnityGI gi = UnityGlobalIllumination(d, 1, normalDirection, ugls_en_data );
lightDirection = gi.light.dir;
lightColor = gi.light.color;
float NdotL = max(0, dot( normalDirection, lightDirection ));
float LdotH = max(0.0,dot(lightDirection, halfDirection));
float3 specularColor = _Specular.rgb;
float specularMonochrome = max( max(specularColor.r, specularColor.g), specularColor.b);
float NdotV = max(0.0,dot( normalDirection, viewDirection ));
float NdotH = max(0.0,dot( normalDirection, halfDirection ));
float VdotH = max(0.0,dot( viewDirection, halfDirection ));
float visTerm = SmithBeckmannVisibilityTerm( NdotL, NdotV, 1.0-gloss );
float normTerm = max(0.0, NDFBlinnPhongNormalizedTerm(NdotH, RoughnessToSpecPower(1.0-gloss)));
float specularPBL = max(0, (NdotL*visTerm*normTerm) * UNITY_PI / 4.0 );
float3 directSpecular = 1 * pow(max(0,dot(halfDirection,normalDirection)),specPow)*specularPBL*lightColor*FresnelTerm(specularColor, LdotH);
half grazingTerm = saturate( gloss + specularMonochrome );
float3 indirectSpecular = (gi.indirect.specular);
indirectSpecular *= FresnelLerp (specularColor, grazingTerm, NdotV);
float3 specular = (directSpecular + indirectSpecular);
NdotL = max(0.0,dot( normalDirection, lightDirection ));
half fd90 = 0.5 + 2 * LdotH * LdotH * (1-gloss);
float3 directDiffuse = ((1 +(fd90 - 1)*pow((1.00001-NdotL), 5)) * (1 + (fd90 - 1)*pow((1.00001-NdotV), 5)) * NdotL) * attenColor;
float3 indirectDiffuse = float3(0,0,0);
indirectDiffuse += gi.indirect.diffuse;
float3 diffuseColor = _LightColor0.rgb;
diffuseColor *= 1-specularMonochrome;
float3 diffuse = (directDiffuse + indirectDiffuse) * diffuseColor;

float3 finalColor = diffuse + specular;
fixed4 finalRGBA = fixed4(finalColor,(saturate((sceneZ-partZ)/_Depth)*_Alpha));
UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
return finalRGBA;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: