《Unity着色器圣经》3.2.5 | ShaderLab Pass

目录索引

译文

着色器的第三个重要部分被称为 Pass。

在一个着色器中可以有多个 pass,但 Unity 在用户新建着色器时,在默认情况下只会在子着色器语义块中预设一个 pass。

回头看看我们在章节初创建的 USB_simple_color 着色器,我们能够找到以下 pass:

Pass 
{ 
    CGPROGRAM 
    #pragma vertex vert 
    #pragma fragment frag 
    // 让雾工作
    #pragma multi_compile_fog 

    #include "UnityCG.cginc"

    struct appdata { … }; 

    struct v2f { … }; 

    sampler2D _MainTex; 
    float4 _MainTex; 
    v2f vert (appdata v) { … } 
    fixed4 frag (v2f i) : SV_Target { … } 
}

从字面上看,“pass”的意思是“渲染通道”。对于使用过 3D 软件(例如 Maya 或 Blender)进行渲染的人来说这个概念会更容易理解一些,因为在处理图像时,一张图像可以生成不同的图层或通道(例如色彩通道、光线通道、遮挡通道等),从而在不同的图层中获得独立的结构。

每个 Pass 一次渲染一个对象,也就是说如果我们的着色器中有两个 pass,模型将在 GPU 上渲染两次,相当于两次绘制调用(draw call)。

一个 Pass 相当于一次绘制调用,这就是为什么我们需要尽可能地减少 pass 数量的原因,否则我们可能会产生很大的图形负荷。

Shader "InspectorPath/shaderName" 
{ 
    Properties { … } 
    SubShader 
    { 
        Tags { "RenderType"="Opaque" } 
        Pass { // 第一个默认pass }
        Pass { // 第二个附加pass } 
    } 
}

原文对照

The third component in our shader corresponds to the passes (Pass).

There can be multiple passes within a shader, however, by default Unity adds only one inside the SubShader field.

If we look at our USB_simple_color shader, we will find the following pass included.

Pass 
{ 
    CGPROGRAM 
    #pragma vertex vert 
    #pragma fragment frag 
    // make fog work 
    #pragma multi_compile_fog 

    #include "UnityCG.cginc"

    struct appdata { … }; 

    struct v2f { … }; 

    sampler2D _MainTex; 
    float4 _MainTex; 
    v2f vert (appdata v) { … } 
    fixed4 frag (v2f i) : SV_Target { … } 
}

A Pass refers to a Render Pass literally. For those who have worked with rendering in 3D software (e.g., Maya or Blender), this concept will be easier to understand since when an image is being processed, it can generate different layers or passes separately (e.g., color Pass, light Pass, occlusion Pass, etc.) and thus obtain a separate composition in different layers.

Each Pass renders one object at a time, that is, if we have two passes in our shader, the object will be rendered twice on the GPU and the equivalent of that would be two draw calls.

A Pass is equal to a draw call which is why we must use the least amount of passes possible, otherwise, we could generate a significant graphic load.

Shader "InspectorPath/shaderName" 
{ 
    Properties { … } 
    SubShader 
    { 
        Tags { "RenderType"="Opaque" } 
        Pass { // first default pass }
        Pass { // second additional pass } 
    } 
}
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容