/// <summary> /// Creates a GameObject from an ARTree /// </summary> /// <param name="tree"></param> /// <returns></returns> private GameObject CreateTreeGameobject(ARTree tree) { GameObject g = Instantiate(rootTree) as GameObject; ProceduralTree script = g.GetComponent <ProceduralTree>(); script.Seed = tree.seed; script.MaxNumVertices = tree.maxNumVertices; script.NumberOfSides = tree.numberOfSides; script.BaseRadius = tree.baseRadius; script.RadiusStep = tree.radiusStep; script.MinimumRadius = tree.minimumRadius; script.BranchRoundness = tree.branchRoundness; script.SegmentLength = tree.segmentLength; script.Twisting = tree.twisting; script.BranchProbability = tree.branchProbability; script.growthPercent = 1.0f;// tree.growthPercent; // Set correct leaf-material tree.ConvertToTexture(); Material m = new Material(Shader.Find("Custom/LeafShader")); m.mainTexture = tree.leafTexture; script.leafMaterial = m; // Change texture on leafs GameObject leaf = g.transform.GetChild(0).gameObject; leaf.GetComponent <ChangeLeafTexture>().ChangeTexture(); g.AddComponent <TimeStampHolder>().TimeStamp = tree.timeStamp; // Set the propper tree texture Renderer treeR = g.GetComponent <Renderer>(); string textureName = tree.materialName; // tree.materialName is textureName; if (textureName == "bark3") // Oak { Debug.Log("BARK3"); treeR.material.mainTexture = barkTextures[0]; } else if (textureName == "bark1") // Rainbow { Debug.Log("BARK1"); treeR.material.mainTexture = barkTextures[1]; } else // Birch { Debug.Log("BIRCH!!!"); g.AddComponent <ProceduralBark>(); ProceduralBark b = g.GetComponent <ProceduralBark>(); b.freq = 0.08f; b.lacunarity = 3.38f; b.gain = -0.78f; b.GenerateTexture(); } return(g); }
private void TreeRemove(object sender, ChildChangedEventArgs args) { if (args.DatabaseError != null) { Debug.LogError(args.DatabaseError.Message); return; } DataSnapshot snap = args.Snapshot; ARTree tree = new ARTree(snap); NewTreeToRemove(tree); }
/// <summary> /// Adds a tree to firebase database /// </summary> public void AddTreeToFirebase(int seed, int maxNumVertices, int numberOfSides, float baseRadius, float radiusStep, float minimumRadius, float branchRoundness, float segmentLength, float twisting, float branchProbability, float growthPercent, string matName) { string key = treeRef.Push().Key; ARTree myTree = new ARTree(seed, maxNumVertices, numberOfSides, baseRadius, radiusStep, minimumRadius, branchRoundness, segmentLength, twisting, branchProbability, growthPercent, key, matName); myTree.ConvertToString(leafMat.mainTexture as Texture2D); Dictionary <string, object> entryVal = myTree.ToDictionary(); Dictionary <string, object> childUpdate = new Dictionary <string, object>(); childUpdate["/Trees/" + key] = entryVal; treeRef.Parent.UpdateChildrenAsync(childUpdate); }
/// <summary> /// Check if any new trees are ready to be added to the scene. /// </summary> void Update() { if (treesToAdd.Count > 0) { ARTree newTree = treesToAdd[0]; // Two step Pop treesToAdd.RemoveAt(0); bool addTree = true; foreach (Transform child in displayTrees.transform) { string timeStampEx = child.gameObject.GetComponent <TimeStampHolder>().TimeStamp; // Do not add tree to scene if it already exists if (newTree.timeStamp == timeStampEx) { Debug.Log("Tree excluded"); addTree = false; break; } } if (addTree) { Debug.Log("Tree added!"); GameObject g = CreateTreeGameobject(newTree); AddTreeToScene(g); } } if (treesToRemove.Count > 0) { ARTree removeTree = treesToRemove[0]; treesToRemove.RemoveAt(0); foreach (Transform child in displayTrees.transform) { string timeStampEx = child.gameObject.GetComponent <TimeStampHolder>().TimeStamp; if (removeTree.timeStamp == timeStampEx) { Debug.Log("Child Removed"); Destroy(child.gameObject); TreeDestroyed(); break; } } } }
private void AddTreeToRemove(ARTree removeTree) { treesToRemove.Add(removeTree); }
/// <summary> /// Checks if new tree exists already. If not, add it to the scene. /// </summary> private void AddTreeToAdd(ARTree newTree) { treesToAdd.Add(newTree); }