【发布时间】:2020-10-07 21:26:50
【问题描述】:
我目前正在学习如何在 GLSL 中生成噪声模式。
我正在尝试使用 GLSL 中的片段着色器创建矩形、颜色和噪声的组合。这是我当前的代码:
#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);
}
目前我只能让它生成黑白正方形和矩形,无论我如何调整“vec3 颜色”变量,我都无法让它以各种颜色显示矩形。
我的问题是:我怎样才能调整我的代码,使它分解成几个矩形而不是正方形和矩形,我怎样才能让我的颜色应用于这些矩形?这一切对我来说仍然很陌生,因此感谢您提供任何帮助。
【问题讨论】:
-
你说的是什么意思?“我怎样才能调整我的代码,使它分解成几个矩形而不是正方形和矩形”?
标签: opengl glsl rectangles fragment-shader noise