void Start() { // Generate Mesh from collider Polygon2D polygon = Polygon2DList.CreateFromGameObject(gameObject)[0]; if (polygon != null) { polygon.CreateMesh3D(gameObject, size, Vector2.zero, Vector2.zero, triangulation); // Setting Mesh material if (material != null) { MeshRenderer meshRenderer = GetComponent <MeshRenderer> (); meshRenderer.material = 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; }