public Quad(Material material, Vector4 rect, Vector4 color, bool flipV = false) { _material = material; _color = color; _vbuffer = new VertexBuffer(VertexFormat.PositionColorUV); _ibuffer = new IndexBuffer(); if (rect == Vector4.Zero) { rect = new Vector4(-0.5f, -0.5f, 0.5f, 0.5f); } _vbuffer.Data = new [] { rect.X, rect.Y, 0f, _color.X, _color.Y, _color.Z, _color.W, 0f, flipV ? 1f : 0f, rect.Z, rect.Y, 0f, _color.X, _color.Y, _color.Z, _color.W, 1f, flipV ? 1f : 0f, rect.Z, rect.W, 0f, _color.X, _color.Y, _color.Z, _color.W, 1f, flipV ? 0f : 1f, rect.X, rect.W, 0f, _color.X, _color.Y, _color.Z, _color.W, 0f, flipV ? 0f : 1f }; _vbuffer.Commit(); _ibuffer.Data = new [] { 0, 1, 2, 2, 3, 0 }; _ibuffer.Commit(); }
public Sprite(Stream stream, TextureSettings settings, Vector4 rect, bool relativeRect, Vector4 color, bool flipV = true) { var tex = new Texture(stream, settings); _material = new SpriteMaterial(new SpriteShader(), tex); if (rect == Vector4.Zero) { rect = new Vector4(0, 0, tex.Size.Width, tex.Size.Height); } else if (relativeRect) { rect.X *= tex.Size.Width; rect.Y *= tex.Size.Height; rect.Z *= tex.Size.Width; rect.W *= tex.Size.Height; } _size = new Vector2(rect.Z - rect.X, rect.W - rect.Y); _vbuffer = new VertexBuffer(VertexFormat.PositionColorUV); _vbuffer.Data = new [] { rect.X, rect.Y, 0f, color.X, color.Y, color.Z, color.W, 0f, flipV ? 1f : 0f, rect.Z, rect.Y, 0f, color.X, color.Y, color.Z, color.W, 1f, flipV ? 1f : 0f, rect.Z, rect.W, 0f, color.X, color.Y, color.Z, color.W, 1f, flipV ? 0f : 1f, rect.X, rect.W, 0f, color.X, color.Y, color.Z, color.W, 0f, flipV ? 0f : 1f }; _vbuffer.Commit(); _ibuffer = new IndexBuffer(); _ibuffer.Data = new [] { 0, 1, 2, 2, 3, 0 }; _ibuffer.Commit(); _ioffset = 0; _icount = 6; _ownResources = true; }
protected override void OnEnd() { var cam = ScopedObject.Find <Camera>(); if (cam == null) { throw new InvalidOperationException("There is no active camera."); } if (_vbuffer == null || _ibuffer == null) { return; } _vbuffer.Commit(); _ibuffer.Commit(); using (_material.Begin()) { cam.Apply(ref Matrix4.Identity); using (_vbuffer.Begin()) { _ibuffer.Draw(0, _iidx); } } }
void Initialize(Stream stream, Shader shader = null) { ThreadContext.Current.EnsureGLContext(); if (shader == null) { shader = new SpriteShader(); } int filterMode = 0; var defs = new Dictionary <string, SpriteDefinition>(); var tr = new TarReader(stream); while (tr.MoveNext(false)) { switch (tr.FileInfo.FileName) { case "atlas.bin": using (var atlasStream = new MemoryStream()) { tr.Read(atlasStream); atlasStream.Position = 0; using (var br = new BinaryReader(atlasStream)) { filterMode = br.ReadInt32(); for (var count = br.ReadInt32(); count > 0; --count) { var key = br.ReadString(); var value = SpriteDefinition.Read(br); defs.Add(key, value); } } } break; case "sheet.png": using (var sheetStream = new MemoryStream((int)tr.FileInfo.SizeInBytes)) { tr.Read(sheetStream); sheetStream.Position = 0; var tex = new Texture(sheetStream, new TextureSettings { MagFilter = filterMode > 0 ? TextureFilter.Linear : TextureFilter.Nearest, MinFilter = filterMode == 1 ? TextureFilter.Linear : filterMode == 2 ? TextureFilter.Trilinear : TextureFilter.Nearest, }); _material = new SpriteMaterial(shader, tex); } break; default: throw new ContentException("Unrecognized atlas file " + tr.FileInfo.FileName); } } if (defs == null) { throw new ContentException("Missing atlas file."); } if (_material == null) { throw new ContentException("Missing image file."); } _sprites = new Dictionary <string, Sprite>(); _vbuffer = new VertexBuffer(VertexFormat.PositionColorUV); _ibuffer = new IndexBuffer(); var vstride = _vbuffer.Format.Stride * 4; int vOffset = 0, iOffset = 0, vcount = 0; var vertices = new float[vstride * defs.Count]; var indices = new int[6 * defs.Count]; var tsz = new Vector2(_material.Texture.Size.Width, _material.Texture.Size.Height); foreach (var kvp in defs) { var def = kvp.Value; var pos = def.Position; var sz = def.Size; var orig = def.Origin; var color = ParseColor(def.Color); orig.X *= sz.X; orig.Y *= sz.Y; if (def.Border != Vector4.Zero) { _sprites.Add(kvp.Key, new SlicedSprite(_material, pos, sz, orig, color, def.Border, def.TileX, def.TileY, def.Hollow)); continue; } var uv = new Vector4( pos.X / tsz.X, ((pos.Y + sz.Y)) / tsz.Y, (pos.X + sz.X) / tsz.X, (pos.Y) / tsz.Y ); Array.Copy(new[] { -orig.X, -orig.Y, 0f, color.X, color.Y, color.Z, color.W, uv.X, uv.Y, sz.X - orig.X, -orig.Y, 0f, color.X, color.Y, color.Z, color.W, uv.Z, uv.Y, sz.X - orig.X, sz.Y - orig.Y, 0f, color.X, color.Y, color.Z, color.W, uv.Z, uv.W, -orig.X, sz.Y - orig.Y, 0f, color.X, color.Y, color.Z, color.W, uv.X, uv.W }, 0, vertices, vOffset, vstride); Array.Copy(new[] { vcount + 0, vcount + 1, vcount + 2, vcount + 2, vcount + 3, vcount + 0 }, 0, indices, iOffset, 6); var sprite = new Sprite(_material, sz, _vbuffer, _ibuffer, iOffset, 6); _sprites.Add(kvp.Key, sprite); vOffset += vstride; iOffset += 6; vcount += 4; } _vbuffer.Data = vertices; _vbuffer.Commit(); _ibuffer.Data = indices; _ibuffer.Commit(); }
public void Build() { if (!_isDirty || _text == null || _font == null) { return; } if (_vbuffer == null) { _vbuffer = new VertexBuffer(VertexFormat.PositionColorUV); } if (_ibuffer == null) { _ibuffer = new IndexBuffer(); } var i = 0; TextRun run = null; var lines = new List <List <TextRun> >(); var line = new List <TextRun>(); //var nestedRuns = new Stack<TextRun>(); //var nestedTags = new Stack<string>(); lines.Add(line); run = new TextRun(_font, i, _kerning, _color); line.Add(run); var trim = false; while (i < _text.Length) { if (char.IsLowSurrogate(_text, i)) { i++; continue; } if (trim) { if (char.IsWhiteSpace(_text, i)) { i++; continue; } else { trim = false; } } var c = char.ConvertToUtf32(_text, i++); if (c == '\n') { run.End = i; line = new List <TextRun>(); lines.Add(line); run = new TextRun(run, i); line.Add(run); continue; } else if (c < 32) { continue; } var ch = run.Font[c]; if (ch == null) { ch = run.Font['?']; if (ch == null) { ch = run.Font[' ']; if (ch == null) { continue; } } } run.Push(ch); if (_width > 0f && line.Sum(tr => tr.Width) > _width) { int count; var back = run.FindBreak(out count); if (i - back <= run.Start) { if (line.Count > 1) { line.RemoveAt(line.Count - 1); line = new List <TextRun>(); lines.Add(line); line.Add(run); trim = true; continue; } else { back = 1; } } i -= back - count; run.Pop(back); line = new List <TextRun>(); lines.Add(line); run = new TextRun(run, i); line.Add(run); trim = true; } } run.End = i; _actualSize.X = lines.Max(l => l.Sum(tr => tr.Width)); _actualSize.Y = lines.Sum(l => l.Max(tr => tr.Height) + _leading); var vertices = new List <float>(); var indices = new List <int>(); var y = 0f; for (i = lines.Count - 1; i >= 0; --i) { var tl = lines[i]; var x = 0f; if (_halign == HorizontalAlignment.Right) { x = _width - tl.Sum(tr => tr.Width); } else if (_halign == HorizontalAlignment.Center) { x = Mathf.Floor((_width - tl.Sum(tr => tr.Width)) / 2f); } foreach (var tr in tl) { tr.Build(vertices, indices, new Vector3(x, y + (tr.Font.LineHeight - tr.Font.Base), 0f)); x += tr.Width; } y += tl.Max(tr => tr.Height) + _leading; } _vbuffer.Data = vertices.ToArray(); _vbuffer.Commit(); _ibuffer.Data = indices.ToArray(); _ibuffer.Commit(); _isDirty = false; }