Пример #1
0
 public RenderInformation(Material material, Entity bufferEntity)
 {
     this.material     = material;
     spriteCount       = SpriteSheetCache.GetLength(material);
     this.bufferEntity = bufferEntity;
     args = new uint[5] {
         0, 0, 0, 0, 0
     };
     argsBuffer = new ComputeBuffer(1, args.Length * sizeof(uint), ComputeBufferType.IndirectArguments);
     // those args are always the same since we always use the same mesh
     args[0]   = (uint)6;
     args[2]   = args[3] = (uint)0;
     updateUvs = true;
 }
        // we should only update the index of the changed datas for
        // indexBuffer, matrixBuffer, and colorBuffer inside a bursted
        // job to avoid overhead
        private int UpdateBuffers(int renderIndex)
        {
            SpriteSheetManager.ReleaseBuffer(renderIndex);

            RenderInformation renderInformation = SpriteSheetManager.renderInformation[renderIndex];
            int instanceCount = EntityManager.GetBuffer <SpriteIndexBuffer>(renderInformation.bufferEntity).Length;

            if (instanceCount > 0)
            {
                if (renderInformation.updateUvs)
                {
                    int stride = instanceCount >= 16 ? 16 : 16 * SpriteSheetCache.GetLength(renderInformation.material);
                    SpriteSheetManager.ReleaseUvBuffer(renderIndex);
                    renderInformation.uvBuffer = new ComputeBuffer(instanceCount, stride);
                    renderInformation.uvBuffer.SetData(EntityManager.GetBuffer <UvBuffer>(renderInformation.bufferEntity).Reinterpret <float4>().AsNativeArray());
                    renderInformation.material.SetBuffer("uvBuffer", renderInformation.uvBuffer);
                    renderInformation.updateUvs = false;
                }

                renderInformation.indexBuffer = new ComputeBuffer(instanceCount, sizeof(int));
                renderInformation.indexBuffer.SetData(EntityManager.GetBuffer <SpriteIndexBuffer>(renderInformation.bufferEntity).Reinterpret <int>().AsNativeArray());
                renderInformation.material.SetBuffer("indexBuffer", renderInformation.indexBuffer);

                renderInformation.matrixBuffer = new ComputeBuffer(instanceCount, 16);
                renderInformation.matrixBuffer.SetData(EntityManager.GetBuffer <MatrixBuffer>(renderInformation.bufferEntity).Reinterpret <float4>().AsNativeArray());
                renderInformation.material.SetBuffer("matrixBuffer", renderInformation.matrixBuffer);

                renderInformation.args[1] = (uint)instanceCount;
                renderInformation.argsBuffer.SetData(renderInformation.args);

                renderInformation.colorsBuffer = new ComputeBuffer(instanceCount, 16);
                renderInformation.colorsBuffer.SetData(EntityManager.GetBuffer <SpriteColorBuffer>(renderInformation.bufferEntity).Reinterpret <float4>().AsNativeArray());
                renderInformation.material.SetBuffer("colorsBuffer", renderInformation.colorsBuffer);
            }
            return(instanceCount);
        }