Exemplo n.º 1
0
    private static void setupPolyshapes(GameObject go)
    {
        // take the mesh vertices
        Vector3[] meshVertices = getMeshVertices(go);
        // split in several convex polygons
        List <Vector2[]> convexVects2 = BayazitPolygonDecomposer.ConvexPartition(meshVertices);

        // add a new ChipmunkPolyShape for every convex polygon
        for (int i = 0, c = convexVects2.Count; i < c; ++i)
        {
            Vector2[] vec2arr = convexVects2[i];
            // for every poly shape component set its vertex[] attribute as the convex vertex[]
            ChipmunkPolyShape polyShape = go.AddComponent <ChipmunkPolyShape>();
            polyShape.verts = vec2arr;
        }
    }
Exemplo n.º 2
0
    protected void OnSceneGUI()
    {
        ChipmunkPolyShape poly = target as ChipmunkPolyShape;

        if (poly != null)
        {
            SetupUndo("edited ChipmunkPolyShape");
            Transform t = poly.transform;

            bool dirty = false;
            var  verts = new List <Vector2>(poly.verts);

            switch (Event.current.type)
            {
            case EventType.mouseUp:
                verts             = new List <Vector2>(poly.hull);
                dirty             = true;
                insertedThisClick = false;
                break;

            case EventType.mouseMove:
                // If you repaint too much, you really use a lot of CPU. Only during mouse move seems reasonable.
                HandleUtility.Repaint();
                break;

            default: break;
            }

            // find closest new point handle.
            if (!insertedThisClick)
            {
                Handles.color = Color.red;
                Vector3 v      = ClosestPoint(t, poly.verts);
                Vector2 vDelta = DotHandle(v) - (Vector2)v;
                if (vDelta != Vector2.zero)
                {
                    // Insert a new vert at the front of the party.
                    verts.Insert(0, t.InverseTransformPoint((Vector2)v + vDelta));
                    insertedThisClick = true;
                    dirty             = true;
                }
            }

            Handles.color = Color.white;
            for (int i = 0; i < verts.Count; i++)
            {
                Vector3 v      = t.TransformPoint(verts[i]);
                Vector2 vDelta = DotHandle(v) - (Vector2)v;
                if (vDelta != Vector2.zero)
                {
                    verts[i] = t.InverseTransformPoint((Vector2)v + vDelta);
                    dirty    = true;
                }
            }

            if (dirty)
            {
                poly.verts = verts.ToArray();
                EditorUtility.SetDirty(target);
            }
        }
    }