当前位置:   article > 正文

Unity中Shader的_Time精度问题_shader _time

shader _time


前言

Unity的Shader中,使用了_Time来达到UV的流动效果,普遍会出现一个问题。我们的UV值会随着时间一直增加(uv值增加了,但是因为超过1就会重复,不影响效果),在一些较老的移动端上,存储不下那么大的值,就会导致精度丢失。


一、U方向上优化

  • 对之前的公式取小数部分
  • u ( t ) = g ( C o l u m n ⋅ t ) C o l u m n = ⌊ C o l u m n ⋅ t ⌋ C o l u m n u(t) = \frac{g(Column·t)}{Column} = \frac{\lfloor Column·t \rfloor}{Column} u(t)=Columng(Columnt)=ColumnColumnt
  • u 2 ( t ) = f r a c ( u ( t ) ) = f r a c ( ⌊ C o l u m n ⋅ t ⌋ C o l u m n ) u_2(t) = frac(u(t)) =frac(\frac{\lfloor Column·t \rfloor}{Column}) u2(t)=frac(u(t))=frac(ColumnColumnt)
    在这里插入图片描述

o.uv.x += frac(floor(_Time.y *_Sequence.y * _Sequence.z)/_Sequence.y);


二、V方向上优化

  • 对之前的公式取小数部分
  • v ( t ) = u ( t C o l u m n ) = ⌊ C o l u m n ⋅ t C o l u m n ⌋ C o l u m n v(t) = u(\frac{t}{Column}) = \frac{\lfloor \frac{Column·t}{Column} \rfloor}{Column} v(t)=u(Columnt)=ColumnColumnColumnt
  • v 2 ( t ) = f r a c ( v ( t ) ) = f r a c ( ⌊ C o l u m n ⋅ t C o l u m n ⌋ C o l u m n ) v_2(t) = frac(v(t)) = frac(\frac{\lfloor \frac{Column·t}{Column} \rfloor}{Column}) v2(t)=frac(v(t))=frac(ColumnColumnColumnt)

在这里插入图片描述

三、最终代码

1、效果

请添加图片描述

2、Shader

Shader "MyShader/URP/P3_9"
{
    Properties
    {
        _Color("Color",Color) = (1,1,1,1)
        _MainTex("MainTex",2D) = "white"{}
        _Sequence("Row(X) Column(Y) Speed(Z)",Vector) = (1,1,1,1)
    }
    SubShader
    {
        Tags
        {
            //告诉引擎,该Shader只用于 URP 渲染管线
            "RenderPipeline"="UniversalPipeline"
            //渲染类型
            "RenderType"="Transparent"
            //渲染队列
            "Queue"="Transparent"
        }
        Blend SrcAlpha OneMinusSrcAlpha Zwrite On
        Pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fog
            #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"

            struct Attribute
            {
                float3 vertexOS : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct Varying
            {
                float4 vertexCS : SV_POSITION;
                float2 uv : TEXCOORD1;
                float fogCoord : TEXCOORD2;
            };

            CBUFFER_START(UnityPerMaterial)
                float4 _Color;
                float4 _MainTex_ST;
                half4 _Sequence;
            CBUFFER_END
            TEXTURE2D(_MainTex);
            SAMPLER(sampler_MainTex);

            Varying vert(Attribute v)
            {
                Varying o;
                o.vertexCS = TransformObjectToHClip(v.vertexOS);
                o.uv = float2(v.uv.x/_Sequence.y,v.uv.y/_Sequence.x + (_Sequence.x - 1)/_Sequence.x);
                o.uv.x += frac(floor(_Time.y *_Sequence.y * _Sequence.z)/_Sequence.y);
                o.uv.y -= frac(floor(_Time.y *_Sequence.y * _Sequence.z / _Sequence.y)/_Sequence.x);
                //o.uv.x += floor(_Time.y);
                //o.uv = float2(v.uv.x/4,v.uv.y/4);
                //o.uv = TRANSFORM_TEX(v.uv,_MainTex);
                o.fogCoord = ComputeFogFactor(o.vertexCS.z);
                return o;
            }

            half4 frag(Varying i) : SV_Target
            {
                float4 mainTex = SAMPLE_TEXTURE2D(_MainTex,sampler_MainTex,i.uv);
                float4 col = mainTex * _Color;
                col.rgb = MixFog(col,i.fogCoord);
                return col;
            }
            ENDHLSL
        }
    }
    
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号