例如,绘制一个这种三角形,我这里不特定指出Unity Editor ,他是一种数学知识。 例如,如图,先求出线段中心点c,然后指定一个边长 = m, 沿线段方向增加m距离,得到第一个点v0,同理,反向减少m距离得到x,然后让x沿垂直线段两边走m,得到其他两个点,三角形就绘制出来了。 然后求一条线段的另外一条垂直线段的方向有一个比较容易的解决办法是叉乘,把一条线段看成x轴,另外一条看成y轴,计算x和z的叉乘就得出y的方向出来了。 我还是简单贴一段我项目中用过的代码吧。
protected override void OnGUI()
{
Texture2D connectionTexture = (Texture2D)UnityEditor.Graphs.Styles.connectionTexture.image;
Handles.color = curState.curSelLinkItem == this ? Color.red : Color.white;
Vector2 start = begin.ToPivot();
Vector2 pEnd = end.ToPivot();
Handles.DrawAAPolyLine(connectionTexture, 4f, new Vector3[] { start, pEnd });
Vector3 cross = Vector3.Cross((start - pEnd).normalized, Vector3.forward);
Debug.LogError($"cross:{cross} - {start-pEnd} - {Vector3.forward}");
Vector3 diff = (pEnd - start);
Vector3 direction = diff.normalized;
Vector3 mid = ((0.5f * diff) + new Vector3(start.x,start.y,diff.z)) - (0.5f * cross);
Vector3 center = mid + direction;
DrawArrow(cross, direction, center, Handles.color);
}
private void DrawArrow(Vector3 cross, Vector3 direction, Vector3 center, Color color)
{
const float sideLength = 6f;
Vector3[] vertices = new Vector3[] {
center + (direction * sideLength),
(center - (direction * sideLength)) + (cross * sideLength),
(center - (direction * sideLength)) - (cross * sideLength)
};
UseLinkArrowMaterial();
GL.Begin(vertices.Length + 1);
GL.Color(color);
for (int i = 0; i < vertices.Length; i++)
{
GL.Vertex(vertices[i]);
}
GL.End();
}
private void UseLinkArrowMaterial()
{
if (linkArrowMaterial == null)
{
var shader = Shader.Find("Lines/Colored Blended") ?? Shader.Find("Legacy Shaders/Transparent/Diffuse") ?? Shader.Find("Transparent/Diffuse");
if (shader == null) return;
linkArrowMaterial = new Material(shader);
}
linkArrowMaterial.SetPass(0);
}
|