- Unity 2018 Shaders and Effects Cookbook
- John P. Doran Alan Zucconi
- 231字
- 2021-06-18 19:04:21
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 | Data| CGIncludes 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.
- Cocos2D-X權威指南(第2版)
- Software Testing using Visual Studio 2012
- Quarkus實踐指南:構建新一代的Kubernetes原生Java微服務
- AutoCAD VBA參數化繪圖程序開發與實戰編碼
- Visual C++應用開發
- MongoDB權威指南(第3版)
- Java EE 8 Application Development
- Swift細致入門與最佳實踐
- Visual FoxPro程序設計習題集及實驗指導(第四版)
- HTML5+CSS3 Web前端開發技術(第2版)
- Solr Cookbook(Third Edition)
- Java高并發核心編程(卷1):NIO、Netty、Redis、ZooKeeper
- Scratch從入門到精通
- 30天學通C#項目案例開發
- 程序員的成長課