获取边缘顶点然后在边缘上绘制线条

时间:2017-01-29 01:16:57

标签: c# unity3d vertex

我正在尝试获取3D对象的顶点,然后使用LineRenderer绘制线条。我希望线条按顺序绘制,而不是穿过模型的中间,但线条穿过模型。下图显示了它的样子:

正如你所看到的,我几乎已经完成了这项任务,但线路正在通过模型。

可能需要对椎骨点进行分类。我怎样才能解决这个问题?如何在不通过对象的情况下在边缘绘制点?

只需将下面的代码附加到一个简单的多维数据集即可查看上述问题:

public class MapEdgeOutline : MonoBehaviour
{
    Color beginColor = Color.yellow;
    Color endColor = Color.red;
    float hightlightSize = 0.1f;

    void Start()
    {
        createEdgeLineOnModel();
    }


    void createEdgeLineOnModel()
    {
        EdgeVertices allVertices = findAllVertices();

        //Get the points from the Vertices
        Vector3[] verticesToDraw = allVertices.vertices.ToArray();

        drawLine(verticesToDraw);
    }

    //Draws lines through the provided vertices
    void drawLine(Vector3[] verticesToDraw)
    {
        //Create a Line Renderer Obj then make it this GameObject a parent of it
        GameObject childLineRendererObj = new GameObject("LineObj");
        childLineRendererObj.transform.SetParent(transform);

        //Create new Line Renderer if it does not exist
        LineRenderer lineRenderer = childLineRendererObj.GetComponent<LineRenderer>();
        if (lineRenderer == null)
        {
            lineRenderer = childLineRendererObj.AddComponent<LineRenderer>();
        }

        //Assign Material to the new Line Renderer
        //Hidden/Internal-Colored
        //Particles/Additive
        lineRenderer.material = new Material(Shader.Find("Hidden/Internal-Colored"));


        //Set color and width
        lineRenderer.SetColors(beginColor, endColor);
        lineRenderer.SetWidth(hightlightSize, hightlightSize);

        //Convert local to world points
        for (int i = 0; i < verticesToDraw.Length; i++)
        {
            verticesToDraw[i] = gameObject.transform.TransformPoint(verticesToDraw[i]);
        }

        //5. Set the SetVertexCount of the LineRenderer to the Length of the points
        lineRenderer.SetVertexCount(verticesToDraw.Length + 1);
        for (int i = 0; i < verticesToDraw.Length; i++)
        {
            //Draw the  line
            Vector3 finalLine = verticesToDraw[i];
            lineRenderer.SetPosition(i, finalLine);

            //Check if this is the last loop. Now Close the Line drawn
            if (i == (verticesToDraw.Length - 1))
            {
                finalLine = verticesToDraw[0];
                lineRenderer.SetPosition(verticesToDraw.Length, finalLine);
            }
        }
    }

    EdgeVertices findAllVertices()
    {
        //Get MeshFilter from Cube
        Mesh mesh = GetComponent<MeshFilter>().mesh;
        Vector3[] meshVerts = mesh.vertices;

        //Get temp vert array
        float[][] meshVertsArray;
        meshVertsArray = new float[meshVerts.Length][];

        //Where to store the vertex points
        EdgeVertices allVertices = new EdgeVertices();
        allVertices.vertices = new List<Vector3>();

        int indexCounter = 0;

        //Get x,y,z vertex point
        while (indexCounter < meshVerts.Length)
        {
            meshVertsArray[indexCounter] = new float[3];

            meshVertsArray[indexCounter][0] = meshVerts[indexCounter].x;
            meshVertsArray[indexCounter][1] = meshVerts[indexCounter].y;
            meshVertsArray[indexCounter][2] = meshVerts[indexCounter].z;

            Vector3 tempVect = new Vector3(meshVertsArray[indexCounter][0], meshVertsArray[indexCounter][1], meshVertsArray[indexCounter][2]);

            //Store the vertex pont
            allVertices.vertices.Add(tempVect);

            indexCounter++;
        }
        return allVertices;
    }
}

public struct EdgeVertices
{
    public List<Vector3> vertices;
}

0 个答案:

没有答案