LYGIA Shader Library

mirror (lygia/v1.1.6/math/mirror)

Transforms the input signal into a triangle wave. For instance, if x goes between 0 and 2, the returned value will go from 0 to 1, and then 1 to 0 in a triangle shape.

Use:

mirror(<vec2|float> x)

Check it on Github


#ifndef FNC_MIRROR
#define FNC_MIRROR
float mirror(in float x) {
    float f = fract(x);
    float m = floor(mod(x, 2.));
    float fm = f * m;
    return f + m - fm * 2.;
}

vec2 mirror(in vec2 v) {
    vec2 f = fract(v);
    vec2 m = floor(mod(v, 2.));
    vec2 fm = f * m;
    return f + m - fm * 2.;
}
#endif

Dependencies:

Use:

mirror(<float2|float> x)

Check it on Github



#ifndef FNC_MIRROR
#define FNC_MIRROR
float mirror(in float x) {
    float f = frac(x);
    float m = floor(mod(x, 2.));
    float fm = f * m;
    return f + m - fm * 2.;
}

float2 mirror(in float2 xy) {
    float2 f = frac(xy);
    float2 m = floor(mod(xy, 2.));
    float2 fm = f * m;
    return f + m - fm * 2.;
}
#endif

Check it on Github

fn mirror(x: f32) -> f32 {
    let f = fract(x);
    let m = floor(x % 2.0);
    let fm = f * m;
    return f + m - fm * 2.0;
}

fn mirror2(xy: vec2<f32>) -> vec2<f32> {
    let f = fract(xy);
    let m = floor(xy % 2.0);
    let fm = f * m;
    return f + m - fm * 2.0;
}

Dependencies:

Use:

mirror(<float2|float> x)

Check it on Github



#ifndef FNC_MIRROR
#define FNC_MIRROR
inline __device__ __host__ float mirror(float x) {
    float f = frac(x);
    float m = floor(mod(x, 2.0f));
    float fm = f * m;
    return f + m - fm * 2.;
}

inline __device__ __host__ float2 mirror(float2 xy) {
    float2 f = frac(xy);
    float2 m = floor(mod(xy, 2.0f));
    float2 fm = f * m;
    return f + m - fm * 2.0f;
}
#endif

Examples

LYGIA is dual-licensed under the Prosperity License and the Patron License for sponsors and contributors.

Sponsors and contributors are automatically added to the Patron License and they can ignore the any non-commercial rule of the Prosperity Licensed software (please take a look to the exception).

It's also possible to get a permanent comercial license hook to a single and specific version of LYGIA.

Get the latest news and releases

Sign up for the news letter bellow, joing the LYGIA's channel on Discord or follow the Github repository