コード例 #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 (_texture != null)
            {
                vb.textureSize = new Vector2(_texture.width, _texture.height);
            }
            else
            {
                vb.textureSize = new Vector2(0, 0);
            }
            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++)
                {
                    Vector2 vec = vb.uvs[i];
                    tmp       = vec.y;
                    vec.y     = yMin + vec.x - xMin;
                    vec.x     = xMin + yMax - tmp;
                    vb.uvs[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)
            {
                Vector3 camPos = _vertexMatrix.cameraPos;
                Vector3 center = new Vector3(camPos.x, camPos.y, 0);
                center -= _vertexMatrix.matrix.MultiplyPoint(center);
                for (int i = 0; i < vertCount; i++)
                {
                    Vector3 pt = vb.vertices[i];
                    pt  = _vertexMatrix.matrix.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
            mesh.SetVertices(vb.vertices);
            if (vb._isArbitraryQuad)
            {
                mesh.SetUVs(0, vb.FixUVForArbitraryQuad());
            }
            else
            {
                mesh.SetUVs(0, vb.uvs);
            }
            mesh.SetColors(vb.colors);
            mesh.SetTriangles(vb.triangles, 0);
            if (vb.uvs2.Count == vb.uvs.Count)
            {
                mesh.SetUVs(1, vb.uvs2);
            }

#if !UNITY_5_6_OR_NEWER
            _colors = null;
#endif
#else
            Vector3 vertices = new Vector3[vertCount];
            Vector2 uv       = new Vector2[vertCount];
            _colors = new Color32[vertCount];
            int triangles = new int[vb.triangles.Count];

            vb.vertices.CopyTo(vertices);
            vb.uvs.CopyTo(uv);
            vb.colors.CopyTo(_colors);
            vb.triangles.CopyTo(triangles);

            mesh.vertices  = vertices;
            mesh.uv        = uv;
            mesh.triangles = triangles;
            mesh.colors32  = _colors;

            if (vb.uvs2.Count == uv.Length)
            {
                uv = new Vector2[vertCount];
                vb.uvs2.CopyTo(uv);
                mesh.uv2 = uv;
            }
#endif
            vb.End();

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