Unity3d Shaderlab Creating custom high-light types

Source: Internet
Author: User
Tags diff pow

Unity3d Shaderlab Creating custom high-light types

In the previous article, we learned about the Unity -based high-light implementation, which is mainly about how to perform vertex operations on the Phong Highlight type and use Input in a surface shader the new parameters of the struct are manipulated in pixels.

So create a new Shader, build a material ball, and then open the editor for Shader editing.

1. Modify 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,30)) =1

}

3. Modify Cgprogram

Cgprogram

#pragma surface surf Phong

Sampler2d _maintex;

FLOAT4 _specularcolor;

FLOAT4 _maintint;

float _specularpower;

4. Writing a custom lighting model Lightingphong

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

Float Diff=dot (s.normal,lightdir);

FLOAT3 reflection=normalize (2.0*s.normal*diff-lightdir);

FLOAT spec = POW (max (0,dot (Reflection,viewdir)), _specularpower);

FLOAT3 Finalspec = _specularcolor.rgb*spec;

Fixed4 C;

c.rgb= (S.albedo*_lightcolor0.rgb*diff) + (_LIGHTCOLOR0.RGB*FINALSPEC);

c.a=1.0;

return C;

}

5. Modify the surf function

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

With the above steps, we complete the custom specular rendering lighting model. The procedure is no different from the previous one. But the lightingphong function We used this time is 4 parameters, the secret of which is to introduce:

As I said before: The light model is a number of normal functions whose names start with the light (Lighting) . They can be declared anywhere in your shader file or in a contained file. The functions are:

Half4 Lighting+name (surfaceoutput s, Half3 lightdir, half atten); It is used in a forward rendering path for a light model that is not related to the direction of sight (for example, diffuse).

Half4 Lighting+name (surfaceoutput s, half3 Lightdir, Half3 viewdir, half atten); It is used in the forward rendering path for the lighting model that is related to the line of sight direction.

Half4 Lighting+name_prepass (surfaceoutput s, Half4 light); It is used in the delay light path.

Note that you do not have to declare all functions. A lighting model either uses a line of sight or is not used. Similarly, if the lighting model is not valid in delayed illumination, you do not have to declare the _prepass function, and then all shaders that use it will compile only forward rendering.

Api See :

Http://www.unitymanual.com/m/Components/SL-SurfaceShaderLighting.html

In the above we create a high-light shader, so we need to choose the viewpoint / perspective-related light function structure. So our function is:

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

This tells the shader that we are building a viewpoint-related lighting model.

Next, the light function calculates the vertex normals and the incident direction of the light first, and we get a return value of 1 objects that are in the direction of the light source, and1 of the surface objects to the direction of the light.

Then we calculate the reflection vector, first scaling the vector of the vertex normals, multiplying the value by 2.0 and multiplying by diff, resulting in a value minus the direction vector value of the light, which enables the normals to bend toward the light source.

So as a normal vector away from the light source, it will be forced toward the direction of the light source.

Go down.

We create the spec value and the color value, in order to obtain these 2 values, we carry on the point multiplication to the reflection vector and the angle of view direction, then specpower the result to the second side, Finally , multiply the specularcolor.rgb by the spec to get our high light value.

The final effect is as follows:

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

Shader "91ygame/phong2" {
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,30)) =1
}
Subshader {
Tags {"Rendertype" = "Opaque"}
LOD 200

Cgprogram
#pragma surface surf Phong

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

struct Input {
FLOAT2 Uv_maintex;
};

Inline fixed4 lightingphong (surfaceoutput s,fixed3 lightdir,half3 viewdir,fixed atten) {
Float Diff=dot (s.normal,lightdir);
FLOAT3 reflection=normalize (2.0*s.normal*diff-lightdir);
FLOAT spec = POW (max (0,dot (Reflection,viewdir)), _specularpower);
FLOAT3 Finalspec = _specularcolor.rgb*spec;
Fixed4 C;
c.rgb= (S.albedo*_lightcolor0.rgb*diff) + (_LIGHTCOLOR0.RGB*FINALSPEC);
c.a=1.0;
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 Creating custom high-light types

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.