官术网_书友最值得收藏!

How it works...

The actual math to perform the normal mapping effect is definitely beyond the scope of this chapter, but Unity has done it all for us already. It has created the functions for us so that we don't have to keep doing it over and over again. This is another reason why Surface Shaders are a really efficient way to write shaders.

If you look in the UnityCG.cginc file found in the Editor | DataCGIncludes folder in your Unity installation directory, you will find the definitions for the UnpackNormal() function. When you declare this function in your Surface Shader, Unity takes the normal map provided and processes it for you, giving you the correct type of data so that you can use it in your per-pixel lighting function. It's a huge time-saver! When sampling a texture, you get RGB values from 0 to 1; however, the directions of a normal vector range from -1 to 1. UnpackNormal() brings these components into the right range.

Once you have processed the normal map with the UnpackNormal() function, you send it back to your SurfaceOutput struct so that it can be used in the lighting function. This is done by using o.Normal = normalMap.rgb;. We will see how the normal is actually used to calculate the final color of each pixel in Chapter 4, Understanding Lighting Models.

主站蜘蛛池模板: 连云港市| 枣阳市| 南投县| 织金县| 兴和县| 凤台县| 沾益县| 磴口县| 常州市| 南丹县| 鄂托克旗| 汉寿县| 丁青县| 南开区| 桑植县| 十堰市| 钟山县| 秭归县| 苏州市| 兴和县| 郯城县| 金华市| 开化县| 喀喇沁旗| 康乐县| 惠东县| 广东省| 霞浦县| 昌图县| 库尔勒市| 淳化县| 马龙县| 武义县| 长武县| 铁力市| 灌云县| 广西| 德阳市| 雷州市| 陇南市| 慈溪市|