public void Execute(Repl repl, ReplParser.CtreeContext tree, bool piped) { var lines = repl.input_output_stack.Pop(); var serializeOptions = new JsonSerializerOptions(); serializeOptions.Converters.Add(new AntlrJson.ParseTreeConverter()); serializeOptions.WriteIndented = false; var in_tuple = JsonSerializer.Deserialize <AntlrJson.ParsingResultSet>(lines, serializeOptions); var nodes = in_tuple.Nodes; var lexer = in_tuple.Lexer; var parser = in_tuple.Parser; StringBuilder sb = new StringBuilder(); foreach (var node in nodes) { TerminalNodeImpl x = TreeEdits.LeftMostToken(node); var ts = x.Payload.TokenSource; sb.AppendLine(); sb.AppendLine( TreeOutput.OutputTree( node, lexer, parser, null).ToString()); } repl.input_output_stack.Push(sb.ToString()); }
public override void Output(ushort x, ushort y, ushort z, TreeOutput output) { for (ushort dy = 0; dy < top + height - 1; dy++) { output(x, (ushort)(y + dy), z, Block.trunk); } for (short dy = (short)-top; dy <= top; ++dy) { for (short dz = (short)-top; dz <= top; ++dz) { for (short dx = (short)-top; dx <= top; ++dx) { short dist = (short)(Math.Sqrt(dx * dx + dy * dy + dz * dz)); if ((dist < top + 1) && rnd.Next(dist) < 2) { ushort xx = (ushort)(x + dx), yy = (ushort)(y + dy + height), zz = (ushort)(z + dz); if (xx != x || zz != z || dy >= top - 1) { output(xx, yy, zz, Block.leaf); } } } } } }
public override void Generate(ushort x, ushort y, ushort z, TreeOutput output) { for (ushort dy = 0; dy < height + size - 1; dy++) { output(x, (ushort)(y + dy), z, Block.Log); } for (int dy = -size; dy <= size; ++dy) { for (int dz = -size; dz <= size; ++dz) { for (int dx = -size; dx <= size; ++dx) { int dist = (int)(Math.Sqrt(dx * dx + dy * dy + dz * dz)); if ((dist < size + 1) && rnd.Next(dist) < 2) { ushort xx = (ushort)(x + dx), yy = (ushort)(y + dy + height), zz = (ushort)(z + dz); if (xx != x || zz != z || dy >= size - 1) { output(xx, yy, zz, Block.Leaves); } } } } } }
void DoBranch(int x, int y, int z, TreeOutput output) { int dx = rnd.Next(-maxExtent, maxExtent); int dz = rnd.Next(-maxExtent, maxExtent); int clusterSize = rnd.Next(1, maxCluster); int branchStart = rnd.Next(branchBaseHeight, height); int branchMax = branchStart + rnd.Next(3, maxBranchHeight); Vec3S32 p1 = new Vec3S32(x, y + branchStart, z); Vec3S32 p2 = new Vec3S32(x + dx, y + branchMax, z + dz); Line(p1, p2, output); int R = clusterSize; Vec3S32[] marks = new Vec3S32[] { new Vec3S32(x + dx - R, y + branchMax - R, z + dz - R), new Vec3S32(x + dx + R, y + branchMax + R, z + dz + R) }; DrawOp op = new EllipsoidDrawOp(); Brush brush = new SolidBrush(Block.Leaves); op.SetMarks(marks); op.Perform(marks, brush, b => output(b.X, b.Y, b.Z, (BlockRaw)b.Block)); }
public static NodeBase createNewTreeOutput() { NodeBase rlt = new TreeOutput(); rlt.initInput(); rlt.guid = Guid.NewGuid().ToString(); return(rlt); }
/// <summary> Outputs the blocks generated by this tree at the given coordinates. </summary> public override void Generate(ushort x, ushort y, ushort z, TreeOutput output) { pos.X = x; pos.Y = y; pos.Z = z; this.output = output; Prepare(); MakeFoliage(); MakeTrunk(); }
void Line(Vec3S32 p1, Vec3S32 p2, TreeOutput output) { LineDrawOp.DrawLine(p1.X, p1.Y, p1.Z, 10000, p2.X, p2.Y, p2.Z, branch); foreach (Vec3S32 P in branch) { output((ushort)P.X, (ushort)P.Y, (ushort)P.Z, Block.Log); } branch.Clear(); }
private void openFile(object sender, RoutedEventArgs e) { Task t = new Task(() => { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "XAML Binary Files|*.xbf"; ofd.CheckFileExists = true; ofd.AddExtension = true; bool? result = ofd.ShowDialog(); if (result.HasValue && result.Value) { try { currentFile = new XbfFile(ofd.FileName); } catch (InvalidXbfException) { Dispatcher.Invoke(() => { MessageBox.Show("The file selected was not a valid XBF file", "Invalid file", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK); }); return; } #if !DEBUG catch(Exception ex) { Dispatcher.Invoke(() => { MessageBox.Show(ex.Message, "Generic ERROR", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK); return; }); } #endif TreeOutput to = new TreeOutput(); var treeout = to.GetOutput(currentFile); Dispatcher.Invoke(() => { xbfTree.ItemsSource = new[] { treeout }; }); XamlOutput xo = new XamlOutput(); var output = xo.GetOutput(currentFile); Dispatcher.Invoke(() => { xbfXml.Text = output.ToString(SaveOptions.OmitDuplicateNamespaces); }); } }); t.Start(); }
void Cuboid(Vec3S32 p1, Vec3S32 p2, TreeOutput output) { for (int y = p1.Y; y <= p2.Y; y++) { for (int z = p1.Z; z <= p2.Z; z++) { for (int x = p1.X; x <= p2.X; x++) { output((ushort)x, (ushort)y, (ushort)z, Block.Leaves); } } } }
public override void Generate(ushort x, ushort y, ushort z, TreeOutput output) { // Generate base trunk Vec3S32 p1 = new Vec3S32(x, y, z); Vec3S32 p2 = new Vec3S32(x, y + height, z); Line(p1, p2, output); for (int i = 0; i < branchAmount; i++) { DoBranch(x, y, z, output); } }
void MakeBranch(int x, int y, int z, TreeOutput output) { int branchX = rnd.Next(-maxExtent, maxExtent); int branchZ = rnd.Next(-maxExtent, maxExtent); int branchStartY = rnd.Next((trunkHeight / 4) + 1, trunkHeight + (trunkHeight / 4)); int branchEndY = branchStartY + rnd.Next(1, maxBranchHeight + 3); Vec3S32 p1 = new Vec3S32(x, y + branchStartY, z); Vec3S32 p2 = new Vec3S32(x + branchX, y + branchEndY, z + branchZ); Line(p1, p2, output); GenCluster(x + branchX, y + branchEndY, z + branchZ, output); }
public override void Generate(ushort x, ushort y, ushort z, TreeOutput output) { // Generate base tree Vec3S32 p1 = new Vec3S32(x, y, z); Vec3S32 p2 = new Vec3S32(x, y + trunkHeight, z); Line(p1, p2, output); GenCluster(x, y + trunkHeight, z, output); // Generate branches for (int i = 0; i < numBranches; i++) { MakeBranch(x, y, z, output); } }
public override void Generate(ushort x, ushort y, ushort z, TreeOutput output) { for (int dy = 0; dy <= height; dy++) { ushort yy = (ushort)(y + dy); if (dy < height) { output(x, yy, z, Block.Log); } for (int i = 0; i < 2; i++) { int dx = rnd.NextDouble() >= 0.5 ? 1 : -1; int dz = rnd.NextDouble() >= 0.5 ? 1 : -1; output((ushort)(x + dx), yy, (ushort)(z + dz), Block.Leaves); } } }
public override void Output(ushort x, ushort y, ushort z, TreeOutput output) { for (ushort dy = 0; dy <= height; dy++) { output(x, (ushort)(y + dy), z, Block.green); } int value = rnd.Next(1, 3); int inX = value == 1 ? -1 : 0; int inZ = value == 2 ? -1 : 0; for (ushort dy = height; dy <= rnd.Next(height + 2, height + 5); dy++) { output((ushort)(x + inX), (ushort)(y + dy), (ushort)(z + inZ), Block.green); } for (ushort dy = height; dy <= rnd.Next(height + 2, height + 5); dy++) { output((ushort)(x - inX), (ushort)(y + dy), (ushort)(z - inZ), Block.green); } }
public override void Output(ushort x, ushort y, ushort z, TreeOutput output) { for (ushort dy = 0; dy <= height; dy++) { output(x, (ushort)(y + dy), z, Block.trunk); } for (int dy = top; dy <= height + 1; dy++) { int extent = dy > height - 1 ? 2 : 3; for (int dz = -extent; dz <= extent; dz++) { for (int dx = -extent; dx <= extent; dx++) { ushort xx = (ushort)(x + dx), yy = (ushort)(y + dy), zz = (ushort)(z + dz); if (xx == x && zz == z && dy <= height) { continue; } if (Math.Abs(dx) == extent && Math.Abs(dz) == extent) { if (dy > height) { continue; } if (rnd.Next(2) == 0) { output(xx, yy, zz, Block.leaf); } } else { output(xx, yy, zz, Block.leaf); } } } } }
public override void Generate(ushort x, ushort y, ushort z, TreeOutput output) { for (int dy = 0; dy <= height; dy++) { if (dy < height) { output(x, (ushort)(y + dy), z, Block.Log); } } for (int dz = -2; dz <= 2; dz++) { for (int dx = -2; dx <= 2; dx++) { if (Math.Abs(dx) != Math.Abs(dz)) { continue; } output((ushort)(x + dx), (ushort)(y + height), (ushort)(z + dz), Block.Leaves); } } }
void GenCluster(int x, int y, int z, TreeOutput output) { Vec3S32 p1, p2; //cross X p1 = new Vec3S32(x - 1, y - 1, z); p2 = new Vec3S32(x + 1, y + 3, z); Cuboid(p1, p2, output); //cross z p1 = new Vec3S32(x, y - 1, z - 1); p2 = new Vec3S32(x, y + 3, z + 1); Cuboid(p1, p2, output); //cuboid x p1 = new Vec3S32(x - 2, y, z - 1); p2 = new Vec3S32(x + 2, y + 2, z + 1); Cuboid(p1, p2, output); //cuboid z p1 = new Vec3S32(x - 1, y, z - 2); p2 = new Vec3S32(x + 1, y + 2, z + 2); Cuboid(p1, p2, output); }
public abstract void Output(ushort x, ushort y, ushort z, TreeOutput output);
private void updateTerrainTree() { Terrain terr = Terrain.activeTerrain; if (terr != null) { List <TreePrototype> treeList = new List <TreePrototype>(); nodeManager.forEachNodes((n) => { if (n.node.value.getNodeType() == NodeType.TreeOutput) { TreeOutput treeNode = (TreeOutput)(n.node.value); if (treeNode.ObjTree != null) { treeNode.treeIndex = treeList.Count; TreePrototype p = new TreePrototype(); p.prefab = treeNode.ObjTree; p.bendFactor = treeNode.bendFactor; treeList.Add(p); } } }); terr.terrainData.treePrototypes = treeList.ToArray(); int w = terr.terrainData.heightmapWidth; int h = terr.terrainData.heightmapHeight; int layers = terr.terrainData.treePrototypes.Length; float pixelSize = terr.terrainData.size.x / w; List <TreeInstance> instances = new List <TreeInstance>(); nodeManager.forEachNodes((n) => { if (n.node.value.getNodeType() == NodeType.TreeOutput) { TreeOutput treeNode = (TreeOutput)n.node.value; if (treeNode.treeIndex < layers) { float[,] values = treeNode.update(nodeManager.seed, nodeManager.baseX, nodeManager.baseY, w, h, 1f, 1f); List <Vector3> treePos = new List <Vector3>(); //TreePrototype prot = terr.terrainData.treePrototypes[texNode.treeIndex]; for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { int treeNum = TreeOutput.getTreeNum(i, j, values[i, j], treeNode.density, treeNode.treeIndex); for (int t = 0; t < treeNum; t++) { Vector2 offset = (TreeOutput.getTreePos(i, j, t, pixelSize * 2f, treeNode.treeIndex)); Vector2 pos = new Vector2(j + offset.y, i + offset.x);//翻转x,y. float height = terr.terrainData.GetInterpolatedHeight(pos.x / w, pos.y / h) / terr.terrainData.size.y; Vector3 newPos = new Vector3(pos.x / w, height, pos.y / h); treePos.Add(newPos); } } } // for (int i = 0; i < treePos.Count; i++) { TreeInstance ins = new TreeInstance(); ins.position = treePos[i]; ins.prototypeIndex = treeNode.treeIndex; ins.color = Color.white; ins.lightmapColor = Color.white; float s = UnityEngine.Random.Range(treeNode.minSize, treeNode.maxSize); ins.heightScale = s; ins.widthScale = s; instances.Add(ins); } } } }); TreeInstance[] treeInstances = new TreeInstance[instances.Count]; for (int i = 0; i < treeInstances.Length; i++) { treeInstances[i] = instances[i]; } terr.terrainData.treeInstances = treeInstances; } }
/// <summary> Outputs the blocks generated by this tree at the given coordinates. </summary> public abstract void Generate(ushort x, ushort y, ushort z, TreeOutput output);