Phone模型
环境光+漫反射+高光反射
高光反射计算公式:
Clight:入射光线的颜色和强度Cighr
Mspecular:材质的高光反射系数
v:视角方向 r:反射方向
其中,反射方向r可以由表面法线n和光源I计算而得:
上述公式可以用CG函数reflect代替:
函数: reflect(i, n)
参数: i,入射方向; n,法线方向。可以是float、 float2、 float3等类型。
描述:当给定入射方向i和法线方向n时,reflect 函数可以返回反射方向。图6.9给出了参数和返回值之间的关系。
由于CG的reflect函数的入射方向要求是由光源指向交点处的,因此我们需要对worldLightDir取反后再传给reflect 函数。
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "Unlit/Phone"
{
Properties
{
_Diffuse ("Diffuse",Color) = (1,1,1,1)
_Specular ("Specular",Color) = (1,1,1,1)
_Gloss("Gloss",Range(8.0,256)) = 20
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
//逐像素光照
Pass
{
Tags {"LightMode" = "ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
#include "Lighting.cginc"
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f
{
float4 pos : SV_POSITION;
float3 worldNormal : TEXCOORD0;
float3 worldPos : TEXCOORD1;
};
fixed4 _Diffuse;
fixed4 _Specular;
float _Gloss;
v2f vert (appdata v)
{
v2f o;
//顶点模型空间转换为裁剪空间
o.pos = UnityObjectToClipPos(v.vertex);
//法线模型空间转到世界空间
o.worldNormal = mul(v.normal,(float3x3)unity_WorldToObject);
//顶点从模型空间转为世界空间
o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
//环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 worldLighDir = normalize(_WorldSpaceLightPos0.xyz);
//漫反射
fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal,worldLighDir));
//世界空间反射光
fixed3 reflectDir = normalize(reflect (-worldLighDir,worldNormal));
//世界空间视线
fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
//高光反射
fixed3 specular = _LightColor0.rgb * _Specular.rgb *pow(saturate(dot(reflectDir,viewDir)),_Gloss);
return fixed4(ambient + diffuse + specular , 1.0);
}
ENDCG
}
}
}
Blinn—Phone模型
Blinn 模型没有使用反射方向,而是引入一个新的矢量h,它是通过对视角方向v和光照方向I相加后再归一化得到的。即
Blinn模型计算高光反射如下:
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "Unlit/Phone"
{
Properties
{
_Diffuse ("Diffuse",Color) = (1,1,1,1)
_Specular ("Specular",Color) = (1,1,1,1)
_Gloss("Gloss",Range(8.0,256)) = 20
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
//逐像素光照
Pass
{
Tags {"LightMode" = "ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
#include "Lighting.cginc"
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f
{
float4 pos : SV_POSITION;
float3 worldNormal : TEXCOORD0;
float3 worldPos : TEXCOORD1;
};
fixed4 _Diffuse;
fixed4 _Specular;
float _Gloss;
v2f vert (appdata v)
{
v2f o;
//顶点模型空间转换为裁剪空间
o.pos = UnityObjectToClipPos(v.vertex);
//法线模型空间转到世界空间
o.worldNormal = mul(v.normal,(float3x3)unity_WorldToObject);
//顶点从模型空间转为世界空间
o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
//环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 worldLighDir = normalize(_WorldSpaceLightPos0.xyz);
//漫反射
fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal,worldLighDir));
//世界空间视线
fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
//Blinn模型新矢量:视角方向和光照方向相加后归一化
fixed3 halfDir = normalize(worldLighDir + viewDir);
//高光反射
fixed3 specular = _LightColor0.rgb * _Specular.rgb *pow(max(0,dot(worldNormal,halfDir)),_Gloss);
return fixed4(ambient + diffuse + specular , 1.0);
}
ENDCG
}
}
}
_WorldSpaceLightPos0 | 交点指向光源方向 |
UnityWorldSpaceLightDir() | 交点指向光源方向 |
ObjSpaceLightDir() | 光源指向交点方向 |