使用BRDF技巧制作角色描边

Basic

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

Shader "BRDFDiffuse"
{
Properties
{
_EmissiveColor ("Emissive Color", Color) = (1,1,1,1)
_AmbientColor ("Ambient Color", Color) = (1,1,1,1)
_MySliderValue ("This is a Slider", Range(0,10)) = 2.5
_RampTex ("Ramp Texture", 2D) = "white"{}
_MainTex ("Main Texture", 2D) = "white"{}
}

SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200

CGPROGRAM
#pragma surface surf HelloWorld

float4 _EmissiveColor;
float4 _AmbientColor;
float _MySliderValue;
sampler2D _RampTex;
sampler2D _MainTex;

inline float4 LightingHelloWorld (SurfaceOutput s, fixed3 lightDir, half3 viewDir, fixed atten)
{
float difLight = dot (s.Normal, lightDir);
float rimLight = dot(s.Normal, viewDir);

float hLambert = difLight * 0.5 + 0.5;
float3 ramp = tex2D(_RampTex, float2(hLambert, rimLight)).rgb;

float4 col;
col.rgb = s.Albedo * _LightColor0.rgb * (ramp);
col.a = s.Alpha;
return col;
}

struct Input
{
float2 uv_MainTex;
};

void surf (Input IN, inout SurfaceOutput o)
{
float3 normalMap = tex2D(_MainTex, IN.uv_MainTex);

float4 c;
c = pow((_EmissiveColor + _AmbientColor), _MySliderValue);

o.Albedo = c.rgb * normalMap;
o.Alpha = c.a;
}

ENDCG
}

FallBack "Diffuse"
}

参数详情

1.LightingXXXX 代表unity的几种内置光照模型 <a herf:”http://docs.unity3d.com/Manual/SL-SurfaceShaderLighting.html">下面内容来自UNITY3D官方文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

Lighting Model declaration

Lighting model is a couple of regular functions with names starting with Lighting. They can be declared anywhere in your shader file or one of included files. The functions are:

half4 Lighting<Name> (SurfaceOutput s, half3 lightDir, half atten); This is used in forward rendering path for light models that are not view direction dependent (e.g. diffuse).
half4 Lighting<Name> (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten); This is used in forward rendering path for light models that are view direction dependent.
half4 Lighting<Name>_PrePass (SurfaceOutput s, half4 light); This is used in deferred lighting path.
Note that you don’t need to declare all functions. A lighting model either uses view direction or it does not. Similarly, if the lighting model will not work in deferred lighting, you just do not declare _PrePass function, and all shaders that use it will compile to forward rendering only.

Decoding Lightmaps

Decoding lightmap data can be customized in a similar fashion as the lighting function for forward and deferred lighting. Use one of the functions below depending on whether your light model is view direction dependent or not. To decode standard Unity lightmap texture data (passed in color, totalColor, indirectOnlyColor and scale arguments) use built-in DecodeLightmap function.

Custom lightmap decoding functions handle forward and deferred lighting rendering paths automatically. However you must be aware that in deferred case Lighting<Name>_PrePass function will be called after lightmap decoding and light argument will contain sum of realtime lighting and lightmaps. If necessary you can distinguish forward and deferred paths by using built-in UNITY_PASS_PREPASSFINAL macro.

Functions to customize decoding of Single lightmaps are:

half4 Lighting<Name>_SingleLightmap (SurfaceOutput s, fixed4 color); This is used for light models that are not view direction dependent (e.g. diffuse).
half4 Lighting<Name>_SingleLightmap (SurfaceOutput s, fixed4 color, half3 viewDir); This is used for light models that are view direction dependent.
Functions to customize decoding of Dual lightmaps are:

half4 Lighting<Name>_DualLightmap (SurfaceOutput s, fixed4 totalColor, fixed4 indirectOnlyColor, half indirectFade); This is used for light models that are not view direction dependent (e.g. diffuse).
half4 Lighting<Name>_DualLightmap (SurfaceOutput s, fixed4 totalColor, fixed4 indirectOnlyColor, half indirectFade, half3 viewDir); This is used for light models that are view direction dependent.

WARNING

关于Lighiting函数的命名

同上原文。在#pragma指定函数的名词,然后使用固定的格式书写完整的光照函数名即可。这里示例代码我用的是Helloworld.

关于BRDF制作描边

还是放弃这种方式吧。效果并不好。

更多预置光照模型

http://docs.unity3d.com/Manual/SL-SurfaceShaderLightingExamples.html