简体   繁体   English

片段着色器 - 画一条线?

[英]Fragment shader - drawing a line?

I was interested in how to draw a line with a specific width (or multiple lines) using a fragment shader. 我对如何使用片段着色器绘制具有特定宽度(或多行)的线感兴趣。 I stumbled on the this post which seems to explain it. 我偶然发现这篇文章似乎在解释它。

The challenge I have is understanding the logic behind it. 我所面临的挑战是理解它背后的逻辑。

A couple of questions: 几个问题:

  1. Our coordinate space in this example is (0.0-1.0,0.0-1.0), correct? 我在这个例子中的坐标空间是(0.0-1.0,0.0-1.0),对吗?
  2. If so, what is the purpose of the "uv" variable. 如果是这样,“uv”变量的目的是什么。 Since thickness is 500, the "uv" variable will be very small. 由于厚度为500,“uv”变量将非常小。 Therefore the distances from it to pont 1 and 2 (stored in the a and b variables)? 因此从它到pont 1和2的距离(存储在a和b变量中)?
  3. Finally, what is the logic behind the h variable? 最后,h变量背后的逻辑是什么?

i will try to answer all of your questions one by one: 我将尝试逐一回答您的所有问题:

1) Yes, this is in fact correct. 1)是的,这实际上是正确的。

2) It is common in 3d computer graphics to express coordinates(within certain boundaries) with floating-point values between 0 and 1(or between -1 and 1). 2)在3d计算机图形中常见的是表示坐标(在某些边界内),浮点值在0和1之间(或在-1和1之间)。 First of all, this makes it quite easy to decide whether a given value crosses said boundary or not, and abstracts away from a concept of "pixel" being a discrete image unit; 首先,这使得很容易判断给定值是否超过所述边界,并且抽象出“像素”是离散图像单元的概念; furthermore this common practise can be found pretty much everywhere else(think of device coordinates or texture coordinates) 此外,这种常见的做法几乎可以在其他任何地方找到(想想设备坐标或纹理坐标)

Don't be afraid that values that you are working with are less than one; 不要害怕与你合作的价值不到一个; in fact, in computer graphics you usually deal with floating-point arithmetics, and FLOAT types are quite good at expressing Real values line around the "1" point. 事实上,在计算机图形学中,你通常会处理浮点算术,而FLOAT类型则非常善于在“1”点附近表达Real值。

3) The formula give for h consists of 2 parts: the square-root part, and the 2/c coefficient. 3)h的公式由2部分组成:平方根部分和2 / c系数。 The square root part should be well known from scholl math classes - this is Heron formula for the area of a triangle(between a,b,c). 平方根部分应该从scholl数学类中众所周知 - 这是三角形区域(在a,b,c之间)的Heron公式。 2/c extracts the height of the said triangle, which is stored in h and is also the distance between point uv and the "ground line" of the triangle. 2 / c提取所述三角形的高度,该高度存储在h中,并且也是点uv与三角形的“地线”之间的距离。 This distance is then used to decide, where is uv in relation to the line p1-p2. 然后使用该距离来确定uv相对于线p1-p2的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM