Exemple #1
0
        public void SlerpContacts(MeshDeformer deformer, Mesh original, Mesh colliderOriginal, ScaffoldWrapper prev, ScaffoldWrapper next)
        {
            if (m_isRigid)
            {
                return;
            }

            if (m_scaffold != null)
            {
                Scaffold prevObj = null;
                if (prev != null)
                {
                    int contactPointIndex = prev.CurveIndices.Max() * 3 + 3;
                    ControlPointMode mode = deformer.GetControlPointMode(contactPointIndex);
                    if (mode != ControlPointMode.Free)
                    {
                        prevObj = prev.Obj;
                    }
                }
                Scaffold nextObj = null;
                if (next != null)
                {
                    int contactPointIndex = next.CurveIndices.Min() * 3;
                    ControlPointMode mode = deformer.GetControlPointMode(contactPointIndex);
                    if (mode != ControlPointMode.Free)
                    {
                        nextObj = next.Obj;
                    }
                }
                m_scaffold.SlerpContacts(deformer, original, colliderOriginal, prevObj, nextObj, m_isRigid);
            }
        }
        public static void Straighten()
        {
            GameObject   selected  = Selection.activeObject as GameObject;
            ControlPoint ctrlPoint = selected.GetComponent <ControlPoint>();
            MeshDeformer deformer  = selected.GetComponentInParent <MeshDeformer>();
            int          index     = 0;

            if (ctrlPoint == null)
            {
                Scaffold scaffold = selected.GetComponent <Scaffold>();
                for (int i = 0; i < deformer.Scaffolds.Length; ++i)
                {
                    if (deformer.Scaffolds[i].Obj == scaffold)
                    {
                        index = deformer.Scaffolds[i].CurveIndices.Min() * 3 + 1;
                        break;
                    }
                }
            }
            else
            {
                index = ctrlPoint.Index;
            }


            Undo.RecordObject(deformer, "Battlehub.MeshDeformer.Straighten");
            MeshDeformerEditor.RecordScaffolds(deformer, "Battlehub.MeshDeformer.Straighten");
            EditorUtility.SetDirty(deformer);
            deformer.Straighten(index);
        }
        private void Update()
        {
            if (Input.GetMouseButtonDown(0))
            {
                Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hitInfo;
                if (Physics.Raycast(ray, out hitInfo))
                {
                    Scaffold scaffold = hitInfo.collider.gameObject.GetComponent <Scaffold>();
                    if (scaffold)
                    {
                        ScaffoldWrapper scaffoldWrapper = MeshDeformer.Scaffolds.Where(s => s.Obj == scaffold).FirstOrDefault();
                        int             curveIndex      = scaffoldWrapper.CurveIndices[0];
                        if (scaffoldWrapper.CurveIndices.Length > 1)
                        {
                            Debug.LogError("Not Supported");
                            return;
                        }

                        Vector3 hitPoint = hitInfo.point;

                        float t = CurveUtils.GetT(MeshDeformer, curveIndex, hitPoint);
                        Debug.Log(t);

                        GameObject         obj    = Instantiate(ObjPrefab);
                        MeshDeformerFollow follow = obj.GetComponent <MeshDeformerFollow>();
                        follow.Spline = MeshDeformer;
                        follow.T      = t;
                    }
                }
            }
        }
        private static void SetMode(GameObject selected, bool isRigid, ControlPointMode mode)
        {
            MeshDeformer meshDeformer = selected.GetComponentInParent <MeshDeformer>();

            if (meshDeformer == null)
            {
                return;
            }
            Scaffold     selectedScaffold     = selected.GetComponent <Scaffold>();
            ControlPoint selectedControlPoint = selected.GetComponent <ControlPoint>();


            Undo.RecordObject(meshDeformer, "Battlehub.MeshDeformer.SetMode");
            MeshDeformerEditor.RecordScaffolds(meshDeformer, "Battlehub.MeshDeformer.SetMode");
            EditorUtility.SetDirty(meshDeformer);

            if (selectedScaffold != null && selectedScaffold.gameObject != meshDeformer.gameObject)
            {
                ScaffoldWrapper scaffold = meshDeformer.Scaffolds.Where(s => s.Obj == selectedScaffold).FirstOrDefault();
                if (scaffold != null)
                {
                    for (int i = 0; i < scaffold.CurveIndices.Length; ++i)
                    {
                        int curveIndex = scaffold.CurveIndices[i];
                        if (mode == ControlPointMode.Free)
                        {
                            meshDeformer.SetIsRigid(curveIndex * 3, isRigid);
                        }

                        if (!isRigid)
                        {
                            meshDeformer.SetControlPointMode(curveIndex * 3, mode);
                            meshDeformer.SetControlPointMode(curveIndex * 3 + 3, mode);
                        }
                    }
                }
                else
                {
                    Debug.LogError("scaffold not found");
                }
            }
            else if (selectedControlPoint != null)
            {
                if (mode == ControlPointMode.Free)
                {
                    meshDeformer.SetIsRigid(selectedControlPoint.Index, isRigid);
                }

                if (!isRigid)
                {
                    meshDeformer.SetControlPointMode(selectedControlPoint.Index, mode);
                }
            }
            else
            {
                MeshDeformerEditor.SetMode(meshDeformer, mode, isRigid);
            }
        }
 public static void RecordScaffolds(MeshDeformer deformer, string name)
 {
     Scaffold[] scaffolds = deformer.GetComponentsInChildren <Scaffold>();
     for (int i = 0; i < scaffolds.Length; ++i)
     {
         Scaffold scaffold = scaffolds[i];
         Undo.RecordObject(scaffold, name);
     }
 }
Exemple #6
0
        protected override SplineBase GetTarget()
        {
            Scaffold scaffold = (Scaffold)target;

            if (scaffold != null)
            {
                SplineBase spline = scaffold.GetComponentInParent <SplineBase>();
                return(spline);
            }
            return(null);
        }
Exemple #7
0
 public ScaffoldWrapper(Scaffold scaffold, bool isRigid)
 {
     m_isRigid = isRigid;
     if (scaffold == null)
     {
         m_isEmptySpace = true;
     }
     else
     {
         m_scaffold   = scaffold;
         m_instanceId = scaffold.GetInstanceID();
     }
 }
        private static void CleanupCombined(GameObject gameObject)
        {
            MeshDeformer deformer = gameObject.GetComponent <MeshDeformer>();

            if (deformer != null)
            {
                UnityEngine.Object.DestroyImmediate(deformer);
            }
            Scaffold scaffold = gameObject.GetComponent <Scaffold>();

            if (scaffold != null)
            {
                UnityEngine.Object.DestroyImmediate(scaffold);
            }
        }
Exemple #9
0
        private void SlerpContacts(MeshDeformer deformer, Mesh mesh, Contact[] contacts, Scaffold prev, Mesh prevMesh, Scaffold next, Mesh nextMesh)
        {
            Vector3[] normals     = null;
            Vector3[] prevNormals = null;
            Vector3[] nextNormals = null;
            if (mesh == null)
            {
                return;
            }

            if (prev != null || next != null)
            {
                normals = mesh.normals;
            }

            if (prevMesh != null && prev != null && (prev != this || m_deformer.Scaffolds.Length == 1 && m_deformer.Loop))
            {
                prevNormals = prevMesh.normals;
                for (int i = 0; i < contacts.Length; ++i)
                {
                    Contact contact    = contacts[i];
                    Vector3 prevNormal = prevNormals[contact.Index2];
                    Vector3 normal     = normals[contact.Index1];
                    Vector3 slerped    = Vector3.Slerp(prevNormal, normal, 0.5f);
                    prevNormals[contact.Index2] = slerped;
                    normals[contact.Index1]     = slerped;
                }
            }

            if (nextMesh != null && next != null && (next != this || m_deformer.Scaffolds.Length == 1 && m_deformer.Loop))
            {
                nextNormals = nextMesh.normals;
                for (int i = 0; i < contacts.Length; ++i)
                {
                    Contact contact    = contacts[i];
                    Vector3 normal     = normals[contact.Index2];
                    Vector3 nextNormal = nextNormals[contact.Index1];
                    Vector3 slerped    = Vector3.Slerp(normal, nextNormal, 0.5f);

                    normals[contact.Index2]     = slerped;
                    nextNormals[contact.Index1] = slerped;
                }
            }

            if (prev != null)
            {
                if (mesh != null)
                {
                    mesh.normals = normals;
                }

                if (this != prev)
                {
                    if (prevMesh != null)
                    {
                        prevMesh.normals = prevNormals;
                    }
                }

                if (next != null && next != prev)
                {
                    if (nextMesh != null)
                    {
                        nextMesh.normals = nextNormals;
                    }
                }
            }
            else if (next != null)
            {
                if (mesh != null)
                {
                    mesh.normals = normals;
                }

                if (prev != null && prev != next)
                {
                    if (prevMesh != null)
                    {
                        prevMesh.normals = prevNormals;
                    }
                }

                if (this != next)
                {
                    if (nextMesh != null)
                    {
                        nextMesh.normals = nextNormals;
                    }
                }
            }
        }
Exemple #10
0
        public void SlerpContacts(MeshDeformer deformer, Mesh original, Mesh colliderOriginal, Scaffold prev, Scaffold next, bool isRigid)
        {
            if (isRigid)
            {
                return;
            }

            Mesh prevMesh = null;
            Mesh nextMesh = null;

            if (prev != null)
            {
                prevMesh = prev.m_mesh;
            }

            if (next != null)
            {
                nextMesh = next.m_mesh;
            }
            SlerpContacts(deformer, m_mesh, deformer.Contacts, prev, prevMesh, next, nextMesh);


            if (colliderOriginal == null)
            {
                return;
            }
            if (prev != null)
            {
                prevMesh = prev.m_colliderMesh;
            }

            if (next != null)
            {
                nextMesh = next.m_colliderMesh;
            }
            SlerpContacts(deformer, m_colliderMesh, deformer.ColliderContacts, prev, prevMesh, next, nextMesh);

            if (m_collider != null)
            {
                m_collider.sharedMesh = null;
                m_collider.sharedMesh = m_colliderMesh;
            }
        }
        public static bool Rollback(GameObject selected)
        {
            MeshDeformer deformer = selected.GetComponentInParent <MeshDeformer>();

            if (deformer != null)
            {
                selected = deformer.gameObject;
                Selection.activeGameObject = selected;
            }
            MeshFilter meshFilter = selected.GetComponent <MeshFilter>();

            if (meshFilter == null)
            {
                EditorUtility.DisplayDialog("MeshFilter required", "Select object with MeshFilter component", "OK");
                return(false);
            }

            if (deformer != null)
            {
                if (!EditorUtility.DisplayDialog("Are you sure?", "This action is irreversible. Deformation will be lost", "Ok", "Cancel"))
                {
                    return(false);
                }

                ControlPoint[] controlPoints = deformer.GetComponentsInChildren <ControlPoint>(true);
                for (int i = 0; i < controlPoints.Length; ++i)
                {
                    Object.DestroyImmediate(controlPoints[i].gameObject);
                }

                Scaffold[] scaffolds = deformer.GetComponentsInChildren <Scaffold>();
                for (int i = 0; i < scaffolds.Length; ++i)
                {
                    if (scaffolds[i].gameObject != deformer.gameObject)
                    {
                        Object.DestroyImmediate(scaffolds[i].gameObject);
                    }
                }

                Mesh original = deformer.Original;
                meshFilter.sharedMesh = original;

                Mesh colliderOriginal = deformer.ColliderOriginal;
                if (colliderOriginal != null)
                {
                    MeshCollider collider = deformer.GetComponent <MeshCollider>();
                    if (collider != null)
                    {
                        collider.sharedMesh = colliderOriginal;
                    }
                }

                Object.DestroyImmediate(deformer);
            }


            Scaffold scaffold = selected.GetComponent <Scaffold>();

            if (scaffold != null)
            {
                Object.DestroyImmediate(scaffold);
            }

            return(true);
        }
        private static void DeformAxis(Axis axis)
        {
            GameObject   selected     = (GameObject)Selection.activeObject;
            Mesh         colliderMesh = null;
            MeshCollider collider     = selected.GetComponent <MeshCollider>();

            if (collider != null)
            {
                colliderMesh = collider.sharedMesh;
            }

            Mesh         mesh     = null;
            MeshFilter   filter   = selected.GetComponent <MeshFilter>();
            MeshDeformer deformer = selected.GetComponent <MeshDeformer>();

            if (filter != null)
            {
                mesh = filter.sharedMesh;
            }

            bool deformerIsNull = (deformer == null);

            bool ok = MeshDeformerIntegration.RaiseBeforeDeform(selected, mesh);

            if (!ok)
            {
                return;
            }

            if (!Rollback(selected))
            {
                return;
            }

            deformer = selected.AddComponent <MeshDeformer>();

            if (filter != null)
            {
                if (selected.transform.localScale != Vector3.one * selected.transform.localScale.x)
                {
                    Debug.LogWarning("Object with Non-uniform scale!");

                    CombineResult result = MeshUtils.Combine(new[] { selected });
                    selected = result.GameObject;
                    filter   = selected.GetComponent <MeshFilter>();
                    mesh     = filter.sharedMesh;

                    MeshCollider combinedCollider = selected.GetComponent <MeshCollider>();
                    if (combinedCollider != null)
                    {
                        GameObject.DestroyImmediate(combinedCollider);
                    }
                }

                if (collider == null)
                {
                    bool result = false;
                    if (deformerIsNull)
                    {
                        result = EditorUtility.DisplayDialog("Mesh Collider?", "Create MeshCollider using MeshFilter's mesh?", "Yes", "No");
                    }

                    if (result)
                    {
                        collider = selected.AddComponent <MeshCollider>();
                        Undo.RegisterCreatedObjectUndo(collider, "Battlehub.MeshDeformer.Deform");
                        collider.sharedMesh       = GameObject.Instantiate(mesh);
                        collider.sharedMesh.name  = mesh.name + " Collider";
                        deformer.ColliderOriginal = collider.sharedMesh;
                    }
                }
                else
                {
                    deformer.ColliderOriginal = colliderMesh;
                }
            }



            deformer.Axis = axis;
            deformer.ResetDeformer();

            Undo.RegisterCreatedObjectUndo(deformer, "Battlehub.MeshDeformer.Deform");

            Scaffold scaffold = selected.GetComponent <Scaffold>();

            if (scaffold != null)
            {
                Undo.RegisterCreatedObjectUndo(scaffold, "Battlehub.MeshDeformer.Deform");
            }


            EditorUtility.SetDirty(deformer);
        }
Exemple #13
0
        protected override void OnInspectorGUIOverride()
        {
            if (m_meshDeformer == null)
            {
                m_meshDeformer = (MeshDeformer)GetTarget();
            }

            if (m_meshDeformer == null)
            {
                return;
            }

            Scaffold        scaffold        = (Scaffold)target;
            ScaffoldWrapper scaffoldWrapper = null;
            int             scaffoldIndex   = -1;

            for (int i = 0; i < m_meshDeformer.Scaffolds.Length; ++i)
            {
                if (scaffold == m_meshDeformer.Scaffolds[i].Obj)
                {
                    scaffoldIndex   = i;
                    scaffoldWrapper = m_meshDeformer.Scaffolds[i];
                    break;
                }
            }

            if (scaffoldWrapper != null)
            {
                EditorGUI.BeginChangeCheck();
                Mesh altMesh = (Mesh)EditorGUILayout.ObjectField("Alt Mesh", scaffold.AltMesh, typeof(Mesh), false);
                if (EditorGUI.EndChangeCheck())
                {
                    EditorUtility.SetDirty(scaffold);
                    scaffold.AltMesh = altMesh;
                    m_meshDeformer.WrapAndDeform(scaffoldWrapper, altMesh);
                }

                if (GUILayout.Button("Reload Mesh"))
                {
                    EditorUtility.SetDirty(scaffold);
                    scaffold.AltMesh = altMesh;
                    m_meshDeformer.WrapAndDeform(scaffoldWrapper, altMesh);
                }

                if (GUILayout.Button("Invert Mesh"))
                {
                    Mesh invertedMesh = ScaleMesh(scaffold.transform, new Vector3(-1, 1, 1), scaffold.SourceMesh);
                    scaffold.AltMesh = invertedMesh;
                    m_meshDeformer.WrapAndDeform(scaffoldWrapper, invertedMesh);
                }

                EditorGUI.BeginChangeCheck();
                bool isRigid = EditorGUILayout.Toggle("Is Rigid", scaffoldWrapper.IsRigid);
                if (EditorGUI.EndChangeCheck())
                {
                    Undo.RecordObject(m_meshDeformer, UNDO_CHANGEMODE);
                    RecordScaffolds(m_meshDeformer, "Battlehub.MeshDeformer.Prepend");
                    EditorUtility.SetDirty(m_meshDeformer);

                    for (int i = 0; i < scaffoldWrapper.CurveIndices.Length; ++i)
                    {
                        int curveIndex = scaffoldWrapper.CurveIndices[i];
                        m_meshDeformer.SetIsRigid(curveIndex * 3, isRigid);
                    }
                }
            }

            if (scaffold.gameObject != m_meshDeformer.gameObject)
            {
                GUILayout.BeginHorizontal();
                {
                    if (scaffoldIndex == 0)
                    {
                        if (GUILayout.Button("Prepend"))
                        {
                            Undo.RecordObject(m_meshDeformer, "Battlehub.MeshDeformer.Prepend");
                            RecordScaffolds(m_meshDeformer, "Battlehub.MeshDeformer.Prepend");
                            Undo.RegisterCreatedObjectUndo(m_meshDeformer.Prepend(), "Battlehub.MeshDeformer.Prepend");
                            EditorUtility.SetDirty(m_meshDeformer);
                            Selection.activeGameObject = m_meshDeformer.GetComponentsInChildren <ControlPoint>(true).First().gameObject;
                        }
                    }

                    if (scaffoldWrapper != null)
                    {
                        if (GUILayout.Button("Insert"))
                        {
                            int curveIndex = scaffoldWrapper.CurveIndices.Min();

                            Undo.RecordObject(m_meshDeformer, "Battlehub.MeshDeformer.Insert");
                            Undo.RegisterCreatedObjectUndo(m_meshDeformer.Insert(curveIndex), "Battlehub.MeshDeformer.Insert");
                            EditorUtility.SetDirty(m_meshDeformer);
                            Selection.activeGameObject = m_meshDeformer.GetComponentsInChildren <ControlPoint>(true).ElementAt(curveIndex + 3).gameObject;
                        }
                    }

                    if (scaffoldIndex == m_meshDeformer.Scaffolds.Length - 1)
                    {
                        if (GUILayout.Button("Append"))
                        {
                            Undo.RecordObject(m_meshDeformer, "Battlehub.MeshDeformer.Append");
                            Undo.RegisterCreatedObjectUndo(m_meshDeformer.Append(), "Battlehub.MeshDeformer.Append");
                            EditorUtility.SetDirty(m_meshDeformer);
                            Selection.activeGameObject = m_meshDeformer.GetComponentsInChildren <ControlPoint>(true).Last().gameObject;
                        }
                    }
                }
                GUILayout.EndHorizontal();
            }

            if (scaffoldWrapper != null)
            {
                if (GUILayout.Button("Straighten"))
                {
                    Undo.RecordObject(m_meshDeformer, "Battlehub.MeshDeformer.Straighten");
                    RecordScaffolds(m_meshDeformer, "Battlehub.MeshDeformer.Straighten");
                    EditorUtility.SetDirty(m_meshDeformer);
                    m_meshDeformer.Straighten(scaffoldWrapper.CurveIndices.Min() * 3 + 1);
                }
            }
        }
Exemple #14
0
        protected override void OnInspectorGUIOverride()
        {
            if (m_meshDeformer == null)
            {
                m_meshDeformer = (MeshDeformer)GetTarget();
            }

            if (m_meshDeformer == null)
            {
                return;
            }

            Scaffold        scaffold        = (Scaffold)target;
            ScaffoldWrapper scaffoldWrapper = null;
            int             scaffoldIndex   = -1;

            for (int i = 0; i < m_meshDeformer.Scaffolds.Length; ++i)
            {
                if (scaffold == m_meshDeformer.Scaffolds[i].Obj)
                {
                    scaffoldIndex   = i;
                    scaffoldWrapper = m_meshDeformer.Scaffolds[i];
                    break;
                }
            }

            if (scaffoldWrapper != null)
            {
                EditorGUI.BeginChangeCheck();
                bool isRigid = EditorGUILayout.Toggle("Is Rigid", scaffoldWrapper.IsRigid);
                if (EditorGUI.EndChangeCheck())
                {
                    Undo.RecordObject(m_meshDeformer, UNDO_CHANGEMODE);
                    RecordScaffolds(m_meshDeformer, "Battlehub.MeshDeformer.Prepend");
                    EditorUtility.SetDirty(m_meshDeformer);

                    for (int i = 0; i < scaffoldWrapper.CurveIndices.Length; ++i)
                    {
                        int curveIndex = scaffoldWrapper.CurveIndices[i];
                        m_meshDeformer.SetIsRigid(curveIndex * 3, isRigid);
                    }
                }
            }

            if (scaffold.gameObject != m_meshDeformer.gameObject)
            {
                GUILayout.BeginHorizontal();
                {
                    if (scaffoldIndex == m_meshDeformer.Scaffolds.Length - 1)
                    {
                        if (GUILayout.Button("Append"))
                        {
                            Undo.RecordObject(m_meshDeformer, "Battlehub.MeshDeformer.Append");
                            Undo.RegisterCreatedObjectUndo(m_meshDeformer.Extend(), "Battlehub.MeshDeformer.Append");
                            EditorUtility.SetDirty(m_meshDeformer);
                            Selection.activeGameObject = m_meshDeformer.GetComponentsInChildren <ControlPoint>(true).Last().gameObject;
                        }
                    }

                    if (scaffoldIndex == 0)
                    {
                        if (GUILayout.Button("Prepend"))
                        {
                            Undo.RecordObject(m_meshDeformer, "Battlehub.MeshDeformer.Prepend");
                            RecordScaffolds(m_meshDeformer, "Battlehub.MeshDeformer.Prepend");
                            Undo.RegisterCreatedObjectUndo(m_meshDeformer.Extend(true), "Battlehub.MeshDeformer.Prepend");
                            EditorUtility.SetDirty(m_meshDeformer);
                            Selection.activeGameObject = m_meshDeformer.GetComponentsInChildren <ControlPoint>(true).First().gameObject;
                        }
                    }
                }
                GUILayout.EndHorizontal();
            }

            if (scaffoldWrapper != null)
            {
                if (GUILayout.Button("Straighten"))
                {
                    Undo.RecordObject(m_meshDeformer, "Battlehub.MeshDeformer.Straighten");
                    RecordScaffolds(m_meshDeformer, "Battlehub.MeshDeformer.Straighten");
                    EditorUtility.SetDirty(m_meshDeformer);
                    m_meshDeformer.Straighten(scaffoldWrapper.CurveIndices.Min() * 3 + 1);
                }
            }
        }