Unity3d Shaderlab using Blinnphong high light type

Source: Internet
Author: User
Tags diff pow

Unity3d Shaderlab using blinnphong high Light type

In the previous article we implemented a custom highlight type, this one, we say Blinn High Light, which is another way to calculate and estimate the high light, which is done by the half-width vector formed by the line of sight and the direction of light.

This is more efficient than forming a reflection vector for our own calculation. the Blinnphong illumination model built into the unitycg.cginc file is the half-width vector.

First, create a shader, a material ball, double-click shader, and open the editor.

1:Properties

Properties {

_maintex ("Base (RGB)", 2D) = "White" {}

_maintint ("Diffuse Tint", Color) = (1,1,1,1)

_specularcolor ("Specular color", color) = (1,1,1,1)

_specularpower ("Specular Power", Range (0.1,30)) =1

}

2:subshader declaration variable

Cgprogram

#pragma surface surf Myblinnphong

Sampler2d _maintex;

FLOAT4 _specularcolor;

FLOAT4 _maintint;

float _specularpower;

3:lightingmyblinnphong Illumination Model implementation

Inline fixed4 lightingmyblinnphong (surfaceoutput s,fixed3 lightdir,half3 viewdir, fixed atten) {

FLOAT3 Halfvector = normalize (Lightdir+viewdir);

float diff = max (0,dot (S.normal, Lightdir));

Float NH = max (0,dot (S.normal, halfvector));

FLOAT spec = POW (nh,_specularpower) *_specularcolor;

FLOAT4 C;

C.rgb = (S.albedo*_lightcolor0.rgb*diff) + (_lightcolor0.rgb*_specularcolor.rgb*spec) * (atten*2);

C.A = S.alpha;

return C;

}

4:surf function

Half4 C = tex2d (_maintex, In.uv_maintex) *_maintint;

The final effect is as follows:

The leftmost is the Blinnphong, the rightmost is the custom Phong, the middle is the most basic highlight effect.

The Blinnphong high-light model and the Phong High-gloss model are almost identical, except that the former uses less code more efficiently, and their effect is almost identical.

In the Lightingmyblinnphong illumination model above, we obtained the half-width vector by normalize in order to obtain the half-angle vector, the line of sight direction and the incident direction superposition .

we then simply multiply the vertex normals and the new half-width vectors to obtain our high-light values and thenSpecularpowermultiply the highlight color value after the power of the second partySpecularcolor

Although the process is simplified, it also gives us excellent high-gloss effects.

Code Start--------------------------------------------------------------------------------

Shader "91ygame/blinnphong" {
Properties {
_maintex ("Base (RGB)", 2D) = "White" {}
_maintint ("Diffuse Tint", Color) = (1,1,1,1)
_specularcolor ("Specular color", color) = (1,1,1,1)
_specularpower ("Specular Power", Range (0.1,30)) =1
}
Subshader {
Tags {"Rendertype" = "Opaque"}
LOD 200

Cgprogram
#pragma surface surf Myblinnphong

Sampler2d _maintex;
FLOAT4 _specularcolor;
FLOAT4 _maintint;
float _specularpower;

struct Input {
FLOAT2 Uv_maintex;
};

Inline fixed4 lightingmyblinnphong (surfaceoutput s,fixed3 lightdir,half3 viewdir, fixed atten) {
FLOAT3 Halfvector = normalize (Lightdir+viewdir);
float diff = max (0,dot (S.normal, Lightdir));
Float NH = max (0,dot (S.normal, halfvector));
FLOAT spec = POW (nh,_specularpower) *_specularcolor;

FLOAT4 C;
C.rgb = (S.albedo*_lightcolor0.rgb*diff) + (_lightcolor0.rgb*_specularcolor.rgb*spec) * (atten*2);
C.A = S.alpha;
return C;
}

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"
}

Code End--------------------------------------------------------------------------------

Unity3d Shaderlab using Blinnphong high light type

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.