Beispiel #1
0
        void UpdateSpriteMesh(bool isClipping, Vector3 cameraPos, float radius)
        {
            int triangles = 0;
            int priCount  = getAndSortPrimitives(ref triangles, isClipping, cameraPos, radius);

            lastCommitSpriteConnt   = priCount;
            lastCommitTriangleCount = triangles;

            if (priCount == 0)
            {
                if (renderMesh != null && renderMesh.vertexCount > 0)
                {
                    renderMesh.subMeshCount = 0;
                }

                return;
            }


            //set vertices, uvs, colors
            int l      = priCount * 4;
            int arrlen = _vertices.Length;

            if (l > arrlen || l < arrlen >> 1)
            {
                if (l > arrlen)
                {
                    arrlen = l + 128;
                }
                else if (l < arrlen >> 1)
                {
                    arrlen = arrlen >> 1;
                }

                _vertices = new Vector3[arrlen];
                _uvs      = new Vector2[arrlen];
                _colors   = new Color[arrlen];
            }

            renderMesh.subMeshCount = 0;

            System.Array.Copy(vertices, _vertices, l);
            System.Array.Copy(UVs, _uvs, l);
            System.Array.Copy(colors, _colors, l);

            renderMesh.vertices = _vertices;
            renderMesh.colors   = _colors;
            renderMesh.uv       = _uvs;



            int c = 0;

            //set submesh count
            renderMesh.subMeshCount = subMeshCount;
            Material[] mats = new Material[subMeshCount];

            //set triangles
            for (int i = 0; i < subMeshCount; i++)
            {
                SubMeshInfo info = subMeshs[i];
                int         len  = info.triangleCnt * 3;
                //get closest array
                FixedArrayAllocator <int> .FixedArrayInfo arrayInfo = FixedArrayAllocator <int> .GetArray(len);

                //copy index
                System.Array.Copy(indices, info.indexBase, arrayInfo.array, 0, len);
                //clean last
                System.Array.Clear(arrayInfo.array, len, arrayInfo.fixedSize - len);
                //set triangle list
                renderMesh.SetTriangles(arrayInfo.array, i);
                //set submesh material
                mats[i] = info.mat;

                c += arrayInfo.fixedSize;
            }

            renderer.materials = mats;

            OnCommitEnd();
        }
Beispiel #2
0
        internal void Init(int maxPrimitiveCount)
        {
            renderer.materials = new Material[0];


            //init component
            meshFilter            = GetComponent <MeshFilter>();
            meshFilter.sharedMesh = new Mesh();
            renderMesh            = meshFilter.sharedMesh;


            this.maxPrimitiveCount = maxPrimitiveCount;
            int count = maxPrimitiveCount;


            if (vertices == null || count > vertices.Length)
            {
                //init vertices, uv, color
                vertices = new Vector3[count * 4];
            }

            if (UVs == null || count > UVs.Length)
            {
                UVs = new Vector2[count * 4];
            }

            if (colors == null || count > colors.Length)
            {
                colors = new Color[count * 4];
            }

            if (indices == null || count > indices.Length)
            {
                //init index with triangle list
                indices = new int[count * 6];
                for (int i = 0; i < count; i++)
                {
                    int srcIdxBase = i * 4;
                    int idxBase    = i * 6;

                    indices[idxBase + 0] = srcIdxBase + 0;
                    indices[idxBase + 1] = srcIdxBase + 1;
                    indices[idxBase + 2] = srcIdxBase + 2;
                    indices[idxBase + 3] = srcIdxBase + 0;
                    indices[idxBase + 4] = srcIdxBase + 2;
                    indices[idxBase + 5] = srcIdxBase + 3;
                }
            }



            //init submesh
            subMeshCount = 0;


            if (subMeshs == null)
            {
                subMeshs = new SubMeshInfo[512];
                for (int i = 0, l = subMeshs.Length; i < l; i++)
                {
                    subMeshs[i] = new SubMeshInfo();
                }
            }

            if (_primitives == null || count > _primitives.Length)
            {
                //init primitive pool
                _primitives = new SpritePrimitive[count];
            }

            if (sortKeys == null || count > sortKeys.Length)
            {
                sortKeys = new int[count];
            }


            FixedArrayAllocator <int> .AddArray(1, 8, 6, 2);

            FixedArrayAllocator <int> .AddArray(256 * 6, count * 6, 256 * 6);

            vertices[0] = new Vector3(-1000000f, -1000000f, -100000f);
            vertices[1] = new Vector3(1000000f, -1000000f, -100000f);
            vertices[2] = new Vector3(-1000000f, 1000000f, 100000f);
            vertices[3] = new Vector3(1000000f, 1000000f, 100000f);

            renderMesh.vertices  = vertices;
            renderMesh.uv        = UVs;
            renderMesh.colors    = colors;
            renderMesh.triangles = new int[] { 0, 1, 2, 1, 2, 3 };

            renderMesh.RecalculateBounds();

            isInit = true;
        }