public void Draw(IBatchable batchable, RgbaFloat color, float x, float y, float width, float height, float originX, float originY, float rotation, float texX1, float texY1, float texX2, float texY2) { CheckForFlush(batchable.Texture); float lowerX = batchable.Area.Position.X + (texX1 * batchable.Area.End.X); float upperX = texX2 * batchable.Area.End.X; float lowerY = batchable.Area.Position.Y + (texY1 * batchable.Area.End.Y); float upperY = texY2 * batchable.Area.End.Y; float ox = originX * width; float oy = originY * height; var rotationMatrix = Matrix3x2.CreateRotation(rotation); var ld = rotationMatrix.MultiplyVector(new Vector2(-ox, -oy)); var lu = rotationMatrix.MultiplyVector(new Vector2(-ox, height - oy)); var ru = rotationMatrix.MultiplyVector(new Vector2(width - ox, -oy)); var rd = rotationMatrix.MultiplyVector(new Vector2(width - ox, height - oy)); var position = new Vector2(x, y); ld += position; lu += position; ru += position; rd += position; PushVertex(lu, lowerX, lowerY, color); PushVertex(rd, upperX, lowerY, color); PushVertex(ld, lowerX, upperY, color); PushVertex(ru, upperX, upperY, color); index++; }
public static bool IsBatched(this IBatchable target) { if (s_counters.TryGetValue(target, out BatchCount? value)) { return(value.Count != 0); } else { return(false); } }
public static void BatchBegin(this IBatchable target) { if (s_counters.TryGetValue(target, out BatchCount? value)) { value.Count++; } else { s_counters.Add(target, new BatchCount()); } }
public void Draw(IBatchable obj, ref Matrix4 world) { if (_vbuffer == null) { _vbuffer = new VertexBuffer(obj.VertexBuffer.Format); } else if (_vbuffer.Format.Stride != obj.VertexBuffer.Format.Stride) { throw new InvalidOperationException("Vertex format mismatch."); } if (_ibuffer == null) { _ibuffer = new IndexBuffer(); } if (_material == null) { _material = obj.Material; } else if (obj.Material != _material) { throw new InvalidOperationException("Material mismatch."); } var stride = obj.VertexBuffer.Format.Stride; var vsrc = obj.VertexBuffer.Data; var isrc = obj.IndexBuffer.Data; var vdst = _vbuffer.Data = ExpandArray(_vbuffer.Data, _vidx + obj.IndexCount * stride); var idst = _ibuffer.Data = ExpandArray(_ibuffer.Data, _iidx + obj.IndexCount); for (var i = obj.IndexOffset; i < obj.IndexOffset + obj.IndexCount; i++) { idst[_iidx] = _iidx; _iidx++; for (var j = 0; j < stride; j++) { vdst[_vidx + j] = vsrc[isrc[i] * stride + j]; } foreach (var el in _vbuffer.Format.Elements) { if (el.Name == "Position") { var j = el.Offset; var pos = new Vector3(vdst[_vidx + j], vdst[_vidx + j + 1], vdst[_vidx + j + 2]); Vector3.Transform(ref pos, ref world, out pos); vdst[_vidx + j] = pos.X; vdst[_vidx + j + 1] = pos.Y; vdst[_vidx + j + 2] = pos.Z; } } _vidx += stride; } }
public static bool IsBatched(this IBatchable target) { BatchCount value = null; if (s_counters.TryGetValue(target, out value)) { return value.Count != 0; } else { return false; } }
public void Draw(IBatchable batchable, RgbaFloat color, float x, float y, float width, float height, float texX1 = 0f, float texY1 = 0f, float texX2 = 1f, float texY2 = 1f) { CheckForFlush(batchable.Texture); float w = width; float h = height; float lowerX = batchable.Area.Position.X + (texX1 * batchable.Area.End.X); float upperX = texX2 * batchable.Area.End.X; float lowerY = batchable.Area.Position.Y + (texY1 * batchable.Area.End.Y); float upperY = texY2 * batchable.Area.End.Y; PushVertex(x, y + h, lowerX, lowerY, color); PushVertex(x + w, y + h, upperX, lowerY, color); PushVertex(x, y, lowerX, upperY, color); PushVertex(x + w, y, upperX, upperY, color); index++; }
public static void BatchCommit(this IBatchable target) { if (s_counters.TryGetValue(target, out BatchCount? value)) { value.Count--; if (value.Count == 0) { target.OnBatchCommitted(); } else if (value.Count < 0) { Log.Error("Called BatchCommit() without BatchBegin()."); value.Count = 0; } } else { Log.Error("Called BatchCommit() without BatchBegin()."); } }
public Sprite(IBatchable texture, Vector2 position, float rotation, Vector2 scale) : this(texture) { this.position = position; this.rotation = rotation; this.scale = scale; }
public Sprite(IBatchable tex) { Texture = tex; }
public UpdateBatch(IBatchable batchable) { _batchable = batchable; }
/// <summary> /// SFML's Drawable.Draw implementation. Processes the currently loaded Graphics into the RenderTarget, then clears itself out. /// </summary> /// <param name="target">A RenderTarget to draw to</param> /// <param name="states">The RenderStates that the SpriteBatch will attempt to return to as it draws</param> public void Draw(RenderTarget target, RenderStates states, bool clearGraphicList = true) { RenderStates currentState = new RenderStates(states); List <Vertex> drawing = new List <Vertex>(this.graphicList.Count * 6); PrimitiveType drawingType = PrimitiveType.Triangles; foreach (var graphic in graphicList) { IBatchable batchable = graphic as IBatchable; if (batchable == null) { Debug.DrawCalls++; target.Draw(drawing.ToArray(), drawingType, currentState); drawing.Clear(); Debug.DrawCalls++; graphic.Draw(target, states); continue; } var drawBatch = false; RenderStates?batchableState = batchable.BatchRenderStates; if (batchableState == null) { batchableState = currentState; } if (batchable.BatchTexture != currentState.Texture) { drawBatch = true; } if (batchableState.Value.BlendMode != currentState.BlendMode) { drawBatch = true; } if (batchableState.Value.Shader != currentState.Shader) { drawBatch = true; } if (batchable.BatchPrimitiveType != drawingType) { drawBatch = true; } if (drawBatch) { Debug.DrawCalls++; target.Draw(drawing.ToArray(), drawingType, currentState); drawing.Clear(); if (batchable.BatchRenderStates != null) { currentState.BlendMode = batchableState.Value.BlendMode; currentState.Shader = batchableState.Value.Shader; } currentState.Texture = batchable.BatchTexture; drawingType = batchable.BatchPrimitiveType; } drawing.AddRange(batchable.BatchVertexes); } Debug.DrawCalls++; target.Draw(drawing.ToArray(), drawingType, currentState); if (clearGraphicList) { this.graphicList.Clear(); } }
public void Draw(IBatchable obj, Matrix4 world) { this.Draw(obj, ref world); }
public void Draw(IBatchable batchable, float x, float y, float width, float height, float originX, float originY, float rotation) { Draw(batchable, RgbaFloat.White, x, y, width, height, originX, originY, rotation, 0f, 0f, 1f, 1f); }