/// <summary> /// Submit a sprite for drawing in the current batch. /// </summary> /// <param name="texture">A texture.</param> /// <param name="position">The drawing location on screen.</param> /// <param name="sourceRectangle"> /// An optional region on the texture which will be rendered. /// If null; draws full texture. /// </param> /// <param name="color">A color mask.</param> /// <param name="rotation">A rotation of this sprite.</param> /// <param name="origin">Center of the rotation. 0,0 by default.</param> /// <param name="scale">A scaling of this sprite.</param> /// <param name="flip">Sprite mirroring flags. Can be combined.</param> /// <param name="sortDepth">A depth of the layer of this sprite.</param> public void Draw( Texture2D texture, Vector2 position, RectangleF?sourceRectangle, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteFlip flip, float sortDepth) { AssertValidArguments(texture, nameof(Draw)); origin *= scale; Vector4 texCoord; float w, h; if (sourceRectangle.HasValue) { RectangleF srcRect = sourceRectangle.GetValueOrDefault(); w = srcRect.Width * scale.X; h = srcRect.Height * scale.Y; texCoord = SpriteQuad.GetTexCoord(texture.TexelSize, srcRect); } else { w = texture.Width * scale.X; h = texture.Height * scale.Y; texCoord = new Vector4(0, 0, 1, 1); } SpriteQuad.FlipTexCoords(ref texCoord, flip); float sortKey = GetSortKey(texture, sortDepth); ref var quad = ref GetBatchQuad(texture, sortKey);
public static void FlipTexCoords(ref Vector4 texCoord, SpriteFlip flip) { if ((flip & SpriteFlip.Vertical) != 0) { var tmp = texCoord.W; texCoord.W = texCoord.Y; texCoord.Y = tmp; } if ((flip & SpriteFlip.Horizontal) != 0) { var tmp = texCoord.Z; texCoord.Z = texCoord.X; texCoord.X = tmp; } }
public override void OnInspectorGUI() { SpriteFlip script = (SpriteFlip)target; DrawDefaultInspector(); bool flip = (bool)EditorGUILayout.Toggle("Flip", script.flippedX); script.flip(flip); if (GUI.changed) { EditorUtility.SetDirty(script); serializedObject.ApplyModifiedProperties(); } }
public static void Draw( this SpriteBatch spriteBatch, TextureRegion2D textureRegion, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteFlip flip, float layerDepth, RectangleF?clippingRectangle = null) { if (textureRegion.Bounds.IsVisible(ref position, origin, scale, clippingRectangle, out RectangleF srcRect)) { //System.Console.WriteLine(srcRect); spriteBatch.Draw(textureRegion.Texture, position, srcRect, color, rotation, origin, scale, flip, layerDepth); } }
public void Draw( Texture2D texture, Vector2?position = null, RectangleF?destinationRectangle = null, RectangleF?sourceRectangle = null, Vector2?origin = null, float rotation = 0f, Vector2?scale = null, Color?color = null, SpriteFlip flip = SpriteFlip.None, float layerDepth = 0f) { // Assign default values to null parameters here, as they are not compile-time constants if (!color.HasValue) { color = Color.White; } if (!origin.HasValue) { origin = Vector2.Zero; } if (!scale.HasValue) { scale = Vector2.One; } // If both drawRectangle and position are null, // or if both have been assigned a value, raise an error if (destinationRectangle.HasValue == position.HasValue) { throw new ArgumentException( "Expected drawRectangle or position, but received neither or both."); } if (position.HasValue) // Call Draw() using position { Draw( texture, position.GetValueOrDefault(), sourceRectangle, color.GetValueOrDefault(), rotation, origin.GetValueOrDefault(), scale.GetValueOrDefault(), flip, layerDepth); } else // Call Draw() using drawRectangle { Draw( texture, destinationRectangle.GetValueOrDefault(), sourceRectangle, color.GetValueOrDefault(), rotation, origin.GetValueOrDefault(), flip, layerDepth); } }