public void Bake(Terrain terrain, VegetationLayers layers) { var sizeFuzzyFudgeFactor = new Vector3(0.5f, 1.667f, 0.5f); var data = terrain.terrainData; var protos = data.treePrototypes; var origin = terrain.GetPosition(); rootBounds = new Bounds(); dataBounds = new List <Bounds>(1000); dataTypes = new List <VegetationType>(1000); for (int i = 0, n = data.treeInstanceCount; i < n; ++i) { var tree = data.GetTreeInstance(i); VegetationType type; Bounds bounds; if (GetVegetationInfo(protos[tree.prototypeIndex].prefab, layers, out type, out bounds)) { bounds.center = origin + Vector3.Scale(data.size, tree.position); bounds.size = Vector3.Scale(bounds.size, sizeFuzzyFudgeFactor); rootBounds.SetMinMax( Vector3.Min(rootBounds.min, bounds.min), Vector3.Max(rootBounds.max, bounds.max)); dataBounds.Add(bounds); dataTypes.Add(type); } } }
static bool GetVegetationInfo( GameObject @object, VegetationLayers layers, out VegetationType type, out Bounds bounds) { if (@object) { if ((layers.undergrowthLayerMask & (1 << @object.layer)) != 0) { type = VegetationType.Undergrowth; } else { goto bail; } var renderer = @object.GetComponentInChildren <Renderer>(); if (!renderer) { goto bail; } bounds = renderer.bounds; return(true); } bail: type = VegetationType.None; bounds = default(Bounds); return(false); }