Writing surface shaders

Source: Internet
Author: User

Writing surface shaders]

Writing shaders that interact with lighting is complex. There are different light types, different shadow options, different rendering paths (forward and deferred rendering), and the shader shocould somehow handle all that complexity.

Surface shaders in unity isCode Generation ApproachThat makes it much easier to write your shaders than using low level vertex/pixel shader programs. note that there is no custom versions, magic or ninjas involved in surface shaders; it just generates all the repetitive code that wowould have to be written by hand. you still write shader code in CG/HLSL.

You define a "surface function" thatTakes any Uvs or dataYou need as input, and fills in output structureSurfaceOutput. Surfaceoutput basically describesProperties of the surface(It's albedo color, normal, emission, specularity etc.). You write this code in CG/HLSL.

Surface shader compiler then figures out what inputs are needed, what outputs are filled and so on, and generates actual vertex & pixel shaders, as well as rendering passes to handle forward and deferred rendering.

  

Surface shader compile directives]

Surface shader is placed insideCGPROGRAM..ENDCGBlock, just like any other shader. The differences are:

  • It must be placed inside subshader block, not inside pass. Surface shader will compile into multiple passes itself.
  • It uses#pragma surface ...Directive to indicate it's a surface shader.

The#pragma surfaceDirective is:

  

Required parameters:

  • Surfacefunction-which CG function has surface shader code. The function shocould have the formvoid surf (Input IN, inout SurfaceOutput o), Where input is a structure you have defined. Input shoshould contain any texture coordinates and extra automatic variables needed by surface function.
  • Lightmodel-lighting model to use. Built-in ones areLambert(Diffuse)AndBlinnPhong(Specular). See custom lighting models page for how to write your own.

Optional parameters]

The Input StructureInputGenerally has any texture coordinates needed by the shader.Texture coordinates must be named"uv"Followed by texture name(Or start it with"uv2"To use second texture coordinate set ).

Reference: file: // C:/program % 20 Files % 20 (x86)/Unity/Editor/data/documentation/html/en/manual/SL-SurfaceShaders.html

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.