简体   繁体   English

iOS金属线宽

[英]iOS Metal line width

I would like to set the width of a line that I'm drawing in Metal. 我想设置我在Metal中绘制的线条的宽度。 I can set the size of a point with point_size as explained here: 我可以使用point_size设置点的大小,如下所述:

https://developer.apple.com/library/prerelease/ios/documentation/Metal/Reference/MTLRenderCommandEncoder_Ref/index.html https://developer.apple.com/library/prerelease/ios/documentation/Metal/Reference/MTLRenderCommandEncoder_Ref/index.html

But, I'm not sure how it works with lines. 但是,我不确定它如何与线条配合使用。

Short answer would be there is no way to control line width in the same way as a point size in Metal. 简短的回答是没有办法以与Metal中的点大小相同的方式控制线宽。 Even in OpenGL graphics API, the function to do this (which used to exist as gllinewidth function) is now deprecated. 即使在OpenGL图形API中,现在也不推荐使用此函数(以前作为gllinewidth函数存在)。

An option would be to draw the line as a quad (a box), with two triangles. 一个选项是将线条绘制为四边形(一个方框),带有两个三角形。 This would let you control the width of the line. 这样可以控制线条的宽度。

If you want to stick to the line primitive itself, for some particular reason, the equivalent OpenGL question has been asked on StackOverflow already as seen here . 如果你想坚持到线原始本身,对于一些特殊的原因,相当于OpenGL的问题已经被问在计算器上已经是看到这里 The shader can be simple translated to Metal shading API. 着色器可以简单地转换为Metal shading API。

If you're dead set on using lines instead of drawing a 2D box with two rectangles you can draw multiple lines next to each other. 如果您设置使用线条而不是绘制带有两个矩形的2D框,则可以在彼此旁边绘制多条线。 You can do that by drawing the same two vertices multiple times with one drawPrimitives call, just increase the instanceCount to simulate the thickness and then in your vertex shader function you could use your vertex_id and a modulus operation or some other type of logic to translate the position of the line to simulate thickness. 你可以通过一次drawPrimitives调用多次绘制相同的两个顶点,只需增加instanceCount来模拟厚度,然后在你的顶点着色器函数中你可以使用你的vertex_id和一个模数运算或其他类型的逻辑来翻译线的位置来模拟厚度。 This will probably be a lot easier than trying to do something fancy in a fragment shader. 这可能比尝试在片段着色器中做一些奇特的事情要容易得多。

If you are drawing multiple lines, multiply your instanceCount by the thickness you want and adjust your vertex shader function logic to place those additional lines next to each other. 如果要绘制多条线,请将instanceCount乘以所需的厚度,并调整顶点着色器函数逻辑,以将这些附加线放在一起。

I experimented with this and found this to be a pretty cumbersome process. 我对此进行了实验,发现这是一个非常繁琐的过程。 As you need additional math to position the extra lines to thicken correctly. 因为您需要额外的数学运算来将额外的线条定位为正确加粗。 For example if you're drawing a horizontal line and want to increase thickness by two you'd want to modify the y value, but if it's vertical you'd want to modify the x. 例如,如果您正在绘制水平线并希望将厚度增加2,则需要修改y值,但如果它是垂直的,则您需要修改x。 Drawing at an angle gets more complicated. 以某个角度绘图变得更加复杂。 You'll also run into issues trying to match lengths with widths if you're attempting to draw shapes. 如果你试图绘制形状,你也会遇到试图将长度与宽度匹配的问题。

I think it's much better to just draw two triangles to make a rectangle to fake a line. 我认为只绘制两个三角形来制作一个矩形来伪造一条线要好得多。 The math will be much easier and simpler to understand. 理解数学将更加容易和简单。

In Geometry, Euclid's lines do not have any width, they only have length, the width they get during drawing is simply for representation. 在Geometry中,Euclid的线条没有任何宽度,它们只有长度,它们在绘制过程中获得的宽度仅用于表示。 So while I think lines may be good for debugging and development, like for displaying vectors or creating a grid to see scale, they're not what you want if you're trying to style them to present them to users. 因此,虽然我认为线条可能适合调试和开发,例如显示矢量或创建网格以查看比例,但如果您尝试将它们设置为向用户呈现它们,则它们不是您想要的。 In that case it's the wrong tool for the job. 在那种情况下,这是工作的错误工具。

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

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