英文:
why do sin/cos return values > 1 in glsl
问题
在Shadertoy中,我有以下的着色器代码:
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// 标准化的像素坐标(从0到1)
vec2 uv = fragCoord/iResolution.xy;
vec3 col = vec3(0.);
col.r = cos(uv.x* 0.05) > 1. ? 1. : 0.;
col.g = sin(uv.y*0.05 - 1.5707963267948966) < -1. ? 1. : 0.;
// 输出到屏幕
fragColor = vec4(col,1.0);
}
我预期cos和sin不应该返回大于1或小于-1的值,因此我应该看到完全黑色的屏幕。但实际上我看到了一小段红色和一小段绿色。有人能解释一下为什么吗?
我还应该注意到,我正在使用Mac,在Chrome和FireFox中重现此问题。在我的iPad或iPhone上没有这个问题。
Shader Toy链接:https://www.shadertoy.com/view/mdfBDl
英文:
I've got the following shader code in shadertoy:
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Normalized pixel coordinates (from 0 to 1)
vec2 uv = fragCoord/iResolution.xy;
vec3 col = vec3(0.);
col.r = cos(uv.x* 0.05) > 1. ? 1. : 0.;
col.g = sin(uv.y*0.05 - 1.5707963267948966) < -1. ? 1. : 0.;
// Output to screen
fragColor = vec4(col,1.0);
}
I would expect that cos and sin should never return values above 1 (or below -1), and thus I should have an entirely black screen. What am actually seeing is a small band of red and a small band of green. Can anyone explain to me why?
I should also note that I'm on a Mac, and reproduce this in Chrome and FireFox. I do not reproduce this on my ipad or iphone.
shader toy link: https://www.shadertoy.com/view/mdfBDl
答案1
得分: 3
这不是一个Shadertoy的问题。这是你的硬件问题。我有一张NVIDIA 3080的显卡,用你的代码看到的是一个全黑的屏幕,但是无论你在使用什么硬件,它在计算三角函数时都有一个非常轻微的近似误差。这并不是不寻常的。
如果你使用 col.g = sin(uv.y*0.05 - 1.5707963267948966) < -1.000001 ? 1. : 0.
这段代码,我猜你会得到全黑?需要多少个零才能让它变成全黑?我敢打赌它非常接近你的硬件的机器精度。
英文:
This isn't a shadertoy issue. It's your hardware. I have an NVIDIA 3080 and see an all black screen with your code, but whatever hardware you're using has a very slight approximation error in how it is computing trig functions. It's not unusual.
If you do col.g = sin(uv.y*0.05 - 1.5707963267948966) < -1.000001 ? 1. : 0.
, I assume you get all black? How many zeroes does it require to go all-black? I'd wager it's very close to machine epsilon for your hardware.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论