这个神奇的值〜0.222在哪里来自此着色器中绘制一个圆圈的?
我正在开发一个在WebGL中吸引一个圆圈的着色器,但它似乎可以工作,但我不确定如何。这是顶点着色器:
in vec2 a_unit;
uniform mat3 u_projection;
uniform vec3 u_transform;
out vec2 v_pos;
void main() {
v_pos = a_unit;
float r = u_transform.z;
float x = u_transform.x - r;
float y = u_transform.y - r;
float w = r * 2.0;
float h = r * 2.0;
mat3 world = mat3(
w, 0, 0,
0, h, 0,
x, y, 1
);
gl_Position = vec4(u_projection * world * vec3(a_unit, 1), 1);
}
这是片段着色器:
in vec2 v_pos;
uniform vec4 u_color;
uniform vec3 u_transform;
uniform mat3 u_projection;
out vec4 outputColor;
void main() {
vec2 dist = v_pos - 0.5;
if (dist.x * dist.x + dist.y * dist.y > .222) {
discard;
}
outputColor = u_color;
}
我传递到着色器a u_proctivection
矩阵,这只是我的相机。我还传递了u_transform
向量,这只是我圆圈的世界坐标(和世界半径)。
因此,例如,u_transform
可能是(200,300,25)
用于在x上绘制一个圆圈的
:300 ,带有radius:25
哦,a_unit
只是一个单位Quad:
[
0, 0,
0, 1,
1, 0,
1, 0,
0, 1,
1, 1,
]
逻辑是我只是绘制一个矩形,然后通过丢弃来夹住任何不半径内的东西。因此绘制了一个圆。
我的困惑在于这一行:
if (dist.x * dist.x + dist.y * dist.y > .222) { discard; }
我通过一遍又一遍的检查来提出值0.222
。我不明白它是如何工作的。为什么这个神奇的数字能够绘制一个圆圈?
我以为我需要将radius
转换为纹理坐标,但似乎不是必需的吗?但是我不确定为什么..
为什么这可以用任意半径绘制圆圈?正确的值是什么,因为0.222
在接近时并不是确切的值。我应该在这里使用什么正确的值,而不是0.222
?
I'm developing a shader that draws a circle in WebGL, and it seems to work, but I'm not sure how. Here is the vertex shader:
in vec2 a_unit;
uniform mat3 u_projection;
uniform vec3 u_transform;
out vec2 v_pos;
void main() {
v_pos = a_unit;
float r = u_transform.z;
float x = u_transform.x - r;
float y = u_transform.y - r;
float w = r * 2.0;
float h = r * 2.0;
mat3 world = mat3(
w, 0, 0,
0, h, 0,
x, y, 1
);
gl_Position = vec4(u_projection * world * vec3(a_unit, 1), 1);
}
And here's the fragment shader:
in vec2 v_pos;
uniform vec4 u_color;
uniform vec3 u_transform;
uniform mat3 u_projection;
out vec4 outputColor;
void main() {
vec2 dist = v_pos - 0.5;
if (dist.x * dist.x + dist.y * dist.y > .222) {
discard;
}
outputColor = u_color;
}
I pass in to the shader a u_projection
matrix, which is just my camera. And I also pass in a u_transform
vector, which is just the world coordinates (and world radius) of my circle.
So for example, u_transform
might be something like (200, 300, 25)
for drawing a circle at x: 200
, y: 300
, with radius: 25
Oh, and a_unit
is just a unit quad:
[
0, 0,
0, 1,
1, 0,
1, 0,
0, 1,
1, 1,
]
The logic is that I just draw a rectangle and then clip out via discarding anything not within the radius. And thus a circle is drawn.
My confusion lies on this line:
if (dist.x * dist.x + dist.y * dist.y > .222) { discard; }
I came up with the value 0.222
by just guessing and checking over and over. I don't understand how it works though. Why is this magical number able to draw a circle?
I thought I would need to convert the radius
into texture coordinates, but it doesn't seem like that is required? But I'm not sure why..
Why does this work to draw a circle with an arbitrary radius? And what is the proper value, because 0.222
, while being close, is not the exact value. What is the proper value I should be using instead of 0.222
here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试以下代码:(这是新代码):
此代码是您可以使用的新代码;)
You can try this code : (This is a new code) :
This code is a new code you can use ;)