markusfisch / ShaderEditor

Android app to create GLSL shaders and use them as live wallpaper
https://play.google.com/store/apps/details?id=de.markusfisch.android.shadereditor
MIT License
924 stars 137 forks source link

Problem with random #210

Closed eliaswolfy closed 1 month ago

eliaswolfy commented 1 month ago

I tried to implement a random hash that I saw on Shadertoy, but the result was not as expected.

#version 320 es

#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif

out vec4 flagColor;
uniform int frame;
uniform vec3 resolution;

// random
uint rngState;
uint PCGHash() {
  uint state = rngState;
  rngState = rngState * 747796405u + 2891336453u;
  uint word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;
  return (word >> 22u) ^ word;
}

float random() {
    return float(PCGHash()) / 4294967296.;
}

void main() {
    float index = gl_FragCoord.y * resolution.x + gl_FragCoord.x;
    rngState = uint(index) + uint(frame) * 719393u;

    flagColor = vec4(vec3(random()), 1);
}

the result: Screenshot_20240929_153516_Shader Editor