private static IEnumerable <VertexPositionTexture> CreateVertices(Texture2D texture, Vector2 position, Rectangle sourceRectangle, TiledMapTileFlipFlags flags = TiledMapTileFlipFlags.None) { var reciprocalWidth = 1f / texture.Width; var reciprocalHeight = 1f / texture.Height; var texelLeft = (sourceRectangle.X + 0.5f) * reciprocalWidth; var texelTop = (sourceRectangle.Y + 0.5f) * reciprocalHeight; var texelRight = (sourceRectangle.X + sourceRectangle.Width) * reciprocalWidth; var texelBottom = (sourceRectangle.Y + sourceRectangle.Height) * reciprocalHeight; VertexPositionTexture vertexTopLeft, vertexTopRight, vertexBottomLeft, vertexBottomRight; vertexTopLeft.Position = new Vector3(position, 0); vertexTopRight.Position = new Vector3(position + new Vector2(sourceRectangle.Width, 0), 0); vertexBottomLeft.Position = new Vector3(position + new Vector2(0, sourceRectangle.Height), 0); vertexBottomRight.Position = new Vector3(position + new Vector2(sourceRectangle.Width, sourceRectangle.Height), 0); vertexTopLeft.TextureCoordinate.Y = texelTop; vertexTopLeft.TextureCoordinate.X = texelLeft; vertexTopRight.TextureCoordinate.Y = texelTop; vertexTopRight.TextureCoordinate.X = texelRight; vertexBottomLeft.TextureCoordinate.Y = texelBottom; vertexBottomLeft.TextureCoordinate.X = texelLeft; vertexBottomRight.TextureCoordinate.Y = texelBottom; vertexBottomRight.TextureCoordinate.X = texelRight; var flipDiagonally = (flags & TiledMapTileFlipFlags.FlipDiagonally) != 0; var flipHorizontally = (flags & TiledMapTileFlipFlags.FlipHorizontally) != 0; var flipVertically = (flags & TiledMapTileFlipFlags.FlipVertically) != 0; if (flipDiagonally) { FloatHelper.Swap(ref vertexTopRight.TextureCoordinate.X, ref vertexBottomLeft.TextureCoordinate.X); FloatHelper.Swap(ref vertexTopRight.TextureCoordinate.Y, ref vertexBottomLeft.TextureCoordinate.Y); } if (flipHorizontally) { if (flipDiagonally) { FloatHelper.Swap(ref vertexTopLeft.TextureCoordinate.Y, ref vertexTopRight.TextureCoordinate.Y); FloatHelper.Swap(ref vertexBottomLeft.TextureCoordinate.Y, ref vertexBottomRight.TextureCoordinate.Y); } else { FloatHelper.Swap(ref vertexTopLeft.TextureCoordinate.X, ref vertexTopRight.TextureCoordinate.X); FloatHelper.Swap(ref vertexBottomLeft.TextureCoordinate.X, ref vertexBottomRight.TextureCoordinate.X); } } if (flipVertically) { if (flipDiagonally) { FloatHelper.Swap(ref vertexTopLeft.TextureCoordinate.X, ref vertexBottomLeft.TextureCoordinate.X); FloatHelper.Swap(ref vertexTopRight.TextureCoordinate.X, ref vertexBottomRight.TextureCoordinate.X); } else { FloatHelper.Swap(ref vertexTopLeft.TextureCoordinate.Y, ref vertexBottomLeft.TextureCoordinate.Y); FloatHelper.Swap(ref vertexTopRight.TextureCoordinate.Y, ref vertexBottomRight.TextureCoordinate.Y); } } yield return(vertexTopLeft); yield return(vertexTopRight); yield return(vertexBottomLeft); yield return(vertexBottomRight); }
public void AddTileVertices(Point2 position, Rectangle?sourceRectangle = null, TiledMapTileFlipFlags flags = TiledMapTileFlipFlags.None) { float texelLeft, texelTop, texelRight, texelBottom; var sourceRectangle1 = sourceRectangle ?? new Rectangle(0, 0, (int)ImageSize.Width, (int)ImageSize.Height); if (sourceRectangle.HasValue) { var reciprocalWidth = 1f / ImageSize.Width; var reciprocalHeight = 1f / ImageSize.Height; texelLeft = (sourceRectangle1.X + 0.5f) * reciprocalWidth; texelTop = (sourceRectangle1.Y + 0.5f) * reciprocalHeight; texelRight = (sourceRectangle1.X + sourceRectangle1.Width) * reciprocalWidth; texelBottom = (sourceRectangle1.Y + sourceRectangle1.Height) * reciprocalHeight; } else { texelLeft = 0; texelTop = 0; texelBottom = 1; texelRight = 1; } VertexPositionTexture vertexTopLeft, vertexTopRight, vertexBottomLeft, vertexBottomRight; vertexTopLeft.Position = new Vector3(position, 0); vertexTopRight.Position = new Vector3(position + new Vector2(sourceRectangle1.Width, 0), 0); vertexBottomLeft.Position = new Vector3(position + new Vector2(0, sourceRectangle1.Height), 0); vertexBottomRight.Position = new Vector3(position + new Vector2(sourceRectangle1.Width, sourceRectangle1.Height), 0); vertexTopLeft.TextureCoordinate.Y = texelTop; vertexTopLeft.TextureCoordinate.X = texelLeft; vertexTopRight.TextureCoordinate.Y = texelTop; vertexTopRight.TextureCoordinate.X = texelRight; vertexBottomLeft.TextureCoordinate.Y = texelBottom; vertexBottomLeft.TextureCoordinate.X = texelLeft; vertexBottomRight.TextureCoordinate.Y = texelBottom; vertexBottomRight.TextureCoordinate.X = texelRight; var flipDiagonally = (flags & TiledMapTileFlipFlags.FlipDiagonally) != 0; var flipHorizontally = (flags & TiledMapTileFlipFlags.FlipHorizontally) != 0; var flipVertically = (flags & TiledMapTileFlipFlags.FlipVertically) != 0; if (flipDiagonally) { FloatHelper.Swap(ref vertexTopRight.TextureCoordinate.X, ref vertexBottomLeft.TextureCoordinate.X); FloatHelper.Swap(ref vertexTopRight.TextureCoordinate.Y, ref vertexBottomLeft.TextureCoordinate.Y); } if (flipHorizontally) { if (flipDiagonally) { FloatHelper.Swap(ref vertexTopLeft.TextureCoordinate.Y, ref vertexTopRight.TextureCoordinate.Y); FloatHelper.Swap(ref vertexBottomLeft.TextureCoordinate.Y, ref vertexBottomRight.TextureCoordinate.Y); } else { FloatHelper.Swap(ref vertexTopLeft.TextureCoordinate.X, ref vertexTopRight.TextureCoordinate.X); FloatHelper.Swap(ref vertexBottomLeft.TextureCoordinate.X, ref vertexBottomRight.TextureCoordinate.X); } } if (flipVertically) { if (flipDiagonally) { FloatHelper.Swap(ref vertexTopLeft.TextureCoordinate.X, ref vertexBottomLeft.TextureCoordinate.X); FloatHelper.Swap(ref vertexTopRight.TextureCoordinate.X, ref vertexBottomRight.TextureCoordinate.X); } else { FloatHelper.Swap(ref vertexTopLeft.TextureCoordinate.Y, ref vertexBottomLeft.TextureCoordinate.Y); FloatHelper.Swap(ref vertexTopRight.TextureCoordinate.Y, ref vertexBottomRight.TextureCoordinate.Y); } } _vertices.Add(vertexTopLeft); _vertices.Add(vertexTopRight); _vertices.Add(vertexBottomLeft); _vertices.Add(vertexBottomRight); Debug.Assert(Vertices.Count <= TiledMapHelper.MaximumVerticesPerModel); }
public void BuildSprite(int indexOffset, ref Matrix2 transformMatrix, Texture2D texture, ref Rectangle sourceRectangle, Color?color = null, FlipFlags flags = FlipFlags.None, float depth = 0) { if (texture == null) { throw new ArgumentNullException(nameof(texture)); } var texelLeft = 0f; var texelTop = 0f; var texelRight = 1f; var texelBottom = 1f; if (sourceRectangle.Width > 0) { texelLeft = (float)sourceRectangle.X / texture.Width; texelTop = (float)sourceRectangle.Y / texture.Height; texelRight = (sourceRectangle.X + sourceRectangle.Width) / (float)texture.Width; texelBottom = (sourceRectangle.Y + sourceRectangle.Height) / (float)texture.Height; } else { sourceRectangle.Width = texture.Width; sourceRectangle.Height = texture.Height; } var color1 = color ?? Color.White; var vertices = Vertices; transformMatrix.Transform(0, 0, ref vertices[0].Position); vertices[0].Position.Z = depth; vertices[0].Color = color1; vertices[0].TextureCoordinate.X = texelLeft; vertices[0].TextureCoordinate.Y = texelTop; transformMatrix.Transform(sourceRectangle.Width, 0, ref vertices[1].Position); vertices[1].Position.Z = depth; vertices[1].Color = color1; vertices[1].TextureCoordinate.X = texelRight; vertices[1].TextureCoordinate.Y = texelTop; transformMatrix.Transform(0, sourceRectangle.Height, ref vertices[2].Position); vertices[2].Position.Z = depth; vertices[2].Color = color1; vertices[2].TextureCoordinate.X = texelLeft; vertices[2].TextureCoordinate.Y = texelBottom; transformMatrix.Transform(sourceRectangle.Width, sourceRectangle.Height, ref vertices[3].Position); vertices[3].Position.Z = depth; vertices[3].Color = color1; vertices[3].TextureCoordinate.X = texelRight; vertices[3].TextureCoordinate.Y = texelBottom; var flipDiagonally = (flags & FlipFlags.FlipDiagonally) != 0; var flipHorizontally = (flags & FlipFlags.FlipHorizontally) != 0; var flipVertically = (flags & FlipFlags.FlipVertically) != 0; if (flipDiagonally) { FloatHelper.Swap(ref vertices[1].TextureCoordinate.X, ref vertices[2].TextureCoordinate.X); FloatHelper.Swap(ref vertices[1].TextureCoordinate.Y, ref vertices[2].TextureCoordinate.Y); } if (flipHorizontally) { if (flipDiagonally) { FloatHelper.Swap(ref vertices[0].TextureCoordinate.Y, ref vertices[1].TextureCoordinate.Y); FloatHelper.Swap(ref vertices[2].TextureCoordinate.Y, ref vertices[3].TextureCoordinate.Y); } else { FloatHelper.Swap(ref vertices[0].TextureCoordinate.X, ref vertices[1].TextureCoordinate.X); FloatHelper.Swap(ref vertices[2].TextureCoordinate.X, ref vertices[3].TextureCoordinate.X); } } if (flipVertically) { if (flipDiagonally) { FloatHelper.Swap(ref vertices[0].TextureCoordinate.X, ref vertices[2].TextureCoordinate.X); FloatHelper.Swap(ref vertices[1].TextureCoordinate.X, ref vertices[3].TextureCoordinate.X); } else { FloatHelper.Swap(ref vertices[0].TextureCoordinate.Y, ref vertices[2].TextureCoordinate.Y); FloatHelper.Swap(ref vertices[1].TextureCoordinate.Y, ref vertices[3].TextureCoordinate.Y); } } VertexCount = 4; AddQuadrilateralIndices(indexOffset); IndexCount = 6; PrimitivesCount = 2; }