LYGIA Shader Library

mod (lygia/math/mod)

An implementation of mod that matches the GLSL mod. Note that HLSL's fmod is different.

Use:

<float|float2|float3|float4> mod(<float|float2|float3|float4> value, <float|float2|float3|float4> modulus)

Check it on Github


#ifndef FNC_MOD
#define FNC_MOD

float mod(in float x, in float y) { return x - y * floor(x / y); }
float2 mod(in float2 x, in float2 y) { return x - y * floor(x / y); }
float3 mod(in float3 x, in float3 y) { return x - y * floor(x / y); }
float4 mod(in float4 x, in float4 y) { return x - y * floor(x / y); }

#endif

Use:

<bool2|bool3|bool4> mod(<float2|float3|float4>, <float2|float3|float4>)

Check it on Github


#ifndef FNC_MOD
#define FNC_MOD

// Scalar mod function (float)
inline float mod(float x, float y) { return fmod(x, y); }

// Vector mod functions (float2, float3, float4)
inline float2 mod(float2 x, float2 y) { return fmod(x, y); }
inline float3 mod(float3 x, float3 y) { return fmod(x, y); }
inline float4 mod(float4 x, float4 y) { return fmod(x, y); }

// Scalar mod function with scalar divisor (float2, float3, float4)
inline float2 mod(float2 x, float y) { return fmod(x, float2(y)); }
inline float3 mod(float3 x, float y) { return fmod(x, float3(y)); }
inline float4 mod(float4 x, float y) { return fmod(x, float4(y)); }

#endif

Check it on Github


fn mod2(x: vec2f, y: vec2f) -> vec2f { return x - y * floor(x / y); }
fn mod3(x: vec3f, y: vec3f) -> vec3f { return x - y * floor(x / y); }
fn mod4(x: vec4f, y: vec4f) -> vec4f { return x - y * floor(x / y); }

Dependencies:

Use:

<float2|float3|float4> mod(<float2|float3|float4> a, <float2|float3|float4> b);
<float2|float3|float4> mod(<float2|float3|float4> a, float b);
<float> mod(float a, float b);

Check it on Github



#ifndef FNC_MOD
#define FNC_MOD

inline __host__ __device__ float  mod(float a, float b) { return fmodf(a, b); }
inline __host__ __device__ float2 mod(const float2& a, const float2& b) { return make_float2(fmodf(a.x, b.x), fmodf(a.y, b.y)); }
inline __host__ __device__ float3 mod(const float3& a, const float3& b) { return make_float3(fmodf(a.x, b.x), fmodf(a.y, b.y), fmodf(a.z, b.z)); }
inline __host__ __device__ float4 mod(const float4& a, const float4& b) { return make_float4(fmodf(a.x, b.x), fmodf(a.y, b.y), fmodf(a.z, b.z), fmodf(a.w, b.w)); }

inline __host__ __device__ float2 mod(const float2& a, float b) { return make_float2(fmodf(a.x, b), fmodf(a.y, b)); }
inline __host__ __device__ float3 mod(const float3& a, float b) { return make_float3(fmodf(a.x, b), fmodf(a.y, b), fmodf(a.z, b)); }
inline __host__ __device__ float4 mod(const float4& a, float b) { return make_float4(fmodf(a.x, b), fmodf(a.y, b), fmodf(a.z, b), fmodf(a.w, b)); }

#endif

Licenses

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