예제 #1
0
 static void RenderGizmo(MegaShape shape, GizmoType gizmoType)
 {
     if ((gizmoType & GizmoType.NotInSelectionHierarchy) != 0)
     {
         if ((gizmoType & GizmoType.Active) != 0)
         {
             DrawGizmos(shape, new Color(1.0f, 1.0f, 1.0f, 1.0f));
             Color col = Color.yellow;
             col.a        = 0.5f;
             Gizmos.color = col;                     //Color.yellow;
             //CursorPos = shape.FindNearestPoint(CursorPos, 5, ref CursorKnot, ref CursorTangent);
             //CursorSpline = shape.FindNearestPoint(CursorPos, 5, ref CursorKnot, ref CursorTangent);
             //Gizmos.DrawSphere(shape.transform.TransformPoint(CursorPos), shape.KnotSize);
             CursorPos = shape.InterpCurve3D(0, CursorPercent * 0.01f, true);
             Gizmos.DrawSphere(shape.transform.TransformPoint(CursorPos), shape.KnotSize * 0.01f);
             Handles.color = Color.white;
             Handles.Label(shape.transform.TransformPoint(CursorPos), "Cursor " + CursorPercent.ToString("0.00") + "% - " + CursorPos);
         }
         else
         {
             DrawGizmos(shape, new Color(1.0f, 1.0f, 1.0f, 0.25f));
         }
     }
     Gizmos.DrawIcon(shape.transform.position, "MegaSpherify icon.png");
     Handles.Label(shape.transform.position, " " + shape.name);
 }
    public void OnSceneGUI()
    {
        //Undo.RegisterUndo(target, "Move Shape Points");
        //undoManager.CheckUndo(target);
        //Undo.CreateSnapshot();

        MegaShape shape = (MegaShape)target;

        Handles.matrix = Matrix4x4.identity;            //shape.transform.localToWorldMatrix;

        //Quaternion rot = shape.transform.rotation;
        //Vector3 trans = shape.transform.position;
        Matrix4x4 tm = shape.transform.localToWorldMatrix;              //Matrix4x4.TRS(trans, rot, Vector3.one);

        if (shape.selcurve > shape.splines.Count - 1)
        {
            shape.selcurve = 0;
        }

        bool recalc = false;

        Vector3 dragplane = Vector3.one;

        Color nocol = new Color(0, 0, 0, 0);

        for (int s = 0; s < shape.splines.Count; s++)
        {
            for (int p = 0; p < shape.splines[s].knots.Count; p++)
            {
                if (shape.drawKnots && s == shape.selcurve)
                {
                    pm = tm.MultiplyPoint(shape.splines[s].knots[p].p);

                    if (showlabels)
                    {
                        if (p == selected && s == shape.selcurve)
                        {
                            Handles.color = Color.white;
                            Handles.Label(pm, " Selected\n" + pm.ToString("0.000"));
                        }
                        else
                        {
                            Handles.color = shape.KnotCol;
                            Handles.Label(pm, " " + p);
                        }
                    }

                    Handles.color = nocol;
                    Vector3 newp = Handles.PositionHandle(pm, Quaternion.identity);
                    if (newp != pm)
                    {
                        Undo.SetSnapshotTarget(shape, "Knot Move");
                    }

                    Vector3 dl = Vector3.Scale(newp - pm, dragplane);
                    shape.splines[s].knots[p].p += dl;                          //Vector3.Scale(newp - pm, dragplane);

                    //delta = shape.splines[s].knots[p].p - pm;

                    shape.splines[s].knots[p].invec  += dl;                     //delta;
                    shape.splines[s].knots[p].outvec += dl;                     //delta;

                    //if ( shape.splines[s].knots[p].p != pm )
                    if (newp != pm)
                    {
                        selected = p;
                        recalc   = true;
                    }

                    //pm = shape.splines[s].knots[p].p;
                }

#if true
                if (shape.drawHandles && s == shape.selcurve)
                {
                    Handles.color = shape.VecCol;
                    pm            = tm.MultiplyPoint(shape.splines[s].knots[p].p);

                    Vector3 ip = tm.MultiplyPoint(shape.splines[s].knots[p].invec);
                    Vector3 op = tm.MultiplyPoint(shape.splines[s].knots[p].outvec);
                    Handles.DrawLine(pm, ip);
                    Handles.DrawLine(pm, op);

                    Handles.color = shape.HandleCol;

                    Vector3 invec = tm.MultiplyPoint(shape.splines[s].knots[p].invec);
                    Handles.color = nocol;
                    Vector3 newinvec = Handles.PositionHandle(invec, Quaternion.identity);

                    if (newinvec != invec)                              //shape.splines[s].knots[p].invec )
                    {
                        Undo.SetSnapshotTarget(shape, "Handle Move");
                    }
                    Vector3 dl = Vector3.Scale(newinvec - invec, dragplane);
                    shape.splines[s].knots[p].invec += dl;              //Vector3.Scale(newinvec - invec, dragplane);
                    if (invec != newinvec)                              //shape.splines[s].knots[p].invec )
                    {
                        if (shape.lockhandles)
                        {
                            //Vector3 d = invec - shape.splines[s].knots[p].invec;
                            shape.splines[s].knots[p].outvec -= dl;
                        }

                        //shape.splines[s].knots[p].invec = invec;
                        selected = p;
                        recalc   = true;
                    }
                    Vector3 outvec = tm.MultiplyPoint(shape.splines[s].knots[p].outvec);

                    Vector3 newoutvec = Handles.PositionHandle(outvec, Quaternion.identity);
                    if (newoutvec != outvec)                            //shape.splines[s].knots[p].outvec )
                    {
                        Undo.SetSnapshotTarget(shape, "Handle Move");
                    }
                    dl = Vector3.Scale(newoutvec - outvec, dragplane);
                    //outvec += dl;	//Vector3.Scale(newoutvec - outvec, dragplane);
                    shape.splines[s].knots[p].outvec += dl;
                    if (outvec != newoutvec)                            //shape.splines[s].knots[p].outvec )
                    {
                        if (shape.lockhandles)
                        {
                            //Vector3 d = outvec - shape.splines[s].knots[p].outvec;
                            shape.splines[s].knots[p].invec -= dl;
                        }

                        //shape.splines[s].knots[p].outvec = outvec;
                        selected = p;
                        recalc   = true;
                    }
                    Vector3 hp = tm.MultiplyPoint(shape.splines[s].knots[p].invec);
                    if (selected == p)
                    {
                        Handles.Label(hp, " " + p);
                    }

                    hp = tm.MultiplyPoint(shape.splines[s].knots[p].outvec);

                    if (selected == p)
                    {
                        Handles.Label(hp, " " + p);
                    }
                }
#endif
            }
        }

        // Draw nearest point (use for adding knot)
        Vector3 wcp          = tm.MultiplyPoint(CursorPos);
        Vector3 newCursorPos = Handles.PositionHandle(wcp, Quaternion.identity);

        if (newCursorPos != wcp)
        {
            Vector3 cd = newCursorPos - wcp;

            CursorPos += cd;

            float calpha = 0.0f;
            CursorPos     = shape.FindNearestPoint(CursorPos, 5, ref CursorKnot, ref CursorTangent, ref calpha);
            CursorPercent = calpha * 100.0f;
        }

        Handles.Label(tm.MultiplyPoint(CursorPos), "Cursor " + CursorPercent.ToString("0.00") + "% - " + CursorPos);

        if (recalc)
        {
            shape.CalcLength(10);
            shape.BuildMesh();
        }

        Handles.matrix = Matrix4x4.identity;
        //undoManager.CheckDirty(target);

        if (GUI.changed)
        {
            //Undo.RegisterCreatedObjectUndo(shape, "plop");
            Undo.CreateSnapshot();
            Undo.RegisterSnapshot();
        }

        Undo.ClearSnapshotTarget();
    }