Пример #1
0
        private void RenderSpriteList(List<_2DSprite> sprites, Effect effect, EffectTechnique technique)
        {
            if (sprites.Count == 0) { return; }

            /** Group by texture **/
            var groupByTexture = GroupByTexture(sprites);
            foreach (var group in groupByTexture)
            {
                var texture = group.Pixel;
                var depth = group.Depth;
                var numSprites = group.Sprites.Count;

                effect.Parameters["pixelTexture"].SetValue(texture);
                if (depth != null)
                {
                    effect.Parameters["depthTexture"].SetValue(depth);
                }

                /** Build vertex data **/
                var verticies = new _2DSpriteVertex[4 * numSprites];
                var indices = new short[6 * numSprites];
                var indexCount = 0;
                var vertexCount = 0;

                foreach (var sprite in group.Sprites)
                {
                    var srcRectangle = sprite.SrcRect;
                    var dstRectangle = sprite.AbsoluteDestRect;

                    indices[indexCount++] = (short)(vertexCount + 0);
                    indices[indexCount++] = (short)(vertexCount + 1);
                    indices[indexCount++] = (short)(vertexCount + 3);
                    indices[indexCount++] = (short)(vertexCount + 1);
                    indices[indexCount++] = (short)(vertexCount + 2);
                    indices[indexCount++] = (short)(vertexCount + 3);
                    // add the new vertices

                    if (sprite.FlipHorizontally)
                    {
                        verticies[vertexCount++] = new _2DSpriteVertex(
                            new Vector3(dstRectangle.Left, dstRectangle.Top, 0)
                            , GetUV(texture, srcRectangle.Right, srcRectangle.Top), sprite.AbsoluteWorldPosition);
                        verticies[vertexCount++] = new _2DSpriteVertex(
                            new Vector3(dstRectangle.Right, dstRectangle.Top, 0)
                            , GetUV(texture, srcRectangle.Left, srcRectangle.Top), sprite.AbsoluteWorldPosition);
                        verticies[vertexCount++] = new _2DSpriteVertex(
                            new Vector3(dstRectangle.Right, dstRectangle.Bottom, 0)
                            , GetUV(texture, srcRectangle.Left, srcRectangle.Bottom), sprite.AbsoluteWorldPosition);
                        verticies[vertexCount++] = new _2DSpriteVertex(
                            new Vector3(dstRectangle.Left, dstRectangle.Bottom, 0)
                            , GetUV(texture, srcRectangle.Right, srcRectangle.Bottom), sprite.AbsoluteWorldPosition);
                    }
                    else
                    {
                        verticies[vertexCount++] = new _2DSpriteVertex(
                            new Vector3(dstRectangle.Left, dstRectangle.Top, 0)
                            , GetUV(texture, srcRectangle.Left, srcRectangle.Top), sprite.AbsoluteWorldPosition);
                        verticies[vertexCount++] = new _2DSpriteVertex(
                            new Vector3(dstRectangle.Right, dstRectangle.Top, 0)
                            , GetUV(texture, srcRectangle.Right, srcRectangle.Top), sprite.AbsoluteWorldPosition);
                        verticies[vertexCount++] = new _2DSpriteVertex(
                            new Vector3(dstRectangle.Right, dstRectangle.Bottom, 0)
                            , GetUV(texture, srcRectangle.Right, srcRectangle.Bottom), sprite.AbsoluteWorldPosition);
                        verticies[vertexCount++] = new _2DSpriteVertex(
                            new Vector3(dstRectangle.Left, dstRectangle.Bottom, 0)
                            , GetUV(texture, srcRectangle.Left, srcRectangle.Bottom), sprite.AbsoluteWorldPosition);
                    }
                }

                effect.CurrentTechnique = technique;
                effect.Begin();
                EffectPassCollection passes = technique.Passes;
                for (int i = 0; i < passes.Count; i++)
                {
                    EffectPass pass = passes[i];
                    pass.Begin();
                    Device.DrawUserIndexedPrimitives<_2DSpriteVertex>(
                        PrimitiveType.TriangleList, verticies, 0, verticies.Length,
                        indices, 0, indices.Length / 3);
                    pass.End();
                }
                effect.End();
            }
        }
Пример #2
0
        private void RenderSpriteList(List <_2DSprite> sprites, Effect effect, EffectTechnique technique)
        {
            if (sprites.Count == 0)
            {
                return;
            }

            /** Group by texture **/
            var groupByTexture = GroupByTexture(sprites);

            foreach (var group in groupByTexture)
            {
                var texture    = group.Pixel;
                var depth      = group.Depth;
                var numSprites = group.Sprites.Count;

                effect.Parameters["pixelTexture"].SetValue(texture);
                if (depth != null)
                {
                    effect.Parameters["depthTexture"].SetValue(depth);
                }

                /** Build vertex data **/
                var verticies   = new _2DSpriteVertex[4 * numSprites];
                var indices     = new short[6 * numSprites];
                var indexCount  = 0;
                var vertexCount = 0;

                foreach (var sprite in group.Sprites)
                {
                    var srcRectangle = sprite.SrcRect;
                    var dstRectangle = sprite.AbsoluteDestRect;

                    indices[indexCount++] = (short)(vertexCount + 0);
                    indices[indexCount++] = (short)(vertexCount + 1);
                    indices[indexCount++] = (short)(vertexCount + 3);
                    indices[indexCount++] = (short)(vertexCount + 1);
                    indices[indexCount++] = (short)(vertexCount + 2);
                    indices[indexCount++] = (short)(vertexCount + 3);
                    // add the new vertices

                    if (sprite.FlipHorizontally)
                    {
                        verticies[vertexCount++] = new _2DSpriteVertex(
                            new Vector3(dstRectangle.Left, dstRectangle.Top, 0)
                            , GetUV(texture, srcRectangle.Right, srcRectangle.Top), sprite.AbsoluteWorldPosition);
                        verticies[vertexCount++] = new _2DSpriteVertex(
                            new Vector3(dstRectangle.Right, dstRectangle.Top, 0)
                            , GetUV(texture, srcRectangle.Left, srcRectangle.Top), sprite.AbsoluteWorldPosition);
                        verticies[vertexCount++] = new _2DSpriteVertex(
                            new Vector3(dstRectangle.Right, dstRectangle.Bottom, 0)
                            , GetUV(texture, srcRectangle.Left, srcRectangle.Bottom), sprite.AbsoluteWorldPosition);
                        verticies[vertexCount++] = new _2DSpriteVertex(
                            new Vector3(dstRectangle.Left, dstRectangle.Bottom, 0)
                            , GetUV(texture, srcRectangle.Right, srcRectangle.Bottom), sprite.AbsoluteWorldPosition);
                    }
                    else
                    {
                        verticies[vertexCount++] = new _2DSpriteVertex(
                            new Vector3(dstRectangle.Left, dstRectangle.Top, 0)
                            , GetUV(texture, srcRectangle.Left, srcRectangle.Top), sprite.AbsoluteWorldPosition);
                        verticies[vertexCount++] = new _2DSpriteVertex(
                            new Vector3(dstRectangle.Right, dstRectangle.Top, 0)
                            , GetUV(texture, srcRectangle.Right, srcRectangle.Top), sprite.AbsoluteWorldPosition);
                        verticies[vertexCount++] = new _2DSpriteVertex(
                            new Vector3(dstRectangle.Right, dstRectangle.Bottom, 0)
                            , GetUV(texture, srcRectangle.Right, srcRectangle.Bottom), sprite.AbsoluteWorldPosition);
                        verticies[vertexCount++] = new _2DSpriteVertex(
                            new Vector3(dstRectangle.Left, dstRectangle.Bottom, 0)
                            , GetUV(texture, srcRectangle.Left, srcRectangle.Bottom), sprite.AbsoluteWorldPosition);
                    }
                }

                effect.CurrentTechnique = technique;
                effect.Begin();
                EffectPassCollection passes = technique.Passes;
                for (int i = 0; i < passes.Count; i++)
                {
                    EffectPass pass = passes[i];
                    pass.Begin();
                    Device.DrawUserIndexedPrimitives <_2DSpriteVertex>(
                        PrimitiveType.TriangleList, verticies, 0, verticies.Length,
                        indices, 0, indices.Length / 3);
                    pass.End();
                }
                effect.End();
            }
        }