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

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.

主站蜘蛛池模板: 宁晋县| 扎鲁特旗| 海淀区| 土默特左旗| 大余县| 磐石市| 海兴县| 南靖县| 乌兰察布市| 洮南市| 天气| 祁阳县| 盖州市| 大渡口区| 泰顺县| 曲沃县| 乐亭县| 大竹县| 志丹县| 宁海县| 浏阳市| 玛纳斯县| 横山县| 葫芦岛市| 卢氏县| 南开区| 肇东市| 农安县| 瑞安市| 马鞍山市| 射阳县| 古田县| 建湖县| 松溪县| 卓资县| 灵璧县| 乌拉特后旗| 乃东县| 会宁县| 襄垣县| 元朗区|