catlikecoding笔记一:My First Lighting

ObjectSpaceNormal:受DynamicBatching影响,自动合并后法线改变;
WorldSpaceNormal: 法线由ObjectSpace到WorldSpace转换的方法(保持法线的垂直性):
1.若只包含旋转变换:
i.normal =mul((float3x3)unity_ObjectToWorld,v.normal);
2.若包含旋转和统一缩放(系数k):
i.normal =mul((1/k)(float3x3)unity_ObjectToWorld,v.normal);
3.一般:
i.normal=mul(transpose(float3x3)unity_WorldToObject),v.normal);
i.normal=UnityObjectToWorldNormal(v.normal);

Diffuse Shading:
1.Light Source
M199sangeyi05L
worldLight=normalize(_WorldSpaceLightPos0.xyz);

2.Clamped Lighting
dot(worldLight,i.normal);
max(0,dot(worldLight,i.normal));
saturate(0,dot(worldLight,i.normal));
#include "UnityStandardBRDF.cginc"
DotClamped(worldLight,i.normal);

3.LightColor _LightColor0.rgb

4.LightMode LightMode=ForwardBase

5.Albedo
albedo=tex2D(_MainTex,uv).rbg*_Tint.rgb;
diffuse=albedo*lightColor*DotClamped(lightDir,i.normal);

Specular Shading
Phong:
viewDir=normalize(_WorldSpaceCameraPos-i.worldPos);
reflectionDir=reflect(-lightDir,i.normal);
_Gloss:表面光滑度度量,越光滑,高光范围越小[0,100]
specular=_SpecularTint.rgb*lightColor*pow(DotClamped(viewDir,reflectionDir),_Gloss);

Blinn-Phong
halfVec=normalize(lightDir+viewDir);
specular=_SpecularTint.rgb*lightColor*pow(DotClamped(halfVec,i.normal),_Gloss);

Energy Conservation
1.根据高光颜色rgb分量 ;
albedo*=1-_SpecularTint.rgb;

2.根据高光颜色最大分量(monochrome)
albedo*=1-max(_SpecularTint.r,max(_Specular.g,_SpecularTint.b));

3.内置函数
#include "UnityStandardUtils.cginc"
float oneMinusRlflectivity;(用于monochrome)
albedo=EnergyConservationBetweenDiffuseAndSpecular(albedo,_SpecularTint.rgb,oneMinusRlflectivity);
这个函数默认monochrome模式,返回albedo*oneMinusReflectivity;


Metallic Workflow
1.simple
_Metallic
_SpecularTint=albedo*_Metallic;
albedo*=(1-_Metallic);
2.内置
DiffuseAndSpecularFromMetallic(albedo, _Metallic, _SpecularTint, oneMinusReflectivity);

PhysicallyBased Shading
#include "UnityPBSLighting.cginc"
UnityLight light;
light.color = _LightColor0.rgb;
light.dir = lightDir;
light.ndotl = DotClamped(i.normal, lightDir);
UnityIndirect indirectLight;
indirectLight.diffuse = 0;
indirectLight.specular = 0;
UNITY_BRDF_PBS(albedo,_SpecularTint,oneMinusReflectivity,_Gloss,i.normal,viewDir,light,in directLight);

你可能感兴趣的:(C#,unity3d)