Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="clipId"></param>
        /// <param name="clipRect"></param>
        /// <param name="softness"></param>
        public void EnterClipping(uint clipId, Rect clipRect)
        {
            _clipStack.Push(clipInfo);

            if (clipped)
            {
                clipRect = ToolSet.Intersection(ref clipInfo.rect, ref clipRect);
            }

            clipped         = true;
            clipInfo.rect   = clipRect;
            clipInfo.clipId = clipId;
            SetScissor(clipInfo.rect);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="clipRect"></param>
        public void EnterClipping(RectangleF clipRect)
        {
            Flush();

            _clipStack.Push(_clipRect);

            if (_clipped)
            {
                clipRect = ToolSet.Intersection(ref _clipRect, ref clipRect);
            }

            _device.RasterizerState = _scissorTestEnabled;

            _clipped  = true;
            _clipRect = clipRect;
            SetScissor();
        }
Ejemplo n.º 3
0
    Rect GetImageUVRect(Image image, Rect localRect, Vector2[] uv)
    {
        Rect imageRect = new Rect(0, 0, image.size.x, image.size.y);
        Rect bound     = ToolSet.Intersection(ref imageRect, ref localRect);
        Rect uvRect    = image.texture.uvRect;

        if (image.scale9Grid != null)
        {
            Rect  gridRect = (Rect)image.scale9Grid;
            float sourceW  = image.texture.width;
            float sourceH  = image.texture.height;
            uvRect = Rect.MinMaxRect(Mathf.Lerp(uvRect.xMin, uvRect.xMax, gridRect.xMin / sourceW),
                                     Mathf.Lerp(uvRect.yMin, uvRect.yMax, (sourceH - gridRect.yMax) / sourceH),
                                     Mathf.Lerp(uvRect.xMin, uvRect.xMax, gridRect.xMax / sourceW),
                                     Mathf.Lerp(uvRect.yMin, uvRect.yMax, (sourceH - gridRect.yMin) / sourceH));

            float vw = imageRect.width - (sourceW - gridRect.width);
            float vh = imageRect.height - (sourceH - gridRect.height);
            uvRect = Rect.MinMaxRect(Mathf.Lerp(uvRect.xMin, uvRect.xMax, (bound.x - gridRect.x) / vw),
                                     Mathf.Lerp(uvRect.yMin, uvRect.yMax, (imageRect.height - bound.yMax - (sourceH - gridRect.yMax)) / vh),
                                     Mathf.Lerp(uvRect.xMin, uvRect.xMax, (bound.xMax - gridRect.x) / vw),
                                     Mathf.Lerp(uvRect.yMin, uvRect.yMax, (imageRect.height - bound.yMin - gridRect.y) / vh));
        }
        else
        {
            uvRect = Rect.MinMaxRect(Mathf.Lerp(uvRect.xMin, uvRect.xMax, bound.xMin / imageRect.width),
                                     Mathf.Lerp(uvRect.yMin, uvRect.yMax, (imageRect.height - bound.yMax) / imageRect.height),
                                     Mathf.Lerp(uvRect.xMin, uvRect.xMax, bound.xMax / imageRect.width),
                                     Mathf.Lerp(uvRect.yMin, uvRect.yMax, (imageRect.height - bound.yMin) / imageRect.height));
        }

        uv[0] = uvRect.position;
        uv[1] = new Vector2(uvRect.xMin, uvRect.yMax);
        uv[2] = new Vector2(uvRect.xMax, uvRect.yMax);
        uv[3] = new Vector2(uvRect.xMax, uvRect.yMin);

        if (image.texture.rotated)
        {
            ToolSet.RotateUV(uv, ref image.texture.uvRect);
        }

        return(uvRect);
    }
Ejemplo n.º 4
0
        public override Rect GetBounds(DisplayObject targetSpace)
        {
            int count = _children.Count;

            Rect rect;

            if (count == 0)
            {
                Vector2 v = TransformPoint(new Vector2(0, 0), targetSpace);
                rect = Rect.MinMaxRect(v.x, v.y, 0, 0);
            }
            else if (count == 1)
            {
                rect = _children[0].GetBounds(targetSpace);
            }
            else
            {
                float minX = float.MaxValue, maxX = float.MinValue;
                float minY = float.MaxValue, maxY = float.MinValue;

                for (int i = 0; i < count; ++i)
                {
                    rect = _children[i].GetBounds(targetSpace);
                    minX = minX < rect.xMin ? minX : rect.xMin;
                    maxX = maxX > rect.xMax ? maxX : rect.xMax;
                    minY = minY < rect.yMin ? minY : rect.yMin;
                    maxY = maxY > rect.yMax ? maxY : rect.yMax;
                }

                rect = Rect.MinMaxRect(minX, minY, maxX, maxY);
            }

            if (_clipRect != null)
            {
                Rect clipRect = TransformRect((Rect)_clipRect, targetSpace);
                rect = ToolSet.Intersection(ref rect, ref clipRect);
            }

            return(rect);
        }
Ejemplo n.º 5
0
        public void EnterClipping(Container container)
        {
            _clipStack.Push(clipInfo);

            Rect rect = container.GetWorldClipRect();

            if (clipped)
            {
                rect = ToolSet.Intersection(ref clipInfo.rect, ref rect);
            }
            clipped = true;

            clipInfo.rect = rect;
            rect.x        = rect.x + rect.width / 2f;
            rect.y        = rect.y + rect.height / 2f;
            rect.width   /= 2f;
            rect.height  /= 2f;
            if (rect.width == 0 || rect.height == 0)
            {
                clipInfo.offset = new Vector2(-2, -2);
                clipInfo.scale  = new Vector2(0, 0);
            }
            else
            {
                clipInfo.offset = new Vector2(-rect.x / rect.width, -rect.y / rect.height);
                clipInfo.scale  = new Vector2(1.0f / rect.width, 1.0f / rect.height);
            }

            clipInfo.clipId = container.internalIndex;

            clipInfo.soft = container.clipSoftness != null;
            if (clipInfo.soft)
            {
                clipInfo.softness = (Vector4)container.clipSoftness;
                float vx = clipInfo.rect.width * Stage.inst.stageHeight * 0.25f;
                float vy = clipInfo.rect.height * Stage.inst.stageHeight * 0.25f;

                if (clipInfo.softness.x > 0)
                {
                    clipInfo.softness.x = vx / clipInfo.softness.x;
                }
                else
                {
                    clipInfo.softness.x = 10000f;
                }

                if (clipInfo.softness.y > 0)
                {
                    clipInfo.softness.y = vy / clipInfo.softness.y;
                }
                else
                {
                    clipInfo.softness.y = 10000f;
                }

                if (clipInfo.softness.z > 0)
                {
                    clipInfo.softness.z = vx / clipInfo.softness.z;
                }
                else
                {
                    clipInfo.softness.z = 10000f;
                }

                if (clipInfo.softness.w > 0)
                {
                    clipInfo.softness.w = vy / clipInfo.softness.w;
                }
                else
                {
                    clipInfo.softness.w = 10000f;
                }
            }
        }
Ejemplo n.º 6
0
        public void EnterClipping(uint clipId, Rect?clipRect, Vector4?softness)
        {
            _clipStack.Push(clipInfo);

            if (clipRect == null)
            {
                if (stencilReferenceValue == 0)
                {
                    stencilReferenceValue = 1;
                }
                else
                {
                    stencilReferenceValue = stencilReferenceValue << 1;
                }
                clipInfo.clipId  = clipId;
                clipInfo.stencil = true;
                clipped          = true;
            }
            else
            {
                rectMaskDepth++;
                clipInfo.stencil = false;

                Rect rect = (Rect)clipRect;
                if (clipped)
                {
                    rect = ToolSet.Intersection(ref clipInfo.rect, ref rect);
                }
                clipped = true;

                /* clipPos = xy * clipBox.zw + clipBox.xy
                 * 利用这个公式,使clipPos变为当前顶点距离剪切区域中心的距离值,剪切区域的大小为2x2
                 * 那么abs(clipPos)>1的都是在剪切区域外
                 */

                clipInfo.rect = rect;
                rect.x        = rect.x + rect.width / 2f;
                rect.y        = rect.y + rect.height / 2f;
                rect.width   /= 2f;
                rect.height  /= 2f;
                if (rect.width == 0 || rect.height == 0)
                {
                    clipInfo.clipBox = new Vector4(-2, -2, 0, 0);
                }
                else
                {
                    clipInfo.clipBox = new Vector4(-rect.x / rect.width, -rect.y / rect.height,
                                                   1.0f / rect.width, 1.0f / rect.height);
                }
                clipInfo.clipId = clipId;
                clipInfo.soft   = softness != null;
                if (clipInfo.soft)
                {
                    clipInfo.softness = (Vector4)softness;
                    float vx = clipInfo.rect.width * Screen.height * 0.25f;
                    float vy = clipInfo.rect.height * Screen.height * 0.25f;

                    if (clipInfo.softness.x > 0)
                    {
                        clipInfo.softness.x = vx / clipInfo.softness.x;
                    }
                    else
                    {
                        clipInfo.softness.x = 10000f;
                    }

                    if (clipInfo.softness.y > 0)
                    {
                        clipInfo.softness.y = vy / clipInfo.softness.y;
                    }
                    else
                    {
                        clipInfo.softness.y = 10000f;
                    }

                    if (clipInfo.softness.z > 0)
                    {
                        clipInfo.softness.z = vx / clipInfo.softness.z;
                    }
                    else
                    {
                        clipInfo.softness.z = 10000f;
                    }

                    if (clipInfo.softness.w > 0)
                    {
                        clipInfo.softness.w = vy / clipInfo.softness.w;
                    }
                    else
                    {
                        clipInfo.softness.w = 10000f;
                    }
                }
            }
        }
Ejemplo n.º 7
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;
        }
Ejemplo n.º 8
0
        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;
        }
Ejemplo n.º 9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="clipId"></param>
        /// <param name="clipRect"></param>
        /// <param name="softness"></param>
        public void EnterClipping(uint clipId, Rect clipRect, Vector4?softness)
        {
            _clipStack.Push(clipInfo);

            if (rectMaskDepth > 0)
            {
                clipRect = ToolSet.Intersection(ref clipInfo.rect, ref clipRect);
            }

            clipped = true;
            clipInfo.rectMaskDepth = ++rectMaskDepth;

            /* clipPos = xy * clipBox.zw + clipBox.xy
             * 利用这个公式,使clipPos变为当前顶点距离剪切区域中心的距离值,剪切区域的大小为2x2
             * 那么abs(clipPos)>1的都是在剪切区域外
             */
            clipInfo.rect    = clipRect;
            clipRect.x       = clipRect.x + clipRect.width * 0.5f;
            clipRect.y       = clipRect.y + clipRect.height * 0.5f;
            clipRect.width  *= 0.5f;
            clipRect.height *= 0.5f;
            if (clipRect.width == 0 || clipRect.height == 0)
            {
                clipInfo.clipBox = new Vector4(-2, -2, 0, 0);
            }
            else
            {
                clipInfo.clipBox = new Vector4(-clipRect.x / clipRect.width, -clipRect.y / clipRect.height,
                                               1.0f / clipRect.width, 1.0f / clipRect.height);
            }
            clipInfo.clipId = clipId;
            clipInfo.soft   = softness != null;
            if (clipInfo.soft)
            {
                clipInfo.softness = (Vector4)softness;
                float vx = clipInfo.rect.width * Screen.height * 0.25f;
                float vy = clipInfo.rect.height * Screen.height * 0.25f;

                if (clipInfo.softness.x > 0)
                {
                    clipInfo.softness.x = vx / clipInfo.softness.x;
                }
                else
                {
                    clipInfo.softness.x = 10000f;
                }

                if (clipInfo.softness.y > 0)
                {
                    clipInfo.softness.y = vy / clipInfo.softness.y;
                }
                else
                {
                    clipInfo.softness.y = 10000f;
                }

                if (clipInfo.softness.z > 0)
                {
                    clipInfo.softness.z = vx / clipInfo.softness.z;
                }
                else
                {
                    clipInfo.softness.z = 10000f;
                }

                if (clipInfo.softness.w > 0)
                {
                    clipInfo.softness.w = vy / clipInfo.softness.w;
                }
                else
                {
                    clipInfo.softness.w = 10000f;
                }
            }
        }
Ejemplo n.º 10
0
        public void PrintTo(Mesh mesh, Rect localRect)
        {
            if (_needRebuild)
            {
                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)
            {
                verts = new Vector3[quadBatch.vertices.Length];
                uv    = new Vector2[quadBatch.uv.Length];

                float rx = this.scaleX / GRoot.contentScaleFactor;
                float ry = this.scaleY / GRoot.contentScaleFactor;

                int   hc         = Mathf.CeilToInt(rx);
                int   vc         = Mathf.CeilToInt(ry);
                float partWidth  = contentRect.width * (rx - (hc - 1));
                float partHeight = contentRect.height * (ry - (vc - 1));
                localRect.xMin *= rx;
                localRect.xMax *= rx;
                localRect.yMin *= ry;
                localRect.yMax *= ry;

                Vector2 offset = new Vector2(0, 0);
                for (int i = 0; i < hc; i++)
                {
                    for (int j = 0; j < vc; j++)
                    {
                        Rect rect = new Rect(i * contentRect.width, j * contentRect.height,
                                             i == (hc - 1) ? partWidth : contentRect.width, j == (vc - 1) ? partHeight : contentRect.height);
                        Rect uvTmp = uvRect;
                        if (i == hc - 1)
                        {
                            uvTmp.xMax = Mathf.Lerp(uvRect.xMin, uvRect.xMax, partWidth / contentRect.width);
                        }
                        if (j == vc - 1)
                        {
                            uvTmp.yMin = Mathf.Lerp(uvRect.yMin, uvRect.yMax, 1 - partHeight / contentRect.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);
                            QuadBatch.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;

                            QuadBatch.FillVertsOfQuad(verts, vertCount, bound, GRoot.contentScaleFactor, GRoot.contentScaleFactor);

                            vertCount += 4;
                        }
                    }
                }
            }
            else if (_scale9Grid == null || (this.scaleX == 1 && this.scaleY == 1))
            {
                verts = new Vector3[quadBatch.vertices.Length];
                uv    = new Vector2[quadBatch.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);
                QuadBatch.FillUVOfQuad(uv, 0, Rect.MinMaxRect(u0, v0, u1, v1));

                bound.x = 0;
                bound.y = 0;
                QuadBatch.FillVertsOfQuad(verts, 0, bound, scaleX, scaleY);
                vertCount += 4;
            }
            else
            {
                verts = new Vector3[36];
                uv    = new Vector2[36];

                localRect.xMin *= this.scaleX;
                localRect.xMax *= this.scaleX;
                localRect.yMin *= this.scaleY;
                localRect.yMax *= this.scaleY;

                float scale9Width  = contentRect.width * this.scaleX / GRoot.contentScaleFactor;
                float scale9Height = contentRect.height * this.scaleY / GRoot.contentScaleFactor;

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

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

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

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

                float scaleXLeft = contentRect.width * this.scaleX / scale9Width;
                float scaleYLeft = contentRect.height * this.scaleY / scale9Height;

                Vector2 offset = new Vector2();
                for (int cy = 0; cy < 3; cy++)
                {
                    for (int cx = 0; cx < 3; cx++)
                    {
                        Rect rect = Rect.MinMaxRect(dCols[cx] * scaleXLeft, dRows[cy] * scaleYLeft,
                                                    dCols[cx + 1] * scaleXLeft, dRows[cy + 1] * scaleYLeft);
                        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] / contentRect.width * uvRect.width,
                                                            uvRect.y + (1 - rows[cy + 1] / contentRect.height) * uvRect.height,
                                                            uvRect.x + cols[cx + 1] / contentRect.width * uvRect.width,
                                                            uvRect.y + (1 - rows[cy] / contentRect.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);
                            QuadBatch.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;
                            QuadBatch.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;
        }