Unity Shader's basic lighting

Source: Internet
Author: User
Tags reflection

How the camera looks at the world

The world that the camera sees in the game is almost the same as what we see in reality.

    • First, the light is emitted from the light source.
    • Then, the light intersects with some objects in the scene (scattering, absorbing).
    • Finally, the camera absorbs some light and produces an image.

There are two results of the intersection of light and objects: scattering (scattering) and absorption (absorption)

    • Scattering: Changes the direction of light only, but does not change the light's density and color, there are two directions: internal and external, corresponding to refraction and reflection.
      • Refraction (refraction): Scattered inside the object, calculated using a diffuse (diffuse) model.
      • Reflection (Reflection): Outside of the scattering object, calculated using a specular reflection (specular) model.
    • Absorption: Only changes the density and color of the light, but does not change the direction of the light.

Different illumination models are used to calculate two different scattering directions: diffuse model and specular reflection model.

    • Diffuse reflection: Indicates how much light is refracted, absorbed, and scattered out of the surface.
    • Specular reflection: Indicates how the surface of an object reflects light.
Standard illumination Model

The light entering into the camera is divided into 4 parts, and each part uses a method to calculate its contribution.

    • Self-Illumination (emissive): How much radiation a surface itself emits when given a direction. (does not illuminate the surrounding object, just appears to be bright)
    • Specular reflection (Specular): The amount of radiation that the surface scatters in the full specular direction when the light shines from the light source to the model surface.
    • Diffuse reflection (diffuse): The amount of radiation that the surface scatters in each direction when light shines from the light source to the model surface.
    • Ambient light (Ambient): Describes all other indirect lighting.
Self-luminous

The spontaneous light color of the material is used directly.

High Light Reflection

Phong Model Formula

Specular = (Light shininess) max (0, v R) ^gloss

      • Light: Source Color
      • Shininess: Inverse luminosity
      • v: Vector of object to camera direction
      • R: The reflection direction vector of the light, which can be calculated using the normal direction and the incident direction of the light, the equation is:r = 2 (n l)n - l)
      • Gloss: Gloss (The larger the number, the smaller the highlight)

Blinn Model Formula

Specular = (Light shininess) max (0, v H) ^gloss

      • Light: Source Color
      • Shininess: Inverse luminosity
      • v: Vector of object to camera direction
      • h: After averaging the v and l , then normalization, formula:h = v + l /| v + L |
      • Gloss: Gloss (The larger the number, the smaller the highlight)
Diffuse reflection

Lambert's Law

Diffuse = (Light diffusecolor) max (0, n l)

      • Light: Source Color
      • Diffusecolor: Material Diffuse color
      • n: Surface normal direction
      • l: unit vector for light source

code example (ambient light + diffuse + specular)

Shader"Unity My shader/diffuse Light"{Properties {_color ("Color", Color) = (1,1,1,1) _specular ("Specular", Color) = (1,1,1,1) _gloss ("Gloss", Range (8.0, the)) = -} subshader {Pass {tags{"Lightmode"="Forwardbase"} cgprogram#pragmaVertex vert#pragmaFragment Frag#include"Unitycg.cginc"#include"Lighting.cginc"fixed4 _color;            Fixed4 _specular; float_gloss; structa2v {float4 vertex:position;            FLOAT3 Normal:normal;            }; structv2f {float4 pos:sv_position;                FLOAT3 worldpos:texcoord0;            FLOAT3 Worldnormal:texcoord1;                        };                v2f Vert (a2v v) {v2f o; O.pos=Unityobjecttoclippos (V.vertex); O.worldpos= Mul (Unity_objecttoworld, V.vertex);//model coordinate vertex transform world coordinate vertexO.worldnormal = Unityobjecttoworldnormal (v.normal);//model coordinate normals transform world coordinate normals                returno; } fixed4 Frag (v2f i): sv_target {fixed3 worldnormal= Normalize (i.worldnormal);//Normal DirectionFixed3 Worldlightdir = Normalize (Unityworldspacelightdir (I.worldpos));//Illumination DirectionFixed3 Worldviewdir = Normalize (Unityworldspaceviewdir (I.worldpos));//Viewing Directionfixed3 Ambient= UNITY_LIGHTMODEL_AMBIENT.XYZ;//Ambient LightFixed3 Diffuse= _lightcolor0.rgb * _color.rgb * MAX (0, Dot (Worldnormal, worldlightdir));//Diffuse Reflectionfixed3 Halfdir= Normalize (Worldviewdir + worldlightdir);//Blinn Model Calculation HFIXED3 specular = _lightcolor0.rgb * _specular.rgb * POW (max (0, Dot (Worldviewdir, Halfdir)), _gloss);//High Light Reflection                returnFIXED4 (ambient + diffuse + specular,1);//Output Color after addition} ENDCG} }}

Unity Shader's basic lighting

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.