public override void RefreshSlicedMeshData(Vector2i resolution,
                                                   RectMargins meshMargins,
                                                   RectMargins uvMargins)
        {
            List <Vector3> verts = new List <Vector3>();
            List <Vector2> uvs   = new List <Vector2>();
            List <int>     tris  = new List <int>();

            for (int vy = 0; vy < resolution.y; vy++)
            {
                for (int vx = 0; vx < resolution.x; vx++)
                {
                    Vector2 vert;
                    vert.x = calculateVertAxis(vx, resolution.x, rect.width, meshMargins.left, meshMargins.right);
                    vert.y = calculateVertAxis(vy, resolution.y, rect.height, meshMargins.top, meshMargins.bottom);
                    verts.Add(vert + new Vector2(rect.x, rect.y));

                    Vector2 uv;
                    uv.x = calculateVertAxis(vx, resolution.x, 1, uvMargins.left, uvMargins.right);
                    uv.y = calculateVertAxis(vy, resolution.y, 1, uvMargins.top, uvMargins.bottom);
                    uvs.Add(uv);
                }
            }

            for (int vy = 0; vy < resolution.y - 1; vy++)
            {
                for (int vx = 0; vx < resolution.x - 1; vx++)
                {
                    int vertIndex = vy * resolution.x + vx;

                    tris.Add(vertIndex);
                    tris.Add(vertIndex + 1 + resolution.x);
                    tris.Add(vertIndex + 1);

                    tris.Add(vertIndex);
                    tris.Add(vertIndex + resolution.x);
                    tris.Add(vertIndex + 1 + resolution.x);
                }
            }

            if (mesh == null)
            {
                mesh = new Mesh();
            }

            mesh.name      = "Panel Mesh";
            mesh.hideFlags = HideFlags.HideAndDontSave;

            mesh.Clear(keepVertexLayout: false);
            mesh.SetVertices(verts);
            mesh.SetTriangles(tris, 0);
            mesh.SetUVs(uvChannel.Index(), uvs);
            mesh.RecalculateBounds();

            remappableChannels = UVChannelFlags.UV0;
        }
Exemple #2
0
 /// <summary>
 /// Set the mesh property equal to the correct mesh given the Sliced Graphic's
 /// current settings.
 ///
 /// Resolution along the X and Y axes are provided, as well as mesh-space and
 /// UV-space margins to pass into calculateVertAxis as border0 and border1 to support
 /// nine slicing (see Mesh Data Support).
 /// </summary>
 public abstract void RefreshSlicedMeshData(Vector2i resolution,
                                            RectMargins meshMargins,
                                            RectMargins uvMargins);
Exemple #3
0
        public override void RefreshSlicedMeshData(Vector2i resolution,
                                                   RectMargins meshMargins,
                                                   RectMargins uvMargins)
        {
            List <Vector3> verts   = new List <Vector3>();
            List <Vector2> uvs     = new List <Vector2>();
            List <Vector3> normals = new List <Vector3>();
            List <int>     tris    = new List <int>();

            // Back
            for (int vy = 0; vy < resolution.y; vy++)
            {
                for (int vx = 0; vx < resolution.x; vx++)
                {
                    Vector2 vert;
                    vert.x = calculateVertAxis(vx, resolution.x, rect.width, meshMargins.left, meshMargins.right);
                    vert.y = calculateVertAxis(vy, resolution.y, rect.height, meshMargins.top, meshMargins.bottom);
                    verts.Add(vert + new Vector2(rect.x, rect.y));
                    normals.Add(Vector3.forward);

                    Vector2 uv;
                    uv.x = calculateVertAxis(vx, resolution.x, 1, uvMargins.left, uvMargins.right);
                    uv.y = calculateVertAxis(vy, resolution.y, 1, uvMargins.top, uvMargins.bottom);
                    uvs.Add(uv);
                }
            }

            int backVertsCount = verts.Count;

            // Front
            float depth = -size.z;

            for (int vy = 0; vy < resolution.y; vy++)
            {
                for (int vx = 0; vx < resolution.x; vx++)
                {
                    Vector3 vert = Vector3.zero;
                    vert.x = calculateVertAxis(vx, resolution.x, rect.width, meshMargins.left, meshMargins.right);
                    vert.y = calculateVertAxis(vy, resolution.y, rect.height, meshMargins.top, meshMargins.bottom);
                    verts.Add(vert + new Vector3(rect.x, rect.y, depth));
                    normals.Add(Vector3.back);

                    Vector2 uv;
                    uv.x = calculateVertAxis(vx, resolution.x, 1, uvMargins.left, uvMargins.right);
                    uv.y = calculateVertAxis(vy, resolution.y, 1, uvMargins.top, uvMargins.bottom);
                    uvs.Add(uv);
                }
            }

            // Back
            for (int vy = 0; vy < resolution.y - 1; vy++)
            {
                for (int vx = 0; vx < resolution.x - 1; vx++)
                {
                    int vertIndex = vy * resolution.x + vx;

                    tris.Add(vertIndex);
                    tris.Add(vertIndex + 1);
                    tris.Add(vertIndex + 1 + resolution.x);

                    tris.Add(vertIndex);
                    tris.Add(vertIndex + 1 + resolution.x);
                    tris.Add(vertIndex + resolution.x);
                }
            }

            // Front
            for (int vy = 0; vy < resolution.y - 1; vy++)
            {
                for (int vx = 0; vx < resolution.x - 1; vx++)
                {
                    int vertIndex = backVertsCount + (vy * resolution.x + vx);

                    tris.Add(vertIndex);
                    tris.Add(vertIndex + 1 + resolution.x);
                    tris.Add(vertIndex + 1);

                    tris.Add(vertIndex);
                    tris.Add(vertIndex + resolution.x);
                    tris.Add(vertIndex + 1 + resolution.x);
                }
            }

            // Edges
            int ex = 0, ey = 0;
            int backVertIdx = verts.Count, frontVertIdx = verts.Count;

            // Left
            for (int vy = 0; vy < resolution.y; vy++) // Repeat back edge, left side
            {
                Vector2 vert;
                vert.x = calculateVertAxis(ex, resolution.x, rect.width, meshMargins.left, meshMargins.right);
                vert.y = calculateVertAxis(vy, resolution.y, rect.height, meshMargins.top, meshMargins.bottom);
                verts.Add(vert + new Vector2(rect.x, rect.y));
                normals.Add(Vector3.left);

                frontVertIdx += 1;

                Vector2 uv;
                uv.x = calculateVertAxis(ex, resolution.x, 1, uvMargins.left, uvMargins.right) + 0.01F /* cheat UVs in, prevents edge tearing */;
                uv.y = calculateVertAxis(vy, resolution.y, 1, uvMargins.top, uvMargins.bottom);
                uvs.Add(uv);
            }
            for (int vy = 0; vy < resolution.y; vy++) // Repeat front edge, left side
            {
                Vector3 vert = Vector3.zero;
                vert.x = calculateVertAxis(ex, resolution.x, rect.width, meshMargins.left, meshMargins.right);
                vert.y = calculateVertAxis(vy, resolution.y, rect.height, meshMargins.top, meshMargins.bottom);
                verts.Add(vert + new Vector3(rect.x, rect.y, depth));
                normals.Add(Vector3.left);

                Vector2 uv;
                uv.x = calculateVertAxis(ex, resolution.x, 1, uvMargins.left, uvMargins.right);
                uv.y = calculateVertAxis(vy, resolution.y, 1, uvMargins.top, uvMargins.bottom);
                uvs.Add(uv);
            }
            for (int vy = 0; vy < resolution.y - 1; vy++) // Add quads
            {
                addQuad(tris, frontVertIdx + vy, backVertIdx + vy, backVertIdx + vy + 1, frontVertIdx + vy + 1);
            }

            // Right
            ex           = resolution.x - 1;
            backVertIdx  = verts.Count;
            frontVertIdx = verts.Count;
            for (int vy = 0; vy < resolution.y; vy++) // Repeat back edge, right side
            {
                Vector2 vert;
                vert.x = calculateVertAxis(ex, resolution.x, rect.width, meshMargins.left, meshMargins.right);
                vert.y = calculateVertAxis(vy, resolution.y, rect.height, meshMargins.top, meshMargins.bottom);
                verts.Add(vert + new Vector2(rect.x, rect.y));
                normals.Add(Vector3.right);

                frontVertIdx += 1;

                Vector2 uv;
                uv.x = calculateVertAxis(ex, resolution.x, 1, uvMargins.left, uvMargins.right) - 0.01F /* cheat UVs in, prevents edge tearing */;
                uv.y = calculateVertAxis(vy, resolution.y, 1, uvMargins.top, uvMargins.bottom);
                uvs.Add(uv);
            }
            for (int vy = 0; vy < resolution.y; vy++) // Repeat front edge, right side
            {
                Vector3 vert = Vector3.zero;
                vert.x = calculateVertAxis(ex, resolution.x, rect.width, meshMargins.left, meshMargins.right);
                vert.y = calculateVertAxis(vy, resolution.y, rect.height, meshMargins.top, meshMargins.bottom);
                verts.Add(vert + new Vector3(rect.x, rect.y, depth));
                normals.Add(Vector3.right);

                Vector2 uv;
                uv.x = calculateVertAxis(ex, resolution.x, 1, uvMargins.left, uvMargins.right);
                uv.y = calculateVertAxis(vy, resolution.y, 1, uvMargins.top, uvMargins.bottom);
                uvs.Add(uv);
            }
            for (int vy = 0; vy < resolution.y - 1; vy++) // Add quads
            {
                addQuad(tris, frontVertIdx + vy + 1, backVertIdx + vy + 1, backVertIdx + vy, frontVertIdx + vy);
            }

            // Top
            ey           = resolution.y - 1;
            backVertIdx  = verts.Count;
            frontVertIdx = verts.Count;
            for (int vx = 0; vx < resolution.x; vx++) // Repeat back edge, upper side
            {
                Vector2 vert;
                vert.x = calculateVertAxis(vx, resolution.x, rect.width, meshMargins.left, meshMargins.right);
                vert.y = calculateVertAxis(ey, resolution.y, rect.height, meshMargins.top, meshMargins.bottom);
                verts.Add(vert + new Vector2(rect.x, rect.y));
                normals.Add(Vector3.up);

                frontVertIdx += 1;

                Vector2 uv;
                uv.x = calculateVertAxis(vx, resolution.x, 1, uvMargins.left, uvMargins.right);
                uv.y = calculateVertAxis(ey, resolution.y, 1, uvMargins.top, uvMargins.bottom) - 0.01F /* cheat UVs in, prevents edge tearing */;
                uvs.Add(uv);
            }
            for (int vx = 0; vx < resolution.x; vx++) // Repeat front edge, upper side
            {
                Vector3 vert = Vector3.zero;
                vert.x = calculateVertAxis(vx, resolution.x, rect.width, meshMargins.left, meshMargins.right);
                vert.y = calculateVertAxis(ey, resolution.y, rect.height, meshMargins.top, meshMargins.bottom);
                verts.Add(vert + new Vector3(rect.x, rect.y, depth));
                normals.Add(Vector3.up);

                Vector2 uv;
                uv.x = calculateVertAxis(vx, resolution.x, 1, uvMargins.left, uvMargins.right);
                uv.y = calculateVertAxis(ey, resolution.y, 1, uvMargins.top, uvMargins.bottom);
                uvs.Add(uv);
            }
            for (int vx = 0; vx < resolution.x - 1; vx++) // Add quads
            {
                addQuad(tris, frontVertIdx + vx, backVertIdx + vx, backVertIdx + vx + 1, frontVertIdx + vx + 1);
            }

            // Bottom
            ey           = 0;
            backVertIdx  = verts.Count;
            frontVertIdx = verts.Count;
            for (int vx = 0; vx < resolution.x; vx++) // Repeat back edge, upper side
            {
                Vector2 vert;
                vert.x = calculateVertAxis(vx, resolution.x, rect.width, meshMargins.left, meshMargins.right);
                vert.y = calculateVertAxis(ey, resolution.y, rect.height, meshMargins.top, meshMargins.bottom);
                verts.Add(vert + new Vector2(rect.x, rect.y));
                normals.Add(Vector3.down);

                frontVertIdx += 1;

                Vector2 uv;
                uv.x = calculateVertAxis(vx, resolution.x, 1, uvMargins.left, uvMargins.right);
                uv.y = calculateVertAxis(ey, resolution.y, 1, uvMargins.top, uvMargins.bottom) + 0.01F /* cheat UVs in, prevents edge tearing */;
                uvs.Add(uv);
            }
            for (int vx = 0; vx < resolution.x; vx++) // Repeat front edge, upper side
            {
                Vector3 vert = Vector3.zero;
                vert.x = calculateVertAxis(vx, resolution.x, rect.width, meshMargins.left, meshMargins.right);
                vert.y = calculateVertAxis(ey, resolution.y, rect.height, meshMargins.top, meshMargins.bottom);
                verts.Add(vert + new Vector3(rect.x, rect.y, depth));
                normals.Add(Vector3.down);

                Vector2 uv;
                uv.x = calculateVertAxis(vx, resolution.x, 1, uvMargins.left, uvMargins.right);
                uv.y = calculateVertAxis(ey, resolution.y, 1, uvMargins.top, uvMargins.bottom);
                uvs.Add(uv);
            }
            for (int vx = 0; vx < resolution.x - 1; vx++) // Add quads
            {
                addQuad(tris, frontVertIdx + vx + 1, backVertIdx + vx + 1, backVertIdx + vx, frontVertIdx + vx);
            }

            if (mesh == null)
            {
                mesh = new Mesh();
            }

            mesh.name      = "Box Mesh";
            mesh.hideFlags = HideFlags.HideAndDontSave;

            mesh.Clear(keepVertexLayout: false);
            mesh.SetVertices(verts);
            mesh.SetNormals(normals);
            mesh.SetTriangles(tris, 0);
            mesh.SetUVs(uvChannel.Index(), uvs);
            mesh.RecalculateBounds();

            remappableChannels = UVChannelFlags.UV0;
        }
        public override void RefreshSlicedMeshData(Vector2i resolution,
                                                   RectMargins meshMargins,
                                                   RectMargins uvMargins)
        {
            resolution.x = Mathf.Max(resolution.x, 4);
            resolution.y = Mathf.Max(resolution.y, 4);

            if (overrideSpriteBorders || !nineSliced)
            {
                // For this calculation, we ignore the provided meshMargins because the Outline
                // graphic allows the user to override its default nine-slicing behaviour for
                // borders.
                meshMargins = new RectMargins(_thickness.x, _thickness.y, _thickness.x, _thickness.y);
            }
            if (!nineSliced)
            {
                float xRatio = _thickness.x / rect.width;
                float yRatio = _thickness.y / rect.height;
                xRatio    = Mathf.Clamp(xRatio, 0F, 0.5F);
                yRatio    = Mathf.Clamp(yRatio, 0F, 0.5F);
                uvMargins = new RectMargins(left: xRatio, right: xRatio,
                                            top: yRatio, bottom: yRatio);
            }

            List <Vector3> verts = new List <Vector3>();
            List <Vector2> uvs   = new List <Vector2>();
            List <int>     tris  = new List <int>();

            for (int vy = 0; vy < resolution.y; vy++)
            {
                for (int vx = 0; vx < resolution.x; vx++)
                {
                    // Outline verts only.
                    if ((vy > 1 && vy < resolution.y - 2) && (vx > 1 && vx < resolution.x - 2))
                    {
                        continue;
                    }

                    Vector2 vert;
                    vert.x = calculateVertAxis(vx, resolution.x, rect.width, meshMargins.left, meshMargins.right, true);
                    vert.y = calculateVertAxis(vy, resolution.y, rect.height, meshMargins.top, meshMargins.bottom, true);
                    verts.Add(vert + new Vector2(rect.x, rect.y));

                    Vector2 uv;
                    uv.x = calculateVertAxis(vx, resolution.x, 1, uvMargins.left, uvMargins.right, true);
                    uv.y = calculateVertAxis(vy, resolution.y, 1, uvMargins.top, uvMargins.bottom, true);
                    uvs.Add(uv);
                }
            }

            int indicesSkippedPerRow = resolution.x - 4;
            int indicesSkipped       = 0;

            for (int vy = 0; vy < resolution.y; vy++)
            {
                for (int vx = 0; vx < resolution.x; vx++)
                {
                    if (vx == resolution.x - 1 || vy == resolution.y - 1)
                    {
                        continue;
                    }
                    if ((vx == 1 && (vy > 0 && vy < resolution.y - 2)) || (vy == 1 && (vx > 0 && vx < resolution.x - 2)))
                    {
                        continue;
                    }
                    if ((vx > 1 && vx < resolution.x - 2) && (vy > 1 && vy < resolution.y - 2))
                    {
                        indicesSkipped += 1;
                        continue;
                    }

                    int vertIndex = vy * resolution.x + vx - indicesSkipped;

                    int right = 1;
                    int down;
                    if (vy == 0 || (vy == 1 && vx < 2) || (vy == resolution.y - 3 && vx > resolution.x - 3) || (vy == resolution.y - 2))
                    {
                        down = resolution.x;
                    }
                    else
                    {
                        down = resolution.x - indicesSkippedPerRow;
                    }

                    // Add quad
                    tris.Add(vertIndex);
                    tris.Add(vertIndex + right + down);
                    tris.Add(vertIndex + right);

                    tris.Add(vertIndex);
                    tris.Add(vertIndex + down);
                    tris.Add(vertIndex + right + down);
                }
            }

            if (mesh == null)
            {
                mesh = new Mesh();
            }

            mesh.name      = "Panel Outline Mesh";
            mesh.hideFlags = HideFlags.HideAndDontSave;

            mesh.Clear(keepVertexLayout: false);
            mesh.SetVertices(verts);
            mesh.SetTriangles(tris, 0);
            mesh.SetUVs(uvChannel.Index(), uvs);
            mesh.RecalculateBounds();

            remappableChannels = UVChannelFlags.UV0;
        }