catlikecoding笔记一:My First Lighting

本文详细介绍了Unity中Shader实现的基本原理,包括法线变换、光照计算、金属工作流程及基于物理的着色等内容。通过具体代码片段展示了如何进行法线转换、计算漫反射和高光效果,以及如何实现能量守恒等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值