Example #1
0
    Vector3 getNormal(polygon cur, List <Vector3> verticesList)
    {
        Vector3 v1  = verticesList[cur.getV(1)] - verticesList[cur.getV(0)];
        Vector3 v2  = verticesList[cur.getV(2)] - verticesList[cur.getV(1)];
        Vector3 res = Vector3.Cross(v1, v2);

        res.Normalize();
        return(res);
    }
Example #2
0
 public void add(polygon p)
 {
     lp.Add(p);
     foreach (edge e in p.le)
     {
         if (!polygon_edges.Contains(e))
         {
             polygon_edges.Add(e);
         }
     }
 }
        private g Paint_Points()
        {
            g g1 = new g();



            if (_parent.Points_List_Private.Count > 0)
            {
                List <MyPoint> Points_for_Polygon = new List <MyPoint>();

                MyPoint p  = new MyPoint(0, MyFunctions.Calculate_Y_Coordinate(_parent, _parent.Points_List_Private.Where(x => x.IsShown).First().Percentage));
                MyPoint p2 = new MyPoint(0, 0);

                Points_for_Polygon.Add(new MyPoint(0, _parent.ChartSettings.InitialHeight));
                Points_for_Polygon.Add(new MyPoint(p.X, p.Y));

                int counter = 0;
                foreach (MyChartPoint item in _parent.Points_List_Private.Where(x => x.IsShown))
                {
                    p2 = new MyPoint(counter * _parent.ChartSettings.StackWidth, MyFunctions.Calculate_Y_Coordinate(_parent, item.Percentage));
                    Points_for_Polygon.Add(p2);

                    counter++;
                }



                Points_for_Polygon.Add(new MyPoint(p2.X, _parent.ChartSettings.InitialHeight));


                StringBuilder sb = new StringBuilder();

                foreach (var item in Points_for_Polygon)
                {
                    sb.Append(item.X);
                    sb.Append(",");
                    sb.Append(item.Y);
                    sb.Append(" ");
                }


                polygon pl = new polygon
                {
                    points = sb.ToString().Trim(),
                    style  = "fill:" + _parent.ChartSettings.AreaColor,
                };

                g1.Children.Add(pl);
            }


            return(g1);
        }
Example #4
0
        public static void Set(OpenGLControl openGLControl)
        {
            inside_shape   = false;
            inside_polygon = false;

            if (selectedShape != null)
            {
                selectedShape = new Shape();
            }

            if (selectedPolygon != null)
            {
                selectedPolygon = new polygon();
            }
        }
Example #5
0
        static polyhedron tetrahedron(Graphics g, PictureBox pb, int size)
        {
            double h = size * Math.Sqrt(3);

            point3d p1 = new point3d(-size, (int)-h / 2, (int)-h / 2);
            point3d p2 = new point3d(size, (int)-h / 2, (int)-h / 2);
            point3d p3 = new point3d(0, (int)-h / 2, (int)h / 2);
            point3d p4 = new point3d(0, (int)h / 2, 0);

            edge e1 = new edge(g, pb, ref p1, ref p2);
            edge e2 = new edge(g, pb, ref p2, ref p3);
            edge e3 = new edge(g, pb, ref p3, ref p1);
            edge e4 = new edge(g, pb, ref p1, ref p4);
            edge e5 = new edge(g, pb, ref p2, ref p4);
            edge e6 = new edge(g, pb, ref p3, ref p4);

            polygon plg1 = new polygon();

            plg1.add(e1);
            plg1.add(e2);
            plg1.add(e3);

            polygon plg2 = new polygon();

            plg2.add(e1);
            plg2.add(e4);
            plg2.add(e5);

            polygon plg3 = new polygon();

            plg3.add(e2);
            plg3.add(e5);
            plg3.add(e6);

            polygon plg4 = new polygon();

            plg4.add(e3);
            plg4.add(e4);
            plg4.add(e6);

            polyhedron res = new polyhedron();

            res.add(plg1);
            res.add(plg2);
            res.add(plg3);
            res.add(plg4);
            return(res);
        }
Example #6
0
    List <polygon> mergeTriples(List <polygon> list, List <Vector3> verticesList)
    {
        List <polygon> res   = new List <polygon>();
        int            index = 0;

        while (list.Count > 0)
        {
            polygon current = list[0];
            list.RemoveAt(0);
            for (int j = 0; j < list.Count;)
            {
                polygon tmp = list[j];

                /*
                 * 0    curIndex
                 * 1    nextIndex
                 * 2    delete1
                 * 3    delete2
                 */
                Dictionary <int, int> paramMerge = new Dictionary <int, int>();

                if (canMergePoint(current, tmp, paramMerge, verticesList))
                {
                    mergePoint(current, tmp, paramMerge);
                    list.RemoveAt(j);
                    break;//暂时不处理四边形
                }
                else
                {
                    j++;
                }
            }
            res.Add(current);
        }
        return(res);
    }
Example #7
0
    public void calculatePolygonMass(ref polygon triangle, float density)
    {
        Vector3 AB = new Vector3();
        Vector3 AC = new Vector3();

        AB = Vector3.Subtract(triangle.vertices[0], triangle.vertices[1]);
        AC = Vector3.Subtract(triangle.vertices[0], triangle.vertices[2]);

        triangle.area = (float)(0.5 * (Vector3.Cross(AB, AC).Length()));
        triangle.mass = triangle.area * 0.001f * density;
    }
Example #8
0
    public void calculateMomentum(ref logicalObject logObject, Matrix3 oldRotation, int numberOfPolygons, polygon[] polygonArray, float totalMass, Vector3 angularVelocity, Vector3 oldLinearMomentum, Vector3 oldPosition)
    {
        forceI.X = forceI.Y = forceI.Z = 0.0f;
        totalForce.X = totalForce.Y = totalForce.Z = 0.0f;
        torque.X = torque.Y = torque.Z = 0.0f;

        //Console.WriteLine("Linear: " + oldLinearMomentum / totalMass);

        //Console.WriteLine("ny");

        for (int i = 0; i < numberOfPolygons; i++)
        {
            forceI = calculateCentroidForce(polygonArray[i], oldLinearMomentum / totalMass, oldRotation, angularVelocity, oldPosition);
            totalForce += forceI;
            torque += Vector3.Cross(oldRotation * polygonArray[i].centerOfMass, forceI);
            //Console.WriteLine("hoho " + Vector3.Cross(oldRotation * polygonArray[i].centerOfMass, forceI));
        }

        //Console.WriteLine("torque " + torque);

        //Console.WriteLine("Total Force: " + totalForce + " Length: " + totalForce.Length());
        logObject.linearMomentum += h * totalForce;
        //Console.WriteLine("Speed: " + logObject.linearMomentum.Length());
        logObject.angularMomentum += h * torque;
    }
Example #9
0
    public Vector3 calculateDragForce(polygon poly, Vector3 linearVelocity, Matrix3 oldRotation, Vector3 angularVelocity, Vector3 objectPosition)
    {
        Normal = Vector3.Cross(poly.vertices[0] - poly.vertices[1], poly.vertices[0] - poly.vertices[2]);

        //if (Normal.Y > 0) Normal = -1.0f * Normal;

        Velocity = new Vector3(0.0f, 0.0f, 0.0f);
        if (usingLinearDrag)
        {
            Velocity -= linearVelocity;
        }
        if (usingAngularDrag)
        {
            Velocity -= Vector3.Cross(angularVelocity, oldRotation * poly.centerOfMass);
        }
        if (usingWind)
        {
            Velocity -= windSim.returnWind(oldRotation * poly.centerOfMass + objectPosition);
        }
        //Velocity = -linearVelocity - Vector3.Cross(angularVelocity, oldRotation * poly.centerOfMass) -windSim.returnWind(oldRotation * poly.centerOfMass + objectPosition);

        //Console.WriteLine("Centroid: " + poly.centerOfMass.Length());
        areaPercent = Math.Abs(Vector3.Dot(oldRotation * Normal, Velocity) / (Normal.Length() * Velocity.Length()));
        //Console.WriteLine("poly Area: " + poly.area);
        return (p * poly.area * areaPercent * 0.5f * Cd * Vector3.Multiply(Velocity, abs(Velocity)));
    }
 => PointInPolygonSetShkyrockett0(polygon, X, Y);
Example #11
0
 DeleteRelation(polygon, segment);
Example #12
0
        static polyhedron octahedron(Graphics g, PictureBox pb, int size)
        {
            point3d p1 = new point3d(-size / 2, 0, 0);
            point3d p2 = new point3d(0, 0, -size / 2);
            point3d p3 = new point3d(size / 2, 0, 0);
            point3d p4 = new point3d(0, 0, size / 2);
            point3d p5 = new point3d(0, size / 2, 0);
            point3d p6 = new point3d(0, -size / 2, 0);

            edge e1  = new edge(g, pb, ref p1, ref p2);
            edge e2  = new edge(g, pb, ref p2, ref p3);
            edge e3  = new edge(g, pb, ref p3, ref p4);
            edge e4  = new edge(g, pb, ref p1, ref p4);
            edge e5  = new edge(g, pb, ref p1, ref p5);
            edge e6  = new edge(g, pb, ref p2, ref p5);
            edge e7  = new edge(g, pb, ref p3, ref p5);
            edge e8  = new edge(g, pb, ref p4, ref p5);
            edge e9  = new edge(g, pb, ref p1, ref p6);
            edge e10 = new edge(g, pb, ref p2, ref p6);
            edge e11 = new edge(g, pb, ref p3, ref p6);
            edge e12 = new edge(g, pb, ref p4, ref p6);

            polygon plg1 = new polygon();

            plg1.add(e1);
            plg1.add(e6);
            plg1.add(e5);

            polygon plg2 = new polygon();

            plg2.add(e2);
            plg2.add(e7);
            plg2.add(e6);

            polygon plg3 = new polygon();

            plg3.add(e3);
            plg3.add(e7);
            plg3.add(e8);

            polygon plg4 = new polygon();

            plg4.add(e4);
            plg4.add(e8);
            plg4.add(e5);

            polygon plg5 = new polygon();

            plg5.add(e1);
            plg5.add(e9);
            plg5.add(e10);

            polygon plg6 = new polygon();

            plg6.add(e2);
            plg6.add(e10);
            plg6.add(e11);

            polygon plg7 = new polygon();

            plg7.add(e3);
            plg7.add(e11);
            plg7.add(e12);

            polygon plg8 = new polygon();

            plg1.add(e4);
            plg1.add(e9);
            plg1.add(e12);

            polyhedron res = new polyhedron();

            res.add(plg1);
            res.add(plg2);
            res.add(plg3);
            res.add(plg4);
            res.add(plg5);
            res.add(plg6);
            res.add(plg7);
            res.add(plg8);
            return(res);
        }
Example #13
0
        //---
        protected void addInsertItem(int ind)
        {
            Item ob = new Item();
            int  sw = comboBoxItems.SelectedIndex;

            switch (sw)
            {
            case 0:
                ob = new point();
                break;

            case 1:
                ob = new mLine();
                break;

            case 2:
                ob = new circle();
                break;

            case 3:
                ob = new ellipse();
                break;

            case 4:
                ob = new triangle();
                break;

            case 5:
                ob = new rectangle();
                break;

            case 6:
                ob = new roundRect();
                break;

            case 7:
                ob = new polygon();
                break;

            case 8:
                ob = new arc();
                break;

            case 9:
                ob = new color();
                break;

            default:

                break;
            }

            if (ind < 0)
            {
                if (listBoxIcons.SelectedIndex < 0)
                {
                    return;
                }
                icons[listBoxIcons.SelectedIndex].add(ob);
                listBoxItems.SelectedIndex = listBoxItems.Items.Count - 1;
            }
            else
            {
                icons[listBoxIcons.SelectedIndex].insert(ind, ob);
                listBoxItems.SelectedIndex = ind;
            }
        }
Example #14
0
        private void openGLControl_MouseUp(object sender, MouseEventArgs e)
        {
            //press left mouse anh draw shape
            if (mode == 1 && e.Button == MouseButtons.Left)
            {
                mouseDown = false;
                end       = e.Location;
                clock     = DateTime.Now.Subtract(startTime);
            }

            /*draw polygon*/
            if (mode == 2)
            {
                selected = false;
                //end of drawing polygon
                if (e.Button == MouseButtons.Right)
                {
                    //add to list
                    list_plg.Add(plg);
                    plg = new polygon();

                    mouseLeft = false;
                    clock    += DateTime.Now.Subtract(startTime);
                }
                else
                if (e.Button == MouseButtons.Left) //continue drawing
                {
                    plg.addPoint(e.Location);      //call function addPoint(Point point) to add new point to list

                    mouseLeft = true;
                    clock    += DateTime.Now.Subtract(startTime);
                }
                mouseDown = false;
            }

            /*select shape*/
            if (mode == 3)
            {
                if (Draw.Draw.CheckInside(openGLControl, Draw.Draw.stackShape, list_plg, start) == true)
                {
                    if (e.Button == MouseButtons.Right)
                    {
                        if (Draw.Draw.inside_shape == true)
                        {
                            Draw.Draw.stackShape.Push(Draw.Draw.selectedShape);
                        }
                        if (Draw.Draw.inside_polygon == true)
                        {
                            list_plg.Add(Draw.Draw.selectedPolygon);
                        }
                        Draw.Draw.Set(openGLControl);
                        mouseLeft = false;
                        mode      = -1;
                    }

                    else
                    {
                        if (mouseDown == true && mouse_move_down.X != -1 && mouse_move_down.Y != -1)
                        {
                            end = e.Location;
                            Draw.Draw.Translate(openGLControl, start, end);
                            start = end;
                            //
                        }
                        if (mouseDown == false)
                        {
                            end = e.Location;
                        }
                        Draw.Draw.Set(openGLControl);
                    }

                    mouseDown = false;

                    if (chooseIcon < 1 && mode != 2)
                    {
                        clock = TimeSpan.Zero;
                    }
                }
            }

            /*scale shape*/
            if (mode == 4)
            {
                if (control_point_order != -1)
                {
                    Draw.Draw.ScaleShape(openGLControl, control_point_order, e.Location);
                    control_point_order = -1;
                    mouse_move_down     = e.Location;
                }
            }
        }
Example #15
0
    public void calculateCentroid(ref polygon triangle)
    {
        Vector3 centroid = new Vector3();

        for (int i = 0; i < triangle.vertices.Length; i++)
        {
            centroid.X += triangle.vertices[i].X;
            centroid.Y += triangle.vertices[i].Y;
            centroid.Z += triangle.vertices[i].Z;
        }

        centroid.X = centroid.X / triangle.vertices.Length;
        centroid.Y = centroid.Y / triangle.vertices.Length;
        centroid.Z = centroid.Z / triangle.vertices.Length;

        triangle.centerOfMass = centroid;

        //Console.WriteLine("Centroid: " + centroid.X + ", " + centroid.Y + ", " + centroid.Z);
    }
Example #16
0
    bool canMergePoint(polygon cur, polygon next, Dictionary <int, int> paramMerge, List <Vector3> verticesList)
    {
        bool res = false;

        //是否有二个点共点,并且uv一样
        if (cur.poly.Count == 4)
        {
            return(res);
        }
        var  curA = -1;
        var  curB = -1;
        var  nextA = -1;
        var  nextB = -1;
        int  i = 0, j = 0;
        bool isSame = false;

        for (i = 0; i < cur.poly.Count; ++i)
        {
            curA = cur.getV(i);
            curB = cur.getV(i + 1);
            for (j = 0; j < next.poly.Count; ++j)
            {
                nextA = next.getV(j);
                nextB = next.getV(j + 1);
                if (curA == nextB && curB == nextA)
                {
                    if (cur.getVT(i) == next.getVT(j + 1) && cur.getVT(i + 1) == next.getVT(j))
                    {
                        /*
                         * 0    curIndex
                         * 1    nextIndex
                         * 2    deleteCount
                         * 3    delete1
                         * 4    delete2
                         */
                        paramMerge[0] = i;
                        paramMerge[1] = j;
                        isSame        = true;
                        break;
                    }
                }
            }
        }
        //法向量是否平行
        if (isSame)
        {
            /**
             * 可能有方向不一样,不能合并,所以比较二个法向量
             */
            var curNor = getNormal(cur, verticesList);
            var nexNor = getNormal(next, verticesList);
            var test   = curNor - nexNor;
            if (test.magnitude < 0.001)
            {
                res = true;
            }
        }
        if (cur.poly.Count == 3)
        {
            /**
             * 删除二个点
             */
            paramMerge[2] = 2;
        }
        return(res);
    }
Example #17
0
        public static List<polygon> calculatePolygons(graphLine[] edges)
        {
            var directedLines = new directedLine[edges.Length*2];
             //Создаем вектора
             for (int i = 0; i < edges.Length; i++)
             {
                 var edge = edges[i];
                 edge.ab = new directedLine { a = edge.p1, b = edge.p2, line = edge };
                 edge.ba = new directedLine { a = edge.p2, b = edge.p1, line = edge };
                 directedLines[i] = edge.ab;
                 directedLines[edges.Length + i] = edge.ba;
             }
             //Связываем правые стороны векторов
             for (int i = 0; i < edges.Length; i++)
             {
                 var edge = edges[i];
                 directedLines[i] = edge.ab;
                 directedLines[edges.Length + i] = edge.ba;

                 var nextAEdge = edge.GetRightEdge(edge.p1);
                 var nextBEdge = edge.GetRightEdge(edge.p2);
                 edge.ba.nextRightLine = nextAEdge.p1 == edge.p1 ? nextAEdge.ab : nextAEdge.ba;
                 edge.ab.nextRightLine = nextBEdge.p1 == edge.p2 ? nextBEdge.ab : nextBEdge.ba;
             }
             //Находим полигоны
             var polygons = new List<polygon>();
             for (int i = 0; i < directedLines.Length; i++)
             {
                 var dir = directedLines[i];
                 if (dir.used)
                     continue;

                 var path = new List<directedLine>();
                 int error = 0;
                 while (true)
                 {
                     dir.used = true;
                     path.Add(dir);

                     if (dir.nextRightLine.used)//exit
                     {
                         var firstIndex = path.IndexOf(dir.nextRightLine);
                         if (firstIndex == -1)
                             break;

                         var polygon = new polygon();
                         for (int j = firstIndex; j < path.Count; j++)
                         {
                             var e = path[j].line;
                             polygon.a &= e.a;
                             polygon.b &= e.b;

                             polygon.points.Add(path[j].b.point);
                         }
                         if (area.simplePolygon(polygon.points.Select(x => (Vector2)x).ToArray()) < 0f &&
                             (polygon.a || polygon.b))
                             polygons.Add(polygon);
                         break;
                     }
                     dir = dir.nextRightLine;

                     if (error >= directedLines.Length)
                     {
                         Debug.LogWarning("Error");
                         break;
                     }
                     error++;
                 }

             }
             return polygons;
        }
Example #18
0
 void mergePoint(polygon cur, polygon next, Dictionary <int, int> paramMerge)
 {
     cur.poly.Insert(paramMerge[0] + 1, next.poly[(paramMerge[1] + 2) % next.poly.Count]);
 }
Example #19
0
        public static void DrawControlPoints(OpenGLControl openGLControl, Stack <Shape> stackShape, List <polygon> list_plg, Point mouse_position)
        {
            int eps = 10;//epsilon
            //bool inside = false;
            var  glCtrl = openGLControl.OpenGL;
            bool inside = false;

            /*traverse shape*/
            foreach (var pos in stackShape)
            {
                int fi_x = pos.firstPoint.X;
                int fi_y = pos.firstPoint.Y;
                int se_x = pos.secondPoint.X;
                int se_y = pos.secondPoint.Y;

                if (fi_x < se_x && fi_y < se_y)//firstPoint: topleft, secondPoint: botright
                {
                    if ((fi_x - eps <= mouse_position.X && mouse_position.X <= se_x + eps) &&
                        (fi_y - eps <= mouse_position.Y && mouse_position.Y < se_y + eps))
                    {
                        inside = true;
                    }
                }

                if (fi_x < se_x && fi_y > se_y)//firstPoint: botleft, secondPoint: topright
                {
                    if ((fi_x - eps <= mouse_position.X && mouse_position.X <= se_x + eps) &&
                        (fi_y + eps <= mouse_position.Y && mouse_position.Y < se_y - eps))
                    {
                        inside = true;
                    }
                }

                if (fi_x > se_x && fi_y < se_y)//firstPoint: topright, secondPoint: botleft
                {
                    if ((se_x - eps <= mouse_position.X && mouse_position.X <= fi_x + eps) &&
                        (fi_y - eps <= mouse_position.Y && mouse_position.Y < se_y + eps))
                    {
                        inside = true;
                    }
                }

                if (fi_x > se_x && fi_y > se_y)//firstPoint: botright, secondPoint: topleft
                {
                    if ((se_x - eps <= mouse_position.X && mouse_position.X <= fi_x + eps) &&
                        (fi_y + eps <= mouse_position.Y && mouse_position.Y < se_y - eps))
                    {
                        inside = true;
                    }
                }

                if (inside == true)
                {
                    if (selectedShape == null)
                    {
                        selectedShape = new Shape();
                    }
                    selectedShape = pos;
                    pos.drawControlPoints(openGLControl);

                    glCtrl.Flush();
                    break;
                }
            }

            //xet cac polygon
            if (inside == false && list_plg != null)
            {
                foreach (var pol in list_plg)
                {
                    pol.SetData_ControlPoint(openGLControl);

                    int fi_x = pol.firstPoint.X;
                    int fi_y = pol.firstPoint.Y;
                    int se_x = pol.secondPoint.X;
                    int se_y = pol.secondPoint.Y;

                    //We calculate position of click-mouse and edges of shape if this distance <=epsilon the inside=true
                    if (fi_x < se_x && fi_y < se_y)//firstPoint: topleft, secondPoint: botright
                    {
                        if ((fi_x - eps <= mouse_position.X && mouse_position.X <= se_x + eps) &&
                            (fi_y - eps <= mouse_position.Y && mouse_position.Y < se_y + eps))
                        {
                            inside = true;
                        }
                    }

                    if (fi_x < se_x && fi_y > se_y)//firstPoint: botleft, secondPoint: topright
                    {
                        if ((fi_x - eps <= mouse_position.X && mouse_position.X <= se_x + eps) &&
                            (fi_y + eps <= mouse_position.Y && mouse_position.Y < se_y - eps))
                        {
                            inside = true;
                        }
                    }

                    if (fi_x > se_x && fi_y < se_y)//firstPoint: topright, secondPoint: botleft
                    {
                        if ((se_x - eps <= mouse_position.X && mouse_position.X <= fi_x + eps) &&
                            (fi_y - eps <= mouse_position.Y && mouse_position.Y < se_y + eps))
                        {
                            inside = true;
                        }
                    }

                    if (fi_x > se_x && fi_y > se_y)//firstPoint: botright, secondPoint: topleft
                    {
                        if ((se_x - eps <= mouse_position.X && mouse_position.X <= fi_x + eps) &&
                            (fi_y + eps <= mouse_position.Y && mouse_position.Y < se_y - eps))
                        {
                            inside = true;
                        }
                    }

                    if (inside == true)
                    {
                        if (selectedPolygon == null)
                        {
                            selectedPolygon = new polygon();
                        }
                        selectedPolygon = pol;
                        pol.drawControlPoints(openGLControl);
                        glCtrl.Flush();
                        break;
                    }
                }
            }

            //push to buffer
            glCtrl.Flush();
        }
Example #20
0
    void Export(string exportPath)
    {
        //init stuff
        Dictionary <string, bool> materialCache = new Dictionary <string, bool>();
        var exportFileInfo = new System.IO.FileInfo(exportPath);

        lastExportFolder = exportFileInfo.Directory.FullName;
        string baseFileName = System.IO.Path.GetFileNameWithoutExtension(exportPath);

        EditorUtility.DisplayProgressBar("Exporting OBJ", "Please wait.. Starting export.", 0);

        //get list of required export things
        MeshFilter[] sceneMeshes;
        if (onlySelectedObjects)
        {
            List <MeshFilter> tempMFList = new List <MeshFilter>();
            foreach (GameObject g in Selection.gameObjects)
            {
                MeshFilter f = g.GetComponent <MeshFilter>();
                if (f != null)
                {
                    tempMFList.Add(f);
                }
            }
            sceneMeshes = tempMFList.ToArray();
        }
        else
        {
            sceneMeshes = FindObjectsOfType(typeof(MeshFilter)) as MeshFilter[];
        }

        if (Application.isPlaying)
        {
            foreach (MeshFilter mf in sceneMeshes)
            {
                MeshRenderer mr = mf.gameObject.GetComponent <MeshRenderer>();
                if (mr != null)
                {
                    if (mr.isPartOfStaticBatch)
                    {
                        EditorUtility.ClearProgressBar();
                        EditorUtility.DisplayDialog("Error", "Static batched object detected. Static batching is not compatible with this exporter. Please disable it before starting the player.", "OK");
                        return;
                    }
                }
            }
        }

        //work on export
        StringBuilder sb          = new StringBuilder();
        StringBuilder sbMaterials = new StringBuilder();

        sb.AppendLine("# Export of " + Application.loadedLevelName);
        sb.AppendLine("# from Aaro4130 OBJ Exporter " + versionString);
        if (generateMaterials)
        {
            sb.AppendLine("mtllib " + baseFileName + ".mtl");
        }
        float maxExportProgress      = (float)(sceneMeshes.Length + 1);
        int   lastVertexIndex        = 0;
        int   lastVertexNormalsIndex = 0;
        int   lastTextureCoordsIndex = 0;

        for (int i = 0; i < sceneMeshes.Length; i++)
        {
            string meshName = sceneMeshes[i].gameObject.name;
            float  progress = (float)(i + 1) / maxExportProgress;
            EditorUtility.DisplayProgressBar("Exporting objects... (" + Mathf.Round(progress * 100) + "%)", "Exporting object " + meshName, progress);
            MeshFilter   mf = sceneMeshes[i];
            MeshRenderer mr = sceneMeshes[i].gameObject.GetComponent <MeshRenderer>();

            if (mr != null && generateMaterials)
            {
                Material[] mats = mr.sharedMaterials;
                for (int j = 0; j < mats.Length; j++)
                {
                    Material m = mats[j];
                    if (!materialCache.ContainsKey(m.name))
                    {
                        materialCache[m.name] = true;
                        sbMaterials.Append(MaterialToString(m));
                        sbMaterials.AppendLine();
                    }
                }
            }

            //export the meshhh :3
            Mesh msh       = mf.sharedMesh;
            int  faceOrder = (int)Mathf.Clamp((mf.gameObject.transform.lossyScale.x * mf.gameObject.transform.lossyScale.z), -1, 1);

            //export vector data (FUN :D)!
            List <int>     verticeIndexList = new List <int>();
            List <Vector3> verticeList      = new List <Vector3>();
            foreach (Vector3 vx in msh.vertices)
            {
                Vector3 v         = vx;
                int     realIndex = findVector3(v, verticeList);
                if (realIndex == -1)
                {
                    verticeList.Add(v);
                    verticeIndexList.Add(verticeList.Count - 1);
                }
                else
                {
                    verticeIndexList.Add(realIndex);
                    continue;
                }

                if (applyScale)
                {
                    v = MultiplyVec3s(v, mf.gameObject.transform.lossyScale);
                }

                if (applyRotation)
                {
                    v = RotateAroundPoint(v, Vector3.zero, mf.gameObject.transform.rotation);
                }

                if (applyPosition)
                {
                    v += mf.gameObject.transform.position;
                }
                v.x *= -1;
                sb.AppendLine("v " + v.x + " " + v.y + " " + v.z);
            }
            List <int>     normalIndexList = new List <int>();
            List <Vector3> normalList      = new List <Vector3>();
            foreach (Vector3 vx in msh.normals)
            {
                Vector3 v         = vx;
                int     realIndex = findVector3(v, normalList);
                if (realIndex == -1)
                {
                    normalList.Add(v);
                    normalIndexList.Add(normalList.Count - 1);
                }
                else
                {
                    normalIndexList.Add(realIndex);
                    continue;
                }
                if (applyScale)
                {
                    v = MultiplyVec3s(v, mf.gameObject.transform.lossyScale.normalized);
                }
                if (applyRotation)
                {
                    v = RotateAroundPoint(v, Vector3.zero, mf.gameObject.transform.rotation);
                }
                v.x *= -1;
                sb.AppendLine("vn " + v.x + " " + v.y + " " + v.z);
            }
            List <int>     uvIndexList = new List <int>();
            List <Vector2> uvList      = new List <Vector2>();
            foreach (Vector2 v in msh.uv)
            {
                Vector2 vv        = v;
                int     realIndex = findVector2(v, uvList);
                if (realIndex == -1)
                {
                    uvList.Add(v);
                    uvIndexList.Add(uvList.Count - 1);
                }
                else
                {
                    uvIndexList.Add(realIndex);
                    continue;
                }
                sb.AppendLine("vt " + v.x + " " + v.y);
            }

            if (splitObjects)
            {
                string exportName = meshName;
                if (objNameAddIdNum)
                {
                    exportName += "_" + i;
                }
                sb.AppendLine("g " + exportName);
            }

            for (int j = 0; j < msh.subMeshCount; j++)
            {
                if (generateMaterials)
                {
                    if (mr != null && j < mr.sharedMaterials.Length)
                    {
                        string matName = mr.sharedMaterials[j].name;
                        sb.AppendLine("usemtl " + matName);
                    }
                    else
                    {
                        sb.AppendLine("usemtl " + meshName + "_sm" + j);
                    }
                }

                int[]          tris        = msh.GetTriangles(j);
                List <polygon> triplesList = new List <polygon>();
                for (int t = 0; t < tris.Length; t += 3)
                {
                    polygon tri = new polygon();
                    tri.poly.Add(new triples(verticeIndexList[tris[t]], normalIndexList[tris[t]], uvIndexList[tris[t]]));
                    tri.poly.Add(new triples(verticeIndexList[tris[t + 1]], normalIndexList[tris[t + 1]], uvIndexList[tris[t + 1]]));
                    tri.poly.Add(new triples(verticeIndexList[tris[t + 2]], normalIndexList[tris[t + 2]], uvIndexList[tris[t + 2]]));
                    triplesList.Add(tri);
                }
                triplesList = mergeTriples(triplesList, verticeList);
                for (int t = 0; t < triplesList.Count; ++t)
                {
                    if (triplesList[t].poly.Count == 3)
                    {
                        if (faceOrder < 0)
                        {
                            sb.AppendLine("f " + ConstructOBJString2(triplesList[t].poly[2].v + 1 + lastVertexIndex, triplesList[t].poly[2].vn + 1 + lastVertexNormalsIndex, triplesList[t].poly[2].vt + 1 + lastTextureCoordsIndex) + " " + ConstructOBJString2(triplesList[t].poly[1].v + 1 + lastVertexIndex, triplesList[t].poly[1].vn + 1 + lastVertexNormalsIndex, triplesList[t].poly[1].vt + 1 + lastTextureCoordsIndex) + " " + ConstructOBJString2(triplesList[t].poly[0].v + 1 + lastVertexIndex, triplesList[t].poly[0].vn + 1 + lastVertexNormalsIndex, triplesList[t].poly[0].vt + 1 + lastTextureCoordsIndex));
                        }
                        else
                        {
                            sb.AppendLine("f " + ConstructOBJString2(triplesList[t].poly[0].v + 1 + lastVertexIndex, triplesList[t].poly[0].vn + 1 + lastVertexNormalsIndex, triplesList[t].poly[0].vt + 1 + lastTextureCoordsIndex) + " " + ConstructOBJString2(triplesList[t].poly[1].v + 1 + lastVertexIndex, triplesList[t].poly[1].vn + 1 + lastVertexNormalsIndex, triplesList[t].poly[1].vt + 1 + lastTextureCoordsIndex) + " " + ConstructOBJString2(triplesList[t].poly[2].v + 1 + lastVertexIndex, triplesList[t].poly[2].vn + 1 + lastVertexNormalsIndex, triplesList[t].poly[2].vt + 1 + lastTextureCoordsIndex));
                        }
                    }
                    else
                    {
                        if (faceOrder < 0)
                        {
                            sb.AppendLine("f " + ConstructOBJString2(triplesList[t].poly[3].v + 1 + lastVertexIndex, triplesList[t].poly[3].vn + 1 + lastVertexNormalsIndex, triplesList[t].poly[3].vt + 1 + lastTextureCoordsIndex) + " " + ConstructOBJString2(triplesList[t].poly[2].v + 1 + lastVertexIndex, triplesList[t].poly[2].vn + 1 + lastVertexNormalsIndex, triplesList[t].poly[2].vt + 1 + lastTextureCoordsIndex) + " " + ConstructOBJString2(triplesList[t].poly[1].v + 1 + lastVertexIndex, triplesList[t].poly[1].vn + 1 + lastVertexNormalsIndex, triplesList[t].poly[1].vt + 1 + lastTextureCoordsIndex) + " " + ConstructOBJString2(triplesList[t].poly[0].v + 1 + lastVertexIndex, triplesList[t].poly[0].vn + 1 + lastVertexNormalsIndex, triplesList[t].poly[0].vt + 1 + lastTextureCoordsIndex));
                        }
                        else
                        {
                            sb.AppendLine("f " + ConstructOBJString2(triplesList[t].poly[0].v + 1 + lastVertexIndex, triplesList[t].poly[0].vn + 1 + lastVertexNormalsIndex, triplesList[t].poly[0].vt + 1 + lastTextureCoordsIndex) + " " + ConstructOBJString2(triplesList[t].poly[1].v + 1 + lastVertexIndex, triplesList[t].poly[1].vn + 1 + lastVertexNormalsIndex, triplesList[t].poly[1].vt + 1 + lastTextureCoordsIndex) + " " + ConstructOBJString2(triplesList[t].poly[2].v + 1 + lastVertexIndex, triplesList[t].poly[2].vn + 1 + lastVertexNormalsIndex, triplesList[t].poly[2].vt + 1 + lastTextureCoordsIndex) + " " + ConstructOBJString2(triplesList[t].poly[3].v + 1 + lastVertexIndex, triplesList[t].poly[3].vn + 1 + lastVertexNormalsIndex, triplesList[t].poly[3].vt + 1 + lastTextureCoordsIndex));
                        }
                    }
                }
            }
            lastVertexIndex        += verticeList.Count;
            lastVertexNormalsIndex += normalList.Count;
            lastTextureCoordsIndex += uvList.Count;
        }

        //write to disk
        System.IO.File.WriteAllText(exportPath, sb.ToString());
        if (generateMaterials)
        {
            System.IO.File.WriteAllText(exportFileInfo.Directory.FullName + "\\" + baseFileName + ".mtl", sbMaterials.ToString());
        }

        //export complete, close progress dialog
        EditorUtility.ClearProgressBar();
        EditorUtility.DisplayDialog("Succeed!", "Scene Objects has been exported to: " + Path.GetFullPath(exportPath), "OK");
    }
Example #21
0
 // räknar ut polygonen triangles center of mass.
 public Vector3 calculateCentroidForce(polygon poly, Vector3 linearVelocity, Matrix3 oldRotation, Vector3 angularVelocity, Vector3 objectPosition)
 {
     gravity = new Vector3(0.0f, -9.82f * poly.mass, 0.0f);
     dragForce = calculateDragForce(poly, linearVelocity, oldRotation, angularVelocity, objectPosition);
     //Console.WriteLine("Dragforce: " + dragForce + "\n");
     if (usingGravity)
     {
         return (Vector3)(gravity + dragForce);
     }
     else
     {
         return (Vector3)(dragForce);
     }
     //return new Vector3();
 }
Example #22
0
 => LineInPolygon0(polygon, start, end);
Example #23
0
 if (ContainsPoint(polygon, point))
 {
     return(point);