I am studying noise from the bookofshaders and in 2d noise this is the code in the book or website.
我正在研究着色器书中的噪波,在2D噪波中,这是书或网站中的代码。
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
// 2D Random
float random (in vec2 st) {
return fract(sin(dot(st.xy,
vec2(12.9898,78.233)))
* 43758.5453123);
}
// 2D Noise based on Morgan McGuire @morgan3d
// https://www.shadertoy.com/view/4dS3Wd
float noise (in vec2 st) {
vec2 i = floor(st);
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));
// Smooth Interpolation
// Cubic Hermine Curve. Same as SmoothStep()
vec2 u = f*f*(3.0-2.0*f);
// u = smoothstep(0.,1.,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;
// Scale the coordinate system to see
// some noise in action
vec2 pos = vec2(st*5.0);
// Use the noise function
float n = noise(pos);
gl_FragColor = vec4(vec3(n), 1.0);
}
There are few things i am not able to understand in this code is
在这段代码中,我不能理解的事情有几点
why is the mixing or combining noise value from all the corner has calculation like that
mix(a, b, u.x) + (c - a)* u.y * (1.0 - u.x) + (d - b) * u.x * u.y;
from initial understanding it is easy guess that everything is linear interpolation but i am not able to understand the thinking behind why u.y*(1.0-u.x)
and u.x*u.y
are used as a factor.
从最初的理解来看,很容易猜测一切都是线性插值法,但我不能理解为什么u*(1.0-u x)和u x*u y被用作一个因子。
is it something like someone choosed this specific way and there is no concerete reason why it is specificially done this way or this mixing is not standard way of mixing but people can mix anyway they want ?
这是不是像是有人选择了这种特定的方式,没有具体的理由来具体地这样做,或者这种混合不是标准的混合方式,但人们可以随心所欲地混合?
and in the line of code
vec2 pos = vec2(st*5.0)
i am not able to understand how does this increase the frequency of noise.
我不能理解这怎么会增加噪音的频率。
can anyone please help me understand this ?
有谁能帮我理解一下吗?
更多回答
@PeterO. thank you for comment, i checked that before posting but could not find answer from that. My main question is what was the thinking behind that math of mixing.
@Petero。谢谢你的评论,我在发帖前检查了一下,但找不到答案。我的主要问题是,混合数学背后的想法是什么。
@yosmo78 yes thank you so much for pointing me to right direction, yes it turned out rearranged bilinear interpolation. Thank you so much
@yosmo78是的,非常感谢你为我指明了正确的方向,是的,结果是重新排列的双线性插值法。非常感谢
@PravinPoudel as for 2), st desribes every pixel in Space in the Space of [(0,0)-(width,height)] as a fraction in the space [(0,0)-(1,1)]. So since you can think that your noise function is f(x) where x is the fraction in the space between 0 to 1 for each component of the vector. You are then sampling that function at each pixel on the screen. then you are just scaling the input to the noise function (in essence shrinking the input space)
@PravinPoudel对于2),st将空间[(0,0)-(宽度,高度)]中的每个像素描述为空间[(0,0)-(1,1)]中的分数。既然你可以认为你的噪声函数是f(X),这里x是从0到1的分数,对于向量的每个分量。然后在屏幕上的每个像素上对该函数进行采样。然后,您只需将输入缩放到噪波函数(实质上是缩小输入空间)
for question 1,
对于问题1,
that equation was just complicated version of
这个方程式只是复杂版本的
float a1 = mix(a, b, u.x);
float b1 = mix(c, d, u.x);
return mix(a1, b1, u.y);
Thank you
谢谢
更多回答
我是一名优秀的程序员,十分优秀!