Exemplo n.º 1
0
    public static Mesh GenerateBoxMesh(Settings settings, Vector3 shift)
    {
        var mesh = new MeshGenerator("Box");
        var size = settings.Size * 0.5f;

        mesh.AddQuad(
            new Vector3(-size.x, +size.y, +size.z) + shift,
            new Vector3(+size.x, +size.y, +size.z) + shift,
            new Vector3(+size.x, +size.y, -size.z) + shift,
            new Vector3(-size.x, +size.y, -size.z) + shift,
            Vector3.up);
        mesh.AddQuad(
            new Vector3(+size.x, -size.y, +size.z) + shift,
            new Vector3(-size.x, -size.y, +size.z) + shift,
            new Vector3(-size.x, -size.y, -size.z) + shift,
            new Vector3(+size.x, -size.y, -size.z) + shift,
            Vector3.down);
        mesh.AddQuad(
            new Vector3(+size.x, +size.y, -size.z) + shift,
            new Vector3(+size.x, +size.y, +size.z) + shift,
            new Vector3(+size.x, -size.y, +size.z) + shift,
            new Vector3(+size.x, -size.y, -size.z) + shift,
            Vector3.right);
        mesh.AddQuad(
            new Vector3(-size.x, +size.y, +size.z) + shift,
            new Vector3(-size.x, +size.y, -size.z) + shift,
            new Vector3(-size.x, -size.y, -size.z) + shift,
            new Vector3(-size.x, -size.y, +size.z) + shift,
            Vector3.down);
        mesh.AddQuad(
            new Vector3(+size.x, -size.y, +size.z) + shift,
            new Vector3(+size.x, +size.y, +size.z) + shift,
            new Vector3(-size.x, +size.y, +size.z) + shift,
            new Vector3(-size.x, -size.y, +size.z) + shift,
            Vector3.forward);
        mesh.AddQuad(
            new Vector3(+size.x, +size.y, -size.z) + shift,
            new Vector3(+size.x, -size.y, -size.z) + shift,
            new Vector3(-size.x, -size.y, -size.z) + shift,
            new Vector3(-size.x, +size.y, -size.z) + shift,
            Vector3.back);
        return(mesh.Generate());
    }
Exemplo n.º 2
0
    public static Mesh GeneratePrysmMesh(Settings settings, Vector3 shift)
    {
        var   mesh       = new MeshGenerator("Prysm");
        int   segments   = settings.edgeCount;
        float startAngle = settings.startAngle / 180f * Mathf.PI;

        Vector3[] sideNormals  = new Vector3[segments + 1];
        Vector3[] topPoints    = new Vector3[segments + 1];
        Vector3[] bottomPoints = new Vector3[segments + 1];
        for (int i = 0; i < topPoints.Length; i++)
        {
            float angleInRad = 2f * Mathf.PI / (float)segments * (float)i + startAngle;
            float angleOfNormalInRad;
            if (settings.smoothNormals)
            {
                angleOfNormalInRad = 2f * Mathf.PI / (float)segments * (float)(i) + startAngle;
            }
            else
            {
                angleOfNormalInRad = 2f * Mathf.PI / (float)segments * (float)(i + 0.5f) + startAngle;
            }
            topPoints[i]    = new Vector3(Mathf.Sin(angleInRad), 0, Mathf.Cos(angleInRad)) * settings.radius + shift;
            bottomPoints[i] = topPoints[i] + new Vector3(0, -settings.depth);
            sideNormals[i]  = new Vector3(Mathf.Sin(angleOfNormalInRad), 0, Mathf.Cos(angleOfNormalInRad));
        }
        Vector3 topCenter    = shift;
        Vector3 bottomCenter = shift + new Vector3(0, -settings.depth);

        for (int i = 0; i < segments; i++)
        {
            mesh.AddTriangle(topCenter, topPoints[i], topPoints[i + 1]);
        }
        if (settings.depth != 0)
        {
            for (int i = 0; i < segments; i++)
            {
                if (settings.smoothNormals)
                {
                    mesh.AddQuad(
                        topPoints[i + 1],
                        topPoints[i],
                        bottomPoints[i],
                        bottomPoints[i + 1],
                        sideNormals[i + 1],
                        sideNormals[i],
                        sideNormals[i],
                        sideNormals[i + 1]
                        );
                }
                else
                {
                    mesh.AddQuad(
                        topPoints[i + 1],
                        topPoints[i],
                        bottomPoints[i],
                        bottomPoints[i + 1],
                        sideNormals[i]
                        );
                }
            }
        }
        for (int i = 0; i < segments; i++)
        {
            mesh.AddTriangle(bottomCenter, bottomPoints [i + 1], bottomPoints [i]);
        }
        return(mesh.Generate());
    }