Beispiel #1
0
        protected override void OnPopulateMesh(VertexHelper vh)
        {
            if (m_mesh == null)
            {
                base.OnPopulateMesh(vh);
                return;
            }
            vh.Clear();
            s_vertBuffer.Clear();
            s_triBuffer.Clear();
            s_colorBuffer.Clear();
            s_uv0Buffer.Clear();

            // Calc Bounds
            mesh.GetVertices(s_vertBuffer);
            mesh.GetTriangles(s_triBuffer, 0);
            mesh.GetColors(s_colorBuffer);
            mesh.GetUVs(0, s_uv0Buffer);
            Bounds bounds = new Bounds(s_vertBuffer[0], Vector3.zero);

            foreach (var vert in s_vertBuffer)
            {
                bounds.Encapsulate(vert);
            }

            Vector2 dims = rectTransform.rect.size;

            // Add the same verts, but now normalized
            int meshVertCount = s_vertBuffer.Count;

            for (int vi = 0; vi < meshVertCount; vi++)
            {
                Vector3  meshVert = s_vertBuffer[vi];
                UIVertex uiVert   = new UIVertex();

                bool  hasColors = mesh.HasVertexAttribute(UnityEngine.Rendering.VertexAttribute.Color);
                Color geoFactor = Color.white;
                if (hasColors && vi < s_colorBuffer.Count)
                {
                    geoFactor = s_colorBuffer[vi];
                }
                uiVert.color = this.color * geoFactor;
                Vector2 normPoint = bounds.CalcNormalizedPoint(meshVert).XY();
                normPoint  = softSlice ? softSlicing.Remap(normPoint, dims) : normPoint;
                normPoint -= 0.5f * Vector2.one;
                Vector3 scaledPos = new Vector3(-normPoint.x * dims.x, normPoint.y * dims.y, 0f);
                uiVert.position = scaledPos;

                uiVert.uv0 = s_uv0Buffer[vi];

                // if( mesh.uv2 != null && vi < mesh.uv2.Length){
                //     uiVert.uv1 = mesh.uv2[vi];
                // }
                // if( mesh.uv3 != null && vi < mesh.uv3.Length){
                //     uiVert.uv2 = mesh.uv3[vi];
                // }
                // if( mesh.uv4 != null && vi < mesh.uv4.Length){
                //     uiVert.uv3 = mesh.uv4[vi];
                // }

                vh.AddVert(uiVert);
            }

            // Same tri indices
            int triCount = s_triBuffer.Count;

            for (int ti = 0; ti < triCount; ti += 3)
            {
                vh.AddTriangle(s_triBuffer[ti + 0], s_triBuffer[ti + 1], s_triBuffer[ti + 2]);
            }
        }