Esempio n. 1
0
        public void Render(IEnumerable <Particle> particles)
        {
            if (particles == null || !particles.Any())
            {
                return;
            }
            int count = particles.Count();

            GraphicsPoint[] vertices = new GraphicsPoint[6 * count];
            //TODO// Allow more advanced blending
            GraphicsPoint[] texCoords = new GraphicsPoint[6 * count];

            GraphicsPoint
                tl = new GraphicsPoint(-Sprite.XOrigin, -Sprite.YOrigin),
                tr = new GraphicsPoint(Sprite.Width - Sprite.XOrigin, -Sprite.YOrigin),
                br = new GraphicsPoint(Sprite.Width - Sprite.XOrigin, Sprite.Height - Sprite.YOrigin),
                bl = new GraphicsPoint(-Sprite.XOrigin, Sprite.Height - Sprite.YOrigin);

            Parallel.ForEach(particles, (particle, loopState, index) =>
            {
                index              *= 6;
                vertices[index]     = (GraphicsPoint)(particle.TransformationMatrix * tl + particle.Location);
                vertices[index + 1] = vertices[index + 3] = (GraphicsPoint)(particle.TransformationMatrix * tr + particle.Location);
                vertices[index + 2] = vertices[index + 4] = (GraphicsPoint)(particle.TransformationMatrix * bl + particle.Location);
                vertices[index + 5] = (GraphicsPoint)(particle.TransformationMatrix * br + particle.Location);

                var texture          = Sprite.SubImage(_frame);
                texCoords[index]     = texture.TopLeft;
                texCoords[index + 1] = texCoords[index + 3] = texture.TopRight;
                texCoords[index + 2] = texCoords[index + 4] = texture.BottomLeft;
                texCoords[index + 5] = texture.BottomRight;
            });


            _renderSystem.SetVertices(vertices);
            _renderSystem.SetColor(Colors.White);
            _renderSystem.SetTexCoords(texCoords);

            _renderSystem.Render(Sprite.SubImage(_frame).Texture, PrimitiveType.Triangles);

            _frame += -_animationSpeed;
        }
        public void Render(IEnumerable <Particle> particles)
        {
            if (particles == null || !particles.Any())
            {
                return;
            }
            int count    = particles.Count();
            var vertices = new GraphicsPoint[_polygonVertices.Length * count];
            var colors   = new Color[_polygonVertices.Length * count];

            Parallel.ForEach(particles, (particle, loopState, index) =>
            {
                index *= _verticesPerParticle;
                for (int c = 0; c < _verticesPerParticle; c++)
                {
                    vertices[index + c] = (GraphicsPoint)(particle.TransformationMatrix * _polygonVertices[c] + particle.Location);
                    colors[index + c]   = particle.Blend;
                }
            });

            _renderSystem.SetVertices(vertices);
            _renderSystem.SetColors(colors);
            _renderSystem.Render(PrimitiveType.Triangles);
        }
Esempio n. 3
0
        /// <summary>
        /// Renders spatial and uv-vertices to be used to draw the specified text under the specified transformation.
        /// </summary>
        /// <returns>The spatial and uv-vertices that would be used to render
        /// the specified text.</returns>
        /// <param name="text">The text to be rendered.</param>
        /// <param name="transform">A matrix specifying the transformation of the text.</param>
        /// <remarks> Characters not found in the font are ignored without being rendered.
        /// Each character is rendered as two triangles for a total of 6 vertices in xy space and uv space.
        /// If overriding this method, the returned arrays should have the same length, which should be
        /// divisible by 6, in order to ensure compatibility with use in Draw.Text functions.
        /// </remarks>
        public virtual (GraphicsPoint[] vertices, GraphicsPoint[] texCoords) RenderVertices(string text, Matrix transform)
        {
            var lines  = LineSplit(text).ToArray();
            var length = lines.SelectMany(str => str.ToCharArray())
                         .Count(Font.HasCharacter);
            double tXScale = 1.0 / Font.Texture.Width, tYScale = 1.0 / Font.Texture.Height;

            var vertices  = new GraphicsPoint[6 * length];
            var texCoords = new GraphicsPoint[6 * length];

            var x0 = 0.0;
            var y0 = 0.0;

            switch (Alignment & Alignment.Vertical)
            {
            case Alignment.Top: y0 = 0; break;

            case Alignment.Center: y0 = -((Font.Height + LineSeparation) * (lines.Length - 1) + Font.Height) / 2; break;

            case Alignment.Bottom: y0 = -((Font.Height + LineSeparation) * (lines.Length - 1) + Font.Height); break;
            }

            var idx = 0;

            for (var l = 0; l < lines.Length; l++)
            {
                var lineWidth = Font.GetWidth(lines[l]);
                switch (Alignment & Alignment.Horizontal)
                {
                case Alignment.Left: x0 = 0; break;

                case Alignment.Center: x0 = -lineWidth / 2f; break;

                case Alignment.Right: x0 = -lineWidth; break;
                }

                var x = x0;
                var y = y0 + l * (Font.Height + LineSeparation);
                for (var i = 0; i < lines[l].Length; i++)
                {
                    if (Font.TryGetCharacter(lines[l][i], out FontCharacter c))
                    {
                        vertices[idx]     = transform * new GraphicsPoint(x + c.XOffset, y + c.YOffset);
                        vertices[idx + 1] = vertices[idx + 3] = transform * new GraphicsPoint(x + c.XOffset + c.Width, y + c.YOffset);
                        vertices[idx + 2] = vertices[idx + 4] = transform * new GraphicsPoint(x + c.XOffset, y + c.YOffset + c.Height);
                        vertices[idx + 5] = transform * new GraphicsPoint(x + c.XOffset + c.Width, y + c.YOffset + c.Height);
                        x += c.XAdvance;
                        if (i < lines[l].Length - 1)
                        {
                            x += Font.GetKerning(lines[l][i], lines[l][i + 1]);
                        }

                        texCoords[idx]     = new GraphicsPoint(tXScale * c.X, tYScale * c.Y);
                        texCoords[idx + 1] = texCoords[idx + 3] = new GraphicsPoint(tXScale * (c.X + c.Width), tYScale * c.Y);
                        texCoords[idx + 2] = texCoords[idx + 4] = new GraphicsPoint(tXScale * c.X, tYScale * (c.Y + c.Height));
                        texCoords[idx + 5] = new GraphicsPoint(tXScale * (c.X + c.Width), tYScale * (c.Y + c.Height));
                    }
                    idx += 6;
                }
            }

            return(vertices, texCoords);
        }