Surface Shader Surface shaders


When your Material We have to deal with the light , It is generally used Surface Shader.Surface Shader Hiding the calculation of light , You just need to surf Set the reflectivity in the function 、 Normal vector equivalence is fine . These values are sent to Lighting model Calculation in , And then it's going to output every pixel RGB value . actually ,SurfaceShader It will eventually be compiled into VertexShader and FragmentShader, So write SurfaceShader It can save us a lot of work .

Operation mode

3D The model is fed into the surface shader , The first is to process the vertices through the vertex adjuster , And then send the results to the surface function for processing , Then send the results to the lighting model , The final output is the color of all pixels .


establish Surface Shader

Template code

Shader "Custom/SurfaceShader" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
half _Glossiness;
half _Metallic;
fixed4 _Color;
void surf (Input IN, inout SurfaceOutputStandard o) {
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
FallBack "Diffuse"

The code analysis

Properties analysis

  1. _Color: The primary color of an object
  2. _MainTex: The texture on the object
  3. _Glossiness: Glossiness , Control the specular power of highlights
  4. _Metallic: Metallicity


Mainly used to control rendering options , Like the order of rendering (Rendering order) And render type (Rendering Type) etc. .
Specific options can be found in Official documents , Light ink blog


LOD yes Level of Detail Abbreviation , Represents the level of detail effect , Mainly by controlling the display effect of details to achieve optimization , Only LOD Less than QualitySettings Of Maximum LOD Level Of Shader Will be used in the bag .
Specifically LOD The definition can refer to WIKI,LOD The value can refer to Unity Canon

CG Program

  1. CGPROGRAM and ENDCG What's in between is CG Program .
  2. Precompiled translation head :#pragma surface surf Standard fullforwardshadows
    surface That means this is one Surface Shader,surf Represents the name of the surface function ,Standard Said the use of Standard The illumination model of light source ,fullforwardshadows It's the shadow type
  3. Precompiled translation head :#pragma target 3.0
    Shader model Version of , For better lighting
  4. The rest is how to operate surf function
    It's just a simple assignment , So what's confusing is CG The grammar ,CG You can refer to The Cg Tutorial

Reference resources

  1. Official documents
  2. Light ink blog
  3. Cats can learn Unity3D Shader Getting started
  4. A gentle introduction to shaders in Unity3D
  5. The Cg Tutorial

Surface Shader More about simple wizard

  1. Unity Shader——Writing Surface Shaders(3)——Surface Shader Lighting Examples

    Surface Shader Lighting example Here are some custom lighting models and Surface Shaders Example . Usual Surface Shader Here's an example . Due to the delayed illumination, it is better in some custom material by material illumination models ...

  2. Unity Shader——Writing Surface Shaders(1)——Surface Shader Examples

    Here you are Surface Shader Some examples of . The following examples focus on using built-in lighting models : For an example of how to use a custom lighting model, see Surface Shader Lighting Examples. Simple We will ...

  3. Surface Shader

    Surface Shader: (1) Must be on SubShdader block , Can't put Pass Inside : (2)#pragma sufrace surfaceFunction lightModel [option ...

  4. 【Unity Shaders】 On Surface Shader The mechanism behind it

    Reprint please indicate the source : Write it at the front all the time ,Unity Surface Shader Behind the scenes ...

  5. 【Unity Shaders】Shader Learning resources and Surface Shader summary

    Write it at the front When I write this article , I study off and on Unity Shader Half a year , In fact, he is a layman . I can also experience a lot of children's shoes that I want to learn well Shader But I can't do it . In the meantime , I found some learning Shader And ...

  6. unity surface shader 1

    Unity ShaderLib :  CGPROGRAM  ENDCG Between CG Code , All other code functions are created by ShaderLib Provide ,CG Some of the methods in this paper, such as tex2D(...) It's also ShaderLib Yes CG Conduct ...

  7. Unity Shader ( 3、 ... and )Surface Shader Mechanism

    from : Write it at the front all the time ,Unity Surface Shader The mechanism behind it has always been ...

  8. unity, surface shader access world position and localposition

    One ,surface shader Medium visit worldposition stay surface shader Access world coordinates in , Just in Input The structure states float3 worldPos that will do , as follows :  struct  ...

  9. 【Unity Shaders】Diffuse Shading—— stay Surface Shader Use in properties

    This series mainly refers to <Unity Shaders and Effects Cookbook> A Book ( Thanks to the author of the book ), At the same time, it will add a little personal understanding or expansion . Here are all the illustrations of the book . Here are the code and resources for this book ...

Random recommendation

  1. Qt Signals and slots in multiple inheritance

    Recently in the company's project to develop serial port , Because of historical reasons, the serial port needs to be packaged , And the wrapper class needs to inherit from a pure C++ class . QSerialPort have access to slot function To get the data read from the serial port , And write to the serial port bytes Number . ...

  2. Myeclipse/eclipse Of Web project Rewrite into Maven project

    1. Import or build web Add the maven Set up 2. Add the following code :<dependencies> Add the red part under the label <dependencies> <dependency&g ...

  3. UCOS Semaphore

    uCOS-II Semaphore OSSemCreate(0) and OSSemCreate(1) Detailed explanation (2014-04-22 18:04:18) Reprint ▼ label : it classification :  operating system stay ucos-II in , In order to achieve the mission ...

  4. codeforces 292E. Copying Data Line segment tree

    Topic link Let's give two lengths of n Array of , Two kinds of operations . The first one is , give x, y, k, take a[x, x+k-1] This paragraph is copied to b[y, y+k-1]. The second kind , give x, Output b[x] Value . Segment tree interval update single point search ...

  5. pycharm function scrapy

    1. open pycharm, Click on File>Open find mySpider Import the project . 2. open File>Settings>Project Click on Project Interpreter On the right ...

  6. Chipmunk The emergence and resolution of zombie physical objects ( 5、 ... and )

    , Because the code to lengthen and shorten the rebound bar is similar, we can only look at one , Let's take a detailed look at the method of shortening . +(void)doStickShorterWork:(Stick *)stick{ GameScene *g ...

  7. Wechat applet summary

    A few days ago, I took time to see the small program , It's fun to find ,mvvm Structure , Grammar than vue Be simple , Built in a series of components , Very convenient . Then the developer tool uploads the code directly , Submit audit , And then release , It feels good . Although I don't plan to be a tool class , But make an introduction ...

  8. Shell perform *.sql

    > mysql -uroot -p123456 > use db_test > source /root/temp.sql

  9. java Program started Acquisition of environment attributes

    System.getProperties().list(System.out); If you want to get a property , For example, common “ operating system ” be   System.getProperty(" ...

  10. Notes Java Time tools

    Notes Java Time tools I don't say much nonsense , Here are the main records of the following tools It's only a matter of time (Data) Get the format of time Format time return String It's only a matter of time (String) Get the date between two times . month . year ...