首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Unity Shaderlab中,如何使用lerp和deltaTime为某个值设置动画

在Unity Shaderlab中,如何使用lerp和deltaTime为某个值设置动画
EN

Stack Overflow用户
提问于 2018-05-20 04:49:40
回答 2查看 923关注 0票数 2

如何使用自定义标准曲面着色器为目标值设置动画效果?

例如:

代码语言:javascript
运行
复制
...
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时,着色器将淡入该值。

EN

回答 2

Stack Overflow用户

发布于 2018-05-20 07:19:59

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

代码语言:javascript
运行
复制
    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"
 }
票数 3
EN

Stack Overflow用户

发布于 2019-10-16 14:28:12

上面答案中的着色器在Unity 2018.2中仍然有效。但是,_SinTime在-1和1之间振荡,而我们希望范围在0和1之间。代码clamp(_SinTime.w, 0, 1)钳位到所需的值,但是alpha停留在0的时间太长。所以我们需要绝对值,比如:abs(_SinTime.w)

修改后的明暗器:

代码语言:javascript
运行
复制
    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"
 }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50429374

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档