Ejemplo n.º 1
0
		public static IPrimitive Convert(PolygonMesh.Mesh simpleMesh, MaterialAbstract partMaterial = null)
		{
			List<IPrimitive> renderCollection = new List<IPrimitive>();

			if (partMaterial == null)
			{
				partMaterial = new SolidMaterial(new RGBA_Floats(.9, .2, .1), .01, 0.0, 2.0);
			}
			int index = 0;
			Vector3[] triangle = new Vector3[3];
			foreach (PolygonMesh.Face face in simpleMesh.Faces)
			{
				foreach (PolygonMesh.Vertex vertex in face.Vertices())
				{
					triangle[index++] = vertex.Position;
					if (index == 3)
					{
						index = 0;
						renderCollection.Add(new TriangleShape(triangle[0], triangle[1], triangle[2], partMaterial));
					}
				}
			}

			return BoundingVolumeHierarchy.CreateNewHierachy(renderCollection);
		}
Ejemplo n.º 2
0
		public static IPrimitive ConvertUnoptomized(PolygonMesh.Mesh simpleMesh)
		{
			List<IPrimitive> renderCollection = new List<IPrimitive>();

			//SolidMaterial redStuff = new SolidMaterial(new RGBA_Floats(.9, .2, .1), .01, 0.0, 2.0);
			SolidMaterial mhBlueStuff = new SolidMaterial(new RGBA_Floats(0, .32, .58), .01, 0.0, 2.0);
			int index = 0;
			Vector3[] triangle = new Vector3[3];
			//PolygonMesh.Mesh simpleMesh = PolygonMesh.Processors.StlProcessing.Load("complex.stl");
			//PolygonMesh.Mesh simpleMesh = PolygonMesh.Processors.StlProcessing.Load("Spider With Base.stl");
			foreach (PolygonMesh.Face face in simpleMesh.Faces)
			{
				foreach (PolygonMesh.Vertex vertex in face.Vertices())
				{
					triangle[index++] = vertex.Position;
					if (index == 3)
					{
						index = 0;
						renderCollection.Add(new TriangleShape(triangle[0], triangle[1], triangle[2], mhBlueStuff));
					}
				}
			}

			return new UnboundCollection(renderCollection);
		}
Ejemplo n.º 3
0
    void PlayCoverEffect()
    {
        float       amount = Mathf.Clamp01(_turningAmount);
        float       ratio;
        bool        isLeft;
        GComponent  turningObj = (_coverTurningOp == CoverTurningOp.ShowFront || _coverTurningOp == CoverTurningOp.HideFront) ? _frontCover : _backCover;
        PolygonMesh mesh       = GetHardMesh(turningObj);

        if (amount < 0.5f)
        {
            ratio  = 1 - amount * 2;
            isLeft = _coverTurningOp == CoverTurningOp.ShowFront || _coverTurningOp == CoverTurningOp.HideBack;
        }
        else
        {
            ratio  = (amount - 0.5f) * 2;
            isLeft = _coverTurningOp == CoverTurningOp.HideFront || _coverTurningOp == CoverTurningOp.ShowBack;
        }

        if (turningObj == _frontCover)
        {
            SetCoverStatus(turningObj, CoverType.Front, !isLeft);
        }
        else
        {
            SetCoverStatus(turningObj, CoverType.Back, isLeft);
        }

        mesh.points.Clear();
        mesh.texcoords.Clear();
        if (isLeft)
        {
            float topOffset = 1f / 8 * (1 - ratio);
            float xOffset   = 1 - ratio;
            mesh.Add(new Vector2(xOffset, 1 + topOffset));
            mesh.Add(new Vector2(xOffset, -topOffset));
            mesh.Add(new Vector2(1, 0));
            mesh.Add(new Vector2(1, 1));
        }
        else
        {
            float topOffset = 1f / 8 * (1 - ratio);
            mesh.Add(new Vector2(0, 1));
            mesh.Add(new Vector2(0, 0));
            mesh.Add(new Vector2(ratio, -topOffset));
            mesh.Add(new Vector2(ratio, 1 + topOffset));
        }

        mesh.texcoords.AddRange(VertexBuffer.NormalizedUV);
    }
Ejemplo n.º 4
0
        public float GetDirectionFromOutsideTunnel(Vector3 PlayerRight, Vector3 PlayerPosition, PolygonMesh Tunnel)
        {
            float MinTunnel, MaxTunnel;

            PolygonMesh.ProjectPolygon(PlayerRight, Tunnel, out MinTunnel, out MaxTunnel);
            float FinalPlayerPosition = Vector3.Dot(PlayerRight, PlayerPosition);

            if (MinTunnel < FinalPlayerPosition && MaxTunnel < FinalPlayerPosition)
            {
                return(-1);
            }
            if (MinTunnel > FinalPlayerPosition && MaxTunnel > FinalPlayerPosition)
            {
                return(1);
            }

            return(0);
        }
Ejemplo n.º 5
0
        public float GetDirectionFromOutsideTunnel(AITunnel ActiveTunnel)
        {
            float MinTunnel, MaxTunnel;

            PolygonMesh.ProjectPolygon(Right, ActiveTunnel.CollisionBox, out MinTunnel, out MaxTunnel);
            float FinalPlayerPosition = Vector3.Dot(Right, Position);

            if (MinTunnel < FinalPlayerPosition && MaxTunnel < FinalPlayerPosition)
            {
                return(-1);
            }
            if (MinTunnel > FinalPlayerPosition && MaxTunnel > FinalPlayerPosition)
            {
                return(1);
            }

            return(0);
        }
Ejemplo n.º 6
0
        public void Initialize()
        {
            Player = new PolygonMesh(new Vector3[8]
            {
                // Calculate the position of the vertices on the top face.
                new Vector3(-1.0f, 1.0f, -1.0f),
                new Vector3(-1.0f, 1.0f, 1.0f),
                new Vector3(1.0f, 1.0f, -1.0f),
                new Vector3(1.0f, 1.0f, 1.0f),

                // Calculate the position of the vertices on the bottom face.
                new Vector3(-1.0f, -1.0f, -1.0f),
                new Vector3(-1.0f, -1.0f, 1.0f),
                new Vector3(1.0f, -1.0f, -1.0f),
                new Vector3(1.0f, -1.0f, 1.0f),
            }, new Vector3[3]
            {
                new Vector3(1.0f, 0.0f, 0.0f),
                new Vector3(-1.0f, 0.0f, 0.0f),
                new Vector3(0.0f, 0.0f, 1.0f),
            });
            CollisionBox = new PolygonMesh(new Vector3[8]
            {
                // Calculate the position of the vertices on the top face.
                new Vector3(-1.0f, 1.0f, -1.0f),
                new Vector3(-1.0f, 1.0f, 1.0f),
                new Vector3(1.0f, 1.0f, -1.0f),
                new Vector3(1.0f, 1.0f, 1.0f),

                // Calculate the position of the vertices on the bottom face.
                new Vector3(-1.0f, -1.0f, -1.0f),
                new Vector3(-1.0f, -1.0f, 1.0f),
                new Vector3(1.0f, -1.0f, -1.0f),
                new Vector3(1.0f, -1.0f, 1.0f),
            }, new Vector3[3]
            {
                new Vector3(1.0f, 0.0f, 0.0f),
                new Vector3(0.0f, 0.0f, 1.0f),
                new Vector3(0.0f, 1.0f, 0.0f),
            });
        }
Ejemplo n.º 7
0
        private void LinkAITunnelTogether()
        {
            foreach (AITunnel ActiveTunnel in ListAITunnel)
            {
                for (int T = ListAITunnel.Count - 1; T >= 0; --T)
                {
                    if (ListAITunnel[T] == ActiveTunnel)
                    {
                        continue;
                    }

                    float CurrentPosition = Vector3.Dot(ListAITunnel[T].Forward, ActiveTunnel.Position);
                    float OtherPosition   = Vector3.Dot(ListAITunnel[T].Forward, ListAITunnel[T].Position);

                    if (CurrentPosition < OtherPosition && PolygonMesh.PolygonCollisionSAT(ActiveTunnel.CollisionBox, ListAITunnel[T].CollisionBox, Vector3.Zero).Collided)
                    {
                        ActiveTunnel.ListNextAITunnel.Add(ListAITunnel[T]);
                    }
                }
            }
        }
Ejemplo n.º 8
0
    void SetPageHard(GComponent obj, bool front)
    {
        obj.touchable = false;
        obj.displayObject.cacheAsBitmap = true;
        if (obj.mask != null)
        {
            obj.mask.RemoveFromParent();
            obj.mask = null;
        }

        PolygonMesh mesh = obj.displayObject.paintingGraphics.GetMeshFactory <PolygonMesh>();

        mesh.usePercentPositions = true;
        mesh.points.Clear();
        mesh.texcoords.Clear();
        obj.displayObject.paintingGraphics.SetMeshDirty();

        if (front)
        {
            mesh.points.AddRange(VertexBuffer.NormalizedPosition);
            mesh.texcoords.AddRange(VertexBuffer.NormalizedUV);
        }
    }
Ejemplo n.º 9
0
		public static IPrimitive Convert(PolygonMesh.MeshGroup meshGroup, MaterialAbstract partMaterial = null)
		{
			List<IPrimitive> renderCollection = new List<IPrimitive>();

			SolidMaterial otherMaterial = new SolidMaterial(new RGBA_Floats(.1, .2, .9), .01, 0.0, 2.0);
			if (partMaterial == null)
			{
				partMaterial = new SolidMaterial(new RGBA_Floats(.9, .2, .1), .01, 0.0, 2.0);
			}
			int index = 0;
			Vector3[] triangle = new Vector3[3];
			foreach (PolygonMesh.Mesh mesh in meshGroup.Meshes)
			{
				int materialIntdex = MeshMaterialData.Get(mesh).MaterialIndex;
				foreach (PolygonMesh.Face face in mesh.Faces)
				{
					foreach (PolygonMesh.Vertex vertex in face.Vertices())
					{
						triangle[index++] = vertex.Position;
						if (index == 3)
						{
							index = 0;
							if (materialIntdex == 1)
							{
								renderCollection.Add(new TriangleShape(triangle[0], triangle[1], triangle[2], partMaterial));
							}
							else
							{
								renderCollection.Add(new TriangleShape(triangle[0], triangle[1], triangle[2], otherMaterial));
							}
						}
					}
				}
			}

			return BoundingVolumeHierarchy.CreateNewHierachy(renderCollection);
		}
Ejemplo n.º 10
0
        public float GetHorizontalDistanceToAITunnel(AITunnel ActiveTunnel)
        {
            if (IsMovingAway(ActiveTunnel))
            {
                return(GetDirectionFromInsideTunnel(ActiveTunnel));
            }

            float Min, Max;

            PolygonMesh.ProjectPolygon(Right, ActiveTunnel.CollisionBox, out Min, out Max);

            float FinalPosition = Vector3.Dot(Right, Position);

            if (FinalPosition < Min && FinalPosition < Max)
            {
                return(-1);
            }
            else if (FinalPosition > Min && FinalPosition > Max)
            {
                return(1);
            }
            //Inside Tunnel
            else if (FinalPosition > Min && FinalPosition < Max)
            {
                return(GetHorizontalDistanceToExitPointOfAITunnel(ActiveTunnel));
            }

            //if perpendicular
            float a = Vector3.Dot(ActiveTunnel.Forward, Right);

            if (a == 0)
            {
                return(-1);
            }

            return(0);
        }
Ejemplo n.º 11
0
 public void ComputeGeometryC(PolygonMesh dualMesh)
 {
     //get the mid-Vertex of each edges
     TriMesh.Vertex[] edgeVertices = new TriMesh.Vertex[mesh.Faces.Count * 3];
     foreach (TriMesh.Edge e in mesh.Edges)
     {
         Vector3D midVertex0 = TriMeshUtil.GetMidPoint(e);
         dualMesh.Vertices.Add(new VertexTraits(midVertex0.x,
                                                midVertex0.y,
                                                midVertex0.z));
     }
     //get circumcentre of each faces
     foreach (TriMesh.Face f in mesh.Faces)
     {
         TriMesh.Vertex vertex0      = f.GetVertex(0);
         TriMesh.Vertex vertex1      = f.GetVertex(1);
         TriMesh.Vertex vertex2      = f.GetVertex(2);
         Triangle       triangle     = new Triangle(vertex0.Traits.Position, vertex1.Traits.Position, vertex2.Traits.Position);
         Vector3D       circumCenter = triangle.ComputeCircumCenter();
         dualMesh.Vertices.Add(new VertexTraits(circumCenter.x,
                                                circumCenter.y,
                                                circumCenter.z));
     }
 }
 public override void Reconstruct(PolygonMesh mesh)
 => Invoke.surface_convexhull_xyz_reconstruct(_ptr, mesh);
Ejemplo n.º 13
0
    //-----------------------------
    private void PlayGraph()
    {
        GComponent obj = _demoObjects["Graph"];

        Shape shape;

        shape = obj.GetChild("pie").asGraph.shape;
        EllipseMesh ellipse = shape.graphics.GetMeshFactory <EllipseMesh>();

        ellipse.startDegree = 30;
        ellipse.endDegreee  = 300;
        shape.graphics.SetMeshDirty();

        shape = obj.GetChild("trapezoid").asGraph.shape;
        PolygonMesh trapezoid = shape.graphics.GetMeshFactory <PolygonMesh>();

        trapezoid.usePercentPositions = true;
        trapezoid.points.Clear();
        trapezoid.points.Add(new Vector2(0f, 1f));
        trapezoid.points.Add(new Vector2(0.3f, 0));
        trapezoid.points.Add(new Vector2(0.7f, 0));
        trapezoid.points.Add(new Vector2(1f, 1f));
        trapezoid.texcoords.Clear();
        trapezoid.texcoords.AddRange(VertexBuffer.NormalizedUV);
        shape.graphics.SetMeshDirty();
        shape.graphics.texture = (NTexture)UIPackage.GetItemAsset("Basics", "change");

        shape = obj.GetChild("line").asGraph.shape;
        LineMesh line = shape.graphics.GetMeshFactory <LineMesh>();

        line.lineWidthCurve = AnimationCurve.Linear(0, 25, 1, 10);
        line.roundEdge      = true;
        line.gradient       = lineGradient;
        line.path.Create(new GPathPoint[] {
            new GPathPoint(new Vector3(0, 120, 0)),
            new GPathPoint(new Vector3(20, 120, 0)),
            new GPathPoint(new Vector3(100, 100, 0)),
            new GPathPoint(new Vector3(180, 30, 0)),
            new GPathPoint(new Vector3(100, 0, 0)),
            new GPathPoint(new Vector3(20, 30, 0)),
            new GPathPoint(new Vector3(100, 100, 0)),
            new GPathPoint(new Vector3(180, 120, 0)),
            new GPathPoint(new Vector3(200, 120, 0)),
        });
        shape.graphics.SetMeshDirty();
        GTween.To(0, 1, 5).SetEase(EaseType.Linear).SetTarget(shape.graphics).OnUpdate((GTweener t) =>
        {
            ((NGraphics)t.target).GetMeshFactory <LineMesh>().fillEnd = t.value.x;
            ((NGraphics)t.target).SetMeshDirty();
        });

        shape = obj.GetChild("line2").asGraph.shape;
        LineMesh line2 = shape.graphics.GetMeshFactory <LineMesh>();

        line2.lineWidth = 3;
        line2.roundEdge = true;
        line2.path.Create(new GPathPoint[] {
            new GPathPoint(new Vector3(0, 120, 0), GPathPoint.CurveType.Straight),
            new GPathPoint(new Vector3(60, 30, 0), GPathPoint.CurveType.Straight),
            new GPathPoint(new Vector3(80, 90, 0), GPathPoint.CurveType.Straight),
            new GPathPoint(new Vector3(140, 30, 0), GPathPoint.CurveType.Straight),
            new GPathPoint(new Vector3(160, 90, 0), GPathPoint.CurveType.Straight),
            new GPathPoint(new Vector3(220, 30, 0), GPathPoint.CurveType.Straight)
        });
        shape.graphics.SetMeshDirty();

        GObject  image = obj.GetChild("line3");
        LineMesh line3 = image.displayObject.graphics.GetMeshFactory <LineMesh>();

        line3.lineWidth = 30;
        line3.roundEdge = false;
        line3.path.Create(new GPathPoint[] {
            new GPathPoint(new Vector3(0, 30, 0), new Vector3(50, -30), new Vector3(150, -50)),
            new GPathPoint(new Vector3(200, 30, 0), new Vector3(300, 130)),
            new GPathPoint(new Vector3(400, 30, 0))
        });
        image.displayObject.graphics.SetMeshDirty();
    }
Ejemplo n.º 14
0
        public static void CreatePolygonMesh()
        {
            // 获取当前文档和数据库,启动事务
            var acDoc   = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
            var acCurDb = acDoc.Database;

            using (var acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                // 以读模式打开块表
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                             OpenMode.ForRead) as BlockTable;

                // 以写模式打开块表记录模型空间
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                                OpenMode.ForWrite) as BlockTableRecord;

                // 创建多边形网格
                var acPolyMesh = new PolygonMesh();
                acPolyMesh.MSize = 4;
                acPolyMesh.NSize = 4;

                // 将新对象添加到块表记录和事务
                acBlkTblRec.AppendEntity(acPolyMesh);
                acTrans.AddNewlyCreatedDBObject(acPolyMesh, true);

                // 添加顶点前,必须先将多边形网格添加到块表记录
                var acPts3dPMesh = new Point3dCollection();
                acPts3dPMesh.Add(new Point3d(0, 0, 0));
                acPts3dPMesh.Add(new Point3d(2, 0, 1));
                acPts3dPMesh.Add(new Point3d(4, 0, 0));
                acPts3dPMesh.Add(new Point3d(6, 0, 1));

                acPts3dPMesh.Add(new Point3d(0, 2, 0));
                acPts3dPMesh.Add(new Point3d(2, 2, 1));
                acPts3dPMesh.Add(new Point3d(4, 2, 0));
                acPts3dPMesh.Add(new Point3d(6, 2, 1));

                acPts3dPMesh.Add(new Point3d(0, 4, 0));
                acPts3dPMesh.Add(new Point3d(2, 4, 1));
                acPts3dPMesh.Add(new Point3d(4, 4, 0));
                acPts3dPMesh.Add(new Point3d(6, 4, 0));

                acPts3dPMesh.Add(new Point3d(0, 6, 0));
                acPts3dPMesh.Add(new Point3d(2, 6, 1));
                acPts3dPMesh.Add(new Point3d(4, 6, 0));
                acPts3dPMesh.Add(new Point3d(6, 6, 0));

                foreach (Point3d acPt3d in acPts3dPMesh)
                {
                    var acPMeshVer = new PolygonMeshVertex(acPt3d);
                    acPolyMesh.AppendVertex(acPMeshVer);
                    acTrans.AddNewlyCreatedDBObject(acPMeshVer, true);
                }

                // 打开当前视口
                ViewportTableRecord acVportTblRec;
                acVportTblRec = acTrans.GetObject(acDoc.Editor.ActiveViewportId,
                                                  OpenMode.ForWrite) as ViewportTableRecord;

                // 旋转当前视口的观察方向
                acVportTblRec.ViewDirection = new Vector3d(-1, -1, 1);
                acDoc.Editor.UpdateTiledViewportsFromDatabase();

                // 提交事务
                acTrans.Commit();
            }
        }
Ejemplo n.º 15
0
    public void OnGUI()
    {
        GUILayout.Label("Sprite", EditorStyles.boldLabel);

        EditorGUI.BeginChangeCheck();
        spriteRenderer = (SpriteRenderer)EditorGUILayout.ObjectField(spriteRenderer, typeof(SpriteRenderer), true);
        if (Selection.activeGameObject != null) {
            GameObject o = Selection.activeGameObject;
            spriteRenderer = o.GetComponent<SpriteRenderer>();
        }
        if (EditorGUI.EndChangeCheck()) {
            polygon = new Vector2[0];
        }

        if (spriteRenderer != null) {
            EditorGUILayout.Separator();

            GUILayout.Label("Simplify", EditorStyles.boldLabel);

            simplify = EditorGUILayout.FloatField("Vertex Dist.", simplify);

            if (GUILayout.Button("Generate Polygon")) {
                Rect r = spriteRenderer.sprite.rect;
                Texture2D tex = spriteRenderer.sprite.texture;
                IBitmap bmp = ArrayBitmap.CreateFromTexture(tex, new Rect(r.x, r.y, r.width, r.height));
                polygon = BitmapHelper.CreateFromBitmap(bmp);
                polygon = SimplifyTools.DouglasPeuckerSimplify(new Vertices(polygon), simplify).ToArray();
                EditorUtility.SetDirty(this);
            }

            GUILayout.Label("Vertices: " + polygon.Length);

            EditorGUILayout.Separator();

            if (polygon.Length > 0 && GUILayout.Button("Create Mesh")) {
                CreateMesh();
                EditorUtility.SetDirty(this);
            }

            if (GUILayout.Button("Create Mesh from Sprite")) {
                SpriteMesh spriteMesh = new SpriteMesh();
                spriteMesh.spriteRenderer = spriteRenderer;
                spriteMesh.CreateSpriteMesh();
                EditorUtility.SetDirty(this);
            }

            EditorGUILayout.Separator();

            if (GUILayout.Button("Create Mesh from Polygon2D Collider")) {
                PolygonCollider2D polygonCollider = spriteRenderer.GetComponent<PolygonCollider2D>();
                if (polygonCollider == null) {
                    polygonCollider = spriteRenderer.gameObject.AddComponent<PolygonCollider2D>();
                }

                PolygonMesh polygonMesh = new PolygonMesh();
                polygonMesh.polygonCollider = polygonCollider;
                polygonMesh.spriteRenderer = spriteRenderer;
                polygonMesh.CreatePolygonMesh();
                EditorUtility.SetDirty(this);
            }

            EditorGUILayout.Separator();

            GUILayout.Label("Create and Edit a Custom Polygon", EditorStyles.boldLabel);
            handleScale = EditorGUILayout.FloatField("Handle Size", handleScale);
            handleColor = EditorGUILayout.ColorField("Handle Color", handleColor);
            polyColor = EditorGUILayout.ColorField("Poly Color", polyColor);

            EditorGUILayout.Separator();

            GUILayout.Label("Ctrl + Click to Add Point, Shift + Click to Add Mid Point, Alt + Click to Remove Points", EditorStyles.whiteLabel);

            EditorGUILayout.Separator();

            if (GUILayout.Button("Create and Edit Polygon")) {
                CreatePolygon();
                EditorUtility.SetDirty(this);
            }

            EditorUtility.SetSelectedRenderState(spriteRenderer, EditorSelectedRenderState.Hidden);

            if (GUILayout.Button("Update Custom Mesh")) {
                if (spriteRenderer != null) UpdateMesh();
                EditorUtility.SetDirty(this);
            }

            if (GUILayout.Button("Save Custom Mesh")) {
                if (spriteRenderer != null) SaveMesh();
            }

            if (GUI.changed) {
                UpdateMesh();
                EditorUtility.SetDirty(this);
            }

            GUILayout.Label("Subdivide Mesh", EditorStyles.boldLabel);

            string[] subdivide = { "0", "1", "2", "3" };

            divisions = EditorGUILayout.Popup("Subdivisions", divisions, subdivide);

            if (GUILayout.Button("Subdivide Mesh")) {
                if (spriteRenderer != null && mesh != null) SubdivideMesh(divisions);
            }

            EditorGUILayout.Separator();

            GUILayout.Label("Load Custom Mesh to Edit", EditorStyles.boldLabel);

            GUILayout.Label("Adding or Deleting points Re-Triangulates Mesh", EditorStyles.whiteLabel);

            EditorGUILayout.Separator();
            customLoadMesh = (Mesh)EditorGUILayout.ObjectField(customLoadMesh, typeof(Mesh), true);

            if (GUILayout.Button("Load Custom Mesh")) {
                if (spriteRenderer != null && customLoadMesh != null) {
                    LoadMesh(customLoadMesh);
                }
            }

            EditorGUILayout.Separator();

            GUILayout.Label("Combine Meshes", EditorStyles.boldLabel);

            GUILayout.Label("Avoid Combining Complex Meshes, results will vary", EditorStyles.whiteLabel);

            EditorGUILayout.Separator();
            combineMesh = (Mesh)EditorGUILayout.ObjectField(combineMesh, typeof(Mesh), true);

            if (GUILayout.Button("Combine Meshes")) {
                if (spriteRenderer != null) CombineMesh();
            }
        }
    }
Ejemplo n.º 16
0
    public void OnGUI()
    {
        GUILayout.Label("Sprite", EditorStyles.boldLabel);

        EditorGUI.BeginChangeCheck();
        spriteRenderer = (SpriteRenderer)EditorGUILayout.ObjectField(spriteRenderer, typeof(SpriteRenderer), true);
        if (Selection.activeGameObject != null)
        {
            GameObject o = Selection.activeGameObject;
            spriteRenderer = o.GetComponent <SpriteRenderer>();
        }
        if (EditorGUI.EndChangeCheck())
        {
            polygon = new Vector2[0];
        }

        if (spriteRenderer != null)
        {
            EditorGUILayout.Separator();

            GUILayout.Label("Simplify", EditorStyles.boldLabel);

            simplify = EditorGUILayout.FloatField("Vertex Dist.", simplify);

            if (GUILayout.Button("Generate Polygon"))
            {
                Rect      r   = spriteRenderer.sprite.rect;
                Texture2D tex = spriteRenderer.sprite.texture;
                IBitmap   bmp = ArrayBitmap.CreateFromTexture(tex, new Rect(r.x, r.y, r.width, r.height));
                polygon = BitmapHelper.CreateFromBitmap(bmp);
                polygon = SimplifyTools.DouglasPeuckerSimplify(new Vertices(polygon), simplify).ToArray();
            }

            GUILayout.Label("Vertices: " + polygon.Length);

            EditorGUILayout.Separator();

            if (polygon.Length > 0 && GUILayout.Button("Create Mesh"))
            {
                CreateMesh();
            }

            if (GUILayout.Button("Create Mesh from Sprite"))
            {
                SpriteMesh spriteMesh = new SpriteMesh();
                spriteMesh.spriteRenderer = spriteRenderer;
                spriteMesh.CreateSpriteMesh();
            }

            EditorGUILayout.Separator();

            if (GUILayout.Button("Create Mesh from Polygon2D Collider"))
            {
                PolygonCollider2D polygonCollider = spriteRenderer.GetComponent <PolygonCollider2D>();
                if (polygonCollider == null)
                {
                    polygonCollider = spriteRenderer.gameObject.AddComponent <PolygonCollider2D>();
                }

                PolygonMesh polygonMesh = new PolygonMesh();
                polygonMesh.polygonCollider = polygonCollider;
                polygonMesh.spriteRenderer  = spriteRenderer;
                polygonMesh.CreatePolygonMesh();
            }
        }
    }
Ejemplo n.º 17
0
        public void MakeTheTopologyB(PolygonMesh dualMesh)
        {
            List <TriMesh.Vertex> boundaryVertices = new List <TriMesh.Vertex>();

            boundaryVertices = TriMeshUtil.RetrieveAllBoundaryVertex(mesh);
            foreach (TriMesh.Face face in mesh.Faces)
            {
                int              index           = face.Index;
                int              baryVertexIndex = mesh.Edges.Count;
                int              edgeVertexIndex;
                int              faceIndex        = index;
                TriMesh.Edge     boundaryEdge     = new TriMesh.Edge();
                TriMesh.HalfEdge tempHalfedge     = new TriMesh.HalfEdge();
                TriMesh.Vertex   vertex0          = mesh.Faces[index].GetVertex(0);
                TriMesh.Vertex   vertex1          = mesh.Faces[index].GetVertex(1);
                TriMesh.Vertex   vertex2          = mesh.Faces[index].GetVertex(2);
                TriMesh.HalfEdge halfedge0        = vertex0.FindHalfedgeTo(vertex1);
                TriMesh.HalfEdge halfedge1        = vertex1.FindHalfedgeTo(vertex2);
                TriMesh.HalfEdge halfedge2        = vertex2.FindHalfedgeTo(vertex0);
                TriMesh.HalfEdge boundaryHalfedge = new TriMesh.HalfEdge();
                tempHalfedge = halfedge0;

                edgeVertexIndex = halfedge0.Edge.Index;
                TriMesh.Vertex firstVertex  = dualMesh.Vertices[baryVertexIndex + faceIndex];
                TriMesh.Vertex secondVertex = dualMesh.Vertices[edgeVertexIndex];
                if (firstVertex.FindHalfedgeTo(secondVertex) == null)
                {
                    List <TriMesh.Vertex> ringVertices = new List <TriMesh.Vertex>();
                    do
                    {
                        ringVertices.Add(dualMesh.Vertices[baryVertexIndex + faceIndex]);
                        ringVertices.Add(dualMesh.Vertices[edgeVertexIndex]);
                        if (tempHalfedge.Opposite.Next.Face != null)
                        {
                            tempHalfedge    = tempHalfedge.Opposite.Next;
                            faceIndex       = tempHalfedge.Face.Index;
                            edgeVertexIndex = tempHalfedge.Edge.Index;
                        }
                        else
                        {
                            tempHalfedge    = tempHalfedge.Opposite.Next;
                            edgeVertexIndex = tempHalfedge.Edge.Index;
                            faceIndex       = tempHalfedge.Opposite.Next.Face.Index;
                        }
                    } while (tempHalfedge != halfedge0);
                    TriMesh.Vertex[] newFaceVertices = new TriMesh.Vertex[ringVertices.Count];
                    for (int i = 0; i < ringVertices.Count; i++)
                    {
                        newFaceVertices[i] = ringVertices[i];
                    }
                    dualMesh.Faces.Add(newFaceVertices);
                }
                tempHalfedge    = halfedge1;
                edgeVertexIndex = halfedge1.Edge.Index;
                firstVertex     = dualMesh.Vertices[baryVertexIndex + faceIndex];
                secondVertex    = dualMesh.Vertices[edgeVertexIndex];
                if (firstVertex.FindHalfedgeTo(secondVertex) == null)
                {
                    List <TriMesh.Vertex> ringVertices = new List <TriMesh.Vertex>();
                    do
                    {
                        ringVertices.Add(dualMesh.Vertices[baryVertexIndex + faceIndex]);
                        ringVertices.Add(dualMesh.Vertices[edgeVertexIndex]);
                        if (tempHalfedge.Opposite.Next != null)
                        {
                            tempHalfedge    = tempHalfedge.Opposite.Next;
                            faceIndex       = tempHalfedge.Face.Index;
                            edgeVertexIndex = tempHalfedge.Edge.Index;
                        }
                        else
                        {
                            continue;
                        }
                    } while (tempHalfedge != halfedge1);
                    TriMesh.Vertex[] newFaceVertices = new TriMesh.Vertex[ringVertices.Count];
                    for (int i = 0; i < ringVertices.Count; i++)
                    {
                        newFaceVertices[i] = ringVertices[i];
                    }
                    dualMesh.Faces.Add(newFaceVertices);
                }
                tempHalfedge    = halfedge2;
                edgeVertexIndex = halfedge2.Edge.Index;
                firstVertex     = dualMesh.Vertices[baryVertexIndex + faceIndex];
                secondVertex    = dualMesh.Vertices[edgeVertexIndex];
                if (firstVertex.FindHalfedgeTo(secondVertex) == null)
                {
                    List <TriMesh.Vertex> ringVertices = new List <TriMesh.Vertex>();
                    do
                    {
                        ringVertices.Add(dualMesh.Vertices[baryVertexIndex + faceIndex]);
                        ringVertices.Add(dualMesh.Vertices[edgeVertexIndex]);
                        if (tempHalfedge.Opposite.Next != null)
                        {
                            tempHalfedge    = tempHalfedge.Opposite.Next;
                            faceIndex       = tempHalfedge.Face.Index;
                            edgeVertexIndex = tempHalfedge.Edge.Index;
                        }
                        else
                        {
                            continue;
                        }
                    } while (tempHalfedge != halfedge2);
                    TriMesh.Vertex[] newFaceVertices = new TriMesh.Vertex[ringVertices.Count];
                    for (int i = 0; i < ringVertices.Count; i++)
                    {
                        newFaceVertices[i] = ringVertices[i];
                    }
                    dualMesh.Faces.Add(newFaceVertices);
                }
            }
        }
Ejemplo n.º 18
0
 void Start()
 {
     polygon = gameObject.GetComponent <PolygonMesh>();
 }
Ejemplo n.º 19
0
    //-----------------------------
    private void PlayGraph()
    {
        GComponent obj = _demoObjects["Graph"];

        Shape shape;

        shape = obj.GetChild("pie").asGraph.shape;
        EllipseMesh ellipse = shape.graphics.GetMeshFactory <EllipseMesh>();

        ellipse.startDegree = 30;
        ellipse.endDegreee  = 300;
        shape.graphics.SetMeshDirty();

        shape = obj.GetChild("trapezoid").asGraph.shape;
        PolygonMesh trapezoid = shape.graphics.GetMeshFactory <PolygonMesh>();

        trapezoid.usePercentPositions = true;
        trapezoid.points.Clear();
        trapezoid.points.Add(new Vector2(0.3f, 0));
        trapezoid.points.Add(new Vector2(0.7f, 0));
        trapezoid.points.Add(new Vector2(1f, 1f));
        trapezoid.points.Add(new Vector2(0f, 1f));
        shape.graphics.SetMeshDirty();

        shape = obj.GetChild("polygon").asGraph.shape;
        shape.DrawPolygon(new Vector2[] {
            new Vector2(20, 30),
            new Vector2(80, 40),
            new Vector2(80, 15),
            new Vector2(140, 50),
            new Vector2(80, 85),
            new Vector2(80, 60),
            new Vector2(20, 70)
        }, shape.color);

        shape = obj.GetChild("polygon2").asGraph.shape;
        shape.DrawRegularPolygon(5, 2, Color.yellow, Color.black, shape.color, -18, null);

        shape = obj.GetChild("radial").asGraph.shape;
        shape.DrawRegularPolygon(6, 2, shape.color, new Color32(0xFF, 0x99, 0x00, 128), shape.color, 0, new float[] { 0.6f, 0.8f, 0.6f, 0.8f, 0.6f, 0.5f });

        shape = obj.GetChild("line").asGraph.shape;
        LineMesh line = shape.graphics.GetMeshFactory <LineMesh>();

        line.lineWidthCurve = AnimationCurve.Linear(0, 25, 1, 10);
        line.roundEdge      = true;
        line.gradient       = lineGradient;
        line.path.Create(new GPathPoint[] {
            new GPathPoint(new Vector3(0, 120, 0)),
            new GPathPoint(new Vector3(20, 120, 0)),
            new GPathPoint(new Vector3(100, 100, 0)),
            new GPathPoint(new Vector3(180, 30, 0)),
            new GPathPoint(new Vector3(100, 0, 0)),
            new GPathPoint(new Vector3(20, 30, 0)),
            new GPathPoint(new Vector3(100, 100, 0)),
            new GPathPoint(new Vector3(180, 120, 0)),
            new GPathPoint(new Vector3(200, 120, 0)),
        });
        shape.graphics.SetMeshDirty();
        GTween.To(0, 1, 5).SetEase(EaseType.Linear).SetTarget(shape.graphics).OnUpdate((GTweener t) =>
        {
            ((NGraphics)t.target).GetMeshFactory <LineMesh>().fillEnd = t.value.x;
            ((NGraphics)t.target).SetMeshDirty();
        });

        shape = obj.GetChild("line2").asGraph.shape;
        LineMesh line2 = shape.graphics.GetMeshFactory <LineMesh>();

        line2.lineWidth = 3;
        line2.roundEdge = true;
        line2.path.Create(new GPathPoint[] {
            new GPathPoint(new Vector3(0, 120, 0), GPathPoint.CurveType.Straight),
            new GPathPoint(new Vector3(60, 30, 0), GPathPoint.CurveType.Straight),
            new GPathPoint(new Vector3(80, 90, 0), GPathPoint.CurveType.Straight),
            new GPathPoint(new Vector3(140, 30, 0), GPathPoint.CurveType.Straight),
            new GPathPoint(new Vector3(160, 90, 0), GPathPoint.CurveType.Straight),
            new GPathPoint(new Vector3(220, 30, 0), GPathPoint.CurveType.Straight)
        });
        shape.graphics.SetMeshDirty();

        GObject  image = obj.GetChild("line3");
        LineMesh line3 = image.displayObject.graphics.GetMeshFactory <LineMesh>();

        line3.lineWidth = 30;
        line3.roundEdge = false;
        line3.path.Create(new GPathPoint[] {
            new GPathPoint(new Vector3(0, 30, 0), new Vector3(50, -30), new Vector3(150, -50)),
            new GPathPoint(new Vector3(200, 30, 0), new Vector3(300, 130)),
            new GPathPoint(new Vector3(400, 30, 0))
        });
        image.displayObject.graphics.SetMeshDirty();
    }
Ejemplo n.º 20
0
    void PlaySoftEffect()
    {
        GComponent  turningObj1 = _objects[2];
        GComponent  turningObj2 = _objects[3];
        PolygonMesh mesh1       = GetSoftMesh(turningObj1);
        PolygonMesh mesh2       = GetSoftMesh(turningObj2);

        /**
         *               a
         *              /  \
         * f(0,0)------/    b--g(w,0)
         * |          /     /  |
         * |         /     /   |
         * |        c     /    |
         * |         \   /     |
         * |          \ /      |
         * e(0,h)-----d--------h(w,h)
         */
        Vector2 pa, pb, pc, pd, pe, pf, pg, ph;
        float   k, angle;
        bool    threePoints = false;

        pc = _dragPoint;
        pe = new Vector2(0, _pageHeight);
        pf = Vector2.zero;
        pg = new Vector2(_pageWidth, 0);
        ph = new Vector2(_pageWidth, _pageHeight);

        FlipPoint(ref pc, _pageWidth * 2, _pageHeight);
        pc.x -= _pageWidth;
        if (pc.x >= _pageWidth)
        {
            return;
        }

        k = (ph.y - pc.y) / (ph.x - pc.x);
        float k2 = 1 + Mathf.Pow(k, 2);
        float min;

        min = ph.x - _pageWidth * 2 / k2;
        if (pc.x < min)
        {
            pc.x = min;
            if (pc.x >= _pageWidth)
            {
                return;
            }
            pc.y = ph.y - k * (ph.x - pc.x);
        }

        min = ph.x - (_pageWidth + _pageHeight * k) * 2 / k2;
        if (pc.x < min)
        {
            pc.x = min;
            if (pc.x >= _pageWidth)
            {
                return;
            }
            pc.y = ph.y - k * (ph.x - pc.x);
        }

        angle = Mathf.Atan(k) * Mathf.Rad2Deg;
        pd    = new Vector2(_pageWidth - k2 * (ph.x - pc.x) / 2, _pageHeight);
        pb    = new Vector2(pd.x + _pageHeight * k, 0);
        pa    = new Vector2();

        if (pb.x > _pageWidth)
        {
            pb.x        = _pageWidth;
            pa          = new Vector2(_pageWidth, _pageHeight - (_pageWidth - pd.x) / k);
            threePoints = true;
        }

        FlipPoint(ref pa, _pageWidth, _pageHeight);
        FlipPoint(ref pb, _pageWidth, _pageHeight);
        FlipPoint(ref pd, _pageWidth, _pageHeight);
        FlipPoint(ref pc, _pageWidth, _pageHeight);
        if (_draggingCorner == Corner.BL || _draggingCorner == Corner.TL)
        {
            angle = -angle;
        }

        switch (_draggingCorner)
        {
        case Corner.BR:
        {
            turningObj1.SetPivot(0, 0, true);
            turningObj1.position = new Vector2(_pageWidth, 0);

            turningObj2.SetPivot(0, 1, true);
            turningObj2.position = new Vector2(_pageWidth + pc.x, pc.y);
            turningObj2.rotation = 2 * angle;

            if (_softShadow != null)
            {
                _softShadow.SetPivot(1, (_softShadow.height - 30) / _softShadow.height, true);
                _softShadow.position = new Vector2(Vector2.Distance(pc, pd), _pageHeight);
                _softShadow.rotation = -angle;
                if (_softShadow.x > _pageWidth - 20)
                {
                    _softShadow.alpha = (_pageWidth - _softShadow.x) / 20;
                }
                else
                {
                    _softShadow.alpha = 1;
                }
            }

            mesh1.points.Clear();
            mesh1.Add(pe);
            mesh1.Add(pf);
            mesh1.Add(pb);
            if (threePoints)
            {
                mesh1.Add(pa);
            }
            mesh1.Add(pd);

            mesh2.points.Clear();
            mesh2.Add(new Vector2(Vector2.Distance(pc, pd), _pageHeight));
            mesh2.Add(new Vector2(0, _pageHeight));
            if (threePoints)
            {
                mesh2.Add(new Vector2(0, _pageHeight - Vector2.Distance(pc, pa)));
            }
            else
            {
                mesh2.Add(new Vector2(0, 0));
                mesh2.Add(new Vector2(Vector2.Distance(pg, pb), 0));
            }
            break;
        }

        case Corner.TR:
        {
            turningObj1.SetPivot(0, 0, true);
            turningObj1.position = new Vector2(_pageWidth, 0);

            turningObj2.SetPivot(0, 0, true);
            turningObj2.position = new Vector2(_pageWidth + pc.x, pc.y);
            turningObj2.rotation = -2 * angle;

            if (_softShadow != null)
            {
                _softShadow.SetPivot(1, 30 / _softShadow.height, true);
                _softShadow.position = new Vector2(Vector2.Distance(pc, pd), 0);
                _softShadow.rotation = angle;
                if (_softShadow.x > _pageWidth - 20)
                {
                    _softShadow.alpha = (_pageWidth - _softShadow.x) / 20;
                }
                else
                {
                    _softShadow.alpha = 1;
                }
            }

            mesh1.points.Clear();
            mesh1.Add(pe);
            mesh1.Add(pf);
            mesh1.Add(pd);
            if (threePoints)
            {
                mesh1.Add(pa);
            }
            mesh1.Add(pb);

            mesh2.points.Clear();
            if (threePoints)
            {
                mesh2.Add(new Vector2(0, Vector2.Distance(pc, pa)));
            }
            else
            {
                mesh2.Add(new Vector2(Vector2.Distance(pb, ph), _pageHeight));
                mesh2.Add(new Vector2(0, _pageHeight));
            }
            mesh2.Add(new Vector2(0, 0));
            mesh2.Add(new Vector2(Vector2.Distance(pc, pd), 0));
            break;
        }

        case Corner.BL:
        {
            turningObj1.SetPivot(0, 0, true);
            turningObj1.position = Vector2.zero;

            turningObj2.SetPivot(1, 1, true);
            turningObj2.position = pc;
            turningObj2.rotation = 2 * angle;

            if (_softShadow != null)
            {
                _softShadow.SetPivot(1, 30 / _softShadow.height, true);
                _softShadow.position = new Vector2(_pageWidth - Vector2.Distance(pc, pd), _pageHeight);
                _softShadow.rotation = 180 - angle;
                if (_softShadow.x < 20)
                {
                    _softShadow.alpha = (_softShadow.x - 20) / 20;
                }
                else
                {
                    _softShadow.alpha = 1;
                }
            }

            mesh1.points.Clear();
            mesh1.Add(pb);
            mesh1.Add(pg);
            mesh1.Add(ph);
            mesh1.Add(pd);
            if (threePoints)
            {
                mesh1.Add(pa);
            }

            mesh2.points.Clear();
            if (!threePoints)
            {
                mesh2.Add(new Vector2(_pageWidth - Vector2.Distance(pf, pb), 0));
                mesh2.Add(new Vector2(_pageWidth, 0));
            }
            else
            {
                mesh2.Add(new Vector2(_pageWidth, _pageHeight - Vector2.Distance(pc, pa)));
            }
            mesh2.Add(new Vector2(_pageWidth, _pageHeight));
            mesh2.Add(new Vector2(_pageWidth - Vector2.Distance(pc, pd), _pageHeight));
            break;
        }

        case Corner.TL:
        {
            turningObj1.SetPivot(0, 0, true);
            turningObj1.position = Vector2.zero;

            turningObj2.SetPivot(1, 0, true);
            turningObj2.position = pc;
            turningObj2.rotation = -2 * angle;

            if (_softShadow != null)
            {
                _softShadow.SetPivot(1, (_softShadow.height - 30) / _softShadow.height, true);
                _softShadow.position = new Vector2(_pageWidth - Vector2.Distance(pc, pd), 0);
                _softShadow.rotation = 180 + angle;
                if (_softShadow.x < 20)
                {
                    _softShadow.alpha = (_softShadow.x - 20) / 20;
                }
                else
                {
                    _softShadow.alpha = 1;
                }
            }

            mesh1.points.Clear();
            mesh1.Add(pd);
            mesh1.Add(pg);
            mesh1.Add(ph);
            mesh1.Add(pb);
            if (threePoints)
            {
                mesh1.Add(pa);
            }

            mesh2.points.Clear();
            mesh2.Add(new Vector2(_pageWidth - Vector2.Distance(pc, pd), 0));
            mesh2.Add(new Vector2(_pageWidth, 0));
            if (threePoints)
            {
                mesh2.Add(new Vector2(_pageWidth, Vector2.Distance(pc, pa)));
            }
            else
            {
                mesh2.Add(new Vector2(_pageWidth, _pageHeight));
                mesh2.Add(new Vector2(_pageWidth - Vector2.Distance(pe, pb), _pageHeight));
            }
            break;
        }
        }
    }
Ejemplo n.º 21
0
    public void OnGUI()
    {
        GUILayout.Label("Sprite", EditorStyles.boldLabel);

        EditorGUI.BeginChangeCheck();
        spriteRenderer = (SpriteRenderer)EditorGUILayout.ObjectField(spriteRenderer, typeof(SpriteRenderer), true);
        if (Selection.activeGameObject != null)
        {
            GameObject o = Selection.activeGameObject;
            spriteRenderer = o.GetComponent <SpriteRenderer>();
        }
        if (EditorGUI.EndChangeCheck())
        {
            polygon = new Vector2[0];
        }

        if (spriteRenderer != null)
        {
            EditorGUILayout.Separator();

            GUILayout.Label("Simplify", EditorStyles.boldLabel);

            simplify = EditorGUILayout.FloatField("Vertex Dist.", simplify);

            if (GUILayout.Button("Generate Polygon"))
            {
                Rect      r   = spriteRenderer.sprite.rect;
                Texture2D tex = spriteRenderer.sprite.texture;
                IBitmap   bmp = ArrayBitmap.CreateFromTexture(tex, new Rect(r.x, r.y, r.width, r.height));
                polygon = BitmapHelper.CreateFromBitmap(bmp);
                polygon = SimplifyTools.DouglasPeuckerSimplify(new Vertices(polygon), simplify).ToArray();
                EditorUtility.SetDirty(this);
            }

            GUILayout.Label("Vertices: " + polygon.Length);

            EditorGUILayout.Separator();

            if (polygon.Length > 0 && GUILayout.Button("Create Mesh"))
            {
                CreateMesh();
                EditorUtility.SetDirty(this);
            }

            if (GUILayout.Button("Create Mesh from Sprite"))
            {
                SpriteMesh spriteMesh = new SpriteMesh();
                spriteMesh.spriteRenderer = spriteRenderer;
                spriteMesh.CreateSpriteMesh();
                EditorUtility.SetDirty(this);
            }

            EditorGUILayout.Separator();

            if (GUILayout.Button("Create Mesh from Polygon2D Collider"))
            {
                PolygonCollider2D polygonCollider = spriteRenderer.GetComponent <PolygonCollider2D>();
                if (polygonCollider == null)
                {
                    polygonCollider = spriteRenderer.gameObject.AddComponent <PolygonCollider2D>();
                }

                PolygonMesh polygonMesh = new PolygonMesh();
                polygonMesh.polygonCollider = polygonCollider;
                polygonMesh.spriteRenderer  = spriteRenderer;
                polygonMesh.CreatePolygonMesh();
                EditorUtility.SetDirty(this);
            }

            EditorGUILayout.Separator();

            GUILayout.Label("Create and Edit a Custom Polygon", EditorStyles.boldLabel);
            handleScale = EditorGUILayout.FloatField("Handle Size", handleScale);
            handleColor = EditorGUILayout.ColorField("Handle Color", handleColor);
            polyColor   = EditorGUILayout.ColorField("Poly Color", polyColor);

            EditorGUILayout.Separator();

            GUILayout.Label("Ctrl + Click to Add Point, Shift + Click to Add Mid Point, Alt + Click to Remove Points", EditorStyles.whiteLabel);

            EditorGUILayout.Separator();

            if (GUILayout.Button("Create and Edit Polygon"))
            {
                CreatePolygon();
                EditorUtility.SetDirty(this);
            }

            EditorUtility.SetSelectedWireframeHidden(spriteRenderer, true);

            if (GUILayout.Button("Update Custom Mesh"))
            {
                if (spriteRenderer != null)
                {
                    UpdateMesh();
                }
                EditorUtility.SetDirty(this);
            }

            if (GUILayout.Button("Save Custom Mesh"))
            {
                if (spriteRenderer != null)
                {
                    SaveMesh();
                }
            }

            if (GUI.changed)
            {
                UpdateMesh();
                EditorUtility.SetDirty(this);
            }

            GUILayout.Label("Subdivide Mesh", EditorStyles.boldLabel);

            string[] subdivide = { "0", "1", "2", "3" };

            divisions = EditorGUILayout.Popup("Subdivisions", divisions, subdivide);

            if (GUILayout.Button("Subdivide Mesh"))
            {
                if (spriteRenderer != null && mesh != null)
                {
                    SubdivideMesh(divisions);
                }
            }

            EditorGUILayout.Separator();

            GUILayout.Label("Load Custom Mesh to Edit", EditorStyles.boldLabel);

            GUILayout.Label("Adding or Deleting points Re-Triangulates Mesh", EditorStyles.whiteLabel);

            EditorGUILayout.Separator();
            customLoadMesh = (Mesh)EditorGUILayout.ObjectField(customLoadMesh, typeof(Mesh), true);

            if (GUILayout.Button("Load Custom Mesh"))
            {
                if (spriteRenderer != null && customLoadMesh != null)
                {
                    LoadMesh(customLoadMesh);
                }
            }

            EditorGUILayout.Separator();

            GUILayout.Label("Combine Meshes", EditorStyles.boldLabel);

            GUILayout.Label("Avoid Combining Complex Meshes, results will vary", EditorStyles.whiteLabel);

            EditorGUILayout.Separator();
            combineMesh = (Mesh)EditorGUILayout.ObjectField(combineMesh, typeof(Mesh), true);

            if (GUILayout.Button("Combine Meshes"))
            {
                if (spriteRenderer != null)
                {
                    CombineMesh();
                }
            }
        }
    }
Ejemplo n.º 22
0
    public void OnGUI() {
        GUILayout.Label("Sprite", EditorStyles.boldLabel);

        EditorGUI.BeginChangeCheck();
        spriteRenderer = (SpriteRenderer)EditorGUILayout.ObjectField(spriteRenderer, typeof(SpriteRenderer), true);
        if (Selection.activeGameObject != null) {
            GameObject o = Selection.activeGameObject;
            spriteRenderer = o.GetComponent<SpriteRenderer>();
        }
        if (EditorGUI.EndChangeCheck()) {
            polygon = new Vector2[0];
        }

        if (spriteRenderer != null) {
            EditorGUILayout.Separator();

            GUILayout.Label("Simplify", EditorStyles.boldLabel);

            simplify = EditorGUILayout.FloatField("Vertex Dist.", simplify);

            if (GUILayout.Button("Generate Polygon")) {
                Rect r = spriteRenderer.sprite.rect;
                Texture2D tex = spriteRenderer.sprite.texture;
                IBitmap bmp = ArrayBitmap.CreateFromTexture(tex, new Rect(r.x, r.y, r.width, r.height));
                polygon = BitmapHelper.CreateFromBitmap(bmp);
                polygon = SimplifyTools.DouglasPeuckerSimplify(new Vertices(polygon), simplify).ToArray();
            }

            GUILayout.Label("Vertices: " + polygon.Length);

            EditorGUILayout.Separator();

            if (polygon.Length > 0 && GUILayout.Button("Create Mesh")) {
                CreateMesh();
            }

            if (GUILayout.Button("Create Mesh from Sprite")) {
                SpriteMesh spriteMesh = new SpriteMesh();
                spriteMesh.spriteRenderer = spriteRenderer;
                spriteMesh.CreateSpriteMesh();
            }

            EditorGUILayout.Separator();

            if (GUILayout.Button("Create Mesh from Polygon2D Collider")) {
                PolygonCollider2D polygonCollider = spriteRenderer.GetComponent<PolygonCollider2D>();
                if (polygonCollider == null) {
                    polygonCollider = spriteRenderer.gameObject.AddComponent<PolygonCollider2D>();
                }

                PolygonMesh polygonMesh = new PolygonMesh();
                polygonMesh.polygonCollider = polygonCollider;
                polygonMesh.spriteRenderer = spriteRenderer;
                polygonMesh.CreatePolygonMesh();

            }
        }
    }
Ejemplo n.º 23
0
 public abstract void Reconstruct(PolygonMesh output);