Unity3d shader's soft high-gloss metal effect

Source: Internet
Author: User
Tags cos pow

Soft High-gloss metal effect

Learn the book in this chapter has a lot of knowledge I also very do not understand, so looked up some information, gradually clear some,, a combination of the knowledge in the book and on-line information and my understanding of the Ming


The main principle is cook-torrance Illumination model algorithm, is a BRDF (bidirectional reflection distribution function), a specific algorithm, see the following


Create a new shader

Let's go through the variables.
_maintint main tone
_roughnesstex Roughness Map (controls highlight size)
_roughness Surface Roughness Degree
_specularcolor High-Gloss color
_specpower High light intensity

_fresnel Fresnel value (reflection coefficient)


Properties {_maintint ("Diffuse Tint", Color) = (1,1,1,1) _maintex ("Base (RGB)", 2D) = "White" {}_roughnesstex ("roughness Texture ", 2D) =" "{}_roughness (" roughness ", Range (0,1)) = 0.5//Surface roughness map _specularcolor (" Specular Color ", Color) = (1, 1, //Highlight Color _specpower ("Specular Power", Range (0,30)) = 2//Highlight intensity _fresnel ("Fresnel Value", Range (0,1.0)) = 0.05}



The greater the roughness reflects the more light, the greater the range of highlights, like diffuse reflection is too rough


Fresnel Fresnel Value:

The Fresnel effect is achieved by adjusting the reflectivity based on the observed surface of the viewer.
For example, you from the surface of the water, paint the surface or the silk is directly above the reflection gloss soft effect is basically not, if the side or flat look, the soft effect of reflection gloss is very obvious, this is the Fresnel effect.
We simply calculate the cosine of the angle between the normal and the line of sight by the dot product operation and then weighting the value.
For smoother surfaces, the weighting coefficients are set smaller between (paint effect, silk, etc.), and for more bump surfaces, the weighting coefficients are set to higher (water waves, liquids, etc.).



Algorithmic aspects:


Mainly implemented in the Lightingmetallicsoft () function.

Inline Fixed4 Lightingmetallicsoft (surfaceoutput s, fixed3 lightdir,half3 viewdir, fixed atten) {FLOAT3 Halfvector = Norma Lize (lightdir+ viewdir);//normalize converted to unit vector float Ndotl = saturate (dot (s.normal, normalize (Lightdir)));// The dot product of the incident light and the surface normal vector is treated as a diffuse illumination intensity factor float Ndoth_raw = dot (s.normal, halfvector);//Two The dot product of a unit vector gets the COS value of the angle of two vectors float Ndoth = saturate (/ *ndoth_raw*/dot (S.normal, Halfvector));//If x is less than 0 returns 0, if x is greater than 1 returns 1, otherwise x is returned, the X is limited to 0-1float Ndotv = saturate (dot (s.normal, Normalize (Viewdir)); float Vdoth = saturate (dot (halfvector, normalize (Viewdir))), Float geoenum = 2.0 * NDOTH;FLOAT3 G1 = ( Geoenum * Ndotv)/NDOTH;FLOAT3 G2 = (Geoenum * ndotl)/Ndoth;float3 G = min (1.0f, min (G1, G2));//take small float roughness = t Ex2d (_roughnesstex, FLOAT2 (Ndoth_raw * 0.5 + 0.5, _roughness)/*uv*/). R;float Fresnel = POW (1.0-vdoth, 5.0);//pow () function: Fresnel *= (1.0-_fresnel) for X's Y-order (Power), Fresnel + = _FRESNEL;FLOAT3 spec = FLOAT3 (Fresnel * G * roughness * roughness) * _spe Cpower;float4 C;c.rgb = (S.albedo * _LIGHTCOLOR0.RGB * ndotl) + (SPEC * _specularcolor.rgb) * (Atten * 2.0f); The color of the parallel light in the//_lightcolor0 scene c.a = S.alpha;return C;} 



At first glance may be a little dizzying, look at me slowly ... = =



Parameters:
Lightdir Illumination Direction
Viewdir the perspective direction of the current coordinates
Attenuation value of atten illumination

dot () Find dot Product
Two the dot product of a unit vector gets the COS value of the angle of two vectors


The dot product of incident light and surface normal vector as a diffuse illumination intensity factor

Ndotl = saturate (dot (s.normal, normalize (Lightdir)));
float Ndoth_raw = dot (s.normal, halfvector);
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-1

float Ndoth = saturate (dot (s.normal, halfvector));
float Ndotv = saturate (dot (s.normal, normalize (Viewdir)));
float Vdoth = saturate (dot (halfvector, normalize (Viewdir)));

Above, the angle cos value of all directions is obtained.


Checked the wiki.

Http://en.wikipedia.org/wiki/Specular_highlight#Cook.E2.80.93Torrance_model
Translate the main part

The cook–torrance illumination model uses a form of reflection (specular)


V Viewdir means the world Space View Direction. Is the perspective direction of the current coordinates.
N normal normal direction vector
L Light Direction vector
H half-width vector used to calculate the intermediate direction vector (halfway vector) of specular reflection (specular reflection)
E Camera/human eye observation direction vector
G geometric attenuation coefficients describe the self-projection due to the micro-plane (microfacets)
F Fresnel value (reflection coefficient)
D micro-plane distribution function (Beckmann distribution factor)

The shader algorithm is slightly different from the wiki,
But the method of G is exactly the same:

For the wiki, find the method:


here is the code :

<pre name= "code" class= "cpp" >float geoenum = 2.0 * NDOTH;FLOAT3 G1 = (Geoenum * ndotv)/NDOTH;FLOAT3 G2 = (geoenum * ndotl)/NDOTH;FLOAT3 G =  min (1.0f, min (G1, G2));//Take Small

The method is exactly the same.

coarse values are read from the R channel of the Rough map roughness = tex2d (_roughnesstex, FLOAT2 (Ndoth_raw * 0.5 + 0.5, _roughness)/*uv*/). R;

To find the Fresnel value (reflection coefficient)

Pow (x , y) function: Find x's y-square (Power)

Fresnel = POW (1.0-vdoth, 5.0); Fresnel *= (1.0-_fresnel); Fresnel + = _fresnel; the ratio of high light to the whole object = Fresnel value (reflection coefficient) * geometrical attenuation coefficient * The square of the rough value again with externally controllable specpower high light intensity do product spec = FLOAT3 (Fresnel * G * roughness * roughness) * _specpower;

The final reflection color _lightcolor0 is the color of the parallel light in the scene atten is the attenuation value of the light C.rgb = (S.albedo * _LIGHTCOLOR0.RGB * ndotl) + (spec * _specularcolor. RGB) * (Atten * 2.0f);

Three textures with different roughness are given below:

Roughness is reduced from top to bottom

Finally, a result is achieved:

The rightmost is the most coarse and bright range, and the roughness on the left is reduced in turn.

The metallic effect of the capsule body is not very obvious (soft light is more obvious), you can use different models to try

The full code is as follows:

Shader "Custom/testshader" {Properties {_maintint ("Diffuse Tint", Color) = (1,1,1,1) _maintex ("Base (RGB)", 2D) = "White" {}_roughnesstex ("roughness texture", 2D) = "" "{}_roughness (" roughness ", Range (0,1)) = 0.5//Surface roughness map _specularcolor (" Spe Cular color, color) = (1,1,1,1)//Highlight color _specpower ("Specular Power", Range (0,30)) = 2//Highlight Strength _fresnel ("Fresnel Value", Range (0,1.0)) = 0.05}subshader {Tags {"rendertype" = "Opaque"}lod 200cgprogram#pragma surface surf Metallicsoft#pragma Target 3 .0//Soft Metal sampler2d _maintex;sampler2d _roughnesstex;//surface roughness map float _roughness;//surface roughness value float _fresnel;float _specpower ; Float4 _maintint; FLOAT4 _specularcolor;//viewdir means the world Space View Direction. Is the perspective direction of the current coordinate//n normal//v view//l light//h half-width vector used to calculate the intermediate direction vector (specular vector) of specular reflection (reflection halfway) Inlin E fixed4 Lightingmetallicsoft (surfaceoutput s, fixed3 lightdir,half3 viewdir, fixed atten) {FLOAT3 Halfvector = normalize ( lightdir+ viewdir)//normalize convert to unit vector float Ndotl = saturate (dot (s.normal, normalize (Lightdir)));//The dot product of incident light and surface normal vector as diffuse illumination intensity factor float Ndoth_raw = dot (s.normal, halfvector);// The dot product of two unit vectors gets the Cos value of the angle of two vectors float Ndoth = saturate (/*ndoth_raw*/dot (S.normal, Halfvector));//If x is less than 0 returns 0, if x is greater than 1 returns 1; Limit x to 0-1float Ndotv = saturate (dot (s.normal, normalize (Viewdir))), Float Vdoth = saturate (dot (halfvector, normalize ( Viewdir)); float geoenum = 2.0 * NDOTH;FLOAT3 G1 = (Geoenum * ndotv)/NDOTH;FLOAT3 G2 = (Geoenum * ndotl)/NDOTH;FLOAT3 G = min (1.0f, min (G1, G2));//take small float roughness = tex2d (_roughnesstex, FLOAT2 (Ndoth_raw * 0.5 + 0.5, _roughness)/*uv*/). R float Fresnel = POW (1.0-vdoth, 5.0),//pow () function: X's Y (Power) Fresnel *= (1.0-_fresnel), Fresnel + = _FRESNEL;FLOAT3 Spec = Flo AT3 (Fresnel * G * roughness * roughness) * _SPECPOWER;FLOAT4 C;c.rgb = (S.albedo * _LIGHTCOLOR0.RGB * ndotl) + (spec * _sp ECULARCOLOR.RGB) * (Atten * 2.0f); The color of the parallel light in the//_lightcolor0 scene c.a = S.alpha;return C;} struct Input {float2 uv_maintex;}; void Surf (Input in, InOut surfaceoutput o) {half4 c = tex2d (_maintex, In.uv_maintex) * _maintint;o. Albedo = C.rgb;o. Alpha = C.A;} ENDCG} FallBack "Diffuse"}





-------by wolf96







Unity3d shader's soft high-gloss metal 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.