LYGIA Shader Library

random (lygia/v1.1.6/generative/random)

pass a value and get some random normalize value between 0 and 1

Use:

float random[2|3](<float|vec2|vec3> value)

Check it on Github


#ifndef RANDOM_SCALE
#ifdef RANDOM_HIGHER_RANGE
#define RANDOM_SCALE vec4(.1031, .1030, .0973, .1099)
#else
#define RANDOM_SCALE vec4(443.897, 441.423, .0973, .1099)
#endif
#endif

#ifndef FNC_RANDOM
#define FNC_RANDOM
float random(in float x) {
#ifdef RANDOM_SINLESS
    x = fract(x * RANDOM_SCALE.x);
    x *= x + 33.33;
    x *= x + x;
    return fract(x);
#else
    return fract(sin(x) * 43758.5453);
#endif
}

float random(in vec2 st) {
#ifdef RANDOM_SINLESS
    vec3 p3  = fract(vec3(st.xyx) * RANDOM_SCALE.xyz);
    p3 += dot(p3, p3.yzx + 33.33);
    return fract((p3.x + p3.y) * p3.z);
#else
    return fract(sin(dot(st.xy, vec2(12.9898, 78.233))) * 43758.5453);
#endif
}

float random(in vec3 pos) {
#ifdef RANDOM_SINLESS
    pos  = fract(pos * RANDOM_SCALE.xyz);
    pos += dot(pos, pos.zyx + 31.32);
    return fract((pos.x + pos.y) * pos.z);
#else
    return fract(sin(dot(pos.xyz, vec3(70.9898, 78.233, 32.4355))) * 43758.5453123);
#endif
}

float random(in vec4 pos) {
#ifdef RANDOM_SINLESS
    pos = fract(pos * RANDOM_SCALE);
    pos += dot(pos, pos.wzxy + 33.33);
    return fract((pos.x + pos.y) * (pos.z + pos.w));
#else
    float dot_product = dot(pos, vec4(12.9898,78.233,45.164,94.673));
    return fract(sin(dot_product) * 43758.5453);
#endif
}

vec2 random2(float p) {
    vec3 p3 = fract(vec3(p) * RANDOM_SCALE.xyz);
    p3 += dot(p3, p3.yzx + 19.19);
    return fract((p3.xx + p3.yz) * p3.zy);
}

vec2 random2(vec2 p) {
    vec3 p3 = fract(p.xyx * RANDOM_SCALE.xyz);
    p3 += dot(p3, p3.yzx + 19.19);
    return fract((p3.xx + p3.yz) * p3.zy);
}

vec2 random2(vec3 p3) {
    p3 = fract(p3 * RANDOM_SCALE.xyz);
    p3 += dot(p3, p3.yzx + 19.19);
    return fract((p3.xx + p3.yz) * p3.zy);
}

vec3 random3(float p) {
    vec3 p3 = fract(vec3(p) * RANDOM_SCALE.xyz);
    p3 += dot(p3, p3.yzx + 19.19);
    return fract((p3.xxy + p3.yzz) * p3.zyx); 
}

vec3 random3(vec2 p) {
    vec3 p3 = fract(vec3(p.xyx) * RANDOM_SCALE.xyz);
    p3 += dot(p3, p3.yxz + 19.19);
    return fract((p3.xxy + p3.yzz) * p3.zyx);
}

vec3 random3(vec3 p) {
    p = fract(p * RANDOM_SCALE.xyz);
    p += dot(p, p.yxz + 19.19);
    return fract((p.xxy + p.yzz) * p.zyx);
}

vec4 random4(float p) {
    vec4 p4 = fract(p * RANDOM_SCALE);
    p4 += dot(p4, p4.wzxy + 19.19);
    return fract((p4.xxyz + p4.yzzw) * p4.zywx);   
}

vec4 random4(vec2 p) {
    vec4 p4 = fract(p.xyxy * RANDOM_SCALE);
    p4 += dot(p4, p4.wzxy + 19.19);
    return fract((p4.xxyz + p4.yzzw) * p4.zywx);
}

vec4 random4(vec3 p) {
    vec4 p4 = fract(p.xyzx * RANDOM_SCALE);
    p4 += dot(p4, p4.wzxy + 19.19);
    return fract((p4.xxyz + p4.yzzw) * p4.zywx);
}

vec4 random4(vec4 p4) {
    p4 = fract(p4  * RANDOM_SCALE);
    p4 += dot(p4, p4.wzxy + 19.19);
    return fract((p4.xxyz + p4.yzzw) * p4.zywx);
}
#endif

Use:

float random[2|3](<float|float2|float3> value)

Check it on Github


#ifndef RANDOM_SCALE
#if defined(RANDOM_HIGHER_RANGE)
#define RANDOM_SCALE float4(0.1031, 0.1030, 0.0973, 0.1099)
#else
#define RANDOM_SCALE float4(443.897, 441.423, 0.0973, 0.1099)
#endif
#endif
#ifndef RANDOM_SCALE_4
#define RANDOM_SCALE_4 float4(443.897, 441.423, .0973, 1.6334)
#endif

#ifndef FNC_RANDOM
#define FNC_RANDOM

float random(float x)
{
    #if defined(RANDOM_SINLESS)
    x = frac(x * RANDOM_SCALE.x);
    x *= x + 33.33;
    x *= x + x;
    return frac(x);
    #else
    return frac(sin(x) * 43758.5453);
    #endif
}

float random(float2 st)
{
    #if defined(RANDOM_SINLESS)
    float3 p3  = frac(st.xx * RANDOM_SCALE.xyz);
    p3 += dot(p3, p3.yzx + 33.33);
    return frac((p3.x + p3.y) * p3.z);
    #else
    return frac(sin(dot(st.xy, float2(12.9898, 78.233))) * 43758.5453);
    #endif
}

float random(float3 pos)
{
    #if defined(RANDOM_SINLESS)
    pos  = frac(pos * RANDOM_SCALE.xyz);
    pos += dot(pos, pos.zyx + 31.32);
    return frac((pos.x + pos.y) * pos.z);
    #else
    return frac(sin(dot(pos.xyz, float3(70.9898, 78.233, 32.4355))) * 43758.5453123);
    #endif
}

float random(float4 pos)
{
    #if defined(RANDOM_SINLESS)
    pos = frac(pos * RANDOM_SCALE);
    pos += dot(pos, pos.wzyx + 33.33);
    return frac((pos.x + pos.y) * (pos.z + pos.w));
    #else
    return frac(sin(dot(pos, float4(12.9898, 78.233, 45.164, 94.673))) * 43758.5453);
    #endif
}

float2 random2(float p)
{
    float3 p3 = frac(float3(p, p, p) * RANDOM_SCALE.xyz);
    p3 += dot(p3, p3.yzx + 19.19);
    return frac((p3.xx + p3.yz) * p3.zy);
}

float2 random2(float2 p)
{
    float3 p3 = frac(float3(p.x, p.x, p.x) * RANDOM_SCALE.xyz);
    p3 += dot(p3, p3.yzx + 19.19);
    return frac((p3.xx + p3.yz) * p3.zy);
}

float2 random2(float3 p3)
{
    p3 = frac(p3 * RANDOM_SCALE.xyz);
    p3 += dot(p3, p3.yzx + 19.19);
    return frac((p3.xx + p3.yz) * p3.zy);
}

float3 random3(float p)
{
    float3 scale = float3(RANDOM_SCALE.x, RANDOM_SCALE.y, RANDOM_SCALE.z);
    float3 p3 = frac(float3(p, p, p) * scale);
    p3 += dot(p3, p3.yzx + 19.19);
    return frac((p3.xxy + p3.yzz) * p3.zyx);
}


float3 random3(float2 p)
{
    float3 scale = float3(RANDOM_SCALE.x, RANDOM_SCALE.y, RANDOM_SCALE.z);
    float3 p3 = frac(float3(p.x, p.x, p.x) * scale);
    p3 += dot(p3, p3.yzx + 19.19);
    return frac((p3.xxy + p3.yzz) * p3.zyx);
}


float3 random3(float3 p)
{
    p = frac(p * RANDOM_SCALE.xyz);
    p += dot(p, p.yzx + 19.19);
    return frac((p.xxy + p.yzz) * p.zyx);
}

float4 random4(float p)
{
    float4 p4 = frac(float4(p, p, p, p) * RANDOM_SCALE_4);
    p4 += dot(p4, p4.wzyx + 19.19);
    return frac((p4.xxyz + p4.yzzw) * p4.zywx);
}

float4 random4(float2 p)
{
    float4 p4 = frac(float4(p.xx, p.yy) * RANDOM_SCALE_4);
    p4 += dot(p4, p4.wzyx + 19.19);
    return frac((p4.xxyz + p4.yzzw) * p4.zywx);
}

float4 random4(float3 p)
{
    float4 p4 = frac(float4(p.xyz, 1.0) * RANDOM_SCALE_4);
    p4 += dot(p4, p4.wzyx + 19.19);
    return frac((p4.xxyz + p4.yzzw) * p4.zywx);
}

float4 random4(float4 p4)
{
    p4 = frac(p4 * RANDOM_SCALE_4);
    p4 += dot(p4, p4.wzyx + 19.19);
    return frac((p4.xxyz + p4.yzzw) * p4.zywx);
}

#endif

Check it on Github

const RANDOM_SINLESS: bool = true;

const RANDOM_SCALE: vec4f = vec4f(.1031, .1030, .0973, .1099);

fn random(p: f32) -> f32 {
    var x = p;
    if (RANDOM_SINLESS) {
        x = fract(x * RANDOM_SCALE.x);
        x *= x + 33.33;
        x *= x + x;
        return fract(x);
    } else {
        return fract(sin(x) * 43758.5453);
    }
}

fn random2(st: vec2f) -> f32 {
    if (RANDOM_SINLESS) {
        var p3  = fract(vec3(st.xyx) * RANDOM_SCALE.xyz);
        p3 += dot(p3, p3.yzx + 33.33);
        return fract((p3.x + p3.y) * p3.z);
    } else {
        return fract(sin(dot(st.xy, vec2(12.9898, 78.233))) * 43758.5453);
    }
}

fn random3(p: vec3f) -> f32 {
    var pos = p;
    if (RANDOM_SINLESS) {
        pos  = fract(pos * RANDOM_SCALE.xyz);
        pos += dot(pos, pos.zyx + 31.32);
        return fract((pos.x + pos.y) * pos.z);
    } else {
        return fract(sin(dot(pos.xyz, vec3(70.9898, 78.233, 32.4355))) * 43758.5453123);
    }
}

fn random4(p: vec4f) -> f32 {
    var pos = p;
    if (RANDOM_SINLESS) {
        pos = fract(pos * RANDOM_SCALE);
        pos += dot(pos, pos.wzxy+33.33);
        return fract((pos.x + pos.y) * (pos.z + pos.w));
    } else {
        let dot_product = dot(pos, vec4(12.9898,78.233,45.164,94.673));
        return fract(sin(dot_product) * 43758.5453);
    }
}

fn random21(p: f32) -> vec2f {
    var p3 = fract(vec3(p) * RANDOM_SCALE.xyz);
    p3 += dot(p3, p3.yzx + 19.19);
    return fract((p3.xx + p3.yz) * p3.zy);
}

fn random22(p: vec2f) -> vec2f {
    var p3 = fract(p.xyx * RANDOM_SCALE.xyz);
    p3 += dot(p3, p3.yzx + 19.19);
    return fract((p3.xx + p3.yz) * p3.zy);
}

fn random23(p: vec3f) -> vec2f {
    var p3 = p;
    p3 = fract(p3 * RANDOM_SCALE.xyz);
    p3 += dot(p3, p3.yzx + 19.19);
    return fract((p3.xx + p3.yz) * p3.zy);
}

fn random31(p: f32) -> vec3f {
    var p3 = fract(vec3(p) * RANDOM_SCALE.xyz);
    p3 += dot(p3, p3.yzx + 19.19);
    return fract((p3.xxy + p3.yzz) * p3.zyx);
}

fn random32(p: vec2f) -> vec3f {
    var p3 = fract(vec3(p.xyx) * RANDOM_SCALE.xyz);
    p3 += dot(p3, p3.yxz + 19.19);
    return fract((p3.xxy + p3.yzz) * p3.zyx);
}

fn random33(p_: vec3f) -> vec3f {
    var p = fract(p_ * RANDOM_SCALE.xyz);
    p += dot(p, p.yxz + 19.19);
    return fract((p.xxy + p.yzz) * p.zyx);
}

fn random41(p: f32) -> vec4f {
    var p4 = fract(p * RANDOM_SCALE);
    p4 += dot(p4, p4.wzxy + 19.19);
    return fract((p4.xxyz + p4.yzzw) * p4.zywx);
}

fn random42(p: vec2f) -> vec4f {
    var p4 = fract(p.xyxy * RANDOM_SCALE);
    p4 += dot(p4, p4.wzxy + 19.19);
    return fract((p4.xxyz + p4.yzzw) * p4.zywx);
}

fn random43(p: vec3f) -> vec4f {
    var p4 = fract(p.xyzx  * RANDOM_SCALE);
    p4 += dot(p4, p4.wzxy + 19.19);
    return fract((p4.xxyz + p4.yzzw) * p4.zywx);
}

fn random44(p: vec4f) -> vec4f {
    var p4 = p;
    p4 = fract(p4  * RANDOM_SCALE);
    p4 += dot(p4, p4.wzxy + 19.19);
    return fract((p4.xxyz + p4.yzzw) * p4.zywx);
}

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