《Unity着色器圣经》3.1.1 | MPD指数滑条与整数范围

目录索引

译文

这两种绘制器在处理数字范围和精度时非常有用,比如我们可以用指数滑条(PowerSlider)非线性地控制曲线。

指数滑条的声明语法如下所示:

[PowerSlider(3.0)] _PropertyName ("Display name", Range (0.01, 1)) = 0.08

整数范围(IntRange)“人如其名”,是一个返回整数的范围。它的声明语法如下所示:

[IntRange] _PropertyName ("Display name", Range (0, 255)) = 100

注意一下,如果我们想在着色器中使用这两种绘制器,我们可以直接在 CGPROGRAM 语义块中声明它们的连接变量。为了方便各位理解,让我们来看看下面的例子:

Shader "InspectorPath/shaderName" 
{ 
    Properties 
    { 
        // declare drawer 
        [PowerSlider(3.0)] _Brightness ("Brightness", Range (0.01, 1)) = 0.08 
        [IntRange] _Samples ("Samples", Range (0, 255)) = 100 
    } 
    SubShader 
    { 
        Pass 
        { 
            CGPROGRAM 
            … 
            // generate connection variables 
            float _Brightness; 
            int _Samples; 
            … 
            ENDCG 
        } 
    } 
}

在上面的例子中,我们声明了一个叫做“_Brightness”的指数滑条和一个叫做“_Samples”的整数范围,接着我们在 CGPROGRAM 语义块中用相同的名字声明了它们的连接变量。


原文对照

These drawers are quite useful when working with numerical ranges and precision. On the one hand, we have the PowerSlider, which allows us to generate a non-linear slider with curve control.

Its syntax is as follows:

[PowerSlider(3.0)] _PropertyName ("Display name", Range (0.01, 1)) = 0.08

On the other hand, we have the IntRange which, as its name says, adds a numerical range of integer values.

Its syntax is as follows:

[IntRange] _PropertyName ("Display name", Range (0, 255)) = 100

Note that, if we want to use these properties within our shader, they have to be declared within the CGPROGRAM in the same way as a conventional property. To understand how to use it, we will do the following operation:

Shader "InspectorPath/shaderName" 
{ 
    Properties 
    { 
        // declare drawer 
        [PowerSlider(3.0)] _Brightness ("Brightness", Range (0.01, 1)) = 0.08 
        [IntRange] _Samples ("Samples", Range (0, 255)) = 100 
    } 
    SubShader 
    { 
        Pass 
        { 
            CGPROGRAM 
            … 
            // generate connection variables 
            float _Brightness; 
            int _Samples; 
            … 
            ENDCG 
        } 
    } 
}

In the example above, we declare a PowerSlider called _Brightness and an IntRange called _Samples. Finally, using the same names, we generate our connection variables within the CGPROGRAM.

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容