Unity shader--writing Surface Shaders (1)--surface Shader Examples

Source: Internet
Author: User

Here are some examples of surface shader. The following examples focus on using built-in lighting models, and for examples of how to use custom lighting models, see surface Shader Lighting Examples.

Simple

We will start with a very simple shader, and gradually improve on this basis. The following shader will place the surface color "white". It uses the built-in Lambert (diffuse) illumination model.

Shader"Example/diffuse Simple"{subshader {Tags {"Rendertype"="Opaque"} cgprogram#pragmaSurface Surf LambertstructInput {float4 color:color;      }; voidSurf (Input in, InOut surfaceoutput o) {O.albedo=1; } ENDCG} Fallback"Diffuse"  }

Here's what it's like to use two lights to act on a model:

Texture

An all-white object is too boring, so let's add a texture. We will add a Properties block to the shader so that we can get a texture selector in the material. The following bold changes are other:

Shader"Example/diffuse Texture"{Properties {_maintex ("Texture", 2D) =" White"{}} subshader {Tags {"Rendertype"="Opaque"} cgprogram #pragma Surface Surf LambertstructInput {float2 Uv_maintex;      };      Sampler2d _maintex; voidSurf (Input in, InOut surfaceoutput o) {O.albedo=tex2d (_maintex, In.uv_maintex). RGB; } ENDCG} Fallback"Diffuse"  }

Normal map

Let's add some discovery stickers:

Shader"Example/diffuse Bump"{Properties {_maintex ("Texture", 2D) =" White"{} _bumpmap ("BumpMap", 2D) ="Bump"{}} subshader {Tags {"Rendertype"="Opaque"} cgprogram#pragmaSurface Surf LambertstructInput {float2 Uv_maintex;      FLOAT2 Uv_bumpmap;      };      Sampler2d _maintex;      Sampler2d _bumpmap; voidSurf (Input in, InOut surfaceoutput o) {O.albedo=tex2d (_maintex, In.uv_maintex). RGB; O.normal=Unpacknormal (tex2d (_bumpmap, In.uv_bumpmap)); } ENDCG} Fallback"Diffuse"  }

Edge Illumination

Now try adding some edge lighting (Rim Lighting) to highlight the edges of an object. We will add some scattered light based on the angle between the surface normals and the line of sight. To do this, we'll use the built-in surface shader variablesviewDir。

Shader"Example/rim"{Properties {_maintex ("Texture", 2D) =" White"{} _bumpmap ("BumpMap", 2D) ="Bump"{} _rimcolor ("Rim Color", Color) = (0.26,0.19,0.16,0.0) _rimpower ("Rim Power", Range (0.5,8.0)) =3.0} subshader {Tags {"Rendertype"="Opaque"} cgprogram#pragmaSurface Surf LambertstructInput {float2 Uv_maintex;          FLOAT2 Uv_bumpmap;      FLOAT3 Viewdir;      };      Sampler2d _maintex;      Sampler2d _bumpmap;      FLOAT4 _rimcolor; float_rimpower; voidSurf (Input in, InOut surfaceoutput o) {O.albedo=tex2d (_maintex, In.uv_maintex). RGB; O.normal=Unpacknormal (tex2d (_bumpmap, In.uv_bumpmap)); Half Rim=1.0-saturate (dot (normalize (in.viewdir), o.normal)); O.emission= _rimcolor.rgb *pow (rim, _rimpower); } ENDCG} Fallback"Diffuse"  }

Detail Texture

To have a different effect, let's add a texture that combines the textures with the base texture. Detail textures Use the same uvs in the material, but often use different tiling. So we have to use different UV coordinates as input.

Shader"Example/detail"{Properties {_maintex ("Texture", 2D) =" White"{} _bumpmap ("BumpMap", 2D) ="Bump"{} _detail ("Detail", 2D) ="Gray"{}} subshader {Tags {"Rendertype"="Opaque"} cgprogram#pragmaSurface Surf LambertstructInput {float2 Uv_maintex;          FLOAT2 Uv_bumpmap;      FLOAT2 Uv_detail;      };      Sampler2d _maintex;      Sampler2d _bumpmap;      Sampler2d _detail; voidSurf (Input in, InOut surfaceoutput o) {O.albedo=tex2d (_maintex, In.uv_maintex). RGB; O.albedo*= tex2d (_detail, In.uv_detail). RGB *2; O.normal=Unpacknormal (tex2d (_bumpmap, In.uv_bumpmap)); } ENDCG} Fallback"Diffuse"  }

Using a checkerboard texture does not make much sense, but it shows the effect:

Unity shader--writing Surface Shaders (1)--surface Shader Examples

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.