void AddVegetationItemsToScene(VegetationCell vegetationCell, GameObject parent, VegetationItemInfoPro vegetationItemInfoPro) { if (!vegetationItemInfoPro.VegetationPrefab) { return; } VegetationSystemPro vegetationSystemPro = _sceneVegetationBaker.VegetationSystemPro; vegetationSystemPro.SpawnVegetationCell(vegetationCell, vegetationItemInfoPro.VegetationItemID); NativeList <MatrixInstance> vegetationInstanceList = vegetationSystemPro.GetVegetationItemInstances(vegetationCell, vegetationItemInfoPro.VegetationItemID); for (int j = 0; j <= vegetationInstanceList.Length - 1; j++) { Matrix4x4 vegetationItemMatrix = vegetationInstanceList[j].Matrix; Vector3 position = MatrixTools.ExtractTranslationFromMatrix(vegetationItemMatrix); Vector3 scale = MatrixTools.ExtractScaleFromMatrix(vegetationItemMatrix); Quaternion rotation = MatrixTools.ExtractRotationFromMatrix(vegetationItemMatrix); GameObject vegetationItem = Instantiate(vegetationItemInfoPro.VegetationPrefab, parent.transform); vegetationItem.transform.position = position; vegetationItem.transform.localScale = scale; vegetationItem.transform.rotation = rotation; vegetationItem.isStatic = _sceneVegetationBaker.ExportStatic; } vegetationCell.ClearCache(); }
void BakeVegetationItemColliders(ColliderManager colliderManager, VegetationItemInfoPro vegetationItemInfoPro) { GC.Collect(); GameObject rootItem = new GameObject("Baked colliders_" + vegetationItemInfoPro.Name + "_" + vegetationItemInfoPro.VegetationItemID); #if UNITY_EDITOR if (!Application.isPlaying) { EditorUtility.DisplayProgressBar("Bake Collider: " + vegetationItemInfoPro.Name, "Bake cell", 0); } #endif int colliderCount = 0; for (int i = 0; i <= VegetationSystemPro.VegetationCellList.Count - 1; i++) { VegetationCell vegetationCell = VegetationSystemPro.VegetationCellList[i]; #if UNITY_EDITOR if (i % 10 == 0) { if (!Application.isPlaying) { EditorUtility.DisplayProgressBar("Bake Collider: " + vegetationItemInfoPro.Name, "Bake cell " + i + "/" + (VegetationSystemPro.VegetationCellList.Count - 1), i / ((float)VegetationSystemPro.VegetationCellList.Count - 1)); } } #endif VegetationSystemPro.SpawnVegetationCell(vegetationCell, vegetationItemInfoPro.VegetationItemID); NativeList <MatrixInstance> vegetationInstanceList = VegetationSystemPro.GetVegetationItemInstances(vegetationCell, vegetationItemInfoPro.VegetationItemID); for (int j = 0; j <= vegetationInstanceList.Length - 1; j++) { Matrix4x4 vegetationItemMatrix = vegetationInstanceList[j].Matrix; Vector3 position = MatrixTools.ExtractTranslationFromMatrix(vegetationItemMatrix); Vector3 scale = MatrixTools.ExtractScaleFromMatrix(vegetationItemMatrix); Quaternion rotation = MatrixTools.ExtractRotationFromMatrix(vegetationItemMatrix); ItemSelectorInstanceInfo itemSelectorInstanceInfo = new ItemSelectorInstanceInfo { Position = position, Scale = scale, Rotation = rotation }; GameObject newCollider = colliderManager.ColliderPool.GetObject(itemSelectorInstanceInfo); newCollider.hideFlags = HideFlags.None; newCollider.transform.SetParent(rootItem.transform, true); SetNavmeshArea(newCollider, vegetationItemInfoPro.NavMeshArea); if (SetBakedCollidersStatic) { SetStatic(newCollider); } if (ConvertBakedCollidersToMesh) { CreateNavMeshColliderMeshes(newCollider); } colliderCount++; } vegetationCell.ClearCache(); } VegetationSystemPro.ClearCache(vegetationItemInfoPro.VegetationItemID); #if UNITY_EDITOR if (!Application.isPlaying) { EditorUtility.ClearProgressBar(); } #endif if (colliderCount == 0) { DestroyImmediate(rootItem); } }
/// <summary> /// BakeVegetationItem will bake all instances of a VegetationItem from the rules to the Persisitent Vegetation Storage. The original rule will set "Include in Terrain" to false. /// </summary> /// <param name="vegetationItemID"></param> public void BakeVegetationItem(string vegetationItemID) { if (!VegetationSystemPro) { return; } if (vegetationItemID == "") { Debug.Log("vegetationItemID empty"); return; } GC.Collect(); VegetationItemInfoPro vegetationItemInfo = VegetationSystemPro.GetVegetationItemInfo(vegetationItemID); vegetationItemInfo.EnableRuntimeSpawn = true; #if UNITY_EDITOR if (!Application.isPlaying) { EditorUtility.DisplayProgressBar("Bake vegetation item: " + vegetationItemInfo.Name, "Spawn all cells", 0); } #endif for (int i = 0; i <= VegetationSystemPro.VegetationCellList.Count - 1; i++) { VegetationCell vegetationCell = VegetationSystemPro.VegetationCellList[i]; #if UNITY_EDITOR if (i % 10 == 0) { if (!Application.isPlaying) { EditorUtility.DisplayProgressBar("Bake vegetation item: " + vegetationItemInfo.Name, "Spawn cell " + i + "/" + (VegetationSystemPro.VegetationCellList.Count - 1), i / ((float)VegetationSystemPro.VegetationCellList.Count - 1)); } } #endif VegetationSystemPro.SpawnVegetationCell(vegetationCell, vegetationItemID); NativeList <MatrixInstance> vegetationInstanceList = VegetationSystemPro.GetVegetationItemInstances(vegetationCell, vegetationItemID); for (int j = 0; j <= vegetationInstanceList.Length - 1; j++) { Matrix4x4 vegetationItemMatrix = vegetationInstanceList[j].Matrix; //AddVegetationItemInstance(vegetationItemID, MatrixTools.ExtractTranslationFromMatrix(vegetationItemMatrix), // MatrixTools.ExtractScaleFromMatrix(vegetationItemMatrix), // MatrixTools.ExtractRotationFromMatrix(vegetationItemMatrix), false,0); PersistentVegetationStoragePackage.AddVegetationItemInstance(vegetationCell.Index, vegetationItemID, MatrixTools.ExtractTranslationFromMatrix(vegetationItemMatrix) - VegetationSystemPro.VegetationSystemPosition, MatrixTools.ExtractScaleFromMatrix(vegetationItemMatrix), MatrixTools.ExtractRotationFromMatrix(vegetationItemMatrix), 0, vegetationInstanceList[j].DistanceFalloff); } vegetationCell.ClearCache(); } VegetationSystemPro.ClearCache(vegetationItemID); vegetationItemInfo.EnableRuntimeSpawn = false; #if UNITY_EDITOR if (!Application.isPlaying) { EditorUtility.ClearProgressBar(); } #endif }