FillUVOfQuad() 공개 정적인 메소드

public static FillUVOfQuad ( Vector2 uv, int index, Rect rect ) : void
uv UnityEngine.Vector2
index int
rect UnityEngine.Rect
리턴 void
예제 #1
0
        public static void FillHorizontal(OriginHorizontal origin, float amount, Rect vertRect, Rect uvRect, Vector3[] verts, Vector2[] uv)
        {
            if (origin == OriginHorizontal.Left)
            {
                vertRect.width = vertRect.width * amount;
                uvRect.width   = uvRect.width * amount;
            }
            else
            {
                vertRect.x    += vertRect.width * (1 - amount);
                vertRect.width = vertRect.width * amount;
                uvRect.x      += uvRect.width * (1 - amount);
                uvRect.width   = uvRect.width * amount;
            }

            NGraphics.FillVertsOfQuad(verts, 0, vertRect);
            NGraphics.FillUVOfQuad(uv, 0, uvRect);
        }
예제 #2
0
        public static void FillVertical(OriginVertical origin, float amount, Rect vertRect, Rect uvRect, Vector3[] verts, Vector2[] uv)
        {
            if (origin == OriginVertical.Bottom)
            {
                vertRect.y     += vertRect.height * (1 - amount);
                vertRect.height = vertRect.height * amount;
                uvRect.height   = uvRect.height * amount;
            }
            else
            {
                vertRect.height = vertRect.height * amount;
                uvRect.y       += uvRect.height * (1 - amount);
                uvRect.height   = uvRect.height * amount;
            }

            NGraphics.FillVertsOfQuad(verts, 0, vertRect);
            NGraphics.FillUVOfQuad(uv, 0, uvRect);
        }
예제 #3
0
        public static void FillRadial360(Origin360 origin, float amount, bool clockwise, Rect vertRect, Rect uvRect, Vector3[] verts, Vector2[] uv)
        {
            switch (origin)
            {
            case Origin360.Top:
                if (amount < 0.5f)
                {
                    amount          = amount / 0.5f;
                    vertRect.width /= 2;
                    uvRect.width   /= 2;
                    if (clockwise)
                    {
                        vertRect.x += vertRect.width;
                        uvRect.x   += uvRect.width;
                    }
                    FillRadial180(clockwise ? Origin180.Left : Origin180.Right, amount, clockwise, vertRect, uvRect, verts, uv);
                    verts[8] = verts[9] = verts[10] = verts[11] = verts[7];
                    uv[8]    = uv[9] = uv[10] = uv[11] = uv[7];
                }
                else
                {
                    vertRect.width /= 2;
                    uvRect.width   /= 2;
                    if (!clockwise)
                    {
                        vertRect.x += vertRect.width;
                        uvRect.x   += uvRect.width;
                    }
                    amount = (amount - 0.5f) / 0.5f;
                    FillRadial180(clockwise ? Origin180.Right : Origin180.Left, amount, clockwise, vertRect, uvRect, verts, uv);

                    if (clockwise)
                    {
                        vertRect.x += vertRect.width;
                        uvRect.x   += uvRect.width;
                    }
                    else
                    {
                        vertRect.x -= vertRect.width;
                        uvRect.x   -= uvRect.width;
                    }
                    NGraphics.FillVertsOfQuad(verts, 8, vertRect);
                    NGraphics.FillUVOfQuad(uv, 8, uvRect);
                }
                break;

            case Origin360.Bottom:
                if (amount < 0.5f)
                {
                    amount          = amount / 0.5f;
                    vertRect.width /= 2;
                    uvRect.width   /= 2;
                    if (!clockwise)
                    {
                        vertRect.x += vertRect.width;
                        uvRect.x   += uvRect.width;
                    }
                    FillRadial180(clockwise ? Origin180.Right : Origin180.Left, amount, clockwise, vertRect, uvRect, verts, uv);
                    verts[8] = verts[9] = verts[10] = verts[11] = verts[7];
                    uv[8]    = uv[9] = uv[10] = uv[11] = uv[7];
                }
                else
                {
                    vertRect.width /= 2;
                    uvRect.width   /= 2;
                    if (clockwise)
                    {
                        vertRect.x += vertRect.width;
                        uvRect.x   += uvRect.width;
                    }
                    amount = (amount - 0.5f) / 0.5f;
                    FillRadial180(clockwise ? Origin180.Left : Origin180.Right, amount, clockwise, vertRect, uvRect, verts, uv);

                    if (clockwise)
                    {
                        vertRect.x -= vertRect.width;
                        uvRect.x   -= uvRect.width;
                    }
                    else
                    {
                        vertRect.x += vertRect.width;
                        uvRect.x   += uvRect.width;
                    }
                    NGraphics.FillVertsOfQuad(verts, 8, vertRect);
                    NGraphics.FillUVOfQuad(uv, 8, uvRect);
                }
                break;

            case Origin360.Left:
                if (amount < 0.5f)
                {
                    amount = amount / 0.5f;
                    if (clockwise)
                    {
                        vertRect.height /= 2;
                        uvRect.yMin     += uvRect.height / 2;
                    }
                    else
                    {
                        vertRect.yMin += vertRect.height / 2;
                        uvRect.yMax   -= uvRect.height / 2;
                    }
                    FillRadial180(clockwise ? Origin180.Bottom : Origin180.Top, amount, clockwise, vertRect, uvRect, verts, uv);
                    verts[8] = verts[9] = verts[10] = verts[11] = verts[7];
                    uv[8]    = uv[9] = uv[10] = uv[11] = uv[7];
                }
                else
                {
                    if (clockwise)
                    {
                        vertRect.yMin += vertRect.height / 2;
                        uvRect.yMax   -= uvRect.height / 2;
                    }
                    else
                    {
                        vertRect.height /= 2;
                        uvRect.yMin     += uvRect.height / 2;
                    }
                    amount = (amount - 0.5f) / 0.5f;
                    FillRadial180(clockwise ? Origin180.Top : Origin180.Bottom, amount, clockwise, vertRect, uvRect, verts, uv);

                    if (clockwise)
                    {
                        vertRect.y -= vertRect.height;
                        uvRect.y   += uvRect.height;
                    }
                    else
                    {
                        vertRect.y += vertRect.height;
                        uvRect.y   -= uvRect.height;
                    }
                    NGraphics.FillVertsOfQuad(verts, 8, vertRect);
                    NGraphics.FillUVOfQuad(uv, 8, uvRect);
                }
                break;

            case Origin360.Right:
                if (amount < 0.5f)
                {
                    if (clockwise)
                    {
                        vertRect.yMin += vertRect.height / 2;
                        uvRect.yMax   -= uvRect.height / 2;
                    }
                    else
                    {
                        vertRect.height /= 2;
                        uvRect.yMin     += uvRect.height / 2;
                    }
                    amount = amount / 0.5f;
                    FillRadial180(clockwise ? Origin180.Top : Origin180.Bottom, amount, clockwise, vertRect, uvRect, verts, uv);
                    verts[8] = verts[9] = verts[10] = verts[11] = verts[7];
                    uv[8]    = uv[9] = uv[10] = uv[11] = uv[7];
                }
                else
                {
                    if (clockwise)
                    {
                        vertRect.height /= 2;
                        uvRect.yMin     += uvRect.height / 2;
                    }
                    else
                    {
                        vertRect.yMin += vertRect.height / 2;
                        uvRect.yMax   -= uvRect.height / 2;
                    }

                    amount = (amount - 0.5f) / 0.5f;
                    FillRadial180(clockwise ? Origin180.Bottom : Origin180.Top, amount, clockwise, vertRect, uvRect, verts, uv);

                    if (clockwise)
                    {
                        vertRect.y += vertRect.height;
                        uvRect.y   -= uvRect.height;
                    }
                    else
                    {
                        vertRect.y -= vertRect.height;
                        uvRect.y   += uvRect.height;
                    }
                    NGraphics.FillVertsOfQuad(verts, 8, vertRect);
                    NGraphics.FillUVOfQuad(uv, 8, uvRect);
                }
                break;
            }
        }
예제 #4
0
        public static void FillRadial180(Origin180 origin, float amount, bool clockwise, Rect vertRect, Rect uvRect, Vector3[] verts, Vector2[] uv)
        {
            switch (origin)
            {
            case Origin180.Top:
                if (amount <= 0.5f)
                {
                    vertRect.width /= 2;
                    uvRect.width   /= 2;
                    if (clockwise)
                    {
                        vertRect.x += vertRect.width;
                        uvRect.x   += uvRect.width;
                    }
                    amount = amount / 0.5f;
                    FillRadial90(clockwise ? Origin90.TopLeft : Origin90.TopRight, amount, clockwise, vertRect, uvRect, verts, uv);
                    verts[4] = verts[5] = verts[6] = verts[7] = verts[0];
                    uv[4]    = uv[5] = uv[6] = uv[7] = uv[0];
                }
                else
                {
                    vertRect.width /= 2;
                    uvRect.width   /= 2;
                    if (!clockwise)
                    {
                        vertRect.x += vertRect.width;
                        uvRect.x   += uvRect.width;
                    }
                    amount = (amount - 0.5f) / 0.5f;
                    FillRadial90(clockwise ? Origin90.TopRight : Origin90.TopLeft, amount, clockwise, vertRect, uvRect, verts, uv);

                    if (clockwise)
                    {
                        vertRect.x += vertRect.width;
                        uvRect.x   += uvRect.width;
                    }
                    else
                    {
                        vertRect.x -= vertRect.width;
                        uvRect.x   -= uvRect.width;
                    }
                    NGraphics.FillVertsOfQuad(verts, 4, vertRect);
                    NGraphics.FillUVOfQuad(uv, 4, uvRect);
                }
                break;

            case Origin180.Bottom:
                if (amount <= 0.5f)
                {
                    vertRect.width /= 2;
                    uvRect.width   /= 2;
                    if (!clockwise)
                    {
                        vertRect.x += vertRect.width;
                        uvRect.x   += uvRect.width;
                    }
                    amount = amount / 0.5f;
                    FillRadial90(clockwise ? Origin90.BottomRight : Origin90.BottomLeft, amount, clockwise, vertRect, uvRect, verts, uv);
                    verts[4] = verts[5] = verts[6] = verts[7] = verts[0];
                    uv[4]    = uv[5] = uv[6] = uv[7] = uv[0];
                }
                else
                {
                    vertRect.width /= 2;
                    uvRect.width   /= 2;
                    if (clockwise)
                    {
                        vertRect.x += vertRect.width;
                        uvRect.x   += uvRect.width;
                    }
                    amount = (amount - 0.5f) / 0.5f;
                    FillRadial90(clockwise ? Origin90.BottomLeft : Origin90.BottomRight, amount, clockwise, vertRect, uvRect, verts, uv);

                    if (clockwise)
                    {
                        vertRect.x -= vertRect.width;
                        uvRect.x   -= uvRect.width;
                    }
                    else
                    {
                        vertRect.x += vertRect.width;
                        uvRect.x   += uvRect.width;
                    }
                    NGraphics.FillVertsOfQuad(verts, 4, vertRect);
                    NGraphics.FillUVOfQuad(uv, 4, uvRect);
                }
                break;

            case Origin180.Left:
                if (amount <= 0.5f)
                {
                    if (clockwise)
                    {
                        vertRect.height /= 2;
                        uvRect.yMin     += uvRect.height / 2;
                    }
                    else
                    {
                        vertRect.yMin += vertRect.height / 2;
                        uvRect.yMax   -= uvRect.height / 2;
                    }
                    amount = amount / 0.5f;
                    FillRadial90(clockwise ? Origin90.BottomLeft : Origin90.TopLeft, amount, clockwise, vertRect, uvRect, verts, uv);
                    verts[4] = verts[5] = verts[6] = verts[7] = verts[0];
                    uv[4]    = uv[5] = uv[6] = uv[7] = uv[0];
                }
                else
                {
                    if (clockwise)
                    {
                        vertRect.yMin += vertRect.height / 2;
                        uvRect.yMax   -= uvRect.height / 2;
                    }
                    else
                    {
                        vertRect.height /= 2;
                        uvRect.yMin     += uvRect.height / 2;
                    }
                    amount = (amount - 0.5f) / 0.5f;
                    FillRadial90(clockwise ? Origin90.TopLeft : Origin90.BottomLeft, amount, clockwise, vertRect, uvRect, verts, uv);

                    if (clockwise)
                    {
                        vertRect.y -= vertRect.height;
                        uvRect.y   += uvRect.height;
                    }
                    else
                    {
                        vertRect.y += vertRect.height;
                        uvRect.y   -= uvRect.height;
                    }
                    NGraphics.FillVertsOfQuad(verts, 4, vertRect);
                    NGraphics.FillUVOfQuad(uv, 4, uvRect);
                }
                break;

            case Origin180.Right:
                if (amount <= 0.5f)
                {
                    if (clockwise)
                    {
                        vertRect.yMin += vertRect.height / 2;
                        uvRect.yMax   -= uvRect.height / 2;
                    }
                    else
                    {
                        vertRect.height /= 2;
                        uvRect.yMin     += uvRect.height / 2;
                    }
                    amount = amount / 0.5f;
                    FillRadial90(clockwise ? Origin90.TopRight : Origin90.BottomRight, amount, clockwise, vertRect, uvRect, verts, uv);
                    verts[4] = verts[5] = verts[6] = verts[7] = verts[0];
                    uv[4]    = uv[5] = uv[6] = uv[7] = uv[0];
                }
                else
                {
                    if (clockwise)
                    {
                        vertRect.height /= 2;
                        uvRect.yMin     += uvRect.height / 2;
                    }
                    else
                    {
                        vertRect.yMin += vertRect.height / 2;
                        uvRect.yMax   -= uvRect.height / 2;
                    }
                    amount = (amount - 0.5f) / 0.5f;
                    FillRadial90(clockwise ? Origin90.BottomRight : Origin90.TopRight, amount, clockwise, vertRect, uvRect, verts, uv);

                    if (clockwise)
                    {
                        vertRect.y += vertRect.height;
                        uvRect.y   -= uvRect.height;
                    }
                    else
                    {
                        vertRect.y -= vertRect.height;
                        uvRect.y   += uvRect.height;
                    }
                    NGraphics.FillVertsOfQuad(verts, 4, vertRect);
                    NGraphics.FillUVOfQuad(uv, 4, uvRect);
                }
                break;
            }
        }
예제 #5
0
        public static void FillRadial90(Origin90 origin, float amount, bool clockwise, Rect vertRect, Rect uvRect, Vector3[] verts, Vector2[] uv)
        {
            NGraphics.FillVertsOfQuad(verts, 0, vertRect);
            NGraphics.FillUVOfQuad(uv, 0, uvRect);
            if (amount < 0.001f)
            {
                verts[0] = verts[1] = verts[2] = verts[3];
                uv[0]    = uv[1] = uv[2] = uv[3];
                return;
            }
            if (amount > 0.999f)
            {
                return;
            }

            switch (origin)
            {
            case Origin90.BottomLeft:
            {
                if (clockwise)
                {
                    float v = Mathf.Tan(Mathf.PI / 2 * (1 - amount));
                    float h = vertRect.width * v;
                    if (h > vertRect.height)
                    {
                        float ratio = (h - vertRect.height) / h;
                        verts[2].x -= vertRect.width * ratio;
                        verts[3]    = verts[2];

                        uv[2].x -= uvRect.width * ratio;
                        uv[3]    = uv[2];
                    }
                    else
                    {
                        float ratio = h / vertRect.height;
                        verts[3].y += h;
                        uv[3].y    += uvRect.height * ratio;
                    }
                }
                else
                {
                    float v = Mathf.Tan(Mathf.PI / 2 * amount);
                    float h = vertRect.width * v;
                    if (h > vertRect.height)
                    {
                        float ratio = (h - vertRect.height) / h;
                        verts[1].x += vertRect.width * (1 - ratio);
                        uv[1].x    += uvRect.width * (1 - ratio);
                    }
                    else
                    {
                        float ratio = h / vertRect.height;
                        verts[2].y -= vertRect.height * (1 - ratio);
                        verts[1]    = verts[2];

                        uv[2].y -= uvRect.height * (1 - ratio);
                        uv[1]    = uv[2];
                    }
                }
            }
            break;

            case Origin90.BottomRight:
            {
                if (clockwise)
                {
                    float v = Mathf.Tan(Mathf.PI / 2 * amount);
                    float h = vertRect.width * v;
                    if (h > vertRect.height)
                    {
                        float ratio = (h - vertRect.height) / h;
                        verts[2].x -= vertRect.width * (1 - ratio);
                        uv[2].x    -= uvRect.width * (1 - ratio);
                    }
                    else
                    {
                        float ratio = h / vertRect.height;
                        verts[1].y -= vertRect.height * (1 - ratio);
                        verts[2]    = verts[3];

                        uv[1].y -= uvRect.height * (1 - ratio);
                        uv[2]    = uv[3];
                    }
                }
                else
                {
                    float v = Mathf.Tan(Mathf.PI / 2 * (1 - amount));
                    float h = vertRect.width * v;
                    if (h > vertRect.height)
                    {
                        float ratio = (h - vertRect.height) / h;
                        verts[1].x += vertRect.width * ratio;
                        verts[0]    = verts[1];

                        uv[1].x += uvRect.width * ratio;
                        uv[0]    = uv[1];
                    }
                    else
                    {
                        float ratio = h / vertRect.height;
                        verts[0].y += h;
                        uv[0].y    += uvRect.height * ratio;
                    }
                }
            }
            break;

            case Origin90.TopLeft:
            {
                if (clockwise)
                {
                    float v = Mathf.Tan(Mathf.PI / 2 * amount);
                    float h = vertRect.width * v;
                    if (h > vertRect.height)
                    {
                        float ratio = (h - vertRect.height) / h;
                        verts[0].x += vertRect.width * (1 - ratio);
                        uv[0].x    += uvRect.width * (1 - ratio);
                    }
                    else
                    {
                        float ratio = h / vertRect.height;
                        verts[3].y += vertRect.height * (1 - ratio);
                        verts[0]    = verts[3];

                        uv[3].y += uvRect.height * (1 - ratio);
                        uv[0]    = uv[3];
                    }
                }
                else
                {
                    float v = Mathf.Tan(Mathf.PI / 2 * (1 - amount));
                    float h = vertRect.width * v;
                    if (h > vertRect.height)
                    {
                        float ratio = (h - vertRect.height) / h;
                        verts[3].x -= vertRect.width * ratio;
                        verts[2]    = verts[3];
                        uv[3].x    -= uvRect.width * ratio;
                        uv[2]       = uv[3];
                    }
                    else
                    {
                        float ratio = h / vertRect.height;
                        verts[2].y -= h;
                        uv[2].y    -= uvRect.height * ratio;
                    }
                }
            }
            break;

            case Origin90.TopRight:
            {
                if (clockwise)
                {
                    float v = Mathf.Tan(Mathf.PI / 2 * (1 - amount));
                    float h = vertRect.width * v;
                    if (h > vertRect.height)
                    {
                        float ratio = (h - vertRect.height) / h;
                        verts[0].x += vertRect.width * ratio;
                        verts[1]    = verts[2];
                        uv[0].x    += uvRect.width * ratio;
                        uv[1]       = uv[2];
                    }
                    else
                    {
                        float ratio = h / vertRect.height;
                        verts[1].y -= vertRect.height * ratio;
                        uv[1].y    -= uvRect.height * ratio;
                    }
                }
                else
                {
                    float v = Mathf.Tan(Mathf.PI / 2 * amount);
                    float h = vertRect.width * v;
                    if (h > vertRect.height)
                    {
                        float ratio = (h - vertRect.height) / h;
                        verts[3].x -= vertRect.width * (1 - ratio);
                        uv[3].x    -= uvRect.width * (1 - ratio);
                    }
                    else
                    {
                        float ratio = h / vertRect.height;
                        verts[0].y += vertRect.height * (1 - ratio);
                        verts[3]    = verts[0];
                        uv[0].y    += uvRect.height * (1 - ratio);
                        uv[3]       = uv[0];
                    }
                }
            }
            break;
            }
        }
예제 #6
0
        /// <summary>
        /// 截取当前图片的一部分输出到另一个Mesh。不支持图片的填充模式、九宫格的平铺模式。
        /// </summary>
        /// <param name="mesh">目标Mesh</param>
        /// <param name="localRect">制定图片的区域</param>
        public void PrintTo(Mesh mesh, Rect localRect)
        {
            if (_requireUpdateMesh)
            {
                Rebuild();
            }

            Rect uvRect = _texture.uvRect;

            if (_flip != FlipType.None)
            {
                ToolSet.FlipRect(ref uvRect, _flip);
            }

            Vector3[] verts;
            Vector2[] uv;
            Color32[] colors;
            int[]     triangles;
            int       vertCount = 0;

            if (!_scaleByTile || _scale9Grid == null || (_texture.width == _contentRect.width && _texture.height == _contentRect.height))
            {
                verts = new Vector3[graphics.vertices.Length];
                uv    = new Vector2[graphics.uv.Length];

                Rect bound = ToolSet.Intersection(ref _contentRect, ref localRect);

                float u0 = bound.xMin / _contentRect.width;
                float u1 = bound.xMax / _contentRect.width;
                float v0 = (_contentRect.height - bound.yMax) / _contentRect.height;
                float v1 = (_contentRect.height - bound.yMin) / _contentRect.height;
                u0 = Mathf.Lerp(uvRect.xMin, uvRect.xMax, u0);
                u1 = Mathf.Lerp(uvRect.xMin, uvRect.xMax, u1);
                v0 = Mathf.Lerp(uvRect.yMin, uvRect.yMax, v0);
                v1 = Mathf.Lerp(uvRect.yMin, uvRect.yMax, v1);
                NGraphics.FillUVOfQuad(uv, 0, Rect.MinMaxRect(u0, v0, u1, v1));

                bound.x = 0;
                bound.y = 0;
                NGraphics.FillVertsOfQuad(verts, 0, bound);
                vertCount += 4;
            }
            else if (_scaleByTile)
            {
                verts = new Vector3[graphics.vertices.Length];
                uv    = new Vector2[graphics.uv.Length];

                int   hc         = Mathf.CeilToInt(_contentRect.width / _texture.width);
                int   vc         = Mathf.CeilToInt(_contentRect.height / _texture.height);
                float tailWidth  = _contentRect.width - (hc - 1) * _texture.width;
                float tailHeight = _contentRect.height - (vc - 1) * _texture.height;

                Vector2 offset = Vector2.zero;
                for (int i = 0; i < hc; i++)
                {
                    for (int j = 0; j < vc; j++)
                    {
                        Rect rect = new Rect(i * _texture.width, j * _texture.height,
                                             i == (hc - 1) ? tailWidth : _texture.width, j == (vc - 1) ? tailHeight : _texture.height);
                        Rect uvTmp = uvRect;
                        if (i == hc - 1)
                        {
                            uvTmp.xMax = Mathf.Lerp(uvRect.xMin, uvRect.xMax, tailWidth / _texture.width);
                        }
                        if (j == vc - 1)
                        {
                            uvTmp.yMin = Mathf.Lerp(uvRect.yMin, uvRect.yMax, 1 - tailHeight / _texture.height);
                        }

                        Rect bound = ToolSet.Intersection(ref rect, ref localRect);
                        if (bound.xMax - bound.xMin >= 0 && bound.yMax - bound.yMin > 0)
                        {
                            float u0 = (bound.xMin - rect.x) / rect.width;
                            float u1 = (bound.xMax - rect.x) / rect.width;
                            float v0 = (rect.y + rect.height - bound.yMax) / rect.height;
                            float v1 = (rect.y + rect.height - bound.yMin) / rect.height;
                            u0 = Mathf.Lerp(uvTmp.xMin, uvTmp.xMax, u0);
                            u1 = Mathf.Lerp(uvTmp.xMin, uvTmp.xMax, u1);
                            v0 = Mathf.Lerp(uvTmp.yMin, uvTmp.yMax, v0);
                            v1 = Mathf.Lerp(uvTmp.yMin, uvTmp.yMax, v1);
                            NGraphics.FillUVOfQuad(uv, vertCount, Rect.MinMaxRect(u0, v0, u1, v1));

                            if (i == 0 && j == 0)
                            {
                                offset = new Vector2(bound.x, bound.y);
                            }
                            bound.x -= offset.x;
                            bound.y -= offset.y;

                            NGraphics.FillVertsOfQuad(verts, vertCount, bound);

                            vertCount += 4;
                        }
                    }
                }
            }
            else
            {
                Rect gridRect = (Rect)_scale9Grid;

                if (_flip != FlipType.None)
                {
                    ToolSet.FlipInnerRect(_texture.width, _texture.height, ref gridRect, _flip);
                }

                GenerateGrids(gridRect, uvRect);

                verts = new Vector3[36];
                uv    = new Vector2[36];
                Vector2 offset = new Vector2();

                Rect  drawRect;
                Rect  texRect;
                int   row, col;
                float u0, u1, v0, v1;

                for (int pi = 0; pi < 9; pi++)
                {
                    col      = pi % 3;
                    row      = pi / 3;
                    drawRect = Rect.MinMaxRect(gridX[col], gridY[row], gridX[col + 1], gridY[row + 1]);
                    texRect  = Rect.MinMaxRect(gridTexX[col], gridTexY[row + 1], gridTexX[col + 1], gridTexY[row]);
                    Rect bound = ToolSet.Intersection(ref drawRect, ref localRect);
                    if (bound.xMax - bound.xMin >= 0 && bound.yMax - bound.yMin > 0)
                    {
                        u0 = (bound.xMin - drawRect.x) / drawRect.width;
                        u1 = (bound.xMax - drawRect.x) / drawRect.width;
                        v0 = (drawRect.yMax - bound.yMax) / drawRect.height;
                        v1 = (drawRect.yMax - bound.yMin) / drawRect.height;
                        u0 = Mathf.Lerp(texRect.xMin, texRect.xMax, u0);
                        u1 = Mathf.Lerp(texRect.xMin, texRect.xMax, u1);
                        v0 = Mathf.Lerp(texRect.yMin, texRect.yMax, v0);
                        v1 = Mathf.Lerp(texRect.yMin, texRect.yMax, v1);
                        NGraphics.FillUVOfQuad(uv, vertCount, Rect.MinMaxRect(u0, v0, u1, v1));

                        if (vertCount == 0)
                        {
                            offset = new Vector2(bound.x, bound.y);
                        }
                        bound.x -= offset.x;
                        bound.y -= offset.y;
                        NGraphics.FillVertsOfQuad(verts, vertCount, bound);

                        vertCount += 4;
                    }
                }
            }

            if (vertCount != verts.Length)
            {
                Array.Resize(ref verts, vertCount);
                Array.Resize(ref uv, vertCount);
            }
            int triangleCount = (vertCount >> 1) * 3;

            triangles = new int[triangleCount];
            int k = 0;

            for (int i = 0; i < vertCount; i += 4)
            {
                triangles[k++] = i;
                triangles[k++] = i + 1;
                triangles[k++] = i + 2;

                triangles[k++] = i + 2;
                triangles[k++] = i + 3;
                triangles[k++] = i;
            }

            colors = new Color32[vertCount];
            for (int i = 0; i < vertCount; i++)
            {
                Color col = _color;
                col.a     = this.alpha;
                colors[i] = col;
            }

            if (_texture.rotated)
            {
                NGraphics.RotateUV(uv, ref uvRect);
            }

            mesh.Clear();
            mesh.vertices  = verts;
            mesh.uv        = uv;
            mesh.triangles = triangles;
            mesh.colors32  = colors;
        }
예제 #7
0
파일: Image.cs 프로젝트: zz0733/Fishing
        public void PrintTo(Mesh mesh, Rect localRect)
        {
            if (_requireUpdateMesh)
            {
                Rebuild();
            }

            Rect uvRect = _texture.uvRect;

            if (_flip != FlipType.None)
            {
                ToolSet.FlipRect(ref uvRect, _flip);
            }

            Vector3[] verts;
            Vector2[] uv;
            Color32[] colors;
            int[]     triangles;
            int       vertCount = 0;

            if (!_scaleByTile || _scale9Grid == null || (_texture.width == _contentRect.width && _texture.height == _contentRect.height))
            {
                verts = new Vector3[graphics.vertices.Length];
                uv    = new Vector2[graphics.uv.Length];

                Rect bound = ToolSet.Intersection(ref _contentRect, ref localRect);

                float u0 = bound.xMin / _contentRect.width;
                float u1 = bound.xMax / _contentRect.width;
                float v0 = (_contentRect.height - bound.yMax) / _contentRect.height;
                float v1 = (_contentRect.height - bound.yMin) / _contentRect.height;
                u0 = Mathf.Lerp(uvRect.xMin, uvRect.xMax, u0);
                u1 = Mathf.Lerp(uvRect.xMin, uvRect.xMax, u1);
                v0 = Mathf.Lerp(uvRect.yMin, uvRect.yMax, v0);
                v1 = Mathf.Lerp(uvRect.yMin, uvRect.yMax, v1);
                NGraphics.FillUVOfQuad(uv, 0, Rect.MinMaxRect(u0, v0, u1, v1));

                bound.x = 0;
                bound.y = 0;
                NGraphics.FillVertsOfQuad(verts, 0, bound);
                vertCount += 4;
            }
            else if (_scaleByTile)
            {
                verts = new Vector3[graphics.vertices.Length];
                uv    = new Vector2[graphics.uv.Length];

                int   hc         = Mathf.CeilToInt(_contentRect.width / _texture.width);
                int   vc         = Mathf.CeilToInt(_contentRect.height / _texture.height);
                float tailWidth  = _contentRect.width - (hc - 1) * _texture.width;
                float tailHeight = _contentRect.height - (vc - 1) * _texture.height;

                Vector2 offset = Vector2.zero;
                for (int i = 0; i < hc; i++)
                {
                    for (int j = 0; j < vc; j++)
                    {
                        Rect rect = new Rect(i * _texture.width, j * _texture.height,
                                             i == (hc - 1) ? tailWidth : _texture.width, j == (vc - 1) ? tailHeight : _texture.height);
                        Rect uvTmp = uvRect;
                        if (i == hc - 1)
                        {
                            uvTmp.xMax = Mathf.Lerp(uvRect.xMin, uvRect.xMax, tailWidth / _texture.width);
                        }
                        if (j == vc - 1)
                        {
                            uvTmp.yMin = Mathf.Lerp(uvRect.yMin, uvRect.yMax, 1 - tailHeight / _texture.height);
                        }

                        Rect bound = ToolSet.Intersection(ref rect, ref localRect);
                        if (bound.xMax - bound.xMin >= 0 && bound.yMax - bound.yMin > 0)
                        {
                            float u0 = (bound.xMin - rect.x) / rect.width;
                            float u1 = (bound.xMax - rect.x) / rect.width;
                            float v0 = (rect.y + rect.height - bound.yMax) / rect.height;
                            float v1 = (rect.y + rect.height - bound.yMin) / rect.height;
                            u0 = Mathf.Lerp(uvTmp.xMin, uvTmp.xMax, u0);
                            u1 = Mathf.Lerp(uvTmp.xMin, uvTmp.xMax, u1);
                            v0 = Mathf.Lerp(uvTmp.yMin, uvTmp.yMax, v0);
                            v1 = Mathf.Lerp(uvTmp.yMin, uvTmp.yMax, v1);
                            NGraphics.FillUVOfQuad(uv, vertCount, Rect.MinMaxRect(u0, v0, u1, v1));

                            if (i == 0 && j == 0)
                            {
                                offset = new Vector2(bound.x, bound.y);
                            }
                            bound.x -= offset.x;
                            bound.y -= offset.y;

                            NGraphics.FillVertsOfQuad(verts, vertCount, bound);

                            vertCount += 4;
                        }
                    }
                }
            }
            else
            {
                verts = new Vector3[36];
                uv    = new Vector2[36];

                float[] rows;
                float[] cols;
                float[] dRows;
                float[] dCols;
                Rect    gridRect = (Rect)_scale9Grid;

                rows = new float[] { 0, gridRect.yMin, gridRect.yMax, _texture.height };
                cols = new float[] { 0, gridRect.xMin, gridRect.xMax, _texture.width };

                if (_contentRect.height >= (_texture.height - gridRect.height))
                {
                    dRows = new float[] { 0, gridRect.yMin, _contentRect.height - (_texture.height - gridRect.yMax), _contentRect.height }
                }
                ;
                else
                {
                    float tmp = gridRect.yMin / (_texture.height - gridRect.yMax);
                    tmp   = _contentRect.height * tmp / (1 + tmp);
                    dRows = new float[] { 0, tmp, tmp, _contentRect.height };
                }

                if (_contentRect.width >= (_texture.width - gridRect.width))
                {
                    dCols = new float[] { 0, gridRect.xMin, _contentRect.width - (_texture.width - gridRect.xMax), _contentRect.width }
                }
                ;
                else
                {
                    float tmp = gridRect.xMin / (_texture.width - gridRect.xMax);
                    tmp   = _contentRect.width * tmp / (1 + tmp);
                    dCols = new float[] { 0, tmp, tmp, _contentRect.width };
                }

                Vector2 offset = new Vector2();
                for (int cy = 0; cy < 3; cy++)
                {
                    for (int cx = 0; cx < 3; cx++)
                    {
                        Rect rect  = Rect.MinMaxRect(dCols[cx], dRows[cy], dCols[cx + 1], dRows[cy + 1]);
                        Rect bound = ToolSet.Intersection(ref rect, ref localRect);
                        if (bound.xMax - bound.xMin >= 0 && bound.yMax - bound.yMin > 0)
                        {
                            Rect texBound = Rect.MinMaxRect(uvRect.x + cols[cx] / _texture.width * uvRect.width,
                                                            uvRect.y + (1 - rows[cy + 1] / _texture.height) * uvRect.height,
                                                            uvRect.x + cols[cx + 1] / _texture.width * uvRect.width,
                                                            uvRect.y + (1 - rows[cy] / _texture.height) * uvRect.height);

                            float u0 = (bound.xMin - rect.x) / rect.width;
                            float u1 = (bound.xMax - rect.x) / rect.width;
                            float v0 = (rect.y + rect.height - bound.yMax) / rect.height;
                            float v1 = (rect.y + rect.height - bound.yMin) / rect.height;
                            u0 = Mathf.Lerp(texBound.xMin, texBound.xMax, u0);
                            u1 = Mathf.Lerp(texBound.xMin, texBound.xMax, u1);
                            v0 = Mathf.Lerp(texBound.yMin, texBound.yMax, v0);
                            v1 = Mathf.Lerp(texBound.yMin, texBound.yMax, v1);
                            NGraphics.FillUVOfQuad(uv, vertCount, Rect.MinMaxRect(u0, v0, u1, v1));

                            if (vertCount == 0)
                            {
                                offset = new Vector2(bound.x, bound.y);
                            }
                            bound.x -= offset.x;
                            bound.y -= offset.y;
                            NGraphics.FillVertsOfQuad(verts, vertCount, bound);

                            vertCount += 4;
                        }
                    }
                }
            }

            if (vertCount != verts.Length)
            {
                Array.Resize(ref verts, vertCount);
                Array.Resize(ref uv, vertCount);
            }
            int triangleCount = (vertCount >> 1) * 3;

            triangles = new int[triangleCount];
            int k = 0;

            for (int i = 0; i < vertCount; i += 4)
            {
                triangles[k++] = i;
                triangles[k++] = i + 1;
                triangles[k++] = i + 2;

                triangles[k++] = i + 2;
                triangles[k++] = i + 3;
                triangles[k++] = i;
            }

            colors = new Color32[vertCount];
            for (int i = 0; i < vertCount; i++)
            {
                Color col = _color;
                col.a     = this.alpha;
                colors[i] = col;
            }

            mesh.Clear();
            mesh.vertices  = verts;
            mesh.uv        = uv;
            mesh.triangles = triangles;
            mesh.colors32  = colors;
        }