gpt4 book ai didi

math - 谁能解释一下这个 GLSL 片段着色器在做什么?

转载 作者:行者123 更新时间:2023-12-04 09:32:34 24 4
gpt4 key购买 nike

我意识到这是一个以数学为中心的问题,但是...如果您查看此网页(并且有一个不错的显卡)http://mrdoob.github.com/three.js/examples/webgl_shader.html

如果您查看源代码,您会注意到一个看起来很吓人的片段着色器。

我不是在寻找详细的解释,而是在寻找正在发生的事情的想法,或者关于这里究竟发生了什么的信息来源..我不是在寻找 GLSL 指南,而是关于数学的信息。我意识到这可能更适合 Math StackExchange 站点,但我想我会先在这里尝试...

<script id="fragmentShader" type="x-shader/x-fragment">

uniform vec2 resolution;
uniform float time;

void main() {

vec2 p = -1.0 + 2.0 * gl_FragCoord.xy / resolution.xy;
float a = time*40.0;
float d,e,f,g=1.0/40.0,h,i,r,q;
e=400.0*(p.x*0.5+0.5);
f=400.0*(p.y*0.5+0.5);
i=200.0+sin(e*g+a/150.0)*20.0;
d=200.0+cos(f*g/2.0)*18.0+cos(e*g)*7.0;
r=sqrt(pow(i-e,2.0)+pow(d-f,2.0));
q=f/r;
e=(r*cos(q))-a/2.0;f=(r*sin(q))-a/2.0;
d=sin(e*g)*176.0+sin(e*g)*164.0+r;
h=((f+d)+a/2.0)*g;
i=cos(h+r*p.x/1.3)*(e+e+a)+cos(q*g*6.0)*(r+h/3.0);
h=sin(f*g)*144.0-sin(e*g)*212.0*p.x;
h=(h+(f-e)*q+sin(r-(a+h)/7.0)*10.0+i/4.0)*g;
i+=cos(h*2.3*sin(a/350.0-q))*184.0*sin(q-(r*4.3+a/12.0)*g)+tan(r*g+h)*184.0*cos(r*g+h);
i=mod(i/5.6,256.0)/64.0;
if(i<0.0) i+=4.0;
if(i>=2.0) i=4.0-i;
d=r/350.0;
d+=sin(d*d*8.0)*0.52;
f=(sin(a*g)+1.0)/2.0;
gl_FragColor=vec4(vec3(f*i/1.6,i/2.0+d/13.0,i)*d*p.x+vec3(i/1.3+d/8.0,i/2.0+d/18.0,i)*d*(1.0-p.x),1.0);

}

</script>

最佳答案

Monjori来自演示场景。

简单的答案是它使用一个公式来生成一个模式。 WebGL 将为屏幕上的每个像素调用一次此函数。唯一会改变的是时间和 gl_FragCoord,它是正在绘制的像素的位置。

让我们稍微分解一下

  // this is the resolution of the window
uniform vec2 resolution;

// this is a count in seconds.
uniform float time;

void main() {
// gl_FragCoord is the position of the pixel being drawn
// so this code makes p a value that goes from -1 to +1
// x and y
vec2 p = -1.0 + 2.0 * gl_FragCoord.xy / resolution.xy;

// a = the time speed up by 40
float a = time*40.0;

// declare a bunch of variables.
float d,e,f,g=1.0/40.0,h,i,r,q;

// e goes from 0 to 400 across the screen
e=400.0*(p.x*0.5+0.5);

// f goes from 0 to 400 down the screen
f=400.0*(p.y*0.5+0.5);

// i goes from 200 + or - 20 based
// on the sin of e * 1/40th + the slowed down time / 150
// or in other words slow down even more.
// e * 1/40 means e goes from 0 to 1
i=200.0+sin(e*g+a/150.0)*20.0;

// d is 200 + or - 18.0 + or - 7
// the first +/- is cos of 0.0 to 0.5 down the screen
// the second +/i is cos of 0.0 to 1.0 across the screen
d=200.0+cos(f*g/2.0)*18.0+cos(e*g)*7.0;

// I'm stopping here. You can probably figure out the rest
// see answer
r=sqrt(pow(i-e,2.0)+pow(d-f,2.0));
q=f/r;
e=(r*cos(q))-a/2.0;f=(r*sin(q))-a/2.0;
d=sin(e*g)*176.0+sin(e*g)*164.0+r;
h=((f+d)+a/2.0)*g;
i=cos(h+r*p.x/1.3)*(e+e+a)+cos(q*g*6.0)*(r+h/3.0);
h=sin(f*g)*144.0-sin(e*g)*212.0*p.x;
h=(h+(f-e)*q+sin(r-(a+h)/7.0)*10.0+i/4.0)*g;
i+=cos(h*2.3*sin(a/350.0-q))*184.0*sin(q-(r*4.3+a/12.0)*g)+tan(r*g+h)*184.0*cos(r*g+h);
i=mod(i/5.6,256.0)/64.0;
if(i<0.0) i+=4.0;
if(i>=2.0) i=4.0-i;
d=r/350.0;
d+=sin(d*d*8.0)*0.52;
f=(sin(a*g)+1.0)/2.0;
gl_FragColor=vec4(vec3(f*i/1.6,i/2.0+d/13.0,i)*d*p.x+vec3(i/1.3+d/8.0,i/2.0+d/18.0,i)*d*(1.0-p.x),1.0);
}

尝试查看正在发生的事情的好处之一是在着色器中插入早期退出。首先你可以在这里看到着色器

http://glsl.heroku.com/e#1579.0

或者

https://www.shadertoy.com/view/lsfyRS

如果我们转到第 11 行
      e=400.0*(p.x*0.5+0.5);

并在它之后插入这样的东西
      e=400.0*(p.x*0.5+0.5);
gl_FragColor = vec4(e / 400.0, 0, 0, 1);
return;

只要我们将值转换为从 0 到 1 的值,我们就可以看到结果

例如转到第 14 行
d=200.0+cos(f*g/2.0)*18.0+cos(e*g)*7.0;

由于我们知道它从 200 +/- 18 +/- 7 开始,即 175 + 225,因此将其转换为 0 到 1
d=200.0+cos(f*g/2.0)*18.0+cos(e*g)*7.0;
float tmp = (d - 175.0) / 50.0;
gl_FragColor = vec4(tmp, 0, 0, 1);
return;

会给你一些想法它在做什么。

我相信你可以解决剩下的问题。

关于math - 谁能解释一下这个 GLSL 片段着色器在做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9151238/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com