【问题标题】:In Unity Shaderlab, how can you animate towards a value using lerp and deltaTime在 Unity Shaderlab 中,如何使用 lerp 和 deltaTime 为值设置动画
【发布时间】:2018-10-29 23:03:35
【问题描述】:

如何使用自定义标准表面着色器将值动画化为目标值?

例如:

...
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 时,着色器将向该值淡化。

【问题讨论】:

  • 我将不得不与 ShaderLab 一起讨论,看看我能用内置值做什么,但应该注意deltaTime 不会是正确的。您可以做的是设置一个属性值,然后从MonoBehavior 脚本修改它。
  • forum.unity.com/threads/… 这帮助我理解了为什么我的方法不正确

标签: unity3d shader shaderlab


【解决方案1】:

有几种方法可以做到这一点。其中之一是使用内置着色器变量:

    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"
 }

【讨论】:

  • _SinTime 将导致属性来回摆动,这可能不适用于提问者的目的,但这是一个不错的起点。问题是确保它以所需的方式夹紧(即从 1 开始并在 0 停止)。
【解决方案2】:

上面答案中的着色器在 Unity 2018.2 中仍然有效。然而,_SinTime 在 -1 和 1 之间波动,而我们希望范围在 0 和 1 之间。代码 clamp(_SinTime.w, 0, 1) 钳制在所需的值上,但是 alpha 保持在零的时间过长。所以我们需要绝对值,比如:abs(_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"
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多