Search Unity

Pingpong function

Discussion in 'Shaders' started by suctioncup, Apr 18, 2014.

  1. suctioncup

    suctioncup

    Joined:
    May 19, 2012
    Posts:
    273
    Hello,

    I am creating a vertex shader to expand the model, and contract it, expand, and contract etc etc. I currently have the expanding, and the contracting I can do. Its the expanding AND THEN contracting, AND THEN expanding. I can only seem to do one at a time.

    Does CG have an equivalent to the Mathf.PingPong?

    Thanks
     
  2. WhiskyJoe

    WhiskyJoe

    Joined:
    Aug 21, 2012
    Posts:
    143
    As far as I know, there isn't really a function that does this for you. You can probably make this yourself by swapping values after a specific condition is met, but I think you will be better of by just passing the pingpong value from script to the shader.
     
  3. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    how about using a sin or cos value?
     
  4. aniv

    aniv

    Joined:
    Jan 19, 2011
    Posts:
    135
    Kamyker likes this.
  5. suctioncup

    suctioncup

    Joined:
    May 19, 2012
    Posts:
    273
    _SinTime! I knew I had forgotten something obvious. Thanks heaps, and thanks for the link to the values page. I was googling 'Shader math / Shader math functions' and couldnt find anything. Thanks again.
     
  6. WhiskyJoe

    WhiskyJoe

    Joined:
    Aug 21, 2012
    Posts:
    143
    It might not really matter in your case, but would passing a value that you can use for pingpong purposes be cheaper than using a sin/cos function as those are generally expensive in use?
     
  7. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    _SinTime isn't a function :)
     
  8. WhiskyJoe

    WhiskyJoe

    Joined:
    Aug 21, 2012
    Posts:
    143
    But it still uses the sin function I guess? Except that it's likely to be set from within the engine.
     
  9. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    If you look in UnityShaderVariables.cginc, you see it says:
    Code (csharp):
    1. CBUFFER_START(UnityPerCamera)
    2.     // Time values from Unity
    3.     uniform float4 _Time;
    4.     uniform float4 _SinTime;
    5.     uniform float4 _CosTime;
    6.     uniform float4 unity_DeltaTime; // dt, 1/dt, smoothdt, 1/smoothdt
    So I'm pretty sure they're set from the CPU, instead of being calculated on the GPU, and I assume they take about as much time as other shader properties. I should measure that of course...
     
  10. Jason-Michael

    Jason-Michael

    Joined:
    Jul 6, 2015
    Posts:
    20
    I checked the cg standard library and could not find any function named Pingpong or something like that, but I think we can write one, just like this :

    // return value falls in [0, 1] section //
    float Pingpong()
    {
    int remainder = fmod(floor(_Time.y), 2);
    return remainder==1?1-frac(_Time.y):frac(_Time.y);
    }

    if you want to expand the return value's range just multiply by another value, I tested and it worked well. Hope this could do some help.
     
    sama-van and PrimalCoder like this.