《Unity着色器圣经》9.0.6 | 节点

目录索引

译文

在第四章中,我们学习了几种常用的函数,其中包括 clamp, abs, min, max 等等等等。这些函数在 Shader graph 中都有具有相同功能的对应节点,例如 Unity 官方文档对于 Clamp 节点的介绍如下:

以下示例代码表示此节点的一种可能结果。

void Unity_Clamp_float4(float4 In, float4 Min, float4 Max, out float4 Out)
{
    Out = clamp(In, Min, Max);
}

在上面的代码示例中,我们可以看到返回类型为“void”的 Unity_Clamp_float4 函数有三个四维向量输入(In、Min 和 Max)。如图 9.0.6a 所示,在节点的构造中也可以找到这些输入。

对于名为“Out”的输出,它只包含了函数所执行操作的输出结果。

图片[1]-《Unity着色器圣经》9.0.6 | 节点-软件开发学习笔记
Fig. 9.0.6a

因为 Shader Graph 是基于 HLSL 语言的,我们也可以参照第 4.0.4 小节,在以“.shader”为拓展名的文件中使用这些函数。

void Unity_Clamp_float4(float4 In, float4 Min, float4 Max, out float4 Out)
{... }

half4 frag (v2f i) : SV_Target
{
    float4 c = 0;
    Unity_Clamp_float4(_Value, 0, 1, c);
    return c;
}

原文对照

In the first chapter, we reviewed the operations performed by some intrinsic functions, among them: clamp, abs, min, max, and many others. The nodes in the Shader Graph are the graphical representation of these functions; therefore, they fulfill the same functionalities, e.g., according to the official Unity documentation, the Clamp node is articulated as follows:

The following example code represents a possible outcome of this node.

void Unity_Clamp_float4(float4 In, float4 Min, float4 Max, out float4 Out)
{
    Out = clamp(In, Min, Max);
}

we can see in the previous example, the Unity_Clamp_float4 function of type “void” has three inputs corresponding to four-dimensional vectors. Among them, we can find “In, Min and Max.” These same values can be seen graphically in the construction of the node, as shown in Figure 9.0.6a.

As for the output called “Out,” it simply has the operation to be carried out.

图片[1]-《Unity着色器圣经》9.0.6 | 节点-软件开发学习笔记
Fig. 9.0.6a

Since Shader Graph is based on HLSL language, we can use these same functions inside a shader with “.shader” extension, following the points mentioned in section 4.0.4.

void Unity_Clamp_float4(float4 In, float4 Min, float4 Max, out float4 Out)
{... }

half4 frag (v2f i) : SV_Target
{
    float4 c = 0;
    Unity_Clamp_float4(_Value, 0, 1, c);
    return c;
}
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容