Search Unity

Question Why is there no permanent cycle?

Discussion in 'Shaders' started by Zimaell, Feb 24, 2023.

  1. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    409
    I use baked animation example here is shader example

    Code (CSharp):
    1. struct appdata{
    2.                 float2 uv : TEXCOORD0;
    3.                 float4 pos : POSITION;
    4.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    5.                 };
    6.  
    7.             struct v2f{
    8.                 float2 uv : TEXCOORD0;
    9.                 float4 vertex : SV_POSITION;
    10.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    11.                 };
    12.  
    13.             CBUFFER_START(UnityPerMaterial)
    14.                 float _AnimLen;
    15.                 sampler2D _MainTex;
    16.                 float4 _MainTex_ST;
    17.                 sampler2D _AnimMap;
    18.                 float4 _AnimMap_TexelSize;//x == 1/width
    19.             CBUFFER_END
    20.            
    21.             float4 ObjectToClipPos (float3 pos){
    22.                 return mul (UNITY_MATRIX_VP, mul (UNITY_MATRIX_M, float4 (pos,1)));
    23.                 }
    24.            
    25.             v2f vert (appdata v, uint vid : SV_VertexID){
    26.                 UNITY_SETUP_INSTANCE_ID(v);
    27.                 float f = _Time.y / _AnimLen;
    28.                 fmod(f, 1.0);
    29.                 float animMap_x = (vid + 0.5) * _AnimMap_TexelSize.x;
    30.                 float animMap_y = f;
    31.                 float4 pos = tex2Dlod(_AnimMap, float4(animMap_x, animMap_y, 0, 0));
    32.                 v2f o;
    33.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    34.                 o.vertex = ObjectToClipPos(pos);
    35.                 return o;
    36.                 }
    37.            
    38.             float4 frag (v2f i) : SV_Target{
    39.                 float4 col = tex2D(_MainTex, i.uv);
    40.                 return col;
    41.                 }
    But the animation only plays one cycle and stops.

    How to make the cycle permanent?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    line 28:
    fmod(f, 1.0);
    , that doesn't do anything. Most intrinsic HLSL function don't modify the input variable(s), but rather return a value. So that line should be:
    f = fmod(f, 1.0);

    Or, really:
    f = frac(f);
    since there's no reason to use the
    fmod()
    function when the divisor is 1.0.