Пример #1
0
        /// <summary>
        /// Draw a 3D sprite (or add it to the draw list depending on the sortMode).
        /// </summary>
        /// <param name="texture">The texture to use during the draw</param>
        /// <param name="worldMatrix">The world matrix of the element</param>
        /// <param name="sourceRectangle">The rectangle indicating the source region of the texture to use</param>
        /// <param name="elementSize">The size of the sprite in the object space</param>
        /// <param name="color">The color to apply to the texture image.</param>
        /// <param name="imageOrientation">The rotation to apply on the image uv</param>
        /// <param name="swizzle">Swizzle mode indicating the swizzle use when sampling the texture in the shader</param>
        /// <param name="depth">The depth of the element. If null, it is calculated using world and view-projection matrix.</param>
        public void Draw(Texture texture, ref Matrix worldMatrix, ref RectangleF sourceRectangle, ref Vector2 elementSize, ref Color4 color,
                         ImageOrientation imageOrientation = ImageOrientation.AsIs, SwizzleMode swizzle = SwizzleMode.None, float?depth = null)
        {
            // Check that texture is not null
            if (texture == null)
            {
                throw new ArgumentNullException("texture");
            }

            // Skip items with null size
            if (elementSize.LengthSquared() < MathUtil.ZeroTolerance)
            {
                return;
            }

            // Calculate the information needed to draw.
            var drawInfo = new Sprite3DDrawInfo
            {
                Source =
                {
                    X      = sourceRectangle.X / texture.ViewWidth,
                    Y      = sourceRectangle.Y / texture.ViewHeight,
                    Width  = sourceRectangle.Width / texture.ViewWidth,
                    Height = sourceRectangle.Height / texture.ViewHeight,
                },
                ColorScale = color,
                ColorAdd   = new Color4(0, 0, 0, 0),
                Swizzle    = swizzle,
            };

            var matrix = worldMatrix;

            matrix.M11 *= elementSize.X;
            matrix.M12 *= elementSize.X;
            matrix.M13 *= elementSize.X;
            matrix.M21 *= elementSize.Y;
            matrix.M22 *= elementSize.Y;
            matrix.M23 *= elementSize.Y;

            Vector4.Transform(ref vector4UnitX, ref matrix, out drawInfo.UnitXWorld);
            Vector4.Transform(ref vector4UnitY, ref matrix, out drawInfo.UnitYWorld);

            // rotate origin and unit axis if need.
            var leftTopCorner = new Vector4(-0.5f, 0.5f, 0, 1);

            if (imageOrientation == ImageOrientation.Rotated90)
            {
                var unitX = drawInfo.UnitXWorld;
                drawInfo.UnitXWorld = -drawInfo.UnitYWorld;
                drawInfo.UnitYWorld = unitX;
                leftTopCorner       = new Vector4(-0.5f, -0.5f, 0, 1);
            }
            Vector4.Transform(ref leftTopCorner, ref matrix, out drawInfo.LeftTopCornerWorld);

            float depthSprite;

            if (depth.HasValue)
            {
                depthSprite = depth.Value;
            }
            else
            {
                Vector4 projectedPosition;
                var     worldPosition = new Vector4(worldMatrix.TranslationVector, 1.0f);
                Vector4.Transform(ref worldPosition, ref transformationMatrix, out projectedPosition);
                depthSprite = projectedPosition.Z / projectedPosition.W;
            }

            var elementInfo = new ElementInfo(StaticQuadBufferInfo.VertexByElement, StaticQuadBufferInfo.IndicesByElement, in drawInfo, depthSprite);

            Draw(texture, in elementInfo);
        }
Пример #2
0
        /// <summary>
        /// Batch a new border image draw to the draw list.
        /// </summary>
        /// <param name="texture">The texture to use during the draw</param>
        /// <param name="worldMatrix">The world matrix of the element</param>
        /// <param name="sourceRectangle">The rectangle indicating the source region of the texture to use</param>
        /// <param name="elementSize">The size of the ui element</param>
        /// <param name="borderSize">The size of the borders in the texture in pixels (left/right/top/bottom)</param>
        /// <param name="color">The color to apply to the texture image.</param>
        /// <param name="depthBias">The depth bias of the ui element</param>
        /// <param name="imageOrientation">The rotation to apply on the image uv</param>
        /// <param name="swizzle">Swizzle mode indicating the swizzle use when sampling the texture in the shader</param>
        /// <param name="snapImage">Indicate if the image needs to be snapped or not</param>
        public void DrawImage(Texture texture, ref Matrix worldMatrix, ref RectangleF sourceRectangle, ref Vector3 elementSize, ref Vector4 borderSize,
                              ref Color color, int depthBias, ImageOrientation imageOrientation = ImageOrientation.AsIs, SwizzleMode swizzle = SwizzleMode.None, bool snapImage = false)
        {
            if (texture == null)
            {
                throw new ArgumentNullException(nameof(texture));
            }

            // Skip items with null size
            if (elementSize.Length() < MathUtil.ZeroTolerance)
            {
                return;
            }

            // Calculate the information needed to draw.
            var drawInfo = new UIImageDrawInfo
            {
                Source =
                {
                    X      = sourceRectangle.X / texture.ViewWidth,
                    Y      = sourceRectangle.Y / texture.ViewHeight,
                    Width  = sourceRectangle.Width / texture.ViewWidth,
                    Height = sourceRectangle.Height / texture.ViewHeight,
                },
                DepthBias  = depthBias,
                ColorScale = color,
                ColorAdd   = new Color(0, 0, 0, 0),
                Swizzle    = swizzle,
                SnapImage  = snapImage,
                Primitive  = borderSize == Vector4.Zero ? PrimitiveType.Rectangle : PrimitiveType.BorderRectangle,
                BorderSize = new Vector4(borderSize.X / sourceRectangle.Width, borderSize.Y / sourceRectangle.Height, borderSize.Z / sourceRectangle.Width, borderSize.W / sourceRectangle.Height),
            };

            var rotatedSize = imageOrientation == ImageOrientation.AsIs ? elementSize : new Vector3(elementSize.Y, elementSize.X, 0);

            drawInfo.VertexShift = new Vector4(borderSize.X / rotatedSize.X, borderSize.Y / rotatedSize.Y, 1f - borderSize.Z / rotatedSize.X, 1f - borderSize.W / rotatedSize.Y);

            var matrix = worldMatrix;

            matrix.M11 *= elementSize.X;
            matrix.M12 *= elementSize.X;
            matrix.M13 *= elementSize.X;
            matrix.M21 *= elementSize.Y;
            matrix.M22 *= elementSize.Y;
            matrix.M23 *= elementSize.Y;
            matrix.M31 *= elementSize.Z;
            matrix.M32 *= elementSize.Z;
            matrix.M33 *= elementSize.Z;

            Matrix worldViewProjection;

            Matrix.Multiply(ref matrix, ref viewProjectionMatrix, out worldViewProjection);
            drawInfo.UnitXWorld = worldViewProjection.Row1;
            drawInfo.UnitYWorld = worldViewProjection.Row2;

            // rotate origin and unit axis if need.
            var leftTopCorner = vector4LeftTop;

            if (imageOrientation == ImageOrientation.Rotated90)
            {
                var unitX = drawInfo.UnitXWorld;
                drawInfo.UnitXWorld = -drawInfo.UnitYWorld;
                drawInfo.UnitYWorld = unitX;
                leftTopCorner       = new Vector4(-0.5f, 0.5f, 0, 1);
            }
            Vector4.Transform(ref leftTopCorner, ref worldViewProjection, out drawInfo.LeftTopCornerWorld);

            var verticesPerElement = 4;
            var indicesPerElement  = 6;

            if (drawInfo.Primitive == PrimitiveType.BorderRectangle)
            {
                verticesPerElement = 16;
                indicesPerElement  = 54;
            }

            var elementInfo = new ElementInfo(verticesPerElement, indicesPerElement, ref drawInfo, depthBias);

            Draw(texture, ref elementInfo);
        }
Пример #3
0
        internal void DrawCharacter(Texture texture, ref Matrix worldViewProjectionMatrix, ref RectangleF sourceRectangle, ref Color color, int depthBias, SwizzleMode swizzle)
        {
            if (texture == null)
            {
                throw new ArgumentNullException(nameof(texture));
            }

            // Calculate the information needed to draw.
            var drawInfo = new UIImageDrawInfo
            {
                Source =
                {
                    X      = sourceRectangle.X / texture.ViewWidth,
                    Y      = sourceRectangle.Y / texture.ViewHeight,
                    Width  = sourceRectangle.Width / texture.ViewWidth,
                    Height = sourceRectangle.Height / texture.ViewHeight,
                },
                DepthBias          = depthBias,
                ColorScale         = color,
                ColorAdd           = new Color(0, 0, 0, 0),
                Swizzle            = swizzle,
                Primitive          = PrimitiveType.Rectangle,
                VertexShift        = Vector4.Zero,
                UnitXWorld         = worldViewProjectionMatrix.Row1,
                UnitYWorld         = worldViewProjectionMatrix.Row2,
                LeftTopCornerWorld = worldViewProjectionMatrix.Row4,
            };

            var elementInfo = new ElementInfo(4, 6, ref drawInfo, depthBias);

            Draw(texture, ref elementInfo);
        }
 /// <summary>
 /// Initializes a new <see cref="VertexPositionColorTexture"/> instance.
 /// </summary>
 /// <param name="position">The position of this vertex.</param>
 /// <param name="color">The color of this vertex.</param>
 /// <param name="textureCoordinate">UV texture coordinates.</param>
 /// <param name="swizzle">The swizzle mode</param>
 public VertexPositionColorTextureSwizzle(Vector4 position, Color color, Color colorAdd, Vector2 textureCoordinate, SwizzleMode swizzle)
     : this()
 {
     Position          = position;
     ColorScale        = color;
     ColorAdd          = colorAdd;
     TextureCoordinate = textureCoordinate;
     Swizzle           = (int)swizzle;
 }
Пример #5
0
        internal unsafe void DrawSprite(Texture texture, ref RectangleF destination, bool scaleDestination, ref RectangleF?sourceRectangle, Color4 color, Color4 colorAdd,
                                        float rotation, ref Vector2 origin, SpriteEffects effects, ImageOrientation orientation, float depth, SwizzleMode swizzle = SwizzleMode.None, bool realSize = false)
        {
            // Check that texture is not null
            if (texture == null)
            {
                throw new ArgumentNullException("texture");
            }

            // Put values in next ElementInfo
            var elementInfo = new ElementInfo();
            var spriteInfo  = &elementInfo.DrawInfo;

            float width;
            float height;

            // If the source rectangle has a value, then use it.
            if (sourceRectangle.HasValue)
            {
                var rectangle = sourceRectangle.Value;
                spriteInfo->Source.X = rectangle.X;
                spriteInfo->Source.Y = rectangle.Y;
                width  = rectangle.Width;
                height = rectangle.Height;
            }
            else
            {
                // Else, use directly the size of the texture
                spriteInfo->Source.X = 0.0f;
                spriteInfo->Source.Y = 0.0f;
                width  = texture.ViewWidth;
                height = texture.ViewHeight;
            }

            // Sets the width and height
            spriteInfo->Source.Width  = width;
            spriteInfo->Source.Height = height;

            // Scale the destination box
            if (scaleDestination)
            {
                if (orientation == ImageOrientation.Rotated90)
                {
                    destination.Width  *= height;
                    destination.Height *= width;
                }
                else
                {
                    destination.Width  *= width;
                    destination.Height *= height;
                }
            }

            // Sets the destination
            spriteInfo->Destination = destination;

            // Copy all other values.
            spriteInfo->Origin.X      = origin.X;
            spriteInfo->Origin.Y      = origin.Y;
            spriteInfo->Rotation      = rotation;
            spriteInfo->Depth         = depth;
            spriteInfo->SpriteEffects = effects;
            spriteInfo->ColorScale    = color;
            spriteInfo->ColorAdd      = colorAdd;
            spriteInfo->Swizzle       = swizzle;
            spriteInfo->TextureSize.X = texture.ViewWidth;
            spriteInfo->TextureSize.Y = texture.ViewHeight;
            spriteInfo->Orientation   = orientation;

            elementInfo.VertexCount = StaticQuadBufferInfo.VertexByElement;
            elementInfo.IndexCount  = StaticQuadBufferInfo.IndicesByElement;
            elementInfo.Depth       = depth;

            Draw(texture, ref elementInfo);
        }
Пример #6
0
        /// <summary>
        /// Adds a sprite to a batch of sprites for rendering using the specified texture, position, source rectangle, color, rotation, origin, scale, effects, and layer.
        /// </summary>
        /// <param name="texture">A texture.</param>
        /// <param name="position">The location (in screen coordinates) to draw the sprite.</param>
        /// <param name="sourceRectangle">A rectangle that specifies (in texels) the source texels from a texture. Use null to draw the entire texture. </param>
        /// <param name="color">The color to tint a sprite. Use Color4.White for full color with no tinting.</param>
        /// <param name="rotation">Specifies the angle (in radians) to rotate the sprite about its center.</param>
        /// <param name="origin">The sprite origin in the texture in pixels (dependent of image orientation). Default value is (0,0) which represents the upper-left corner.</param>
        /// <param name="scale">Scale factor.</param>
        /// <param name="effects">Effects to apply.</param>
        /// <param name="orientation">The source image orientation</param>
        /// <param name="layerDepth">The depth of a layer. By default, 0 represents the front layer and 1 represents a back layer. Use SpriteSortMode if you want sprites to be sorted during drawing.</param>
        public void Draw(Texture texture, Vector2 position, RectangleF?sourceRectangle, Color4 color, float rotation,
                         Vector2 origin, float scale = 1f, SpriteEffects effects = SpriteEffects.None, ImageOrientation orientation = ImageOrientation.AsIs, float layerDepth = 0, Color4 colorAdd = default(Color4), SwizzleMode swizzle = SwizzleMode.None)
        {
            var destination = new RectangleF(position.X, position.Y, scale, scale);

            DrawSprite(texture, ref destination, true, ref sourceRectangle, color, colorAdd, rotation, ref origin, effects, orientation, layerDepth, swizzle);
        }
Пример #7
0
 /// <summary>
 /// Adds a sprite to a batch of sprites for rendering using the specified texture, destination rectangle, source rectangle, color, rotation, origin, effects and layer.
 /// </summary>
 /// <param name="texture">A texture.</param>
 /// <param name="orientation">The source image orientation</param>
 /// <param name="destinationRectangle">A rectangle that specifies (in screen coordinates) the destination for drawing the sprite. If this rectangle is not the same size as the source rectangle, the sprite will be scaled to fit.</param>
 /// <param name="sourceRectangle">A rectangle that specifies (in texels) the source texels from a texture. Use null to draw the entire texture. </param>
 /// <param name="color">The color to tint a sprite. Use Color.White for full color with no tinting.</param>
 /// <param name="rotation">Specifies the angle (in radians) to rotate the sprite about its center.</param>
 /// <param name="origin">The sprite origin in the texture in pixels (dependent of image orientation). Default value is (0,0) which represents the upper-left corner.</param>
 /// <param name="effects">Effects to apply.</param>
 /// <param name="layerDepth">The depth of a layer. By default, 0 represents the front layer and 1 represents a back layer. Use SpriteSortMode if you want sprites to be sorted during drawing.</param>
 public void Draw(Texture texture, RectangleF destinationRectangle, RectangleF?sourceRectangle, Color4 color, float rotation, Vector2 origin,
                  SpriteEffects effects = SpriteEffects.None, ImageOrientation orientation = ImageOrientation.AsIs, float layerDepth = 0f, Color4 colorAdd = default(Color4), SwizzleMode swizzle = SwizzleMode.None)
 {
     DrawSprite(texture, ref destinationRectangle, false, ref sourceRectangle, color, colorAdd, rotation, ref origin, effects, orientation, layerDepth, swizzle);
 }
Пример #8
0
        /// <summary>
        /// Batch a new border image draw to the draw list.
        /// </summary>
        /// <param name="texture">The texture to use during the draw</param>
        /// <param name="texture1">An optional texture that can be used to substitute <paramref name="texture"/>'s alpha</param>
        /// <param name="worldMatrix">The world matrix of the element</param>
        /// <param name="sourceRectangle">The rectangle indicating the source region of the texture to use</param>
        /// <param name="elementSize">The size of the ui element</param>
        /// <param name="borderSize">The size of the borders in the texture in pixels (left/right/top/bottom)</param>
        /// <param name="color">The color to apply to the texture image.</param>
        /// <param name="depthBias">The depth bias of the ui element</param>
        /// <param name="imageOrientation">The rotation to apply on the image uv</param>
        /// <param name="swizzle">Swizzle mode indicating the swizzle use when sampling the texture in the shader</param>
        /// <param name="snapImage">Indicate if the image needs to be snapped or not</param>
        public void DrawImage(Texture texture, Texture texture1, ref Matrix worldMatrix, ref RectangleF sourceRectangle, ref Vector3 elementSize, ref Vector4 borderSize,
                              ref Color color, int depthBias, ImageOrientation imageOrientation = ImageOrientation.AsIs, SwizzleMode swizzle = SwizzleMode.None, bool snapImage = false)
        {
            // Check that texture is not null
            if (texture == null)
            {
                throw new ArgumentNullException("texture");
            }

            // Skip items with null size
            if (elementSize.Length() < MathUtil.ZeroTolerance)
            {
                return;
            }

            // End the current batching session and change the effect if required
            if (texture1 == null && separateAlphaEffectBinded || texture1 != null && !separateAlphaEffectBinded)
            {
                End();
                separateAlphaEffectBinded = !separateAlphaEffectBinded;
                Begin(separateAlphaEffectBinded? uiSeparateAlphaEffect: null, SortMode, BlendState, SamplerState, DepthStencilState, RasterizerState, StencilReferenceValue);
            }

            // Calculate the information needed to draw.
            var drawInfo = new UIImageDrawInfo
            {
                Source =
                {
                    X      = sourceRectangle.X / texture.ViewWidth,
                    Y      = sourceRectangle.Y / texture.ViewHeight,
                    Width  = sourceRectangle.Width / texture.ViewWidth,
                    Height = sourceRectangle.Height / texture.ViewHeight
                },
                DepthBias  = depthBias,
                Color      = color,
                Swizzle    = swizzle,
                SnapImage  = snapImage,
                Primitive  = borderSize == Vector4.Zero? PrimitiveType.Rectangle : PrimitiveType.BorderRectangle,
                BorderSize = new Vector4(borderSize.X / sourceRectangle.Width, borderSize.Y / sourceRectangle.Width, borderSize.Z / sourceRectangle.Height, borderSize.W / sourceRectangle.Height),
            };

            var rotatedSize = imageOrientation == ImageOrientation.AsIs? elementSize: new Vector3(elementSize.Y, elementSize.X, 0);

            drawInfo.VertexShift = new Vector4(borderSize.X / rotatedSize.X, 1f - borderSize.Y / rotatedSize.X, borderSize.Z / rotatedSize.Y, 1f - borderSize.W / rotatedSize.Y);

            var matrix = worldMatrix;

            matrix.M11 *= elementSize.X;
            matrix.M12 *= elementSize.X;
            matrix.M13 *= elementSize.X;
            matrix.M21 *= elementSize.Y;
            matrix.M22 *= elementSize.Y;
            matrix.M23 *= elementSize.Y;
            matrix.M31 *= elementSize.Z;
            matrix.M32 *= elementSize.Z;
            matrix.M33 *= elementSize.Z;

            Matrix worldViewProjection;

            Matrix.Multiply(ref matrix, ref viewProjectionMatrix, out worldViewProjection);
            Vector4.Transform(ref vector4UnitX, ref worldViewProjection, out drawInfo.UnitXWorld);
            Vector4.Transform(ref vector4UnitY, ref worldViewProjection, out drawInfo.UnitYWorld);

            // rotate origin and unit axis if need.
            var leftTopCorner = vector4LeftTop;

            if (imageOrientation == ImageOrientation.Rotated90)
            {
                var unitX = drawInfo.UnitXWorld;
                drawInfo.UnitXWorld = -drawInfo.UnitYWorld;
                drawInfo.UnitYWorld = unitX;
                leftTopCorner       = new Vector4(-0.5f, 0.5f, 0, 1);
            }
            Vector4.Transform(ref leftTopCorner, ref worldViewProjection, out drawInfo.LeftTopCornerWorld);

            var verticesPerElement = 4;
            var indicesPerElement  = 6;

            if (drawInfo.Primitive == PrimitiveType.BorderRectangle)
            {
                verticesPerElement = 16;
                indicesPerElement  = 54;
            }

            var elementInfo = new ElementInfo(verticesPerElement, indicesPerElement, ref drawInfo, depthBias);

            Draw(texture, texture1, ref elementInfo);
        }
Пример #9
0
        internal void DrawCharacter(Texture texture, ref Matrix worldViewProjectionMatrix, ref RectangleF sourceRectangle, ref Color color, int depthBias, SwizzleMode swizzle)
        {
            // Check that texture is not null
            if (texture == null)
            {
                throw new ArgumentNullException("texture");
            }

            // End the current batching session and change the effect if required
            if (separateAlphaEffectBinded)
            {
                End();
                separateAlphaEffectBinded = !separateAlphaEffectBinded;
                Begin(separateAlphaEffectBinded ? uiSeparateAlphaEffect : null, separateAlphaEffectBinded ? uiSeparateAlphaParameterCollectionGroup : null, SortMode, BlendState, SamplerState, DepthStencilState, RasterizerState, StencilReferenceValue);
            }

            // Calculate the information needed to draw.
            var drawInfo = new UIImageDrawInfo
            {
                Source =
                {
                    X      = sourceRectangle.X / texture.ViewWidth,
                    Y      = sourceRectangle.Y / texture.ViewHeight,
                    Width  = sourceRectangle.Width / texture.ViewWidth,
                    Height = sourceRectangle.Height / texture.ViewHeight
                },
                DepthBias          = depthBias,
                Color              = color,
                Swizzle            = swizzle,
                Primitive          = PrimitiveType.Rectangle,
                VertexShift        = Vector4.Zero,
                UnitXWorld         = worldViewProjectionMatrix.Row1,
                UnitYWorld         = worldViewProjectionMatrix.Row2,
                LeftTopCornerWorld = worldViewProjectionMatrix.Row4,
            };

            var elementInfo = new ElementInfo(4, 6, ref drawInfo, depthBias);

            Draw(texture, null, ref elementInfo);
        }