LYGIA Shader Library

srandom (lygia/generative/srandom)

Signed Random

Use:

srandomX(<vec2|vec3> x)

Check it on Github


#ifndef FNC_SRANDOM
#define FNC_SRANDOM

float srandom(in float x) {
  return -1. + 2. * fract(sin(x) * 43758.5453);
}

float srandom(in vec2 st) {
  return -1. + 2. * fract(sin(dot(st.xy, vec2(12.9898, 78.233))) * 43758.5453);
}

float srandom(in vec3 pos) {
  return -1. + 2. * fract(sin(dot(pos.xyz, vec3(70.9898, 78.233, 32.4355))) * 43758.5453123);
}

float srandom(in vec4 pos) {
    float dot_product = dot(pos, vec4(12.9898,78.233,45.164,94.673));
    return -1. + 2. * fract(sin(dot_product) * 43758.5453);
}

vec2 srandom2(in vec2 st) {
    const vec2 k = vec2(.3183099, .3678794);
    st = st * k + k.yx;
    return -1. + 2. * fract(16. * k * fract(st.x * st.y * (st.x + st.y)));
}

vec3 srandom3(in vec3 p) {
    p = vec3( dot(p, vec3(127.1, 311.7, 74.7)),
            dot(p, vec3(269.5, 183.3, 246.1)),
            dot(p, vec3(113.5, 271.9, 124.6)));
    return -1. + 2. * fract(sin(p) * 43758.5453123);
}

vec2 srandom2(in vec2 p, const in float tileLength) {
    p = mod(p, vec2(tileLength));
    return srandom2(p);
}

vec3 srandom3(in vec3 p, const in float tileLength) {
    p = mod(p, vec3(tileLength));
    return srandom3(p);
}

#endif

Use:

srandomX(<float2|float3> x)

Check it on Github


#ifndef FNC_SRANDOM
#define FNC_SRANDOM

float2 srandom2(in float2 st) {
    const float2 k = float2(.3183099, .3678794);
    st = st * k + k.yx;
    return -1. + 2. * frac(16. * k * frac(st.x * st.y * (st.x + st.y)));
}

float3 srandom3(in float3 p) {
    p = float3( dot(p, float3(127.1, 311.7, 74.7)),
            dot(p, float3(269.5, 183.3, 246.1)),
            dot(p, float3(113.5, 271.9, 124.6)));
    return -1. + 2. * frac(sin(p) * 43758.5453123);
}

#endif

Use:

srandomX(<float2|float3> x)

Check it on Github


#ifndef FNC_SRANDOM
#define FNC_SRANDOM

float srandom(float x) {
  return -1. + 2. * fract(sin(x) * 43758.5453);
}

float srandom(float2 st) {
  return -1. + 2. * fract(sin(dot(st.xy, float2(12.9898, 78.233))) * 43758.5453);
}

float srandom(float3 pos) {
  return -1. + 2. * fract(sin(dot(pos.xyz, float3(70.9898, 78.233, 32.4355))) * 43758.5453123);
}

float srandom(float4 pos) {
    float dot_product = dot(pos, float4(12.9898,78.233,45.164,94.673));
    return -1. + 2. * fract(sin(dot_product) * 43758.5453);
}

float2 srandom2(float2 st) {
    const float2 k = float2(.3183099, .3678794);
    st = st * k + k.yx;
    return -1. + 2. * fract(16. * k * fract(st.x * st.y * (st.x + st.y)));
}

float3 srandom3(float3 p) {
    p = float3( dot(p, float3(127.1, 311.7, 74.7)),
            dot(p, float3(269.5, 183.3, 246.1)),
            dot(p, float3(113.5, 271.9, 124.6)));
    return -1. + 2. * fract(sin(p) * 43758.5453123);
}

float2 srandom2(float2 p, const float tileLength) {
    p = mod(p, float2(tileLength));
    return srandom2(p);
}

float3 srandom3(float3 p, const float tileLength) {
    p = mod(p, float3(tileLength));
    return srandom3(p);
}

#endif

Dependencies:

Use:

<f32> srandom(<f32>)
<f32> srandom2(<vec2f>)
<f32> srandom3(<vec3f>)
<f32> srandom4(<vec4f>)
<vec2> srandom22(<vec2f>)
<vec3> srandom33(<vec3f>)
<vec4> srandom44(<vec4f>)
<vec3> srandom_tile22(<vec2f>, <f32>)
<vec4> srandom_tile33(<vec3f>, <f32>)

Check it on Github



fn srandom(x: f32) -> f32 {
  return -1. + 2. * fract(sin(x) * 43758.5453);
}

fn srandom2(st: vec2f) -> f32 {
  return -1. + 2. * fract(sin(dot(st.xy, vec2(12.9898, 78.233))) * 43758.5453);
}

fn srandom3(pos: vec3f) -> f32 {
  return -1. + 2. * fract(sin(dot(pos.xyz, vec3(70.9898, 78.233, 32.4355))) * 43758.5453123);
}

fn srandom4(pos: vec4f) -> f32{
    let dot_product = dot(pos, vec4(12.9898,78.233,45.164,94.673));
    return -1. + 2. * fract(sin(dot_product) * 43758.5453);
}

fn srandom22(st: vec2f) -> vec2f {
    let k = vec2(.3183099, .3678794);
    let tmp = st * k + k.yx;
    return -1. + 2. * fract(16. * k * fract(tmp.x * tmp.y * (tmp.x + tmp.y)));
}

fn srandom33(p: vec3f) -> vec3f {
    let tmp = vec3( dot(p, vec3(127.1, 311.7, 74.7)),
            dot(p, vec3(269.5, 183.3, 246.1)),
            dot(p, vec3(113.5, 271.9, 124.6)));
    return -1. + 2. * fract(sin(tmp) * 43758.5453123);
}

fn srandom_tile22(p: vec2f, tileLength: f32) -> vec2f {
    let tmp = mod2(p, vec2(tileLength));
    return srandom22(tmp);
}

fn srandom_tile33(p: vec3f, tileLength: f32) -> vec3f {
    let tmp = mod3(p, vec3(tileLength));
    return srandom33(tmp);
}

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