What exactly does this calculation mean?
我是GLSL的新手,正在这里学习本教程。
(它正在使用ShaderToy)
https://gamedevelopment.tutsplus.com/tutorials/a-beginners-guide-to-coding-graphics-shaders--cms-23313
我的问题是为什么可以通过将fragCoord的x坐标除以iResolution(screensize)来将x坐标设置为0-1。
这可能只是一个数学问题,但是我很困惑" iResolution.x"到底是什么意思,或者在这里进行了什么样的计算。 (它是矢量除法吗?)
1 2 3 4 5 6 7 8 9 10 11 12 | void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec2 xy = fragCoord.xy; //We obtain our coordinates for the current pixel xy.x = xy.x / iResolution.x; //We divide the coordinates by the screen size xy.y = xy.y / iResolution.y; // Now x is 0 for the leftmost pixel, and 1 for the rightmost pixel vec4 solidRed = vec4(0,0.0,0.0,1.0); //This is actually black right now if(xy.x > 0.5){ solidRed.r = 1.0; //Set its red component to 1.0 } fragColor = solidRed; } |
其他答案是正确的。
1 2 | xy.x = xy.x / iResolution.x; //We divide the coordinates by the screen size xy.y = xy.y / iResolution.y |
给出归一化的值,其中xy.x从0跨到1,xy.y从0到1在屏幕上,这似乎正是注释所说的
请务必注意,尽管
请注意,如果您不熟悉GLSL和WebGL,则可能需要考虑一些webgl教程。另请参阅有关shadertoy
的答案