private void DrawMeshTypeInspector(MeshCreator meshCreator)
    {
        meshCreator.meshType = (MeshCreator.MeshType)EditorGUILayout.EnumPopup("Type", meshCreator.meshType);
        GUILayoutOption[] emptyLayout = null;

        switch (meshCreator.meshType)
        {
        case MeshCreator.MeshType.Triangle:
            meshCreator.triangleVertex1 = EditorGUILayout.Vector2Field("First vertex", meshCreator.triangleVertex1);
            meshCreator.triangleVertex2 = EditorGUILayout.Vector2Field("Second vertex", meshCreator.triangleVertex2);
            meshCreator.triangleVertex3 = EditorGUILayout.Vector2Field("Third vertex", meshCreator.triangleVertex3);
            break;

        case MeshCreator.MeshType.Rectangle:
            meshCreator.boxSize = EditorGUILayout.Vector2Field("Size", meshCreator.boxSize);
            break;

        case MeshCreator.MeshType.Circle:
            meshCreator.circleRadius            = EditorGUILayout.Slider("Radius", meshCreator.circleRadius, 0.0001f, 16, null);
            meshCreator.circleSides             = EditorGUILayout.IntSlider("Sides", meshCreator.circleSides, 3, 128);
            meshCreator.circleUseCircleCollider = EditorGUILayout.Toggle("Use CircleCollider", meshCreator.circleUseCircleCollider);
            break;

        case MeshCreator.MeshType.Quadrangle:
            meshCreator.quadrangleVertex1 = EditorGUILayout.Vector3Field("First vertex", meshCreator.quadrangleVertex1);
            meshCreator.quadrangleVertex2 = EditorGUILayout.Vector3Field("Second vertex", meshCreator.quadrangleVertex2);
            meshCreator.quadrangleVertex3 = EditorGUILayout.Vector3Field("Third vertex", meshCreator.quadrangleVertex3);
            meshCreator.quadrangleVertex4 = EditorGUILayout.Vector3Field("Fourth vertex", meshCreator.quadrangleVertex4);
            break;

        case MeshCreator.MeshType.Ellipse:
            meshCreator.ellipseHorizontalRadius = EditorGUILayout.Slider("Horizontal Radius", meshCreator.ellipseHorizontalRadius, 0.0001f, 16, null);
            meshCreator.ellipseVerticalRadius   = EditorGUILayout.Slider("Vertical Radius", meshCreator.ellipseVerticalRadius, 0.0001f, 16, null);
            meshCreator.ellipseSides            = EditorGUILayout.IntSlider("Sides", meshCreator.ellipseSides, 3, 128);
            break;

        case MeshCreator.MeshType.PointedCircle:
            meshCreator.pointedCircleRadius = EditorGUILayout.Slider("Radius", meshCreator.pointedCircleRadius, 0.0001f, 16, null);
            meshCreator.pointedCircleSides  = EditorGUILayout.IntSlider("Sides", meshCreator.pointedCircleSides, 3, 128);
            meshCreator.pointedCircleShift  = EditorGUILayout.Vector2Field("Shift", meshCreator.pointedCircleShift);
            break;

        case MeshCreator.MeshType.Cake:
            meshCreator.cakeRadius      = EditorGUILayout.Slider("Radius", meshCreator.cakeRadius, 0.0001f, 16, null);
            meshCreator.cakeSides       = EditorGUILayout.IntSlider("Sides", meshCreator.cakeSides, 3, 128);
            meshCreator.cakeSidesToFill = EditorGUILayout.IntSlider("Sides to fill", meshCreator.cakeSidesToFill, 1, meshCreator.cakeSides);
            break;

        case MeshCreator.MeshType.Convex:
            convexPointList.Draw();
            break;

        case MeshCreator.MeshType.Star:
            meshCreator.starRadiusA = EditorGUILayout.Slider("Radius A", meshCreator.starRadiusA, 0.0001f, 16, null);
            meshCreator.starRadiusB = EditorGUILayout.Slider("Radius B", meshCreator.starRadiusB, 0.0001f, 16, null);
            meshCreator.starSides   = EditorGUILayout.IntSlider("Sides", meshCreator.starSides, 3, 128);
            break;

        case MeshCreator.MeshType.Gear:
            meshCreator.gearInnerRadius = EditorGUILayout.Slider("Inner radius", meshCreator.gearInnerRadius, 0.0001f, 16, null);
            meshCreator.gearRootRadius  = EditorGUILayout.Slider("Root radius", meshCreator.gearRootRadius, meshCreator.gearInnerRadius, meshCreator.gearOuterRadius, null);
            meshCreator.gearOuterRadius = EditorGUILayout.Slider("Outer radius", meshCreator.gearOuterRadius, meshCreator.gearRootRadius, 16, null);
            meshCreator.gearSides       = EditorGUILayout.IntSlider("Sides", meshCreator.gearSides, 3, 128);
            break;

        case MeshCreator.MeshType.Line:
            meshCreator.lineWidth             = EditorGUILayout.Slider("Line width", meshCreator.lineWidth, 0.0001f, 1f, null);
            meshCreator.lineUseDoubleCollider = EditorGUILayout.Toggle("Use double collider", meshCreator.lineUseDoubleCollider, emptyLayout);
            linePointsList.Draw();
            break;

        case MeshCreator.MeshType.TriangulatedMesh:
            triangulatedPointList.Draw();
            break;

        case MeshCreator.MeshType.SplineShape:
            splineShapePointList.Draw();
            var points = CatmullRomSpline.GetPoints(meshCreator.splinePoints.ToArray(), meshCreator.splineResolution);
            DrawSplineInspector(meshCreator, points, true);
            break;

        case MeshCreator.MeshType.SplineCurve:
            splineCurvePointList.Draw();
            meshCreator.splineCurveWidth             = EditorGUILayout.Slider("Width", meshCreator.splineCurveWidth, 0.0001f, 5f);
            meshCreator.splineCurveUseDoubleCollider = EditorGUILayout.Toggle("Use double collider",
                                                                              meshCreator.splineCurveUseDoubleCollider);
            DrawSplineInspector(meshCreator, CatmullRomSpline.GetPoints(meshCreator.splineCurvePoints, meshCreator.splineResolution, false), false);
            break;

        case MeshCreator.MeshType.SplineConvexShape:
            convexSplinePointList.Draw();
            List <Vector2> splineConvexPoints = ConvexHull.QuickHull(meshCreator.convexSplinePoints);
            splineConvexPoints = CatmullRomSpline.GetPoints(splineConvexPoints, meshCreator.splineResolution, true);
            DrawSplineInspector(meshCreator, splineConvexPoints, true);
            break;

        default:
            throw new System.ArgumentOutOfRangeException();
        }

        SceneView.RepaintAll();
    }
Exemplo n.º 2
0
    void OnDrawGizmos()
    {
        Gizmos.color = Color.white;

        //cache position
        Vector2 p2 = transform.position;

        Vector2 lastPos;
        float   angleDelta;
        float   angleShift;

        switch (meshType)
        {
        case MeshType.Triangle:
            Gizmos.DrawLine(p2 + triangleVertex1, p2 + triangleVertex2);
            Gizmos.DrawLine(p2 + triangleVertex2, p2 + triangleVertex3);
            Gizmos.DrawLine(p2 + triangleVertex3, p2 + triangleVertex1);
            break;

        case MeshType.Rectangle:
            Gizmos.DrawLine(p2 + new Vector2(-boxSize.x / 2, -boxSize.y / 2), p2 + new Vector2(boxSize.x / 2, -boxSize.y / 2));
            Gizmos.DrawLine(p2 + new Vector2(boxSize.x / 2, -boxSize.y / 2), p2 + new Vector2(boxSize.x / 2, boxSize.y / 2));
            Gizmos.DrawLine(p2 + new Vector2(boxSize.x / 2, boxSize.y / 2), p2 + new Vector2(-boxSize.x / 2, boxSize.y / 2));
            Gizmos.DrawLine(p2 + new Vector2(-boxSize.x / 2, boxSize.y / 2), p2 + new Vector2(-boxSize.x / 2, -boxSize.y / 2));
            break;

        case MeshType.Circle:
            lastPos    = Vector2.right * circleRadius;
            angleDelta = 360 * Mathf.Deg2Rad / circleSides;
            for (int i = 1; i < circleSides + 1; i++)
            {
                var vertPos = new Vector2(Mathf.Cos(i * angleDelta), Mathf.Sin(i * angleDelta)) * circleRadius;
                Gizmos.DrawLine(p2 + lastPos, p2 + vertPos);
                lastPos = vertPos;
            }
            break;

        case MeshType.Quadrangle:
            Gizmos.DrawLine(p2 + quadrangleVertex1, p2 + quadrangleVertex2);
            Gizmos.DrawLine(p2 + quadrangleVertex2, p2 + quadrangleVertex3);
            Gizmos.DrawLine(p2 + quadrangleVertex3, p2 + quadrangleVertex4);
            Gizmos.DrawLine(p2 + quadrangleVertex4, p2 + quadrangleVertex1);
            break;

        case MeshType.Ellipse:
            lastPos    = Vector2.right * ellipseHorizontalRadius;
            angleDelta = 360 * Mathf.Deg2Rad / ellipseSides;
            for (int i = 1; i < ellipseSides + 1; i++)
            {
                var vertPos = new Vector2(Mathf.Cos(i * angleDelta) * ellipseHorizontalRadius, Mathf.Sin(i * angleDelta) * ellipseVerticalRadius);
                Gizmos.DrawLine(p2 + lastPos, p2 + vertPos);
                lastPos = vertPos;
            }
            break;

        case MeshType.PointedCircle:
            lastPos    = Vector2.right * pointedCircleRadius;
            angleDelta = 360 * Mathf.Deg2Rad / pointedCircleSides;
            for (int i = 1; i < pointedCircleSides + 1; i++)
            {
                var vertPos = new Vector2(Mathf.Cos(i * angleDelta), Mathf.Sin(i * angleDelta)) * pointedCircleRadius;
                Gizmos.DrawLine(p2 + lastPos, p2 + vertPos);
                lastPos = vertPos;
            }

            float angle = Mathf.Atan2(pointedCircleShift.y, pointedCircleShift.x);

            Vector2 a = new Vector2(Mathf.Cos(angle + Mathf.Deg2Rad * 90), Mathf.Sin(angle + Mathf.Deg2Rad * 90)) * pointedCircleRadius;
            Vector2 b = new Vector2(Mathf.Cos(angle - Mathf.Deg2Rad * 90), Mathf.Sin(angle - Mathf.Deg2Rad * 90)) * pointedCircleRadius;

            Gizmos.DrawLine(p2 + a, p2 + pointedCircleShift);
            Gizmos.DrawLine(p2 + pointedCircleShift, p2 + b);
            Gizmos.DrawLine(p2 + b, p2 + a);
            break;

        case MeshType.Cake:
            lastPos    = Vector2.right * cakeRadius;
            angleDelta = 360 * Mathf.Deg2Rad / cakeSides;
            for (int i = 1; i < cakeSidesToFill + 1; i++)
            {
                var vertPos = new Vector2(Mathf.Cos(i * angleDelta), Mathf.Sin(i * angleDelta)) * cakeRadius;
                Gizmos.DrawLine(p2 + lastPos, p2 + vertPos);
                lastPos = vertPos;
            }
            if (cakeSidesToFill != cakeSides)
            {
                Gizmos.DrawLine(p2, p2 + lastPos);
                Gizmos.DrawLine(p2, p2 + Vector2.right * cakeRadius);
            }

            break;

        case MeshType.Convex:
            if (convexPoints != null && convexPoints.Count < 2)
            {
                return;
            }
            var convexOutline = ConvexHull.QuickHull(convexPoints);
            for (int i = 0; i < convexOutline.Count; i++)
            {
                Gizmos.DrawLine(p2 + convexOutline[i], p2 + convexOutline[(i + 1) % convexOutline.Count]);
            }
            break;

        case MeshType.Star:
            angleDelta = 360 / (float)starSides / 2 * Mathf.Deg2Rad;
            angleShift = -360f / (starSides * 4) * Mathf.Deg2Rad;
            lastPos    = new Vector3(Mathf.Cos(angleShift), Mathf.Sin(angleShift)) * starRadiusA;
            for (int i = 1; i < starSides * 2 + 1; i++)
            {
                var vertPos = new Vector2(Mathf.Cos(i * angleDelta + angleShift),
                                          Mathf.Sin(i * angleDelta + angleShift));
                vertPos *= (i % 2 == 0 ? starRadiusA : starRadiusB);
                Gizmos.DrawLine(p2 + lastPos, p2 + vertPos);
                lastPos = vertPos;
            }
            break;

        case MeshType.Gear:
            angleDelta = 360 * Mathf.Deg2Rad / gearSides / 2;
            angleShift = angleDelta * 0.5f;
            float   outerAngleShift = angleDelta * 0.2f;
            Vector2 lastInnerPos    = new Vector2(Mathf.Cos(angleShift), Mathf.Sin(angleShift)) * gearInnerRadius;
            Vector2 lastRootPos     = new Vector2(Mathf.Cos(angleShift), Mathf.Sin(angleShift)) * gearRootRadius;
            Vector2 lastOuterPos    = new Vector2(Mathf.Cos(angleShift + outerAngleShift), Mathf.Sin(angleShift + outerAngleShift)) * gearOuterRadius;
            for (int i = 1; i < gearSides * 2 + 1; i++)
            {
                var vertPos = new Vector2(Mathf.Cos(i * angleDelta + angleShift), Mathf.Sin(i * angleDelta + angleShift)) * gearInnerRadius;
                Gizmos.DrawLine(p2 + lastInnerPos, p2 + vertPos);
                lastInnerPos = vertPos;

                vertPos = new Vector3(Mathf.Cos(i * angleDelta + angleShift), Mathf.Sin(i * angleDelta + angleShift)) * gearRootRadius;
                Gizmos.DrawLine(p2 + lastRootPos, p2 + vertPos);
                lastRootPos = vertPos;

                int sign = (i % 2) * 2 - 1;
                vertPos = new Vector3(Mathf.Cos(i * angleDelta + angleShift - outerAngleShift * sign), Mathf.Sin(i * angleDelta + angleShift - outerAngleShift * sign)) * gearOuterRadius;
                Gizmos.DrawLine(p2 + lastRootPos, p2 + vertPos);
                if (i % 2 == 1)
                {
                    Gizmos.DrawLine(p2 + lastOuterPos, p2 + vertPos);
                }
                lastOuterPos = vertPos;
            }
            break;

        case MeshType.Line:
            for (int i = 0; i < linePoints.Count - 1; i++)
            {
                Gizmos.DrawLine(p2 + linePoints[i], p2 + linePoints[i + 1]);
            }

            if (linePoints.Count < 2 || lineWidth <= 0.0001f)
            {
                return;
            }

            Vector2[] points = GetLinePoints();
            for (int i = 0; i < points.Length - 1; i++)
            {
                Gizmos.DrawLine(p2 + points[i], p2 + points[i + 1]);
            }
            break;

        case MeshType.TriangulatedMesh:
            for (int i = 0; i < triangulatedPoints.Count; i++)
            {
                Gizmos.DrawLine(p2 + triangulatedPoints[i],
                                p2 + triangulatedPoints[(i + 1) % triangulatedPoints.Count]);
            }
            break;

        case MeshType.SplineShape:
            List <Vector2> shapePoints = CatmullRomSpline.GetPoints(splinePoints.ToArray(), splineResolution);
            shapePoints = SimplifySplinePoints(shapePoints, true);
            for (int i = 0; i < shapePoints.Count; i++)
            {
                Gizmos.DrawLine(p2 + shapePoints[i], p2 + shapePoints[(i + 1) % shapePoints.Count]);
            }
            break;

        case MeshType.SplineCurve:
            List <Vector2> curvePoints = CatmullRomSpline.GetPoints(splineCurvePoints, splineResolution, false);
            curvePoints = SimplifySplinePoints(curvePoints, false);
            int len = curvePoints.Count;
            if (len <= 1)
            {
                return;
            }

            for (int i = 0; i < len - 1; i++)
            {
                Gizmos.DrawLine(p2 + curvePoints[i], p2 + curvePoints[i + 1]);
            }

            List <Vector2> leftCurvePoints  = new List <Vector2>();
            List <Vector2> rightCurvePoints = new List <Vector2>();
            // first vertex
            {
                Vector2 dir = (curvePoints[1] - curvePoints[0]).normalized;
                dir = new Vector2(-dir.y, dir.x) * splineCurveWidth;
                Gizmos.DrawLine(p2 + curvePoints[0] - dir, p2 + curvePoints[0] + dir);
                leftCurvePoints.Add(p2 + curvePoints[0] - dir);
                rightCurvePoints.Add(p2 + curvePoints[0] + dir);
            }
            // second to last - 1 vertices
            for (int i = 1; i < len - 1; i++)
            {
                float   leftAngle   = Mathf.Atan2(curvePoints[i].y - curvePoints[i - 1].y, curvePoints[i].x - curvePoints[i - 1].x) * Mathf.Rad2Deg + 90;
                float   rightAngle  = Mathf.Atan2(curvePoints[i + 1].y - curvePoints[i].y, curvePoints[i + 1].x - curvePoints[i].x) * Mathf.Rad2Deg + 90;
                float   middleAngle = leftAngle + Mathf.DeltaAngle(leftAngle, rightAngle) * 0.5f;
                Vector2 dir         = new Vector2(Mathf.Cos(middleAngle * Mathf.Deg2Rad), Mathf.Sin(middleAngle * Mathf.Deg2Rad)) * splineCurveWidth;
                //Gizmos.DrawLine(p2 + curvePoints[i] - dir, p2 + curvePoints[i] + dir);
                leftCurvePoints.Add(p2 + curvePoints[i] - dir);
                rightCurvePoints.Add(p2 + curvePoints[i] + dir);
            }
            // last vertex
            {
                Vector2 dir = (curvePoints[len - 2] - curvePoints[len - 1]).normalized;
                dir = new Vector2(-dir.y, dir.x) * splineCurveWidth;
                Gizmos.DrawLine(p2 + curvePoints[len - 1] - dir, p2 + curvePoints[len - 1] + dir);
                leftCurvePoints.Add(p2 + curvePoints[len - 1] + dir);
                rightCurvePoints.Add(p2 + curvePoints[len - 1] - dir);
            }

            for (int i = 0; i < leftCurvePoints.Count - 1; i++)
            {
                Gizmos.DrawLine(leftCurvePoints[i], leftCurvePoints[i + 1]);
            }
            for (int i = 0; i < rightCurvePoints.Count - 1; i++)
            {
                Gizmos.DrawLine(rightCurvePoints[i], rightCurvePoints[i + 1]);
            }
            break;

        case MeshType.SplineConvexShape:
            List <Vector2> splineConvexPoints = ConvexHull.QuickHull(convexSplinePoints);
            splineConvexPoints = CatmullRomSpline.GetPoints(splineConvexPoints, splineResolution);
            splineConvexPoints = SimplifySplinePoints(splineConvexPoints, true);
            for (int i = 0; i < splineConvexPoints.Count; i++)
            {
                Gizmos.DrawLine(p2 + splineConvexPoints[i], p2 + splineConvexPoints[(i + 1) % splineConvexPoints.Count]);
            }
            break;

        default:
            throw new System.ArgumentOutOfRangeException();
        }
    }