Exemple #1
0
        void UpdateMeshNow()
        {
            _meshDirty = false;

            if (_texture == null || _meshFactory == null)
            {
                if (mesh.vertexCount > 0)
                {
                    mesh.Clear();

                    if (meshModifier != null)
                    {
                        meshModifier();
                    }
                }
                return;
            }

            VertexBuffer vb = VertexBuffer.Begin();

            vb.contentRect = _contentRect;
            vb.uvRect      = _texture.uvRect;
            if (_flip != FlipType.None)
            {
                if (_flip == FlipType.Horizontal || _flip == FlipType.Both)
                {
                    float tmp = vb.uvRect.xMin;
                    vb.uvRect.xMin = vb.uvRect.xMax;
                    vb.uvRect.xMax = tmp;
                }
                if (_flip == FlipType.Vertical || _flip == FlipType.Both)
                {
                    float tmp = vb.uvRect.yMin;
                    vb.uvRect.yMin = vb.uvRect.yMax;
                    vb.uvRect.yMax = tmp;
                }
            }
            vb.vertexColor = _color;
            _meshFactory.OnPopulateMesh(vb);

            int vertCount = vb.currentVertCount;

            if (vertCount == 0)
            {
                if (mesh.vertexCount > 0)
                {
                    mesh.Clear();

                    if (meshModifier != null)
                    {
                        meshModifier();
                    }
                }
                vb.End();
                return;
            }

            if (_texture.rotated)
            {
                float xMin = _texture.uvRect.xMin;
                float yMin = _texture.uvRect.yMin;
                float yMax = _texture.uvRect.yMax;
                float tmp;
                for (int i = 0; i < vertCount; i++)
                {
                    Vector4 vec = vb.uv0[i];
                    tmp       = vec.y;
                    vec.y     = yMin + vec.x - xMin;
                    vec.x     = xMin + yMax - tmp;
                    vb.uv0[i] = vec;
                }
            }

            hasAlphaBackup = vb._alphaInVertexColor;
            if (hasAlphaBackup)
            {
                if (_alphaBackup == null)
                {
                    _alphaBackup = new List <byte>();
                }
                else
                {
                    _alphaBackup.Clear();
                }
                for (int i = 0; i < vertCount; i++)
                {
                    Color32 col = vb.colors[i];
                    _alphaBackup.Add(col.a);

                    col.a        = (byte)(col.a * _alpha);
                    vb.colors[i] = col;
                }
            }
            else if (_alpha != 1)
            {
                for (int i = 0; i < vertCount; i++)
                {
                    Color32 col = vb.colors[i];
                    col.a        = (byte)(col.a * _alpha);
                    vb.colors[i] = col;
                }
            }

            if (_vertexMatrix != null)
            {
                Matrix4x4 mm     = (Matrix4x4)_vertexMatrix;
                Vector3   camPos = _cameraPosition != null ? (Vector3)_cameraPosition : Vector3.zero;
                Vector3   center = new Vector3(camPos.x, camPos.y, 0);
                center -= mm.MultiplyPoint(center);
                for (int i = 0; i < vertCount; i++)
                {
                    Vector3 pt = vb.vertices[i];
                    pt  = mm.MultiplyPoint(pt);
                    pt += center;
                    Vector3 vec    = pt - camPos;
                    float   lambda = -camPos.z / vec.z;
                    pt.x = camPos.x + lambda * vec.x;
                    pt.y = camPos.y + lambda * vec.y;
                    pt.z = 0;

                    vb.vertices[i] = pt;
                }
            }

            mesh.Clear();

#if UNITY_5_2 || UNITY_5_3_OR_NEWER
            if (vb._isArbitraryQuad)
            {
                vb.FixUVForArbitraryQuad();
            }

            mesh.SetVertices(vb.vertices);
            mesh.SetUVs(0, vb.uv0);
            mesh.SetColors(vb.colors);
            mesh.SetTriangles(vb.triangles, 0);

#if !UNITY_5_6_OR_NEWER
            _colors = null;
#endif
#else
            if (_vertices == null || _vertices.Length != vertCount)
            {
                _vertices = new Vector3[vertCount];
                _uv       = new Vector2[vertCount];
                _colors   = new Color32[vertCount];
            }
            vb.vertices.CopyTo(_vertices);
            vb.uv0.CopyTo(_uv);
            vb.colors.CopyTo(_colors);

            if (_triangles == null || _triangles.Length != vb.triangles.Count)
            {
                _triangles = new int[vb.triangles.Count];
            }
            vb.triangles.CopyTo(_triangles);

            mesh.vertices  = _vertices;
            mesh.uv        = _uv;
            mesh.triangles = _triangles;
            mesh.colors32  = _colors;
#endif
            vb.End();

            if (meshModifier != null)
            {
                meshModifier();
            }
        }
        void UpdateMeshNow()
        {
            _meshDirty = false;

            if (_texture == null || _meshFactory == null)
            {
                if (_vertices.Count > 0)
                {
                    _vertices.Clear();
                    _uv0.Clear();
                    _colors.Clear();
                    _triangles.Clear();

                    if (meshModifier != null)
                    {
                        meshModifier();
                    }
                }
                return;
            }

            VertexBuffer vb = VertexBuffer.Begin();

            vb.contentRect = _contentRect;
            vb.uvRect      = _texture.uvRect;
            if (_flip != FlipType.None)
            {
                if (_flip == FlipType.Horizontal || _flip == FlipType.Both)
                {
                    float tmp = vb.uvRect.X;
                    vb.uvRect.X     = vb.uvRect.X + vb.uvRect.Width;
                    vb.uvRect.Width = tmp - vb.uvRect.X;
                }
                if (_flip == FlipType.Vertical || _flip == FlipType.Both)
                {
                    float tmp = vb.uvRect.Y;
                    vb.uvRect.Y      = vb.uvRect.Y + vb.uvRect.Height;
                    vb.uvRect.Height = tmp - vb.uvRect.Y;
                }
            }
            vb.vertexColor = _color;
            _meshFactory.OnPopulateMesh(vb);

            int vertCount = vb.currentVertCount;

            if (vertCount == 0)
            {
                if (_vertices.Count > 0)
                {
                    _vertices.Clear();
                    _uv0.Clear();
                    _colors.Clear();
                    _triangles.Clear();

                    if (meshModifier != null)
                    {
                        meshModifier();
                    }
                }
                vb.End();
                return;
            }

            if (_texture.rotated)
            {
                float xMin = _texture.uvRect.X;
                float yMin = _texture.uvRect.Y;
                float yMax = _texture.uvRect.Bottom;
                float tmp;
                for (int i = 0; i < vertCount; i++)
                {
                    Vector2 vec = vb.uv0[i];
                    tmp       = vec.Y;
                    vec.Y     = yMin + vec.X - xMin;
                    vec.X     = xMin + yMax - tmp;
                    vb.uv0[i] = vec;
                }
            }

            _vertices.Clear();
            _uv0.Clear();
            _colors.Clear();
            _triangles.Clear();
            _vertices.AddRange(vb.vertices);
            _uv0.AddRange(vb.uv0);
            _colors.AddRange(vb.colors);
            _triangles.AddRange(vb.triangles);

            vb.End();

            if (meshModifier != null)
            {
                meshModifier();
            }
        }