private IEnumerator SolveContactPoints()//对选中的模型计算接触点
    {
        yield return(new WaitForSeconds(0));

        buttonGCP.interactable = false;                                       //计算的时候不能再点击按钮
        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); //记录开始时间
        sw.Start();
        textTimeCost.text = "";                                               //重新显示时间
        sliderGCP.value   = 0;                                                //进度条归零
        var selectedObjList               = new List <ListNode>();            //选中对象们
        var unselectedObjList             = new List <ListNode>();            //未选中对象们
        int totalSelectedObjVerticeNum    = 0;                                //总的选中物体顶点数
        int totalUnselectedObjVerticeNum  = 0;                                //总的未选中物体顶点数
        int totalSelectedObjTriangleNum   = 0;                                //总的选中物体三角面片数,用来控制进度条显示
        int totalUnselectedObjTriangleNum = 0;                                //总的未选中物体三角面片数

        yield return(new WaitForSeconds(0));

        foreach (var item in model3d.GetComponentsInChildren <SkinnedMeshRenderer>())//获得所有物体每个三角面片包围盒
        {
            var vertices   = ObjManager.GetRealVertices(item);
            var triangles  = item.sharedMesh.triangles;
            var boundsList = new List <Bounds>();
            var objBounds  = ObjManager.GetBoundsOfVector3Array(vertices);

            if (item.tag.Equals(Macro.SELECTED))
            {
                for (int i = 0; i < triangles.Length; i += 3)
                {
                    Vector3[] tmp = { vertices[triangles[i]], vertices[triangles[i + 1]], vertices[triangles[i + 2]] };
                    boundsList.Add(ObjManager.GetBoundsOfVector3Array(tmp));
                }
                selectedObjList.Add(new ListNode(objBounds, boundsList));
                totalSelectedObjVerticeNum  += vertices.Length;
                totalSelectedObjTriangleNum += triangles.Length;
            }
            else if (item.tag.Equals(Macro.UNSELECTED))
            {
                for (int i = 0; i < triangles.Length; i += 3)
                {
                    Vector3[] tmp = { vertices[triangles[i]], vertices[triangles[i + 1]], vertices[triangles[i + 2]] };
                    boundsList.Add(ObjManager.GetBoundsOfVector3Array(tmp));
                }
                unselectedObjList.Add(new ListNode(objBounds, boundsList));
                totalUnselectedObjVerticeNum  += vertices.Length;
                totalUnselectedObjTriangleNum += triangles.Length;
            }
        }
        yield return(new WaitForSeconds(0));

        Debug.Log("selectedObjNum: " + selectedObjList.Count);
        Debug.Log("unselectedObjNum: " + unselectedObjList.Count);
        Debug.Log("totalSelectedObjVerticeNum: " + totalSelectedObjVerticeNum);
        Debug.Log("totalSelectedObjVerticeNum: " + totalUnselectedObjVerticeNum);
        Debug.Log("totalSelectedObjTriangleNum: " + totalSelectedObjTriangleNum);
        Debug.Log("totalUnselectedObjTriangleNum: " + totalUnselectedObjTriangleNum);
        float sliderValueFz = 0;                   //slider value的分子
        var   xjBoundsList  = new List <Bounds>(); //不同物体相交的区域

        foreach (var so in selectedObjList)        //求交
        {
            foreach (var uso in unselectedObjList)
            {
                if (!DealBoundsIntersection(so.objBounds, uso.objBounds, false, xjBoundsList))//两物体的包围盒不相交
                {
                    sliderValueFz += so.triangleBoundsList.Count;
                    continue;
                }
                foreach (var sotb in so.triangleBoundsList)
                {
                    sliderValueFz++;
                    if (!DealBoundsIntersection(sotb, uso.objBounds, false, xjBoundsList))//选中物体三角面片包围盒不与未选中物体包围盒相交
                    {
                        continue;
                    }
                    foreach (var usotb in uso.triangleBoundsList)
                    {
                        if (DealBoundsIntersection(sotb, usotb, true, xjBoundsList))
                        {
                        }
                    }
                    var tmpV = sliderValueFz * 3 / (unselectedObjList.Count * totalSelectedObjTriangleNum);
                    if (tmpV - sliderGCP.value >= 0.05)
                    {
                        sliderGCP.value = tmpV;//更新进度条的值
                        yield return(new WaitForSeconds(0));
                    }
                }
            }
        }

        var contactPointNum = CreateContactPoints(xjBoundsList); //显示接触点

        sliderGCP.value = 1;                                     //计算完毕,进度条的值置为1
        Debug.Log("contactPointNum: " + contactPointNum);
        buttonGCP.interactable = true;                           //计算完成可以点击按钮
        sw.Stop();
        ShowTime(sw.Elapsed.TotalSeconds);
    }
Exemple #2
0
    public void ProjectSelect()  //投影选中的模型
    {
        var o = objManager.panelStatusController.selectedObj;

        if (o == null)
        {
            return;
        }
        foreach (var item in o.GetComponentsInChildren <SkinnedMeshRenderer>())
        {
            var vertices  = ObjManager.GetRealVertices(item);
            var triangles = item.sharedMesh.triangles;
            for (int i = 0; i < triangles.Length; i += 3)
            {
                var tmp = new Vector2[] {
                    WorldPointToPixel(vertices[triangles[i]]),
                    WorldPointToPixel(vertices[triangles[i + 1]]),
                    WorldPointToPixel(vertices[triangles[i + 2]])
                };
                var pList = new List <Vector2>();
                for (int j = 0; j < tmp.Length; j++)
                {
                    var   u          = tmp[j];
                    var   v          = tmp[(j + 1) % tmp.Length];
                    float dx         = 0;
                    float dy         = 0;
                    int   totalPixel = 0;
                    if (Mathf.Abs(v.x - u.x) > Mathf.Abs(v.y - u.y))
                    {
                        dx         = u.x > v.x ? -1 : 1;
                        dy         = (v.y - u.y) / (v.x - u.x) * dx;
                        totalPixel = (int)Mathf.Abs(v.x - u.x) + 1;
                    }
                    else if (v.y == u.y)
                    {
                        dx         = dy = 1;
                        totalPixel = 1;
                    }
                    else
                    {
                        dy         = u.y > v.y ? -1 : 1;
                        dx         = (v.x - u.x) / (v.y - u.y) * dy;
                        totalPixel = (int)Mathf.Abs(v.y - u.y) + 1;
                    }
                    var d   = new Vector2(dx, dy);
                    int cnt = 0;
                    while (cnt < totalPixel)
                    {
                        cnt++;
                        pList.Add(new Vector2(u.x, u.y));
                        u += d;
                    }
                }
                foreach (var v in pList)
                {
                    int x = (int)v.x;
                    int y = (int)v.y;
                    if (IfInMap(x, y))
                    {
                        area[areaId][x, y] = 1;
                        t2d.SetPixel(x, y, colorSet[areaId]);
                    }
                }
            }
        }
        FillDrawMap();
    }