gpt4 book ai didi

glsl - Shadertoy - fragCoord vs iResolution vs fragColor

转载 作者:行者123 更新时间:2023-12-03 16:53:23 34 4
gpt4 key购买 nike

总的来说,我对 Shadertoy 和 GLSL 相当陌生。我已经成功地将许多 Shadertoy 着色器复制到 Blender 中,但实际上并不知道它是如何工作的。我一直在寻找教程,但我更像是一个视觉学习者。

如果有人可以解释,或者甚至更好,提供一些描述 fragCoord、iResolution 和 fragColor 之间差异的图像。那很好啊!

我主要对数字感兴趣。因为我使用 Blender 我习惯了 Canvas 是 0 到 1 -或- -1 到 1

这个特别让我有点困惑。

vec2 u = (fragCoord - iResolution.xy * .5) / iResolution.y * 8.;

example

在不知道坐标系的情况下,我无法在 Blender 中重现剩余的代码。

任何帮助将不胜感激!

最佳答案

这是正常的,您无法在不知道坐标系的情况下在 blender 中重现此代码。
The Shadertoy documentation states:

Image shaders implement the mainImage() function to generateprocedural images by calculating a color for each pixel in the image.This function is invoked once in each pixel and the host applicationmust provide the appropriate input data and retrieve the output colorto assign it to the corresponding pixel on the screen. The signatureof this function is:

void mainImage( out vec4 fragColor, in vec2 fragCoord);

where fragCoord contains the coordinates of the pixel for which theshader must calculate a color. These coordinates are counted in pixelswith values from 0.5 to resolution-0.5 over the entire renderingsurface and the resolution of this surface is transmitted to theshader via the uniform iResolution variable.


让我解释。 iResolution变量是 uniform vec3它包含窗口的尺寸,并通过一些 openGL 代码发送到着色器。 fragCoord variable 是一个内置变量,它包含应用着色器的像素的坐标。
更具体地说:
  • fragCoord : 是 vec2即 X 轴上的 0 > 640 和 Y 轴上的 0 > 360
  • iResolution : 是 vec2 X 值为 640,Y 值为 360


  • Standard normalize uv
    此图像是使用以下代码计算的:
        // Normalized pixel coordinates (between 0 and 1)
    vec2 uv = fragCoord/iResolution.xy;

    // Set R and G values based on position
    vec3 col = vec3(uv.x,uv.y,0);

    // Output to screen
    fragColor = vec4(col,1.0);
    输出范围从 0,0在左下角和 1,1在右上角。这是 OpenGL 设置的默认左下角窗口空间。

    Windows centred uv
    这个图像是用以下代码计算的:
        // Normalized pixel coordinates (between -0.5 and 0.5)
    vec2 uv = (fragCoord - iResolution.xy * 0.5)/iResolution.xy;

    // Set R and G values based on position
    vec3 col = vec3(uv.x,uv.y,0);

    // Output to screen
    fragColor = vec4(col,1.0);
    输出范围从 -0.5,-0.5在左下角和 0.5,0.5因为
    在第一步中,我们减去窗口大小的一半 [ 0.5 ] 从每个像素坐标 [ fragCoord ]。您可以看到红色和绿色值直到很久以后才开始可见的效果。
    您可能还想标准化 只有通过将第一步更改为 y 轴
    vec2 uv = (fragCoord - iResolution.xy * 0.5)/iResolution.y;
    根据我们的目的,如果您对两个轴进行标准化,图像可能看起来很奇怪,因此这是一种可能的策略。

    ceil() uv to see more clearly
    这个图像是用以下代码计算的:
        // Normalized pixel coordinates (between -0.5 to 0.5)
    vec2 uv = (fragCoord - iResolution.xy * 0.5)/iResolution.xy;

    // Set R and G values based on position using ceil() function
    // The ceil() function returns the smallest integer that is greater than the uv value
    vec3 col = vec3(ceil(uv.x),ceil(uv.y),0);

    // Output to screen
    fragColor = vec4(col,1.0);
    ceil()函数可以让我们看到图像的中心是 0, 0

    至于shadertoy文档的第二部分:

    The output color is returned in fragColor as a four-component vector,the last component being ignored by the client. The result isretrieved in an "out" variable in anticipation of the future additionof several rendering targets.


    这真的意味着 fragColor包含四个值,这些值被购买到渲染管道的下一个阶段。 You can find more about in and out variables here . fragColor 中的值确定应用着色器的像素的颜色。
    如果您想了解有关着色器的更多信息,这些是一些不错的起点:
    the book of shader - uniform
    learn OpenGL - shader

    关于glsl - Shadertoy - fragCoord vs iResolution vs fragColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58684315/

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