Unity3d 在Unity Shaderlab中,如何使用lerp和deltaTime为值设置动画
如何使用自定义标准曲面着色器为值设置朝向目标值的动画 例如:Unity3d 在Unity Shaderlab中,如何使用lerp和deltaTime为值设置动画,unity3d,shader,shaderlab,Unity3d,Shader,Shaderlab,如何使用自定义标准曲面着色器为值设置朝向目标值的动画 例如: ... float targetAlpha; //Set by the property block, or a C# script float currrentAlpha; //Used internally only. void surf (Input IN, inout SurfaceOutputStandard o) { currrentAlpha = lerp(currrentAlpha, targetAlpha,
...
float targetAlpha; //Set by the property block, or a C# script
float currrentAlpha; //Used internally only.
void surf (Input IN, inout SurfaceOutputStandard o)
{
currrentAlpha = lerp(currrentAlpha, targetAlpha, unity_DeltaTime.x);
o.Alpha = currrentAlpha;
}
此代码不起作用,但应该演示目标是什么:当设置targetAlpha时,着色器将向该值淡入。可以这样做的方法很少。其中之一是使用内置着色器变量:
Shader "Custom/Test" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_TargetAlpha ("TargetAlpha", Range(0, 1)) = 0.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows alpha:fade
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
half _Alpha;
fixed4 _Color;
half _TargetAlpha;
void surf (Input IN, inout SurfaceOutputStandard o)
{
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = lerp(o.Alpha, _TargetAlpha, clamp(_SinTime.w, 0, 1));
}
ENDCG
}
FallBack "Diffuse"
}
上面答案中的着色器在Unity 2018.2中仍然有效。但是,
\u SinTime
在-1和1之间振荡,而我们希望范围在0和1之间。代码钳制(\u SinTime.w,0,1)
钳制到所需的值,但是alpha保持在零的时间太长。所以我们需要绝对值,比如:abs(\u SinTime.w)
修改的着色器:
Shader "Custom/Test" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_TargetAlpha ("TargetAlpha", Range(0, 1)) = 0.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows alpha:fade
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
half _Alpha;
fixed4 _Color;
half _TargetAlpha;
void surf (Input IN, inout SurfaceOutputStandard o)
{
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = lerp(o.Alpha, _TargetAlpha, abs(_SinTime.w));
}
ENDCG
}
FallBack "Diffuse"
}
我必须与ShaderLab讨价还价,看看我能用内置值做些什么,但应该注意的是,
deltaTime
是不正确的。您可以做的是设置一个属性值,然后从monobhavior
脚本修改该属性值。这有助于我理解为什么我的方法不正确,因为时间会导致属性来回振荡,这可能不符合询问者的目的,但这是一个不错的起点。问题在于确保以所需的方式夹紧(即从1开始,在0停止)。