Unity3d game development from zero single row (10)-Attack on shader cont.

Source: Internet
Author: User
Tags diff

Custom Lighting Models

Before that, let's look at the structure of Surfaceoutput, which is a structure that contains most of the rendering features of an object's surface, with the following structure:

struct Surfaceoutput {half3 albedo;//texture color half3 normal;//normal half3 emission;//self-illuminated, unaffected by illumination half specular;//high light index half Gloss ;//Gloss Half Alpha;//alpha channel};

Basically all of the shader functions have to deal with this structure.


Unity's own lighting implementation is defined in some *.cginc files, to customize the lighting model, as long as the unity comes with a light model.

Replace the end of the statement in the following line with the corresponding light calculation function.

#pragma surface surf nolight


Non-illuminated material shader

Shader "Custom/nolight" {Properties {_maintex ("Base (RGB)", 2D) = "White" {}}subshader {Tags {"rendertype" = "Opaque"}lod 200cgprogram#pragma Surface surf nolightsampler2d _maintex;inline float4 lightingnolight (surfaceoutput s, fixed3 Lightdir, Fixed3 atten) {float4 Col;col.rgb = S.ALBEDO;COL.A = S.alpha;return col;} struct Input {float2 uv_maintex;}; void Surf (Input in, InOut surfaceoutput o) {half4 c = tex2d (_maintex, In.uv_maintex); O. Albedo = C.rgb;o. Alpha = C.A;} ENDCG} FallBack "Diffuse"}


Implement a diffuse yourself

Inline Float4 Lightingbasicdiffuse (surfaceoutput s, fixed3 lightdir, fixed atten) {float diflight = max (0, Dot (s . Normal, Lightdir));  float4 Col;  Col.rgb = S.albedo * _lightcolor0.rgb * (diflight * atten * 2);  col.a = S.alpha;  return col;}



On the left is nolight, and the right is simplediffuse.


The same can be customized to achieve a variety of lighting models, lambert,blinning,,,,,


Some ways to add light functions

Half4 Lightingname (surfaceoutput s, half3 lightdir,half atten) {}

This function is used for forward rendering that do not require a perspective direction.

Half4 Lightingname (surfaceoutput s, half3 Lightdir, Half3 viewdir, half atten) {}

This function is used for forward rendering where the direction of view is required.

Half4 Lightingname_prepass (surfaceoutput s, Half4 light) {}

This function is used for deferred rendering.


Controlling shader dynamically modifying materials with code

Make a slight change to simplediffuse and add a color to the overlay.

Shader "Custom/simplediffuse" {Properties {_maintex ("Base (RGB)", 2D) = "White" {}_color ("Main Color", Color) = (0,1,0,1 )}subshader {Tags {"rendertype" = "Opaque"}lod 200cgprogram#pragma surface surf lambertsampler2d _maintex;float4 _color; struct Input {float2 uv_maintex;}; void Surf (Input in, InOut surfaceoutput o) {half4 c = tex2d (_maintex, In.uv_maintex); O. Albedo = C.rgb * _color.rgb;o. Alpha = C.A;} ENDCG} FallBack "Diffuse"}

Hang a script above the model (with Meshrenderer) to implement the following

Meshchanger

Using unityengine;using System.collections;public class Materialchanger:monobehaviour {float time = 0.0f;float changeSp eed1 = 2.0f;float ChangeSpeed2 = 5.0f; Renderer render;//Use this for initializationvoid Start () {render = transform. Getcomponent<renderer> ();} Update is called once per framevoid Update () {Float V1 = (Mathf.cos (time * changeSpeed1) + 1.0f) * 0.5f;float v2 = (Ma Thf. Sin (Time * changeSpeed2) + 1.0f) * 0.5f;render.materials[0]. SetColor ("_color", New Color (v1, v2, v2)); time + = Time.deltatime;}}

Modify the _color option of the No. 0 material according to the time, the result is like this





The middle process of self-brain repair.


Using gradient textures to handle lighting

The first step is to prepare a gradient texture, the principle is to calculate the current position of the light and normal dot product, indexed to the gradient image of the pixel value, and finally superimposed it and diffuse.


Shader "Custom/ramptexture" {Properties {_maintex ("Base (RGB)", 2D) = "White" {}_ramptex ("Ramp Tex (RGB)", 2D) = "White" {}}subshader {Tags {"rendertype" = "Opaque"}lod 200cgprogram#pragma surface Surf ramplightsampler2d _maintex;sampler2d _ Ramptex;half4 Lightingramplight (surfaceoutput s, Half3 lightdir, half atten) {half ndotl = dot (s.normal, lightDir); float diff = Ndotl * 0.5 + 0.5;half3 ramp = tex2d (_ramptex, Float2 (diff, diff)). Rgb;half4 C;c.rgb = S.albedo * _lightcolor0.rgb * Ramp * (Atten * 2); 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); O. Albedo = C.rgb;o. Alpha = C.A;} ENDCG} FallBack "Diffuse"}

Rendering results



Absence of textures (elephants are not piglets)




Change the index map

The results are as follows (Toon shading)



Reference

Unity Shaders and Effects CookBook


Unity3d game development from zero single row (10)-Attack on shader cont.

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.