internal void UpdateText() { if (Text == null) { return; } CanvasRoot.Height = Text.LineCount * LineHeight; CanvasRoot.Invalidate(); GutterRoot.Text = this.Text; GutterRoot.UpdateHeight(CanvasRoot.Height); if (DiffMap != null) { DiffMap.UpdateTextViews(); } Text.ScrollToLineRequested -= Text_ScrollToLineRequested; Text.ScrollToLineRequested += Text_ScrollToLineRequested; }
void LoadDiffMaps() { Report("Loading Diff Maps", 1); morphShapesData = new List <List <Vector3> >(); morphShapesLinks = new List <List <int> > (); // First, get the UV data from the mesh... Vector2[] baseUVs; if (metaMorphSettings.UVLayerOne == true) { baseUVs = mesh.uv; } else { baseUVs = mesh.uv2; } // let's cycle through all the diff maps in Diff_Maps. int diffMapLoopMax = diffMaps.Length; for (int diffMapLoop = 0; diffMapLoop < diffMapLoopMax; diffMapLoop++) { // Temporary variables for building List <Vector3> loadDiffMap = new List <Vector3> (); List <int> loadDiffMapL = new List <int> (); if (diffMaps[diffMapLoop].scale == Vector3.zero) { diffMaps[diffMapLoop].scale = new Vector3(1, 1, 1); } // Set the name if it was not alreay set... if (diffMaps[diffMapLoop].name == "") { diffMaps[diffMapLoop].name = diffMaps[diffMapLoop].image.name; } // Grab the Diff Map data... DiffMap aDiffMap = diffMaps[diffMapLoop]; // And get the parts of it we need for processing... Texture2D diffMapImage = aDiffMap.image; Vector3 diffMapScale = aDiffMap.scale; // We now read the mesh, uv, and Diff Map to get the shape data. and store it. for (int vert = 0; vert < baseUVs.Length; vert++) { int UV_x = (int)Mathf.Round(baseUVs[vert].x * diffMapImage.width); int UV_y = (int)Mathf.Round(baseUVs[vert].y * diffMapImage.height); // These int test_x = (int)Mathf.Round((diffMapImage.GetPixel(UV_x, UV_y).r - 0.5f) * 255); int test_y = (int)Mathf.Round((diffMapImage.GetPixel(UV_x, UV_y).g - 0.5f) * 255); int test_z = (int)Mathf.Round((diffMapImage.GetPixel(UV_x, UV_y).b - 0.5f) * 255); if (!(test_x == 0 && test_y == 0 && test_z == 0)) { // Okay, now we grab the color data for the pixel under the UV point for this vert. We then convert it to a number from -1.0 to 1.0, and multiply it by Diff_Map_Scale. float UVC_r = ((diffMapImage.GetPixel(UV_x, UV_y).r / 0.5f) - 1) * -1 * diffMapScale.x; // Why -1? Because the relation to blender is reversed for some reason... float UVC_g = ((diffMapImage.GetPixel(UV_x, UV_y).g / 0.5f) - 1) * diffMapScale.y; float UVC_b = ((diffMapImage.GetPixel(UV_x, UV_y).b / 0.5f) - 1) * diffMapScale.z; Vector3 vert_xyz_shift = new Vector3(UVC_r, UVC_g, UVC_b); loadDiffMap.Add(vert_xyz_shift); loadDiffMapL.Add(vert); } } Report("Object " + name + ": Diff Map '" + diffMapImage.name + "' changes " + loadDiffMapL.Count + " out of " + baseMesh.Length + " verts...", 2); // This part stores not only the mesh modifications for this shape, but also the indexes of the array that are being changes in the mesh. // This data allows us to cycle through only the mesh points that will be changed, instead of running though all of points of the mesh. // Massive speed increase: You only pay for the points you morph. morphShapesData.Add(loadDiffMap); // We're storing an array into an array here. morphShapesLinks.Add(loadDiffMapL); // We're storing an array into an array here. } }
private ScarifStructure(int version, NbtMap idMap, DiffMap diffMap) : this(idMap) { Version = version; BlockDiffMap = diffMap; }
public ScarifStructure(NbtMap blockTranslationMap) { BlockDiffMap = new DiffMap(); BlockTranslationMap = blockTranslationMap; }
public static ScarifStructure Load(string filename) { using (var fs = File.OpenRead(filename)) using (var bs = new BrotliStream(fs, CompressionMode.Decompress)) using (var s = new BinaryReader(bs)) { var idMap = new NbtMap(); var diffMap = new DiffMap(); var identBytes = new byte[Magic.Length]; var read = s.Read(identBytes, 0, identBytes.Length); var ident = Encoding.UTF8.GetString(identBytes); if (ident != Magic || read != identBytes.Length) { throw new IOException("Input file not SCARIF structure"); } var version = s.ReadInt32(); var numChunks = s.ReadInt32(); var numIdMapEntries = s.ReadInt32(); for (var entryIdx = 0; entryIdx < numIdMapEntries; entryIdx++) { var id = s.ReadInt16(); var name = ReadNullTerminatedString(s); idMap.Add(id, name); } for (var chunkIdx = 0; chunkIdx < numChunks; chunkIdx++) { var chunkX = s.ReadInt32(); var chunkZ = s.ReadInt32(); var numBlocks = s.ReadInt32(); var blocks = new BlockDiffEntry[numBlocks]; for (var blockIdx = 0; blockIdx < numBlocks; blockIdx++) { // Format: // 0x 0000 1111 // xxxx zzzz var xz = s.ReadByte(); var x = (byte)((xz & 0xF0) >> 4); var z = (byte)(xz & 0x0F); var y = s.ReadByte(); var id = s.ReadInt16(); var flags = (ScarifBlock.BlockFlags)s.ReadByte(); byte metadata = 0; NbtTree tileTag = null; if (flags.Has(ScarifBlock.BlockFlags.Metadata)) { metadata = s.ReadByte(); } if (flags.Has(ScarifBlock.BlockFlags.Nbt)) { var len = s.ReadInt32(); if (len <= 0) { throw new IOException("Zero-length NBT present"); } var bytes = s.ReadBytes(len); using (var ms = new MemoryStream(bytes)) tileTag = new NbtTree(ms); } if (idMap.ContainsKey(id)) { blocks[blockIdx] = new BlockDiffEntry(new BlockPosition(x, y, z), new ScarifBlock(id, metadata, tileTag)); } else { throw new IOException($"Unknown block ID found: {id}"); } } diffMap.Add(new ChunkPosition(chunkX, chunkZ), blocks); } return(new ScarifStructure(version, idMap, diffMap)); } }
private void canvas_RegionsInvalidated(CanvasVirtualControl sender, CanvasRegionsInvalidatedEventArgs args) { if (Text == null) { return; } // Update the diff map if (_parentScrollViewer == null) { _parentScrollViewer = VisualTreeHelper.FindParent <ScrollViewer>(this); if (_parentScrollViewer != null) { _parentScrollViewer.ViewChanged += (s, e) => { if (DiffMap != null) { DiffMap.UpdateTextViews(); } }; // Force update once if (DiffMap != null) { DiffMap.UpdateTextViews(); } } } if (_language == null && !string.IsNullOrEmpty(FileExtension)) { _language = Languages.FindById(FileExtension); } foreach (Windows.Foundation.Rect region in args.InvalidatedRegions) { using (CanvasDrawingSession ds = sender.CreateDrawingSession(region)) { ds.Clear(_defaultBackgroundColor); int startLine = (int)Math.Clamp(Math.Floor(region.Top / LineHeight), 0, Text.LineCount); // add 2 to the end line count. we want to "overdraw" a bit if the line is going to be cut-off int endLine = (int)Math.Clamp(Math.Ceiling(region.Bottom / LineHeight) + 2, 0, Text.LineCount); StringBuilder stringRegion = new StringBuilder(); for (int i = startLine; i < endLine; i++) { ITextLine line = Text.GetLine(i); if (line is DiffTextLine diffLine) { if (diffLine.ChangeType == DiffLineType.Empty) { stringRegion.AppendLine(); // null line } else { stringRegion.AppendLine(Text.GetLine(i).ToString()); } } } using (var canvasText = new CanvasTextLayout(ds, stringRegion.ToString(), _textFormat, 9999, (float)region.Height)) { // Handle the diff line background colors int index = 0; for (int i = startLine; i < endLine; i++) { var line = Text.GetLine(i); if (line is DiffTextLine diffLine) { switch (diffLine.ChangeType) { case DiffLineType.Insert: { ds.FillRectangle(0, i * LineHeight, (float)this.ActualWidth, MathF.Ceiling(LineHeight), _addedBackgroundColor); if (_language == null) { canvasText.SetBrush(index, line.Length, _addedForegroundBrush); } } break; case DiffLineType.Remove: { ds.FillRectangle(0, i * LineHeight, (float)this.ActualWidth, MathF.Ceiling(LineHeight), _removedBackgroundColor); if (_language == null) { canvasText.SetBrush(index, line.Length, _removedForegroundBrush); } } break; case DiffLineType.Empty: { ds.FillRectangle(0, i * LineHeight, (float)this.ActualWidth, MathF.Ceiling(LineHeight), _nullBrush); } break; case DiffLineType.Unchanged: default: break; } } index += line.Length + 2; // add for newline } // Syntax highlight if (_language != null) { _colorizer.FormatLine(_language, CanvasRoot, canvasText, stringRegion.ToString()); } // Draw the text ds.DrawTextLayout(canvasText, 0, startLine * LineHeight, _defaultForegroundBrush); } } } }