Example #1
0
    public override void DrawSceneGUI()
    {
        MegaFFD ffd = (MegaFFD)target;

        bool snapshot = false;

        if (ffd.DisplayGizmo)
        {
            MegaModifiers context = ffd.GetComponent <MegaModifiers>();

            if (context && context.DrawGizmos)
            {
                Vector3 size  = ffd.lsize;
                Vector3 osize = ffd.lsize;
                osize.x = 1.0f / size.x;
                osize.y = 1.0f / size.y;
                osize.z = 1.0f / size.z;

                Matrix4x4  tm1 = Matrix4x4.identity;
                Quaternion rot = Quaternion.Euler(ffd.gizmoRot);
                tm1.SetTRS(-(ffd.gizmoPos + ffd.Offset), rot, Vector3.one);

                Matrix4x4 tm = Matrix4x4.identity;
                Handles.matrix = Matrix4x4.identity;

                if (context != null && context.sourceObj != null)
                {
                    tm = context.sourceObj.transform.localToWorldMatrix * tm1;
                }
                else
                {
                    tm = ffd.transform.localToWorldMatrix * tm1;
                }

                DrawGizmos(ffd, tm);                    //Handles.matrix);

                Handles.color = Color.yellow;
        #if false
                int pc = ffd.GridSize();
                pc = pc * pc * pc;
                for (int i = 0; i < pc; i++)
                {
                    Vector3 p = ffd.GetPoint(i);                        // + ffd.bcenter;

                    //pm = Handles.PositionHandle(p, Quaternion.identity);
                    pm = Handles.FreeMoveHandle(p, Quaternion.identity, ffd.KnotSize * 0.1f, Vector3.zero, Handles.CircleCap);

                    pm  -= ffd.bcenter;
                    p    = Vector3.Scale(pm, osize);
                    p.x += 0.5f;
                    p.y += 0.5f;
                    p.z += 0.5f;

                    ffd.pt[i] = p;
                }
        #endif
                int gs = ffd.GridSize();
                //int i = 0;

                Vector3 ttp = Vector3.zero;

                for (int z = 0; z < gs; z++)
                {
                    for (int y = 0; y < gs; y++)
                    {
                        for (int x = 0; x < gs; x++)
                        {
                            int index = ffd.GridIndex(x, y, z);
                            //Vector3 p = ffd.GetPoint(i);	// + ffd.bcenter;
                            Vector3 lp = ffd.GetPoint(index);
                            Vector3 p  = lp;                            //tm.MultiplyPoint(lp);	//ffdi);	// + ffd.bcenter;

                            Vector3 tp = tm.MultiplyPoint(p);
                            if (handles)
                            {
                                ttp = Handles.PositionHandle(tp, Quaternion.identity);

                                //pm = tm.inverse.MultiplyPoint(Handles.PositionHandle(tm.MultiplyPoint(p), Quaternion.identity));
                                //pm = PositionHandle(p, Quaternion.identity, handleSize, ffd.gizCol1.a);
                            }
                            else
                            {
                                ttp = Handles.FreeMoveHandle(tp, Quaternion.identity, ffd.KnotSize * 0.1f, Vector3.zero, Handles.CircleCap);
                            }

                            if (ttp != tp)
                            {
                                if (!snapshot)
                                {
                                    MegaUndo.SetSnapshotTarget(ffd, "FFD Lattice Move");
                                    snapshot = true;
                                }
                            }

                            pm = tm.inverse.MultiplyPoint(ttp);
                            Vector3 delta = pm - p;

                            //pm = lp + delta;

                            //ffd.SetPoint(x, y, z, pm);
                            pm  -= ffd.bcenter;
                            p    = Vector3.Scale(pm, osize);
                            p.x += 0.5f;
                            p.y += 0.5f;
                            p.z += 0.5f;

                            ffd.pt[index] = p;

                            if (mirrorX)
                            {
                                int y1 = y - (gs - 1);
                                if (y1 < 0)
                                {
                                    y1 = -y1;
                                }

                                if (y != y1)
                                {
                                    Vector3 p1 = ffd.GetPoint(ffd.GridIndex(x, y1, z));                                         // + ffd.bcenter;

                                    delta.y = -delta.y;
                                    p1     += delta;
                                    p1     -= ffd.bcenter;
                                    p       = Vector3.Scale(p1, osize);
                                    p.x    += 0.5f;
                                    p.y    += 0.5f;
                                    p.z    += 0.5f;

                                    ffd.pt[ffd.GridIndex(x, y1, z)] = p;
                                }
                            }

                            if (mirrorY)
                            {
                                int z1 = z - (gs - 1);
                                if (z1 < 0)
                                {
                                    z1 = -z1;
                                }

                                if (z != z1)
                                {
                                    Vector3 p1 = ffd.GetPoint(ffd.GridIndex(x, y, z1));                                         // + ffd.bcenter;

                                    delta.z = -delta.z;
                                    p1     += delta;
                                    p1     -= ffd.bcenter;
                                    p       = Vector3.Scale(p1, osize);
                                    p.x    += 0.5f;
                                    p.y    += 0.5f;
                                    p.z    += 0.5f;

                                    ffd.pt[ffd.GridIndex(x, y, z1)] = p;
                                }
                            }

                            if (mirrorZ)
                            {
                                int x1 = x - (gs - 1);
                                if (x1 < 0)
                                {
                                    x1 = -x1;
                                }

                                if (x != x1)
                                {
                                    Vector3 p1 = ffd.GetPoint(ffd.GridIndex(x1, y, z));                                         // + ffd.bcenter;

                                    delta.x = -delta.x;
                                    p1     += delta;
                                    p1     -= ffd.bcenter;
                                    p       = Vector3.Scale(p1, osize);
                                    p.x    += 0.5f;
                                    p.y    += 0.5f;
                                    p.z    += 0.5f;

                                    ffd.pt[ffd.GridIndex(x1, y, z)] = p;
                                }
                            }
                        }
                    }
                }

                Handles.matrix = Matrix4x4.identity;

                if (GUI.changed && snapshot)
                {
                    MegaUndo.CreateSnapshot();
                    MegaUndo.RegisterSnapshot();
                }

                MegaUndo.ClearSnapshotTarget();
            }
        }
    }