问题描述
我目前正在尝试统一编写一个着色器,如果两个国家不属于同一个玩家,则该着色器会在风险风格的游戏中围绕国家绘制三角形图案(视觉辅助来查看您的边界).
I am currently trying to write a shader in unity that draws a triangular pattern around countries in a risk-styled game if both countries are not owned by the same player (visual aid to see your borders).
现在,我在让着色器正确设置国家/地区时遇到问题.
Right now, I'm having an issue with making the shader set the countries properly.
它总是将国家 0 设置在左侧,国家 1 设置在右侧 - 国家 0 和 1 是通过程序设置的.
It always sets country 0 to the left, and country 1 to the right - country 0 and 1 are set programically.
线,一个边界,可以在 0 到 359 度之间.
The line, a border, can be between 0 and 359 degrees.
我如何找到国家 0 和 1 是我在线中点的左侧和右侧画了 3 个点,一个 .01f、一个 .1f 和一个 1f 远离每个方向的中点,然后旋转它们到适当位置的中点.
How I find the countries 0 and 1 is I draw 3 points to the left and right of the midpoint of the line, one .01f, one .1f and one 1f away from the midpoints in each direction, then spin them around the midpoint to the appropriate location.
之后,我会进行奇偶校验,看看这些点是在每个国家的内部还是外部,并比较权重结果(最近的得 3 分,中的得 2 分,最远的得 1,以防万一有人建造了一个真正搞砸了与另一个国家相邻的国家).
After that I do an even-odd check to see if the points are inside or outside of each country, and compare the weight results (closest gets 3 points, mid gets 2, furthest gets 1, just in case someone builds a really screwed up country that flanks the other country).
在我的测试地图中,一个接近等切的八边形,边界显示正确(在我颠倒国家 0 和 1 的位置后,如果角度超过 90 且小于或等于 180).可以正常工作,但在其他地图中效果不佳.
In my test map, a close to equally sliced octagon, the borders showed up correctly (after I reversed the positions of country 0 and 1 in the event the angle was over 90 and less then or equal 180). Worked without a flaw, but in other maps it doesn't work very well.
除了国家/地区分配外,其他一切都很好,所以我很好奇是否有人知道更好的方法来确定哪个点在左侧或旋转线上,或者更好的概念性方法来处理这个问题.
Everything but the country allocation works well, so I'm curious if anyone knows of a better way to figure out which point is to the left or a spun line, or a better conceptual way to handle this.
上面基本上是当我在做的时候,红色是左,右是蓝色,然后我只是检查 3 个不同的点,然后权衡找到的左右两边,并把它偶/奇到适当的国家(一个在 +/- .01,另一个在 +/- .1 和第三个 1,以防出现偶数/奇数舍入问题.
That above is basically when I'm doing, red being left right being blue, then I'm just checking 3 different spots then weighing in the lefts and rights found with even/odding it into the appropriate countries (one at +/- .01, the other at +/- .1 and the third 1, in case of even/odd rounding issues with closeness).
如果我发现 A 国在右侧,我会翻转它们,因为根据我绘制的角度它在左侧.(我的着色器先渲染左,后渲染右,这就是我这样做的原因).
I then flip them if I find that country A is to the right, as it is on the left according to the angles I had draw. (my shader renders left first and right second, hence why I do this).
推荐答案
一条线上的左/右方向是什么?
从上次编辑开始,这不是您的情况.为什么不使用点积?
From last edit is this not your case. Why not use dot product?
因此,如果行在 -x
方向上,则结果为负,如果在 +x
方向上,则结果为正.如果结果为零,则表示该线仅上升或下降,或者它只是一个点.如果您需要特定方向而不是左/右,则使用适当的 a
向量而不是 x
轴.
So if the line goes in -x
direction the result is negative and if in the +x
direction then the result is positive. if the result is zero that means the line goes up or down only or it is juts a point. If you need specific direction instead of left/right then use appropriate a
vector instead of x
axis.
dot(a,b)=a.x*b.x+a.y*b.y
二维dot(a,b)=a.x*b.x+a.y*b.y+a.z*b.z
3D
dot(a,b)=a.x*b.x+a.y*b.y
in 2Ddot(a,b)=a.x*b.x+a.y*b.y+a.z*b.z
in 3D
图像与 a
向量为单位大小的情况相关,在这种情况下,点的结果是 b
到 a
的垂直投影就像图片上的一样
Image is relevant for cases where a
vector is in unit size in that case the result of dot is perpendicular projection of b
into a
just like on image
某个点在哪一边?
我想这就是你需要的.
如您所见,如果您处理线 (P0,P1)
和点 P
您想要分类为三角形,那么它的 多边形缠绕也决定了线的一侧.所以对于隐式轴方向:
As you can see if you handle line (P0,P1)
and point P
you want to classify as triangle then its polygon winding determines also the side of the line. So for implicit axis directions:
- 顺时针(顺时针)多边形缠绕表示线的右侧
- CCW(逆时针)多边形缠绕表示线的左侧
- CW(clockwise) polygon winding means right side of the line
- CCW(counter-clockwise) polygon winding means left side of the line
如何上链?...只需计算法线向量并获取其 Z
坐标.它的极性(符号)决定了绕组(CW/CCW 或相反的方式取决于坐标系).
How to get winding? ... simply compute normal vector and get its Z
coordinate. Its polarity (sign) determines winding (CW/CCW or the other way around depends on the coordinate system).
- 法向量计算为三角形的两个顶点的叉积
(P1-P0)x(P-P1)
不需要计算其他轴只需 z
所以:
No need to compute other axises just the z
so:
normal.z = ((P1.x-P0.x)*(Py-P1.y)) - ((P1.y-P0.y)*(Px-P1.x))代码>
现在只需执行 if (normal.z<0) ... else ...
它永远不应该为零,除非您将其称为线上的点或线是点... 看这里类似的问题:Determine rotation direction/toward/variable point on a circle
Now just do if (normal.z<0) ... else ...
it should never be zero unless you call it for point on the line or the line is a point ... look here at similar question: Determine rotation direction /toward/ variable point on a circle
这篇关于旋转后查找线/点左侧的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!