예제 #1
0
        public void Init()
        {
            mesh    = GetComponent <MeshFilter>().sharedMesh;
            verts   = mesh.vertices;
            Deforms = new Puppet2D_VertexDeform[verts.Length];
            List <Vector3> uniquePos = new List <Vector3>();

            int index = 0;

            foreach (Vector3 vert in verts)
            {
                vertPos = transform.TransformPoint(vert);
                GameObject Deform = new GameObject("vert" + index);
                Deform.transform.position = vertPos;
                Deform.transform.parent   = transform;
                Puppet2D_VertexDeform giz = Deform.AddComponent <Puppet2D_VertexDeform>();
                giz.ConnectedIndexes = new List <int>();
                if (uniquePos.Contains(vert))
                {
                    giz.Active = false;
                    int dupIndex = uniquePos.IndexOf(vert);
                    Deforms[dupIndex].ConnectedIndexes.Add(index);
                    giz.ConnectedIndexes.Add(dupIndex);
                }

                uniquePos.Add(vert);


                giz.CurrentIndex = index;
                Deforms[index]   = giz;
                giz._parent      = this;
                giz.IntialPos    = vertPos;

                index++;
            }
            int id1, id2, id0;

            for (int triIndex = 0; triIndex < mesh.triangles.Length; triIndex += 3)
            {
                id0 = mesh.triangles[triIndex];
                id1 = mesh.triangles[triIndex + 1];
                id2 = mesh.triangles[triIndex + 2];

                if (!Deforms[id0].ConnectedIndexes.Contains(id1))
                {
                    Deforms[id0].ConnectedIndexes.Add(id1);
                }
                if (!Deforms[id0].ConnectedIndexes.Contains(id2))
                {
                    Deforms[id0].ConnectedIndexes.Add(id2);
                }

                if (!Deforms[id1].ConnectedIndexes.Contains(id0))
                {
                    Deforms[id1].ConnectedIndexes.Add(id0);
                }
                if (!Deforms[id1].ConnectedIndexes.Contains(id2))
                {
                    Deforms[id1].ConnectedIndexes.Add(id2);
                }

                if (!Deforms[id2].ConnectedIndexes.Contains(id0))
                {
                    Deforms[id2].ConnectedIndexes.Add(id0);
                }
                if (!Deforms[id2].ConnectedIndexes.Contains(id1))
                {
                    Deforms[id2].ConnectedIndexes.Add(id1);
                }
            }


            _intialised = true;
        }
예제 #2
0
        void Run()
        {
            //ColorVal = Color.white;
            // Change the size if the user requests it
            if (_lastKnownSize != _size)
            {
                _lastKnownSize = _size;
                CURRENT_SIZE   = _size;
            }

            // Ensure the rest of the gizmos know the size has changed...
            if (CURRENT_SIZE != _lastKnownSize)
            {
                _lastKnownSize = CURRENT_SIZE;
                _size          = _lastKnownSize;
            }



            // FALL OFF WIP


            if (_lastKnownFalloff != _falloff)
            {
                _lastKnownFalloff = _falloff;
                CURRENT_FALLOFF   = _falloff;
            }


            if (CURRENT_FALLOFF != _lastKnownFalloff)
            {
                _lastKnownFalloff = CURRENT_FALLOFF;
                _falloff          = _lastKnownFalloff;
            }

            if (UndoRedo)
            {
                return;
            }

            float dist = 0;

            List <int>  WorkingList = new List <int>();
            Queue <int> indexQueue  = new Queue <int>();

            List <GameObject> testGo = new List <GameObject>();

            foreach (GameObject go in Selection.gameObjects)
            {
                Puppet2D_VertexDeform vd = go.GetComponent <Puppet2D_VertexDeform>();
                if (vd)
                {
                    vd.Selected = true;
                    //if(vd.Active)
                    testGo.Add(go);
                }
            }

            if (testGo.Contains(this.gameObject))
            {
                //if (Active)
                {
                    WorkingList.Add(CurrentIndex);
                    Editted  = true;
                    ColorVal = Color.green;

                    _parent.verts[CurrentIndex] = _parent.Deforms[CurrentIndex].transform.position;

                    for (int c = 0; c < ConnectedIndexes.Count; c++)
                    {
                        indexQueue.Enqueue(ConnectedIndexes[c]);
                    }
                    Color blendColor = Color.white;
                    while (indexQueue.Count > 0)
                    {
                        int workingIndex = indexQueue.Dequeue();
                        if (!_parent.Deforms[workingIndex].Editted)
                        {
                            _parent.Deforms[workingIndex].ColorVal = blendColor;
                            WorkingList.Add(workingIndex);
                            _parent.Deforms[workingIndex].Editted = true;

                            dist = Vector3.Distance(IntialPos, _parent.Deforms[workingIndex].IntialPos);
                            if (dist < _falloff)
                            {
                                float blend = (1 - (dist / _falloff));
                                Undo.RecordObject(_parent.Deforms[workingIndex].transform, "bs record");
                                Undo.RecordObject(_parent, "bs record");
                                if (!_parent.Deforms[workingIndex].Selected)
                                {
                                    _parent.Deforms[workingIndex].transform.position += (transform.position - CurrentPos) * blend;
                                }
                                _parent.verts[workingIndex] = _parent.Deforms[workingIndex].transform.position;

                                blendColor = Color.Lerp(Color.white, Color.green, blend);
                                _parent.Deforms[workingIndex].ColorVal = blendColor;

                                for (int c = 0; c < _parent.Deforms[workingIndex].ConnectedIndexes.Count; c++)
                                {
                                    indexQueue.Enqueue(_parent.Deforms[workingIndex].ConnectedIndexes[c]);
                                }
                            }
                        }
                    }
                }
            }
        }