# Unity Shaders and Effects Cookbook (3-6) Create anisotropic high light type (Anisotropic) to simulate brushed metal effects

Source: Internet
Author: User
Tags pow sin

This time to learn the anisotropic high-light type, the name is really awkward, Anisotropic this English word is also very mouthful.

Anisotropy is a type of specular reflection that simulates the direction of a groove on an object, and he modifies or extends the high light in the vertical direction.

For example, the effect of metal wire drawing can be simulated using anisotropy.

Transfer from Http://blog.csdn.net/huutu http://www.thisisgame.com.cn

First, we need to prepare an anisotropic normal map, which represents the directivity of anisotropic specular highlights.

Note After the normal map is imported to unity, tick the type normal map in the properties panel.

First, add the corresponding attribute in the properties block

`Properties {_maintex ("Base (RGB)", 2D) = "White" {}_maintint ("Diffuse Tint", Color) = (1,1,1,1) _specularcolor ("Specular color, color) = (1,1,1,1) _specular ("Specular Amount", range (0,1)) =0.5_specularpower ("Specular Power", range (0,1)) = 0.5_anisodir ("Anisotropic Direction", 2D) = "" "{}_anisooffset (" Anisotropic Offset ", Range ( -1,1)) =-0.2}`

Then declare the corresponding variable for subshader use

`sampler2d _maintex;float4 _maintint;float4  _specularcolor;float _specular;float _SpecularPower;sampler2D _ Anisodir;float _anisooffset;`

The _anisodir is left to the normal map.

Because the data of the normal map needs to be read, the INPUT structure is modified, and the UV variables of the normal map are added.

`struct Input {float2 uv_maintex;float2 uv_anisodir;};`

The default surfaceoutput in Unity does not meet the needs of our shader, so we need to customize a

`struct SURFACEANISOOUTPUT{FIXED3 albedo;fixed3 normal;fixed3 emission;fixed3 anisodirection;half Specular;fixed Gloss ; fixed Alpha;};`

Then, in the Surf function, use Unpacknormal to read the data in the normal map. Passed through Surfaceanisooutput to the lighting model function

`void Surf (Input in, InOut surfaceanisooutput o) {half4 c = tex2d (_maintex, In.uv_maintex) * _MAINTINT;FLOAT3 Anisotex=un Packnormal (tex2d (_anisodir,in.uv_anisodir)); O. Anisodirection=anisotex;o. Specular=_specular;o. Gloss=_specularpower;o. Albedo = C.rgb;o. Alpha = C.A;}`

`Inline fixed4 lightinganisotropic (surfaceanisooutput s,fixed3 lightdir,half3 viewdir,fixed atten) {//Compute half-width vector fixed3 Halfvector=normalize (Normalize (Lightdir) +normalize (Viewdir));//the Cos value for calculating normals and illumination direction float ndotl=saturate (dot (s.normal, Lightdir)//fixed Hdota=dot (Normalize (s.normal + s.anisodirection), halfvector), float Aniso=max (0,sin (radians ( Hdota + _anisooffset) *180)); float spec=saturate (POW (aniso,s.gloss*128) * s.specular); Fixed4 c;c.rgb= ((S.albedo * _ LIGHTCOLOR0.RGB * ndotl)  + (_LIGHTCOLOR0.RGB * _SPECULARCOLOR.RGB * spec)) * (Atten * 2); C.a=1.0;return C;}`

After the Shader is written, create the Material, create the 3D gameobject in the scene, set the Material, and assign the normal map to Material, the final effect is as followstransfer from Http://blog.csdn.net/huutu http://www.thisisgame.com.cn

Implementation principle:

In the illumination model function, the half-width vector is first computed

`Calculates the half-width vector fixed3 halfvector=normalize (normalize (Lightdir) +normalize (Viewdir));`

The vertex normals are then summed with each pixel of the anisotropic normal map, then normalize, and then the dot product operation with the half-width vector.

`Fixed Hdota=dot (normalize (s.normal + s.anisodirection), halfvector);`

This gets the floating-point value hdota.

When Hdota ==1, the surface normal of the object is parallel to the halfvector.

When hdota==0, the surface normal of the object is perpendicular to the halfvector.

Then, use the sin () function to modify the value.

`Loat Aniso=max (0,sin (radians ((Hdota + _anisooffset) *180));`

Finally, the effect of the Aniso value is enlarged, the power is s.gloss to it, and then multiplied by the s.specular value to reduce its strength in the global range.

`Float spec=saturate (POW (aniso,s.gloss*128) * s.specular);`

The book mentions that you need to specify a shader using Shader Model 3.0 mode.

`#pragma target 3.0`

I do not specify the normal after testing is also possible.

Find some comparisons on the target on the wiki

2.0 or default

Compiles the shader under Shader Model 2. Model 2 have more limitations than 3 are more compatible. Uses Shader Model 1.1 for vertex shaders.

vertex:128 instruction limit. fragment:96 instruction Limit (texture + arithmetic), temporary registers and 4 texture indirections.

3.0

Compiles the shader under Shader Model 3. Model 3 is more powerful and flexible than 2 but is less compatible.

Vertex:no instruction limit.

fragment:1024 instruction Limit (texture + arithmetic), temporary registers and 4 texture indirections.

It is the possible to override these limits using #pragma profileoption directive.

For example, #pragma profileoption maxtexindirections=256 raises texture indirections limit to 256.

See #pragma profileoption for more information. Note that some shader model 3.0 features, like derivative instructions, aren ' t supported by vertex or fragment shaders.

You can use the #pragma glsl to translate to GLSL instead which have fewer restrictions. See #pragma GLSL for more information.

View related pages

`Http://www.ceeger.com/Components/SL-ShaderPrograms.htmlhttp://wiki.unity3d.com/index.php?title=Shader_Code`

`Http://pan.baidu.com/s/1eSK1rl4`

Unity Shaders and Effects Cookbook (3-6) Create an anisotropic high-light type (Anisotropic) to simulate brushed metal effects

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.