private void DrawFrame(Context context, Sequence.Sprite frame) { var drawContext = new SpriteDrawingContext() .SpriteTexture(surface) .SourceLTRB(frame.Left, frame.Top, frame.Right, frame.Bottom) .Position(context.Left, context.Top) .DestinationSize(context.Right - context.Left, context.Bottom - context.Top) .Traslate(context.PivotX, context.PivotY) .ScaleSize(context.ScaleX, context.ScaleY) .RotateX(-context.RotationX) .RotateY(-context.RotationY) .RotateZ(-context.RotationZ) .Traslate(context.PositionX, context.PositionY); drawContext.Color0 = ConvertColor(frame.ColorLeft); drawContext.Color1 = ConvertColor(frame.ColorTop); drawContext.Color2 = ConvertColor(frame.ColorRight); drawContext.Color3 = ConvertColor(frame.ColorBottom); drawContext.ColorMultiply(context.Color); drawContext.BlendMode = (BlendMode)context.ColorBlendMode; if (frame.UTranslation != 0) // HACK to increase performance { drawContext.TextureWrapHorizontal(TextureWrapMode.Repeat, Math.Min(frame.Left, frame.Right), Math.Max(frame.Left, frame.Right)); drawContext.TextureHorizontalShift = frame.UTranslation * context.GlobalFrameIndex; } if (frame.VTranslation != 0) // HACK to increase performance { drawContext.TextureWrapVertical(TextureWrapMode.Repeat, Math.Min(frame.Top, frame.Bottom), Math.Max(frame.Top, frame.Bottom)); drawContext.TextureVerticalShift = frame.VTranslation * context.GlobalFrameIndex; } drawing.AppendSprite(drawContext); }
public void AppendSprite(SpriteDrawingContext context) { SetTextureToDraw(context.SpriteTexture ?? _defaultTexture); var width = _currentTexture.Width; var height = _currentTexture.Height; var viewport = _viewportSize; var index = RequestVertices(4); var buffer = _dataBuffer; buffer[index++] = new Vertex() { X = context.Vec0.X / viewport.Width * +2.0f - 1.0f, Y = context.Vec0.Y / viewport.Height * -2.0f + 1.0f, U = (float)context.SourceLeft / width, V = (float)context.SourceTop / height, Color = context.Color0 }; buffer[index++] = new Vertex() { X = context.Vec1.X / viewport.Width * +2.0f - 1.0f, Y = context.Vec1.Y / viewport.Height * -2.0f + 1.0f, U = (float)context.SourceRight / width, V = (float)context.SourceTop / height, Color = context.Color1 }; buffer[index++] = new Vertex() { X = context.Vec2.X / viewport.Width * +2.0f - 1.0f, Y = context.Vec2.Y / viewport.Height * -2.0f + 1.0f, U = (float)context.SourceLeft / width, V = (float)context.SourceBottom / height, Color = context.Color2 }; buffer[index++] = new Vertex() { X = context.Vec3.X / viewport.Width * +2.0f - 1.0f, Y = context.Vec3.Y / viewport.Height * -2.0f + 1.0f, U = (float)context.SourceRight / width, V = (float)context.SourceBottom / height, Color = context.Color3 }; }
private void DrawCropAtlasTexture() { var sLeft = SpriteModel.Sprite.Left; var sTop = SpriteModel.Sprite.Top; var sRight = SpriteModel.Sprite.Right; var sBottom = SpriteModel.Sprite.Bottom; var cropColor = _settings.EditorBackground; cropColor.A = 0.75f; var invertedCropColor = new ColorF( 1f - cropColor.R, 1f - cropColor.G, 1f - cropColor.G, 1.0f); var context = new SpriteDrawingContext() .SpriteTexture(_atlasTexture) .ColorDefault() .SourceLTRB(0, 0, _cropAtlasTexture.Width, _cropAtlasTexture.Height) .Position(0, 0) .DestinationSize(_cropAtlasTexture.Width, _cropAtlasTexture.Height); context.TextureWrapHorizontal(TextureWrapMode.Repeat, 0, _cropAtlasTexture.Width); context.TextureWrapVertical(TextureWrapMode.Repeat, 0, _cropAtlasTexture.Height); _spriteDrawing.DestinationTexture = _cropAtlasTexture; _spriteDrawing.SetViewport(0, _cropAtlasTexture.Width, 0, _cropAtlasTexture.Height); _spriteDrawing.Clear(_settings.EditorBackground); _spriteDrawing.AppendSprite(context); _spriteDrawing.FillRectangle(0, 0, _cropAtlasTexture.Width, sTop, cropColor); _spriteDrawing.FillRectangle(0, sTop, sLeft, sBottom - sTop, cropColor); _spriteDrawing.FillRectangle(sRight, sTop, _cropAtlasTexture.Width, sBottom - sTop, cropColor); _spriteDrawing.FillRectangle(0, sBottom, _cropAtlasTexture.Width, _cropAtlasTexture.Height, cropColor); _spriteDrawing.DrawRectangle(sLeft - 1, sTop - 1, sRight - sLeft + 2, sBottom - sTop + 2, invertedCropColor); _spriteDrawing.Flush(); _spriteDrawing.DestinationTexture = null; }
public void AppendSprite(SpriteDrawingContext context) { switch (context.BlendMode) { case BlendMode.Default: SetBlendState(BlendStateDefault); break; case BlendMode.Add: SetBlendState(BlendStateAdditive); break; case BlendMode.Subtract: SetBlendState(BlendStateDifference); break; default: SetBlendState(BlendStateDefault); break; } var texture = (context.SpriteTexture as CSpriteTexture)?.Texture; var tw = 1.0f / texture.Width; var th = 1.0f / texture.Height; var textureRegionU = new Vector2(context.TextureRegionLeft * tw, context.TextureRegionRight * tw); var textureRegionV = new Vector2(context.TextureRegionTop * th, context.TextureRegionBottom * th); if (context.TextureWrapU == TextureWrapMode.Default) { textureRegionU = new Vector2(0, 1); } if (context.TextureWrapV == TextureWrapMode.Default) { textureRegionV = new Vector2(0, 1); } if (_textureRegionU != textureRegionU || _textureRegionV != textureRegionV || _textureWrapU != context.TextureWrapU || _textureWrapV != context.TextureWrapV) { Flush(); } var vertexIndex = PrepareVertices(texture); _textureRegionU = textureRegionU; _textureRegionV = textureRegionV; _textureWrapU = context.TextureWrapU; _textureWrapV = context.TextureWrapV; _vertices[vertexIndex + 0].Position = new Vector3(context.Vec0.X, context.Vec0.Y, 0.0f); _vertices[vertexIndex + 0].Color = context.Color0; _vertices[vertexIndex + 0].TextureCoordinate = new Vector2(context.SourceLeft * tw + context.TextureHorizontalShift, context.SourceTop * th + context.TextureVerticalShift); _vertices[vertexIndex + 1].Position = new Vector3(context.Vec1.X, context.Vec1.Y, 0.0f); _vertices[vertexIndex + 1].Color = context.Color1; _vertices[vertexIndex + 1].TextureCoordinate = new Vector2(context.SourceRight * tw + context.TextureHorizontalShift, context.SourceTop * th + context.TextureVerticalShift); _vertices[vertexIndex + 2].Position = new Vector3(context.Vec2.X, context.Vec2.Y, 0.0f); _vertices[vertexIndex + 2].Color = context.Color2; _vertices[vertexIndex + 2].TextureCoordinate = new Vector2(context.SourceLeft * tw + context.TextureHorizontalShift, context.SourceBottom * th + context.TextureVerticalShift); _vertices[vertexIndex + 3].Position = new Vector3(context.Vec3.X, context.Vec3.Y, 0.0f); _vertices[vertexIndex + 3].Color = context.Color3; _vertices[vertexIndex + 3].TextureCoordinate = new Vector2(context.SourceRight * tw + context.TextureHorizontalShift, context.SourceBottom * th + context.TextureVerticalShift); PushVertices(); }