Unity3d shader to create metal anisotropy effect

Source: Internet
Author: User
Tags pow sin

There are two types of anisotropy, first cut a graph to see the effect
One is the radial anisotropy:



One is linear anisotropy:




This shader is similar to a metal drawing or a high light on the hair. (Linear anisotropy)
It can blend anisotropy and roughness based on the blue channel of the normal map.
Supports diffuse (diffuse), normal (normal), reflection (specular), gloss map (gloss), and transparent (transparency) Shader

Gloss and reflection values are also applied to anisotropic highlights.
The highlight can increase or decrease the offset value of the anisotropic highlight of the surface (offset).
The orientation of the anisotropic specular surface is defined by a directional map like the following.
The normal map effect is the same as the tangent space, and the highlight is defined on the surface. However, they should not convert normal maps in unity.
That is, these graphs do not have to be converted into normal maps (as I understand it).


Members:
anisotropy Direction : Is the direction of the surface highlight, and the tangent space of the normal map is the same direction.
Reflection : The reflection intensity is defined by the red channel of the map. Controls the brightness of specular highlights.
Gloss : The gloss intensity is defined by the green channel of the decal. The more green The reflection highlights the finer the sharper, the less green the reflection the higher the wider the more blurred the light. It is better to set this value to a value other than 0.
Anisotropic Matte : The blue channel of the map defines the mixture of anisotropy and Blinn highlights, the more blue the anisotropy, the greater the blue, the smaller the Blinn.
Anisotropy offset : Enables the anisotropy to be farther or nearer from the center point




Here are the stickers needed for this shader:
radial anisotropic effect:



linear anisotropic effects:



Establish a shader:

First look at the variables:

_specularcolor High-Gloss color
_specpower High light intensity
_specular the number of high-light anisotropy
_anisodir Anisotropic Orientation Map
_anisooffset Anisotropy Offset


Properties {_maintex ("Base (RGB)", 2D) = "White" {}_maintint ("Diffuse Tint", Color) = (1,1,1,1) _specularcolor ("Specular Color ", color) = (1,1,1,1)//Highlight color _specpower (" Specular Power ", Range (0,30)) = 2//Highlight Strength _specular (" Specular Amount ", Range (0 , 1)) = 0.5_anisodir ("Anisotropic Direction", 2D) = "" "{}//anisotropic direction normal map _anisooffset (" Anisotropic Offset ", Range ( -1,1)) =-0. 2//_anisooffset Action Offset}


Let the shader load the new function we defined is not the previous Lambert
#pragma surface surf Anisotropic

Tell the rendering mode using shader model3.0
#pragma target 3.0


To establish a new structure for surfaceanisooutput value
Albedo reflectivity of the light source
Normal Normal Direction
emission Spontaneous Light
anisodirection anisotropy Direction
Specular The coefficients of the exponential part in high-light reflection
Gloss intensity coefficients in specular reflectance
Alpha Transparency

struct SURFACEANISOOUTPUT{FIXED3 albedo;//reflectivity of the light source fixed3 normal;//Normal direction fixed3 emission;//spontaneous light fixed3 anisodirection;// The coefficients of the exponential portion of the half specular;//specular reflection in the anisotropic direction are fixed gloss;//the strength coefficient fixed alpha;//transparency in high-light reflection};


Important execution function lightinganisotropic

First, we find the half-width vector halfvector
To find the point product ndotl of the normal direction and illumination direction again
Half-angle vector and anisotropic direction dot product Hdota
To find out the anisotropy:
The Radian Value of sin ((half-width vector and anisotropy direction dot product + offset value) * 180)
Then the reflective strength spec is obtained according to the anisotropy
Final integration with illumination color, etc.

Inline Fixed4 lightinganisotropic (surfaceanisooutput s, fixed3 Lightdir, Half3 viewdir, fixed atten) {fixed3 Halfvector = Normalize (normalize (Lightdir) + normalize (viewdir)); the//normalize () function converts a vector into a unit vector, float ndotl = saturate (dot (s.normal, LIGHTDIR)), Fixed Hdota = dot (normalize (s.normal + s.anisodirection), halfvector), float aniso = max (0, sin (radians (hdota + _anisooffset) * 180f));//radians () function converts angle value to radians value float spec = saturate (Pow (aniso, S.gloss *) * s.specular);//saturate (x) function if x is less than 0 returns 0, if x is greater than 1 returns 1; X is returned; limit x to 0-1fixed4 C;c.rgb = ((S.albedo * _LIGHTCOLOR0.RGB * ndotl) + (_LIGHTCOLOR0.RGB * _spe) CULARCOLOR.RGB * Spec) * (Atten * 2); c.a = 1.0;return c;}



To accept anisotropic normal maps in the Surf function
Anisotropic direction of conversion to output
And then it's OK.


<span style= "FONT-SIZE:14PX;" >void Surf (Input in, InOut surfaceanisooutput o) {half4 c = tex2d (_maintex, In.uv_maintex) * _MAINTINT;FLOAT3 anisote x = Unpacknormal (tex2d (_anisodir, In.uv_anisodir)), O. Anisodirection = Anisotex;o. Specular = _specular;o. Gloss = _specpower;o. Albedo = C.rgb;o. Alpha = C.A;} </span>


This is the radial effect:






This is the linear effect:








The full code is as follows:

Shader "Custom/textshader" {Properties {_maintex ("Base (RGB)", 2D) = "White" {}_maintint ("Diffuse Tint", Color) = (1,1,1 , 1) _specularcolor ("Specular color", color) = (1,1,1,1)//Highlight color _specpower ("Specular Power", Range (0,30)) = 2//Highlight Intensity _ Specular ("Specular Amount", Range (0, 1)) = 0.5_anisodir ("Anisotropic Direction", 2D) = "" "{}//anisotropic directional normal map _anisooffset (" an Isotropic offset ", Range ( -1,1)) = -0.2//_anisooffset Action offset}subshader {Tags {" rendertype "=" Opaque "}lod 200cgprogram# pragma surface surf anisotropic#pragma target 3.0sampler2d _maintex;sampler2d _anisodir;//anisotropic float4 _maintint;float4 _specularcolor;float _anisooffset;float _specular;float _specpower;struct surfaceanisooutput{fixed3 Albedo;// The reflectivity of the light source fixed3 normal;//Normal direction fixed3 emission;//spontaneous light fixed3 anisodirection;//anisotropy direction half specular;//the coefficients of the exponential portion of the specular reflection are fixed gloss;//intensity coefficient in specular reflectance fixed alpha;//transparency};inline fixed4 lightinganisotropic (surfaceanisooutput s, fixed3 Lightdir, Half3 Viewdir, fixed atten) {fixed3 Halfvector = normalize (normalize (lightdiR) + normalize (Viewdir)),//normalize () function converts the vector to the unit vector float ndotl = saturate (dot (s.normal, lightdir)); Fixed Hdota = dot ( Normalize (S.normal + s.anisodirection), halfvector); float aniso = max (0, sin (radians (Hdota + _anisooffset) * 180f));//r The Adians () function converts the angle value to a radian value of float spec = saturate (Pow (aniso, S.gloss *) * s.specular),//saturate (x) function if x is less than 0 returns 0, if x is greater than 1 returns 1; Otherwise return x, limit x to 0-1fixed4 C;c.rgb = ((S.albedo * _LIGHTCOLOR0.RGB * ndotl) + (_LIGHTCOLOR0.RGB * _SPECULARCOLOR.RGB * spec)) * (Atten * 2); c.a = 1.0;return c;} struct Input {float2 uv_maintex;float2 uv_anisodir;}; void Surf (Input in, InOut surfaceanisooutput o) {half4 c = tex2d (_maintex, In.uv_maintex) * _MAINTINT;FLOAT3 Anisotex = Unpacknormal (tex2d (_anisodir, In.uv_anisodir)); Anisodirection = Anisotex;o. Specular = _specular;o. Gloss = _specpower;o. Albedo = C.rgb;o. Alpha = C.A;} ENDCG} FallBack "Diffuse"}






---------by wolf96

Unity3d shader to create metal anisotropy effect

Contact Us

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.