// Update is called once per frame void Update() { if (isFluid) { avg += ((Time.deltaTime / Time.timeScale) - avg) * 0.03f; float fps = (1F / avg); if (fps < targetFPS && nTrees > minTrees) { isFluid = false; txt.text = "Max number of trees: " + nTrees.ToString(); } else { fpsCounter++; if (fpsCounter > 4) { var newPlant = generator.CreateTree(new Vector3(0, 0, 0)); //newPlant.transform.parent = world.transform; //MeshRenderer[] meshes = newPlant.GetComponentsInChildren<MeshRenderer>(); //foreach (MeshRenderer mesh in meshes) //{ // if (mesh.sharedMaterial.name == "Bianco (Instance)") // { // mesh.gameObject.isStatic = true; // batchBianco.Add(mesh.gameObject); // } // if (mesh.sharedMaterial.name == "Azzurro (Instance)") { // mesh.gameObject.isStatic = true; // batchAzzurro.Add(mesh.gameObject); // } //} //if (batchBianco.Count >= batchSize) { // GameObject[] arr = batchBianco.ToArray(); // StaticBatchingUtility.Combine(arr, world); // batchBianco = new List<GameObject>(); // Debug.Log("STATIC BATCH BIANCO"); //} //if (batchAzzurro.Count >= batchSize) //{ // GameObject[] arr = batchAzzurro.ToArray(); // StaticBatchingUtility.Combine(arr, world); // batchAzzurro = new List<GameObject>(); // Debug.Log("STATIC BATCH AZZURRO"); //} nTrees++; fpsCounter = 0; } txt.text = fps.ToString(); } } }
// Generate a text based representation of the expression tree public static void DisplayTree(this IExpression ans, Action<object> output = null) { output = output ?? Console.Write; var generator = new TreeGenerator(); // -(-(3*Log((-5 + -3*2)-6*-(-Sin(-5/3) + Cos(-3/2))---6/4*a))) var root = BuildTree(ans); string text = generator.CreateTree(root); output(text); TextNode BuildTree(IExpression expr) { var lbl = GetLabel(expr); if (expr is IBinaryExpression be) return new TextNode(lbl, BuildTree(be.Left), BuildTree(be.Right)); if (expr is Neg nx) return new TextNode(lbl, left: BuildTree(nx.Value)); if (expr is IFuncExpression fx) return new TextNode(lbl, left: BuildTree(fx.Value)); return new TextNode(lbl); } // Get a string representation of an IExpression string GetLabel(IExpression e) { switch (e) { case Num n: return n.Value.ToString(CultureInfo.CurrentCulture); case Var v: return v.Name.ToString(); default: return e.GetType().Name; } } }
public static void Demo() { var rnd = new Random(); var generator = new TreeGenerator(3 /* max childs count*/); var tree = generator.CreateTree(4 /*max depth*/, () => rnd.Next() /*node value*/); }