• Writing Surface Shaders


    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 should somehow handle all that complexity.

      Surface Shaders in Unity is a code generation approach that makes it much easier to write lit shaders than using low level vertex/pixel shader programs. Note that there is no custom languages, magic or ninjas involved in Surface Shaders; it just generates all the repetitive code that would have to be written by hand. You still write shader code in Cg / HLSL.

      You define a “surface function” that takes any UVs or data you need as input, and fills in output structure SurfaceOutput. SurfaceOutput basically describes properties 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 inside CGPROGRAM..ENDCG block, 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 surface directive is:

      

      Required parameters:

    • surfaceFunction - which Cg function has surface shader code. The function should have the form of void surf (Input IN, inout SurfaceOutput o), where Input is a structure you have defined. Input should contain any texture coordinates and extra automatic variables needed by surface function.
    • lightModel - lighting model to use. Built-in ones are Lambert (diffuse) and BlinnPhong (specular). See Custom Lighting Models page for how to write your own.

    引用变量

    1、如果在Properties中定义了:_MainTex("Base (RGB)", 2D) = "white" {}

       那么想在Surface中引用,则需先定义:sampler2D _MainTex;

    2、同样,如果定义了Color。也需要先在Surface中定义,如:

      float4 _EmissiveColor;

    Optional parameters

      The input structure Input generally 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).

    参考:file:///C:/Program%20Files%20(x86)/Unity/Editor/Data/Documentation/html/en/Manual/SL-SurfaceShaders.html

  • 相关阅读:
    会议记录补充5月9日
    会议记录补充5月11日
    每日会议记录5月6日
    SQL Server 日期函数
    Jvascript运算符
    For循环
    JS数据类型
    初识Javascript
    检测浏览器版本(综合整理)
    自己实现一个数组的slice方法
  • 原文地址:https://www.cnblogs.com/tekkaman/p/3885870.html
Copyright © 2020-2023  润新知