《Unity着色器圣经》4.2.0. | Timing and animation.

目录索引

译文

在Unity中,有三个用来实现动画效果的内置着色器变量,分别是指_Time、_SinTime和_CosTime。

这些变量是四维向量,其中每个维度表示一个速度级别,例如,“_Time.y”等于自加载场景或级别以来经过的时间(以秒为单位),类似于Time.timeSinceLevelLoad的函数。相反,“_TTime.x”对应于“_Time.y”除以二十。

其语法如下:

// "t" time in seconds.
_Time.x = t / 20;
_Time.y = t;
_Time.z = t * 2;
_Time.w = t * 3;

_CosTime和_SinTime变量具有相同的行为,因为它们对应于_Time的简化函数,例如,_CosTime.w等于运算“cos(_Time.y)”;两者都返回相同的结果,对于“sin(_Time.y)”也是如此。其语法如下:

_SinTime.x = t / 8;
_SinTime.y = t / 4;
_SinTime.z = t / 3;
_SinTime.w = t;                // sin(_Time.y);

_CosTime.x = t / 8;
_CosTime.y = t / 4;
_CosTime.z = t / 3;
_CosTime.w = t;                // cos(_Time.y);

我们可以使用_Time函数来生成超级马里奥图标的偏移动画,以深化概念。为此,我们必须在片段着色器阶段为UV的U坐标添加时间。

fixed4 frag (v2f i) : SV_Target
{
    // add time to the U coordinate
    i.uv.x += _Time.y;
    fixed4 col = tex2D(_MainTex, i.uv);

    return col;
}
图片[1]-《Unity着色器圣经》4.2.0. | Timing and animation.-软件开发学习笔记
(Fig. 4.2.0a. U Offset on a Quad)

或者,我们也可以使用_SinTime和_CosTime生成旋转动画,同时添加U和V运动。

fixed4 frag (v2f i) : SV_Target
{
    // add sine time to the U coordinate
    i.uv.x += _SinTime.w;
    // add cosine time to the V coordinate
    i.uv.y += _CosTime.w;

    fixed4 col = tex2D(_MainTex, i.uv);

    return col;
}
图片[2]-《Unity着色器圣经》4.2.0. | Timing and animation.-软件开发学习笔记
(Fig. 4.2.0b. Offset rotation in both U and V on a Quad. The tiling is equal to 4)

原文对照

In Unity, there are three Built-in Shader Variables that we will frequently use in animating properties for our effects. These refer to _Time, _SinTime, and _CosTime.


Such variables are four-dimensional vectors, where each dimension represents a speed level, e.g., “_Time.y” is equal to the time (in seconds) that elapses since the scene or level has been loaded, similar to the function of the Time.timeSinceLevelLoad. In contrast, “_ Time.x” corresponds to the same value, divided by twenty.


Its syntax is as follows:

// "t" time in seconds.
_Time.x = t / 20;
_Time.y = t;
_Time.z = t * 2;
_Time.w = t * 3;

The _CosTime and _SinTime variables have the same behavior because they correspond to simplified functions of _Time, e.g., _CosTime.w is equal to the operation “cos(_Time.y)”; both return the same result, likewise for “sin(_Time.y)”.

Its syntax is as follows:

_SinTime.x = t / 8;
_SinTime.y = t / 4;
_SinTime.z = t / 3;
_SinTime.w = t;                // sin(_Time.y);

_CosTime.x = t / 8;
_CosTime.y = t / 4;
_CosTime.z = t / 3;
_CosTime.w = t;                // cos(_Time.y);

We could use the _Time function to generate the Super Mario icons’ offset animation to deepen the concept. For this purpose, we would have to add time to the U coordinate of the UV in the fragment shader stage.

fixed4 frag (v2f i) : SV_Target
{
    // add time to the U coordinate
    i.uv.x += _Time.y;
    fixed4 col = tex2D(_MainTex, i.uv);

    return col;
}
图片[1]-《Unity着色器圣经》4.2.0. | Timing and animation.-软件开发学习笔记
(Fig. 4.2.0a. U Offset on a Quad)

Or we could also generate a rotation animation using _SinTime and _CosTime, adding both U and V motion.

fixed4 frag (v2f i) : SV_Target
{
    // add sine time to the U coordinate
    i.uv.x += _SinTime.w;
    // add cosine time to the V coordinate
    i.uv.y += _CosTime.w;

    fixed4 col = tex2D(_MainTex, i.uv);

    return col;
}
图片[2]-《Unity着色器圣经》4.2.0. | Timing and animation.-软件开发学习笔记
(Fig. 4.2.0b. Offset rotation in both U and V on a Quad. The tiling is equal to 4)
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容