【发布时间】:2019-04-08 05:56:31
【问题描述】:
我是 GLSL 的新手,并从此处的教程中学习。 (它正在使用 ShaderToy)
我的问题是为什么您可以通过将 fragCoord 的 x 坐标除以 iResolution(screensize) 来将 x 坐标设置为 0-1。
这可能只是一个数学问题,但我很困惑“iResolution.x”究竟表示什么或这里进行了什么样的计算。 (是矢量划分吗?)
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;
}
【问题讨论】:
-
(可能)
fragCoord.xy是以像素为单位的片段的二维坐标。iResolution.xy是视口的大小(以像素为单位)。fragCoord.xy / iResolution.xy计算片段在 [0.0, 1.0] 范围内的坐标(分别用于x和y)。因此,坐标为xy.x > 0.5的所有片段都位于视口的右半部分。注意,这个可以简化vec2 xy = fragCoord.xy / iResolution.xy;fragColor = vec4(xy.x > 0.5 ? 1.0 : 0.0, 0.0, 0.0, 1.0); -
@Rabbid76:你的评论不是更像是一个答案吗?