void OnSceneGUI()
    {
        MeshSimplify meshSimplify = target as MeshSimplify;

        bool bDrawSpheres = true;

        if (meshSimplify.MeshSimplifyRoot != null)
        {
            if (meshSimplify.MeshSimplifyRoot.ExpandRelevanceSpheres == false)
            {
                bDrawSpheres = false;
            }
        }
        else
        {
            if (meshSimplify.ExpandRelevanceSpheres == false)
            {
                bDrawSpheres = false;
            }
        }

        if (meshSimplify.RelevanceSpheres != null && bDrawSpheres)
        {
            for (int nSphere = 0; nSphere < meshSimplify.RelevanceSpheres.Length; nSphere++)
            {
                if (meshSimplify.RelevanceSpheres[nSphere].Expanded == false)
                {
                    continue;
                }

                RelevanceSphere relevanceSphere = meshSimplify.RelevanceSpheres[nSphere];

                if (Tools.current == Tool.Move)
                {
                    EditorGUI.BeginChangeCheck();
                    Vector3 v3Position = Handles.PositionHandle(relevanceSphere.Position, relevanceSphere.Rotation);
                    if (EditorGUI.EndChangeCheck())
                    {
                        Undo.RecordObject(meshSimplify, "Move Relevance Sphere");
                        relevanceSphere.Position = v3Position;
                        meshSimplify.RestoreOriginalMesh(false, true);
                        meshSimplify.DataDirty = true;
                        EditorUtility.SetDirty(target);
                    }
                }
                else if (Tools.current == Tool.Rotate)
                {
                    EditorGUI.BeginChangeCheck();
                    Quaternion qRotation = Handles.RotationHandle(relevanceSphere.Rotation, relevanceSphere.Position);
                    if (EditorGUI.EndChangeCheck())
                    {
                        Undo.RecordObject(meshSimplify, "Rotate Relevance Sphere");
                        relevanceSphere.Rotation = qRotation;
                        meshSimplify.RestoreOriginalMesh(false, true);
                        meshSimplify.DataDirty = true;
                        EditorUtility.SetDirty(target);
                    }
                }
                else if (Tools.current == Tool.Scale)
                {
                    EditorGUI.BeginChangeCheck();
                    Vector3 v3Scale = Handles.ScaleHandle(relevanceSphere.Scale, relevanceSphere.Position, relevanceSphere.Rotation, HandleUtility.GetHandleSize(relevanceSphere.Position) * 1.0f);
                    if (EditorGUI.EndChangeCheck())
                    {
                        Undo.RecordObject(meshSimplify, "Scale Relevance Sphere");
                        relevanceSphere.Scale = v3Scale;
                        meshSimplify.RestoreOriginalMesh(false, true);
                        meshSimplify.DataDirty = true;
                        EditorUtility.SetDirty(target);
                    }
                }

                if (Event.current.type == EventType.Repaint)
                {
                    Matrix4x4 mtxHandles = Handles.matrix;
                    Handles.matrix = Matrix4x4.TRS(relevanceSphere.Position, relevanceSphere.Rotation, relevanceSphere.Scale);
                    Handles.color  = new Color(0.0f, 0.0f, 1.0f, 0.5f);
                    Handles.SphereHandleCap(0, Vector3.zero, Quaternion.identity, 1.0f, EventType.Repaint);
                    Handles.matrix = mtxHandles;
                }
            }
        }
    }
Esempio n. 2
0
    public unsafe void Compute(List <Vertex> vertices, Mesh m_OriginalMesh, TriangleList[] triangleArray, RelevanceSphere[] aRelevanceSpheres, float[] costs, int[] collapses, int[] m_aVertexPermutation, int[] m_VertexHeap, bool bUseEdgeLength, bool bUseCurvature, float bBorderCurvature, float fOriginalMeshSize)
    {
        computerHeapJob                      = new ComputeHeapJob();
        this.m_aVertexPermutation            = m_aVertexPermutation;
        this.m_VertexHeap                    = m_VertexHeap;
        computerHeapJob.UseEdgeLength        = bUseEdgeLength;
        computerHeapJob.UseCurvature         = bUseCurvature;
        computerHeapJob.fBorderCurvature     = bBorderCurvature;
        computerHeapJob.OriginalMeshSize     = fOriginalMeshSize;
        computerHeapJob.m_aVertexPermutation = new NativeArray <int>(m_aVertexPermutation, Allocator.TempJob);
        computerHeapJob.m_aVertexMap         = new NativeArray <int>(m_VertexHeap, Allocator.TempJob);
        computerHeapJob.idToHeapIndex        = new NativeArray <int>(vertices.Count, Allocator.TempJob);
        computerHeapJob.tempTriangleList     = new NativeList <int>(Allocator.TempJob);
        computerHeapJob.tmpVerticesList      = new NativeList <int>(Allocator.TempJob);
        NativeHeap <VertexStruct> minNativeHeap = NativeHeap <VertexStruct> .CreateMinHeap(vertices.Count, Allocator.TempJob);

        int intSize      = UnsafeUtility.SizeOf <int>();
        int intAlignment = UnsafeUtility.AlignOf <int>();

        //------------------------顶点数据---------------------------------
        for (int i = 0; i < vertices.Count; i++)
        {
            Vertex       v  = vertices[i];
            VertexStruct sv = new VertexStruct()
            {
                ID         = v.m_nID,
                m_fObjDist = costs[i],
                Position   = v.m_v3Position,
                collapse   = collapses[i] == -1 ? -1 : vertices[collapses[i]].m_nID,
                isBorder   = v.IsBorder() ? 1 : 0,
                Normal     = v.Normal,
                UV         = v.UV,
            };
            sv.NewVertexNeighbors(v.m_listNeighbors.Count, intSize, intAlignment, Allocator.TempJob);
            sv.NewFaceTriangle(v.m_listFaces.Count, intSize, intAlignment, Allocator.TempJob);
            sv.idToHeapIndex = computerHeapJob.idToHeapIndex;
            for (int j = 0; j < v.m_listNeighbors.Count; j++)
            {
                sv.AddNeighborVertex(v.m_listNeighbors[j].m_nID);
            }
            for (int j = 0; j < v.m_listFaces.Count; j++)
            {
                sv.AddFaceTriangle(v.m_listFaces[j].Index);
            }
            minNativeHeap.Insert(sv);
        }
        //------------------------顶点数据结束-----------------------------
        //------------------------三角形数据-------------------------------
        List <TriangleStruct> structTrangle = new List <TriangleStruct>();

        for (int n = 0; n < triangleArray.Length; n++)
        {
            List <Triangle> triangles = triangleArray[n].m_listTriangles;
            for (int i = 0; i < triangles.Count; i++)
            {
                Triangle       t  = triangles[i];
                TriangleStruct st = new TriangleStruct()
                {
                    Index     = t.Index,
                    Indices   = (int *)UnsafeUtility.Malloc(t.Indices.Length * intAlignment, intAlignment, Allocator.TempJob),
                    Normal    = t.Normal,
                    faceIndex = (int *)UnsafeUtility.Malloc(t.FaceIndex.Length * intSize, intAlignment, Allocator.TempJob),
                    vertexs   = t.Vertices.Length == 0 ? null : (int *)UnsafeUtility.Malloc(t.Vertices.Length * intSize, intAlignment, Allocator.TempJob),
                };

                for (int j = 0; j < t.Indices.Length; j++)
                {
                    st.Indices[j] = t.Indices[j];
                }
                for (int j = 0; j < t.FaceIndex.Length; j++)
                {
                    st.faceIndex[j] = t.FaceIndex[j];
                }
                for (int j = 0; j < t.Vertices.Length; j++)
                {
                    st.vertexs[j] = t.Vertices[j].m_nID;
                }
                structTrangle.Add(st);
            }
        }
        //------------------------三角形数据结束
        computerHeapJob.Spheres = new NativeArray <StructRelevanceSphere>(aRelevanceSpheres.Length, Allocator.TempJob);
        for (int i = 0; i < aRelevanceSpheres.Length; i++)
        {
            RelevanceSphere       rs  = aRelevanceSpheres[i];
            StructRelevanceSphere srs = new StructRelevanceSphere()
            {
                Transformation = Matrix4x4.TRS(rs.m_v3Position, rs.m_q4Rotation, rs.m_v3Scale),
                Relevance      = rs.m_fRelevance,
            };
            computerHeapJob.Spheres[i] = srs;
        }
        computerHeapJob.triangles = new NativeArray <TriangleStruct>(structTrangle.ToArray(), Allocator.TempJob);

        computerHeapJob.m_VertexHeap = minNativeHeap;
        handle = computerHeapJob.Schedule();
        //Debug.Log(" computerHeapJob.m_VertexHeap.Length" + computerHeapJob.m_VertexHeap.Length);
    }