Writing Surface Shaders 表面着色器写法 - Unity Shader Reference 系列2

Writing Surface Shaders 表面着色器写法

本文档主要是对Unity官方手册的个人理解与总结(其实以翻译记录为主:>)
仅作为个人学习使用,不得作为商业用途,欢迎转载,并请注明出处。
文章中涉及到的操作都是基于Unity2018.2版本
参考链接:https://docs.unity3d.com/Manual/SL-SurfaceShaders.html


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 are 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 HLSL.
Unity中的Surface着色器是一种代码生成方法,它比使用低级的顶点(vertex)/像素(pixel)着色程序更容易编写光照着色。请注意,在表面着色器中没有定制语言、魔术或忍者;它只是可生成所有需要手工编写的重复代码。你仍然可以在HLSL中编写shader 代码。

For some examples, take a look at Surface Shader Examples and Surface Shader Custom Lighting Examples.
在一些例子中,我们来看看表面着色器的例子和表面着色器自定义的光照例子。

How it works
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 HLSL.
你定义了一个“表面函数”,它接受你需要的任何UVs或数据作为输入,并填充输出结构的surface输出。表面输出基本上描述了表面的性质(它是反射率(albedo)、法线、自发光、高光等)。相当于你在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着色器会计算出需要什么输入,什么输出被填满等等,并生成实际的顶点和像素着色器,以及渲染通道来处理前向和延迟渲染。
Standard output structure of surface shaders is this:
表面着色器的标准输出结构如下:

struct SurfaceOutput
{
    fixed3 Albedo;  // diffuse color
    fixed3 Normal;  // tangent space normal, if written
    fixed3 Emission;
    half Specular;  // specular power in 0..1 range
    fixed Gloss;    // specular intensity
    fixed Alpha;    // alpha for transparencies
};

In Unity 5, surface shaders can also use physically based lighting models. Built-in Standard and StandardSpecular lighting models (see below) use these output structures respectively:
在Unity 5中,表面着色器也可以使用基于物理的照明模型。内置的标准和标准的照明模型(见下文)分别使用这些输出结构:

struct SurfaceOutputStandard
{
    fixed3 Albedo;      // base (diffuse or specular) color
    fixed3 Normal;      // tangent space normal, if written
    half3 Emission;
    half Metallic;      // 0=non-metal, 1=metal
    half Smoothness;    // 0=rough, 1=smooth
    half Occlusion;     // occlusion (default 1)
    fixed Alpha;        // alpha for transparencies
};
struct SurfaceOutputStandardSpecular
{
    fixed3 Albedo;      // diffuse color

你可能感兴趣的:(Shader,Reference,Unity,Graphics,图形渲染,教程,文档,笔记,Unity,Shader,Reference,着色器,参考,文档笔记,Unity,Shader,Surface,表面着色器,图形渲染)