- Unity 2018 Shaders and Effects Cookbook
- John P. Doran Alan Zucconi
- 297字
- 2021-06-18 19:04:21
There's more...
You can also add some controls to your normal map shader that lets a user adjust the intensity of the normal map. This is easily done by modifying the x and y components of the normal map variable and then adding it all back together. Add another property to the Properties block and name it _NormalMapIntensity:
_NormalMapIntensity("Normal intensity", Range(0,3)) = 1
In this case, we are giving the property the ability to be between 0 and 3 with a default value of 1. Once created, you'll need to add the variable inside the SubShader:
// Link the property to the CG program
sampler2D _NormalTex;
float4 _MainTint;
float _NormalMapIntensity;
After the property is added, we can make use of it. Multiply the x and y components of the unpacked normal map and reapply this value to the normal map variable with the bolded changes:
void surf (Input IN, inout SurfaceOutputStandard o) {
// Use the tint provided as the base color for the material
o.Albedo = _MainTint;
// Get the normal data out of the normal map texture
// using the UnpackNormal function
float3 normalMap = UnpackNormal(tex2D(_NormalTex,
IN.uv_NormalTex));
normalMap.x *= _NormalMapIntensity;
normalMap.y *= _NormalMapIntensity;
// Apply the new normal to the lighting model
o.Normal = normalize(normalMap.rgb);
}
Normal vectors are supposed to have lengths equal to one. Multiplying them for _NormalMapIntensity changes their length, making normalization necessary. The normalize function will take the vector and adjust it so that it is pointing in the correct direction but with a length of one, which is what we are looking for.
Now, you can let a user adjust the intensity of the normal map in the material's Inspector tab, as follows:

The following screenshot shows the result of modifying the normal map with our scalar values:

- 多媒體CAI課件設計與制作導論(第二版)
- 摩登創(chuàng)客:與智能手機和平板電腦共舞
- 零基礎入門學習Python
- C語言程序設計
- Learning Continuous Integration with TeamCity
- 新一代SDN:VMware NSX 網(wǎng)絡原理與實踐
- RESTful Java Web Services(Second Edition)
- LabVIEW虛擬儀器入門與測控應用100例
- Python爬蟲、數(shù)據(jù)分析與可視化:工具詳解與案例實戰(zhàn)
- Java Web從入門到精通(第2版)
- 邊玩邊學Scratch3.0少兒趣味編程
- Java Hibernate Cookbook
- Microsoft HoloLens By Example
- 和孩子一起學編程:用Scratch玩Minecraft我的世界
- 基于JavaScript的WebGIS開發(fā)