LYGIA Shader Library

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

grad4, used for snoise(vec4 v)

Use:

grad4(<float> j, <vec4> ip)

Check it on Github


#ifndef FNC_GRAD4
#define FNC_GRAD4
vec4 grad4(float j, vec4 ip) {
    const vec4 ones = vec4(1.0, 1.0, 1.0, -1.0);
    vec4 p,s;

    p.xyz = floor( fract (vec3(j) * ip.xyz) * 7.0) * ip.z - 1.0;
    p.w = 1.5 - dot(abs(p.xyz), ones.xyz);
    s = vec4(lessThan(p, vec4(0.0)));
    p.xyz = p.xyz + (s.xyz*2.0 - 1.0) * s.www;

    return p;
}
#endif

Use:

grad4(<float> j, <float4> ip)

Check it on Github

#ifndef FNC_GRAD4
#define FNC_GRAD4

float4 grad4(float j, float4 ip) {
    const float4 ones = float4(1.0, 1.0, 1.0, -1.0);
    float4 p, s;

    p.xyz = floor( frac (float3(j, j, j) * ip.xyz) * 7.0) * ip.z - 1.0;
    p.w = 1.5 - dot(abs(p.xyz), ones.xyz);
    // GLSL: s = float4(lessThan(p, float4(0.0)));
    s = float4(1 - step(float4(0, 0, 0, 0), p));
    p.xyz = p.xyz + (s.xyz * 2.0 - 1.0) * s.www;

    return p;
}

#endif

Check it on Github

fn grad4(j: f32, ip: vec4f) -> vec4f {
    let ones = vec4(1.0, 1.0, 1.0, -1.0);

    var xyz = floor( fract (vec3(j) * ip.xyz) * 7.0) * ip.z - 1.0;
    let w = 1.5 - dot(abs(xyz), ones.xyz);
    let s = select(vec4(0.0), vec4(1.0), vec4(xyz, w) < vec4(0.0));
    xyz = xyz + (s.xyz*2.0 - 1.0) * s.www;

    return vec4(xyz, w);
}

Dependencies:

Use:

grad4(<float> j, <float4> ip)

Check it on Github


#ifndef FNC_GRAD4
#define FNC_GRAD4

inline __host__ __device__ float4 grad4(float j, float4 ip) {
    float4 p, s;

    p.x = floor( frac(j * ip.x) * 7.0f) * ip.z - 1.0f;
    p.y = floor( frac(j * ip.y) * 7.0f) * ip.z - 1.0f;
    p.z = floor( frac(j * ip.z) * 7.0f) * ip.z - 1.0f;

    p.w = 1.5f - dot( abs( make_float3(p.x, p.y, p.z) ), make_float3(1.0f) );

    // GLSL: s = float4(lessThan(p, float4(0.0)));
    s = 1.0f - step(make_float4(0.0f), p);

    p.x = p.x + (s.x * 2.0f - 1.0f) * s.w;
    p.y = p.y + (s.y * 2.0f - 1.0f) * s.w;
    p.z = p.z + (s.z * 2.0f - 1.0f) * s.w;

    return p;
}

#endif

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