Exemple #1
0
        public void Initialize()
        {
            if (GetComponents <Mesh2D>().Length > 1)
            {
                //Slicer2D.Debug.LogError("Multiple 'Mesh2D' components cannot be attached to the same game object");
                return;
            }

            // Generate Mesh from collider
            Polygon2D polygon = Polygon2DList.CreateFromGameObject(gameObject)[0];

            if (polygon != null)
            {
                polygon.CreateMesh(gameObject, materialScale, materialOffset, triangulation);

                meshFilter = GetComponent <MeshFilter>();

                // Setting Mesh material
                if (material != null)
                {
                    meshRenderer = GetComponent <MeshRenderer> ();
                    meshRenderer.sharedMaterial = material;

                    meshRenderer.sortingLayerName = sortingLayerName;
                    meshRenderer.sortingOrder     = sortingOrder;
                }
            }
        }
 public List <Polygon2D> GetPolygons()
 {
     if (polygon == null)
     {
         polygon = Polygon2DList.CreateFromGameObject(gameObject);
     }
     return(polygon);
 }
Exemple #3
0
        public static Mesh PerformTriangulation(Polygon2D polygon, Vector2 UVScale, Vector2 UVOffset)
        {
            Polygon2DList.RemoveClosePoints(polygon.pointsList);

            foreach (Polygon2D hole in polygon.holesList)
            {
                Polygon2DList.RemoveClosePoints(hole.pointsList);
            }

            TriangulationWrapper.Polygon poly = new TriangulationWrapper.Polygon();

            List <Vector2> pointsList   = null;
            List <Vector2> UVpointsList = null;

            Vector3 v = Vector3.zero;

            foreach (Vector2D p in polygon.pointsList)
            {
                v.x = (float)p.x;
                v.y = (float)p.y;

                poly.outside.Add(v);
                poly.outsideUVs.Add(new Vector2(v.x / UVScale.x + .5f + UVOffset.x, v.y / UVScale.y + .5f + UVOffset.y));
            }

            foreach (Polygon2D hole in polygon.holesList)
            {
                pointsList   = new List <Vector2>();
                UVpointsList = new List <Vector2>();

                foreach (Vector2D p in hole.pointsList)
                {
                    v.x = (float)p.x;
                    v.y = (float)p.y;

                    pointsList.Add(v);

                    UVpointsList.Add(new Vector2(v.x / UVScale.x + .5f, v.y / UVScale.y + .5f));
                }

                poly.holes.Add(pointsList);
                poly.holesUVs.Add(UVpointsList);
            }

            return(TriangulationWrapper.CreateMesh(poly));
        }
Exemple #4
0
        void Start()
        {
            // Generate Mesh from collider
            Polygon2D polygon = Polygon2DList.CreateFromGameObject(gameObject)[0];

            if (polygon != null)
            {
                polygon.CreateMesh3D(gameObject, size, Vector2.zero, Vector2.zero, zSize, triangulation);

                // Setting Mesh material
                if (material != null)
                {
                    MeshRenderer meshRenderer = GetComponent <MeshRenderer> ();
                    meshRenderer.sharedMaterial = material;

                    meshRenderer.sortingLayerName = sortingLayerName;
                    meshRenderer.sortingLayerID   = sortingLayerID;
                    meshRenderer.sortingOrder     = sortingOrder;
                }
            }
        }
        public static void SpriteToMesh3D(GameObject gameObject, VirtualSpriteRenderer spriteRenderer, float zSize, PolygonTriangulator2D.Triangulation triangulation = PolygonTriangulator2D.Triangulation.Advanced, Vector2D customUVOffset = null)
        {
            Texture2D texture = null;
            Sprite    sprite  = null;

            if (spriteRenderer.sprite != null)
            {
                sprite  = spriteRenderer.sprite;
                texture = sprite.texture;
            }

            if (customUVOffset == null)
            {
                customUVOffset = Vector2D.Zero();
            }

            float spriteSheetU = (float)(texture.width) / sprite.rect.width;
            float spriteSheetV = (float)(texture.height) / sprite.rect.height;

            Rect rect   = sprite.rect;
            Rect uvRect = new Rect((float)rect.x / texture.width, (float)rect.y / texture.height, (float)rect.width / texture.width, (float)rect.height / texture.height);

            Vector2 scale = new Vector2(spriteSheetU * rect.width / sprite.pixelsPerUnit, spriteSheetV * rect.height / sprite.pixelsPerUnit);

            if (spriteRenderer.flipX)
            {
                scale.x = -scale.x;
            }

            if (spriteRenderer.flipY)
            {
                scale.y = -scale.y;
            }

            float pivotX = sprite.pivot.x / sprite.rect.width - 0.5f;
            float pivotY = sprite.pivot.y / sprite.rect.height - 0.5f;

            float ix = -0.5f + pivotX / spriteSheetU;
            float iy = -0.5f + pivotY / spriteSheetV;

            Vector2 uvOffset = new Vector2(uvRect.center.x + ix, uvRect.center.y + iy);

            customUVOffset.x /= scale.x;
            customUVOffset.y /= scale.y;

            uvOffset += customUVOffset.ToVector2();

            Polygon2D polygon2D = Polygon2DList.CreateFromGameObject(gameObject)[0];

            polygon2D.CreateMesh3D(gameObject, zSize, scale, uvOffset, 0, triangulation);

            MeshRenderer meshRenderer = gameObject.GetComponent <MeshRenderer> ();

            if (meshRenderer == null)
            {
                meshRenderer = gameObject.AddComponent <MeshRenderer> ();
            }

            meshRenderer.sharedMaterial             = spriteRenderer.material;
            meshRenderer.sharedMaterial.mainTexture = texture;
            meshRenderer.sharedMaterial.color       = spriteRenderer.color;

            meshRenderer.sortingLayerName = spriteRenderer.sortingLayerName;
            meshRenderer.sortingLayerID   = spriteRenderer.sortingLayerID;
            meshRenderer.sortingOrder     = spriteRenderer.sortingOrder;
        }