示例#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
        List <Polygon2D> polygons = Polygon2DListCollider2D.CreateFromGameObject(gameObject);

        if (polygons != null && polygons.Count > 0)
        {
            Polygon2DHelper.CreateMesh(polygons, gameObject, materialScale, materialOffset, triangulation);

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

                meshRenderer.sortingLayerName = sortingLayerName;
                meshRenderer.sortingOrder     = sortingOrder;
            }
        }
    }
示例#2
0
    // Sprite To Mesh

    public static void SpriteToMesh(GameObject gameObject, VirtualSpriteRenderer spriteRenderer, PolygonTriangulator2D.Triangulation triangulation = PolygonTriangulator2D.Triangulation.Advanced)
    {
        Texture2D texture = null;
        Sprite    sprite  = null;

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

        Rect spriteRect = sprite.textureRect;

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


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

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

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

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

        float pivotX = sprite.pivot.x / spriteRect.width - 0.5f;
        float pivotY = sprite.pivot.y / spriteRect.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);

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

        polygon2D.CreateMesh(gameObject, scale, uvOffset, 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;
    }