【问题标题】:What exactly does this calculation mean?这个计算究竟意味着什么?
【发布时间】:2019-04-08 05:56:31
【问题描述】:

我是 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”究竟表示什么或这里进行了什么样的计算。 (是矢量划分吗?)

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:你的评论不是更像是一个答案吗?

标签: math glsl webgl shader


【解决方案1】:

其他答案都是正确的。 fragCoord 是当前正在绘制的像素,iResolution 是屏幕的大小所以

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,这似乎 正是 cmets 所说的

需要注意的是,iResolution 和 fragCoord 是用户变量。在这种情况下,我猜你是从 Shadertoy 获得这个 GLSL。这些变量不是 WebGL 或 GLSL 的一部分,它们由 Shadertoy 定义,因此它们的值和含义由 shadertoy 定义。

请注意,如果您不熟悉 GLSL 和 WebGL,您可能需要考虑一些 webgl tutorials。另见this answer about shadertoy

【讨论】:

  • 非常感谢 gman 的回答。现在我完全明白了。是的,我一定会检查这些链接!
【解决方案2】:

iResolution.x 是屏幕的宽度(以像素为单位)。将像素 x 位置除以总宽度会将位置转换为屏幕宽度的一小部分。因此,如果您的屏幕是 1000 像素宽,并且您的当前位置是 x=500,xy.x = xy.x / iResolution.x; 会将xy.x 转换为 0.500。

【讨论】:

  • Bill the lizard,我正在关注您在 billthelizard.com 上的 sicp 上的博客,并且该网站似乎已关闭。你知道为什么吗?
  • 嗨@Jodast。 Blogger 和我的域托管公司之间似乎存在 DNS 错误。我看看今晚能不能解决。谢谢你告诉我!
  • @Jodast 在我解决这些问题之前,您可以在billthelizard.blogspot.com找到我的博客
  • 谢谢,但我的学区屏蔽了 blogspot。 SICP 是我本学期的 compsci 学校项目
猜你喜欢
  • 1970-01-01
  • 2014-03-23
  • 1970-01-01
  • 1970-01-01
  • 2015-10-29
  • 2020-06-05
  • 2021-08-01
  • 2012-10-23
  • 1970-01-01
相关资源
最近更新 更多