Reputation: 61
I am currently learning about generating Noise patterns in GLSL.
I am trying to create a composition of rectangles, colors and noise using a fragment shader in GLSL. Here is my current code:
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
uniform vec2 u_mouse;
// 2D Random
float random (in vec2 st) {
return fract(sin(dot(st.xy,
vec2(12.9898, 79)))
* 43758.5453123);
}
float noise (in vec2 st)
{
vec2 i = floor(st + 1.0);
vec2 f = fract(st);
// Four corners in 2D of a tile
float a = random(i);
float b = random(i + vec2(1.0, 0.0));
float c = random(i + vec2(0.0, 1.0));
float d = random(i + vec2(1.0, 1.0));
vec2 u = f*f*(3.0-2.0*f);
u = smoothstep(0.6, 1.0, f);
// Mix 4 coorners percentages
return mix(a, b, u.x) +
(c - a)* u.y * (1.0 - u.x) +
(d - b) * u.x * u.y;
}
void main() {
vec2 st = gl_FragCoord.xy/u_resolution.xy;
vec3 color = vec3(0.423,0.459,1.000);
vec2 pos = vec2(st * 4.0);
// Use the noise function
float n = noise(pos);
gl_FragColor = vec4(vec3(n), color);
}
Currently all I can get it to generate are black and white squares and rectangles and no matter how I adjust my "vec3 color" variable I can't get it show the rectangles in various colors.
My question is: how can I adjust my code so that it breaks down into just a handful of rectangles instead of squares and rectangles and how can I make my color apply to those rectangles?? This is all still foreign to me so any help is appreciated.
Upvotes: 1
Views: 172
Reputation: 210968
how can I make my color apply to those rectangles
You have to multiply color
by the gradient n
:
gl_FragColor = vec4(color * n, 1.0);
The misalignment of the rectangles is related to the line
u = smoothstep(0.6, 1.0, f)
You have to shift the rectangles dependent on the size of the smooth area:
float noise (in vec2 st)
{
float smoothsize = 0.2;
st -= smoothsize;
vec2 i = floor(st + 1.0);
vec2 f = fract(st);
// [...]
vec2 u = f*f*(3.0-2.0*f);
u = smoothstep(1.0- 2.0*smoothsize, 1.0, f);
// [...]
}
If you want to generate squares instead of rectangles, then you have to scale the st.x
by the aspect ratio:
vec2 st = gl_FragCoord.xy/u_resolution.xy;
st.x *= u_resolution.x/u_resolution.y;
Complete fragment shader:
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
uniform vec2 u_mouse;
// 2D Random
float random (in vec2 st)
{
return fract(sin(dot(st.xy, vec2(12.9898, 79))) * 43758.5453123);
}
float noise (in vec2 st)
{
float smoothsize = 0.2;
st -= smoothsize;
vec2 i = floor(st + 1.0);
vec2 f = fract(st);
// Four corners in 2D of a tile
float a = random(i);
float b = random(i + vec2(1.0, 0.0));
float c = random(i + vec2(0.0, 1.0));
float d = random(i + vec2(1.0, 1.0));
vec2 u = f*f*(3.0-2.0*f);
u = smoothstep(1.0- 2.0*smoothsize, 1.0, f);
// Mix 4 corners percentages
return mix(a, b, u.x) + (c-a) * u.y * (1.0-u.x) + (d-b) * u.x * u.y;
}
void main() {
vec2 st = gl_FragCoord.xy/u_resolution.xy;
// st.x *= u_resolution.x/u_resolution.y; <--- optional
// Use the noise function
float n = noise(st * 4.0);
vec3 color = vec3(0.423,0.459,1.000);
gl_FragColor = vec4(color * n, 1.0);
}
Upvotes: 1