public SelectedVegetationCell(VegetationCell vegetationCell, VegetationStudioCamera vegetationStudioCamera) { VegetationCell = vegetationCell; CameraCount = 0; AddCameraReference(vegetationStudioCamera); }
private void RemoveVegetationCellCollider(VegetationCell vegetationCell) { for (int i = 0; i <= _vegetationCellColliderList.Count - 1; i++) { if (_vegetationCellColliderList[i].VegetationCell != vegetationCell) { continue; } for (int j = 0; j <= _vegetationCellColliderList[i].ColliderList.Count - 1; j++) { if (OnBeforeDestroyColliderDelegate != null) { OnBeforeDestroyColliderDelegate(_vegetationCellColliderList[i].ColliderList[j].GetComponent <Collider>()); } if (Application.isPlaying) { Destroy(_vegetationCellColliderList[i].ColliderList[j]); } else { DestroyImmediate(_vegetationCellColliderList[i].ColliderList[j]); } } _vegetationCellColliderList.RemoveAt(i); break; } vegetationCell.OnSpawnVegetationDelegate -= OnSpawnVegetation; }
private void AddVisisbleCellsFromCamera(VegetationStudioCamera vegetationStudioCamera) { JobCullingGroup jobCullingGroup = vegetationStudioCamera.JobCullingGroup; if (jobCullingGroup == null) { return; } for (int i = 0; i <= jobCullingGroup.VisibleCellIndexList.Length - 1; i++) { VegetationCell vegetationCell = vegetationStudioCamera.PotentialVisibleCellList[jobCullingGroup.VisibleCellIndexList[i]]; SelectedVegetationCell selectedVegetationCell = GetSelectorVegetationCell(vegetationCell); if (selectedVegetationCell != null) { selectedVegetationCell.AddCameraReference(vegetationStudioCamera); } else { selectedVegetationCell = new SelectedVegetationCell(vegetationCell, vegetationStudioCamera); VisibleSelectorVegetationCellList.Add(selectedVegetationCell); OnVegetationCellVisibleDelegate?.Invoke(selectedVegetationCell.VegetationCell); } } }
private void RemoveVisisbleCellsFromCamera(VegetationStudioCamera vegetationStudioCamera) { JobCullingGroup jobCullingGroup = vegetationStudioCamera.JobCullingGroup; if (jobCullingGroup == null) { return; } for (int j = 0; j <= jobCullingGroup.VisibleCellIndexList.Length - 1; j++) { VegetationCell vegetationCell = vegetationStudioCamera.PotentialVisibleCellList[jobCullingGroup.VisibleCellIndexList[j]]; SelectedVegetationCell selectedVegetationCell = GetSelectorVegetationCell(vegetationCell); if (selectedVegetationCell == null) { continue; } selectedVegetationCell.RemoveCameraReference(vegetationStudioCamera); if (selectedVegetationCell.CameraCount == 0) { VisibleSelectorVegetationCellList.Remove(selectedVegetationCell); OnVegetationCellInvisibleDelegate?.Invoke(selectedVegetationCell.VegetationCell); } } }
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 RenderVegetationNow(VegetationSystemPro vegetationSystemPro, ShadowMaskCreator shadowMaskCreator) { Shader overrideShader = Shader.Find("AwesomeTechnologies/Shadows/ShadowHeight"); List <VegetationCell> processCellList = new List <VegetationCell>(); vegetationSystemPro.VegetationCellQuadTree.Query(shadowMaskCreator.AreaRect, processCellList); for (int i = 0; i <= vegetationSystemPro.VegetationPackageProList.Count - 1; i++) { for (int j = 0; j <= vegetationSystemPro.VegetationPackageProList[i].VegetationInfoList.Count - 1; j++) { VegetationItemInfoPro vegetationItemInfoPro = vegetationSystemPro.VegetationPackageProList[i].VegetationInfoList[j]; if (!RenderVegetationType(vegetationItemInfoPro.VegetationType, shadowMaskCreator)) { continue; } for (int k = 0; k <= processCellList.Count - 1; k++) { if (j % 10 == 0) { EditorUtility.DisplayProgressBar("Render vegetation item: " + vegetationItemInfoPro.Name, "Render cell " + k + "/" + (processCellList.Count - 1), k / ((float)processCellList.Count - 1)); } VegetationCell vegetationCell = processCellList[k]; string vegetationItemID = vegetationItemInfoPro.VegetationItemID; vegetationSystemPro.SpawnVegetationCell(vegetationCell, vegetationItemID); NativeList <MatrixInstance> vegetationInstanceList = vegetationSystemPro.GetVegetationItemInstances(vegetationCell, vegetationItemID); VegetationItemModelInfo vegetationItemModelInfo = vegetationSystemPro.GetVegetationItemModelInfo(vegetationItemID); for (int l = 0; l <= vegetationItemModelInfo.VegetationMeshLod0.subMeshCount - 1; l++) { vegetationItemModelInfo.VegetationMaterialsLOD0[l].SetFloat(CullFarStart, 50000); vegetationItemModelInfo.VegetationMaterialsLOD0[l].SetFloat(CullFarDistance, 20); } for (int l = 0; l <= vegetationItemModelInfo.VegetationMeshLod0.subMeshCount - 1; l++) { Material tempMaterial = new Material(vegetationItemModelInfo.VegetationMaterialsLOD0[l]); tempMaterial.shader = overrideShader; tempMaterial.SetPass(0); for (int m = 0; m <= vegetationInstanceList.Length - 1; m++) { Graphics.DrawMeshNow(vegetationItemModelInfo.VegetationMeshLod0, vegetationInstanceList[m].Matrix); } DestroyImmediate(tempMaterial); } } EditorUtility.ClearProgressBar(); } } }
public void DrawDebugGizmos() { for (int i = 0; i <= VisibleSelectorVegetationCellList.Count - 1; i++) { VegetationCell vegetationCell = VisibleSelectorVegetationCellList[i].VegetationCell; Gizmos.color = SelectVegetationCellGizmoColor(VisibleSelectorVegetationCellList[i].CameraCount); Gizmos.DrawWireCube(vegetationCell.VegetationCellBounds.center, vegetationCell.VegetationCellBounds.size); } }
void OnVegetationCellVisible(VegetationCell vegetationCell, int distanceBand) { if (distanceBand <= (int)ColliderRange) { AddVegetationCellCollider(vegetationCell); } #if UNITY_EDITOR EditorUtility.SetDirty(this); #endif }
private SelectedVegetationCell GetSelectorVegetationCell(VegetationCell vegetationCell) { for (int i = 0; i <= VisibleSelectorVegetationCellList.Count - 1; i++) { if (VisibleSelectorVegetationCellList[i].VegetationCell == vegetationCell) { return(VisibleSelectorVegetationCellList[i]); } } return(null); }
private void OnVegetationInvisible(VegetationCell vegetationCell, int distanceBand) { RemoveVegetationCellCollider(vegetationCell); if (vegetationCell.IsVisible) { Debug.Log("strange"); } #if UNITY_EDITOR EditorUtility.SetDirty(this); #endif }
private void OnVegetationCellDistanceBandChanged(VegetationStudioCamera vegetationStudioCamera, VegetationCell vegetationCell, int currentDistanceBand, int previousDistanceBand) { if (currentDistanceBand == 0) { OnVegetationCellVisible(vegetationStudioCamera, vegetationCell); } else if (previousDistanceBand == 0) { OnVegetationCellInvisible(vegetationStudioCamera, vegetationCell); } }
JobHandle LoadVisibleCells(JobHandle processCullingHandle) { for (int i = 0; i <= ReadyToLoadVegetationCellList.Count - 1; i++) { VegetationCell vegetationCell = ReadyToLoadVegetationCellList[i]; if (!vegetationCell.Prepared) { Debug.Log("Unprepared cell" + vegetationCell.Index); continue; } //NativeList<MatrixInstance> matrixInstanceArray = vegetationCell.GetVegetationPackageInstancesList(_vegetationItemIndexes.VegetationPackageIndex,_vegetationItemIndexes.VegetationItemIndex); NativeList <MatrixInstance> matrixInstanceArray = vegetationCell .VegetationPackageInstancesList[_vegetationItemIndexes.VegetationPackageIndex] .VegetationItemMatrixList[_vegetationItemIndexes.VegetationItemIndex]; // if (!matrixInstanceArray.IsCreated) // { // Debug.Log("not created"); // continue; // } if (_useSpawnChance) { AddInstancesSpawnChanceJob addInstancesSpawnChanceJob = new AddInstancesSpawnChanceJob { InstanceList = InstanceList, RandomNumbers = _vegetationSystemPro.VegetationCellSpawner.RandomNumbers, SpawnChance = _spawnChance, MatrixInstanceList = matrixInstanceArray, RandomNumberIndex = vegetationCell.Index + _spawnSeed, VegetationCellIndex = vegetationCell.Index }; processCullingHandle = addInstancesSpawnChanceJob.Schedule(processCullingHandle); } else { AddInstancesJob addInstancesJob = new AddInstancesJob { InstanceList = InstanceList, MatrixInstanceList = matrixInstanceArray, VegetationCellIndex = vegetationCell.Index }; processCullingHandle = addInstancesJob.Schedule(processCullingHandle); } LoadedVegetationCellList.Add(vegetationCell); } ReadyToLoadVegetationCellList.Clear(); return(processCullingHandle); }
public void OnVegetationCellLoaded(VegetationCell vegetationCell) { if (LoadedVegetationCellList.Contains(vegetationCell)) { if (!ReadyToUnloadVegetationCellList.Contains(vegetationCell)) { ReadyToUnloadVegetationCellList.Add(vegetationCell); } if (!ReadyToLoadVegetationCellList.Contains(vegetationCell)) { ReadyToLoadVegetationCellList.Add(vegetationCell); } } }
private void OnClearCacheVegetationCellVegetationItem(VegetationSystemPro vegetationSystemPro, VegetationCell vegetationCell, int vegetationPackageIndex, int vegetationItemIndex) { for (int i = 0; i <= PackageColliderInfoList.Count - 1; i++) { VegetationPackageColliderInfo vegetationPackageColliderInfo = PackageColliderInfoList[i]; for (int j = 0; j <= vegetationPackageColliderInfo.ColliderManagerList.Count - 1; j++) { if (i == vegetationPackageIndex && j == vegetationItemIndex) { ColliderManager colliderManager = vegetationPackageColliderInfo.ColliderManagerList[j]; colliderManager?.VegetationItemSelector.RefreshVegetationCell(vegetationCell); } } } }
private void OnVegetationCellVisible(VegetationStudioCamera vegetationStudioCamera, VegetationCell vegetationCell) { SelectedVegetationCell selectedVegetationCell = GetSelectorVegetationCell(vegetationCell); if (selectedVegetationCell != null) { selectedVegetationCell.AddCameraReference(vegetationStudioCamera); } else { selectedVegetationCell = new SelectedVegetationCell(vegetationCell, vegetationStudioCamera); VisibleSelectorVegetationCellList.Add(selectedVegetationCell); OnVegetationCellVisibleDelegate?.Invoke(selectedVegetationCell.VegetationCell); } }
public void RefreshAllVegetationCells() { for (int i = 0; i <= LoadedVegetationCellList.Count - 1; i++) { VegetationCell vegetationCell = LoadedVegetationCellList[i]; if (!ReadyToUnloadVegetationCellList.Contains(vegetationCell)) { ReadyToUnloadVegetationCellList.Add(vegetationCell); } if (!ReadyToLoadVegetationCellList.Contains(vegetationCell)) { ReadyToLoadVegetationCellList.Add(vegetationCell); } } }
public void RefreshVegetationCell(VegetationCell vegetationCell) { if (!LoadedVegetationCellList.Contains(vegetationCell)) { return; } if (!ReadyToUnloadVegetationCellList.Contains(vegetationCell)) { ReadyToUnloadVegetationCellList.Add(vegetationCell); } if (!ReadyToLoadVegetationCellList.Contains(vegetationCell)) { ReadyToLoadVegetationCellList.Add(vegetationCell); } }
private void OnVegetationCellInvisible(VegetationStudioCamera vegetationStudioCamera, VegetationCell vegetationCell) { SelectedVegetationCell selectedVegetationCell = GetSelectorVegetationCell(vegetationCell); if (selectedVegetationCell == null) { return; } selectedVegetationCell.RemoveCameraReference(vegetationStudioCamera); if (selectedVegetationCell.CameraCount == 0) { VisibleSelectorVegetationCellList.Remove(selectedVegetationCell); OnVegetationCellInvisibleDelegate?.Invoke(selectedVegetationCell.VegetationCell); } }
JobHandle RemoveInvisibleCells(JobHandle processCullingHandle) { _removeVegetationCellIndexList.Clear(); bool needsRemoval = false; for (int i = 0; i <= ReadyToUnloadVegetationCellList.Count - 1; i++) { _removeVegetationCellIndexList.Add(ReadyToUnloadVegetationCellList[i].Index); VegetationCell vegetationCell = ReadyToUnloadVegetationCellList[i]; // ReSharper disable once InlineOutVariableDeclaration int index = LoadedVegetationCellList.IndexOf(ReadyToUnloadVegetationCellList[i]); if (index > -1) { LoadedVegetationCellList.RemoveAtSwapBack(index); } OnVegetationCellInvisibleDelegate?.Invoke(vegetationCell.Index); needsRemoval = true; } ReadyToUnloadVegetationCellList.Clear(); if (needsRemoval) { FlagInstancesForRemovalJob flagInstancesForRemovalJob = new FlagInstancesForRemovalJob { InstanceList = InstanceList, RemoveCellIndexList = _removeVegetationCellIndexList }; processCullingHandle = flagInstancesForRemovalJob.Schedule(processCullingHandle); RemoveInstancesJob removeInstancesJob = new RemoveInstancesJob { InstanceList = InstanceList }; processCullingHandle = removeInstancesJob.Schedule(processCullingHandle); } return(processCullingHandle); }
private void OnVegetationCellChangeDistanceBand(VegetationCell vegetationCell, int distanceBand, int previousDistanceBand) { if (!vegetationCell.IsVisible) { RemoveVegetationCellCollider(vegetationCell); return; } if ((distanceBand <= (int)ColliderRange) && (previousDistanceBand > (int)ColliderRange)) { AddVegetationCellCollider(vegetationCell); } else if (distanceBand > (int)ColliderRange) { RemoveVegetationCellCollider(vegetationCell); } else { #if UNITY_EDITOR EditorUtility.SetDirty(this); #endif } }
private void OnClearCacheVegetationCellVegetationItem(VegetationSystemPro vegetationSystemPro, VegetationCell vegetationCell, int vegetationPackageIndex, int vegetationItemIndex) { for (int i = 0; i <= PackageRuntimePrefabInfoList.Count - 1; i++) { VegetationPackageRuntimePrefabInfo packageRuntimePrefabInfo = PackageRuntimePrefabInfoList[i]; for (int j = 0; j <= packageRuntimePrefabInfo.RuntimePrefabManagerList.Count - 1; j++) { if (i == vegetationPackageIndex && j == vegetationItemIndex) { VegetationItemRuntimePrefabInfo vegetationItemRuntimePrefabInfo = packageRuntimePrefabInfo.RuntimePrefabManagerList[j]; for (int k = 0; k <= vegetationItemRuntimePrefabInfo.RuntimePrefabManagerList.Count - 1; k++) { RuntimePrefabManager runtimePrefabManager = vegetationItemRuntimePrefabInfo.RuntimePrefabManagerList[k]; runtimePrefabManager?.VegetationItemSelector.RefreshVegetationCell(vegetationCell); } } } } }
public void OnVegetationCellInvisible(VegetationCell vegetationCell) { ReadyToUnloadVegetationCellList.Add(vegetationCell); }
public void OnVegetationCellVisible(VegetationCell vegetationCell) { ReadyToLoadVegetationCellList.Add(vegetationCell); }
void CreateCollider(Matrix4x4 colliderMatrix, VegetationPackage vegetationPackage, int i, int j, bool bakeNavmesh, VegetationCell vegetationCell, GameObject currentParent, VegetationItemInfo vegetationItemInfo, VegetationCellCollider vegetationCellCollider) { RuntimeObjectInfo runtimeObjectInfo; Vector3 vegetationItemScale; //TUDO add pool of colliders switch (vegetationPackage.VegetationInfoList[i].ColliderType) { case ColliderType.Capsule: GameObject capsuleColliderObject = new GameObject(vegetationCell.CellIndex + " " + vegetationPackage.VegetationInfoList[i].VegetationType + " CapsuleCollider_" + j); if (!bakeNavmesh) { capsuleColliderObject.hideFlags = HideColliders ? HideFlags.HideAndDontSave : HideFlags.DontSave; } capsuleColliderObject.layer = GetColliderLayer(vegetationPackage.VegetationInfoList[i].VegetationType); vegetationItemScale = MatrixTools.ExtractScaleFromMatrix(colliderMatrix); CapsuleCollider capsuleCollider = capsuleColliderObject.AddComponent <CapsuleCollider>(); capsuleCollider.height = vegetationPackage.VegetationInfoList[i].ColliderHeight; capsuleCollider.radius = vegetationPackage.VegetationInfoList[i].ColliderRadius; capsuleCollider.isTrigger = vegetationPackage.VegetationInfoList[i].ColliderTrigger; capsuleColliderObject.transform.SetParent(currentParent.transform, false); capsuleColliderObject.transform.position = MatrixTools.ExtractTranslationFromMatrix(colliderMatrix) + new Vector3(0, (capsuleCollider.height * vegetationItemScale.y) / 2f, 0); capsuleColliderObject.transform.rotation = MatrixTools.ExtractRotationFromMatrix(colliderMatrix); capsuleColliderObject.transform.localScale = vegetationItemScale; Vector3 colliderOffset = new Vector3( capsuleColliderObject.transform.localScale.x * vegetationPackage.VegetationInfoList[i].ColliderOffset.x, capsuleColliderObject.transform.localScale.y * vegetationPackage.VegetationInfoList[i].ColliderOffset.y, capsuleColliderObject.transform.localScale.z * vegetationPackage.VegetationInfoList[i].ColliderOffset.z); capsuleColliderObject.transform.localPosition += MatrixTools.ExtractRotationFromMatrix(colliderMatrix) * colliderOffset; if (!bakeNavmesh) { AddNavMesObstacle(capsuleColliderObject, vegetationItemInfo); AddVegetationItemInstanceInfo(capsuleColliderObject, colliderMatrix, vegetationItemInfo.VegetationType, vegetationItemInfo.VegetationItemID); vegetationCellCollider.ColliderList.Add(capsuleColliderObject); runtimeObjectInfo = capsuleColliderObject.AddComponent <RuntimeObjectInfo>(); runtimeObjectInfo.VegetationItemInfo = vegetationPackage.VegetationInfoList[i]; if (OnCreateColliderDelegate != null) { OnCreateColliderDelegate(capsuleCollider); } } break; case ColliderType.Sphere: GameObject sphereColliderObject = new GameObject(vegetationCell.CellIndex.ToString() + " " + vegetationPackage.VegetationInfoList[i].VegetationType.ToString() + " SphereCollider_" + j.ToString()); if (!bakeNavmesh) { sphereColliderObject.hideFlags = HideColliders ? HideFlags.HideAndDontSave : HideFlags.DontSave; } sphereColliderObject.layer = GetColliderLayer(vegetationPackage.VegetationInfoList[i].VegetationType); vegetationItemScale = MatrixTools.ExtractScaleFromMatrix(colliderMatrix); SphereCollider sphereCollider = sphereColliderObject.AddComponent <SphereCollider>(); sphereCollider.radius = vegetationPackage.VegetationInfoList[i].ColliderRadius; sphereCollider.isTrigger = vegetationPackage.VegetationInfoList[i].ColliderTrigger; sphereColliderObject.transform.SetParent(currentParent.transform, false); sphereColliderObject.transform.position = MatrixTools.ExtractTranslationFromMatrix(colliderMatrix); // + vegetationPackage.VegetationInfoList[i].ColliderOffset; sphereColliderObject.transform.rotation = MatrixTools.ExtractRotationFromMatrix(colliderMatrix); sphereColliderObject.transform.localScale = vegetationItemScale; Vector3 sphereColliderOffset = new Vector3( sphereColliderObject.transform.localScale.x * vegetationPackage.VegetationInfoList[i].ColliderOffset.x, sphereColliderObject.transform.localScale.y * vegetationPackage.VegetationInfoList[i].ColliderOffset.y, sphereColliderObject.transform.localScale.z * vegetationPackage.VegetationInfoList[i].ColliderOffset.z); sphereColliderObject.transform.localPosition += MatrixTools.ExtractRotationFromMatrix(colliderMatrix) * sphereColliderOffset; if (!bakeNavmesh) { AddNavMesObstacle(sphereColliderObject, vegetationItemInfo); AddVegetationItemInstanceInfo(sphereColliderObject, colliderMatrix, vegetationItemInfo.VegetationType, vegetationItemInfo.VegetationItemID); vegetationCellCollider.ColliderList.Add(sphereColliderObject); runtimeObjectInfo = sphereColliderObject.AddComponent <RuntimeObjectInfo>(); runtimeObjectInfo.VegetationItemInfo = vegetationPackage.VegetationInfoList[i]; if (OnCreateColliderDelegate != null) { OnCreateColliderDelegate(sphereCollider); } } break; case ColliderType.CustomMesh: case ColliderType.Mesh: GameObject meshColliderObject = new GameObject(vegetationCell.CellIndex.ToString() + " " + vegetationPackage.VegetationInfoList[i].VegetationType.ToString() + " MeshCollider_" + j.ToString()); if (!bakeNavmesh) { meshColliderObject.hideFlags = HideColliders ? HideFlags.HideAndDontSave : HideFlags.DontSave; } meshColliderObject.layer = GetColliderLayer(vegetationPackage.VegetationInfoList[i].VegetationType); MeshCollider meshCollider = meshColliderObject.AddComponent <MeshCollider>(); VegetationItemModelInfo vegetationItemModelInfo = VegetationSystem.GetVegetationModelInfo(i); meshCollider.sharedMesh = vegetationItemInfo.ColliderType == ColliderType.CustomMesh ? vegetationItemInfo.ColliderMesh : vegetationItemModelInfo.VegetationMeshLod0; meshColliderObject.transform.SetParent(currentParent.transform, false); meshColliderObject.transform.position = MatrixTools.ExtractTranslationFromMatrix(colliderMatrix);// + vegetationPackage.VegetationInfoList[i].ColliderOffset; meshColliderObject.transform.rotation = MatrixTools.ExtractRotationFromMatrix(colliderMatrix); meshColliderObject.transform.localScale = MatrixTools.ExtractScaleFromMatrix(colliderMatrix); if (!bakeNavmesh) { AddNavMesObstacle(meshColliderObject, vegetationItemInfo); AddVegetationItemInstanceInfo(meshColliderObject, colliderMatrix, vegetationItemInfo.VegetationType, vegetationItemInfo.VegetationItemID); vegetationCellCollider.ColliderList.Add(meshColliderObject); runtimeObjectInfo = meshColliderObject.AddComponent <RuntimeObjectInfo>(); runtimeObjectInfo.VegetationItemInfo = vegetationPackage.VegetationInfoList[i]; if (OnCreateColliderDelegate != null) { OnCreateColliderDelegate(meshCollider); } } break; } }
void RenderVegetation(List <VegetationCell> processCellList, VegetationSystemPro vegetationSystemPro, VegetationColorMaskCreator colorMaskCreator) { for (int i = 0; i <= vegetationSystemPro.VegetationPackageProList.Count - 1; i++) { for (int j = 0; j <= vegetationSystemPro.VegetationPackageProList[i].VegetationInfoList.Count - 1; j++) { VegetationItemInfoPro vegetationItemInfoPro = vegetationSystemPro.VegetationPackageProList[i].VegetationInfoList[j]; if (!RenderVegetationType(vegetationItemInfoPro.VegetationType, colorMaskCreator)) { continue; } // for (int k = 0; k <= processCellList.Count - 1; k++) { if (j % 10 == 0) { EditorUtility.DisplayProgressBar("Render vegetation item: " + vegetationItemInfoPro.Name, "Render cell " + k + "/" + (processCellList.Count - 1), k / ((float)processCellList.Count - 1)); } VegetationCell vegetationCell = processCellList[k]; string vegetationItemID = vegetationItemInfoPro.VegetationItemID; vegetationSystemPro.SpawnVegetationCell(vegetationCell, vegetationItemID); NativeList <MatrixInstance> vegetationInstanceList = vegetationSystemPro.GetVegetationItemInstances(vegetationCell, vegetationItemID); List <Matrix4x4> instanceList = new List <Matrix4x4>(); for (int l = 0; l <= vegetationInstanceList.Length - 1; l++) { Vector3 position = MatrixTools.ExtractTranslationFromMatrix(vegetationInstanceList[l].Matrix); Vector3 scale = MatrixTools.ExtractScaleFromMatrix(vegetationInstanceList[l].Matrix); Quaternion rotation = MatrixTools.ExtractRotationFromMatrix(vegetationInstanceList[l].Matrix); Vector3 newPosition = new Vector3(position.x, 0, position.z); Vector3 newScale; if (vegetationItemInfoPro.VegetationType == VegetationType.Grass || vegetationItemInfoPro.VegetationType == VegetationType.Plant) { newScale = new Vector3(scale.x * colorMaskCreator.VegetationScale, scale.y * colorMaskCreator.VegetationScale, scale.z * colorMaskCreator.VegetationScale); } else { newScale = scale; } Matrix4x4 newMatrix = Matrix4x4.TRS(newPosition, rotation, newScale); instanceList.Add(newMatrix); } VegetationItemModelInfo vegetationItemModelInfo = vegetationSystemPro.GetVegetationItemModelInfo(vegetationItemID); for (int l = 0; l <= vegetationItemModelInfo.VegetationMeshLod0.subMeshCount - 1; l++) { Material tempMaterial = new Material(vegetationItemModelInfo.VegetationMaterialsLOD0[l]); tempMaterial.shader = Shader.Find("AwesomeTechnologies/Vegetation/RenderVegetationColorMask"); tempMaterial.SetPass(0); for (int m = 0; m <= instanceList.Count - 1; m++) { Graphics.DrawMeshNow(vegetationItemModelInfo.VegetationMeshLod0, instanceList[m]); } DestroyImmediate(tempMaterial); } } EditorUtility.ClearProgressBar(); } } // for (int i = 0; i <= vegetationSystem.CurrentVegetationPackage.VegetationInfoList.Count - 1; i++) // { // VegetationItemInfo vegetationItemInfo = vegetationSystem.CurrentVegetationPackage.VegetationInfoList[i]; // //for (int l = 0; l <= vegetationSystem.VegetationModelInfoList[i].VegetationMeshLod0.subMeshCount - 1; l++) // //{ // // vegetationSystem.VegetationModelInfoList[i].VegetationMaterialsLOD0[l].SetFloat("_CullFarStart",50000); // // vegetationSystem.VegetationModelInfoList[i].VegetationMaterialsLOD0[l].SetFloat("_CullFarDistance", 20); // //} // // if (!RenderVegetationType(vegetationItemInfo.VegetationType,colorMaskCreator)) continue; // // for (int j = 0; j <= processCellList.Count - 1; j++) // { // if (j % 100 == 0) // { // EditorUtility.DisplayProgressBar("Render vegetation item: " + vegetationItemInfo.Name, "Render cell " + j + "/" + (processCellList.Count - 1), j / ((float)processCellList.Count - 1)); // } // // VegetationCell vegetationCell = processCellList[j]; // List<Matrix4x4> instanceList = // vegetationCell.DirectSpawnVegetation(vegetationItemInfo.VegetationItemID, true); // // for (int k = 0; k <= instanceList.Count - 1; k++) // { // Vector3 position = MatrixTools.ExtractTranslationFromMatrix(instanceList[k]); // Vector3 scale = MatrixTools.ExtractScaleFromMatrix(instanceList[k]); // Quaternion rotation = MatrixTools.ExtractRotationFromMatrix(instanceList[k]); // Vector3 newPosition = new Vector3(position.x,0,position.z); // Vector3 newScale; // if (vegetationItemInfo.VegetationType == VegetationType.Grass || // vegetationItemInfo.VegetationType == VegetationType.Plant) // { // newScale = new Vector3(scale.x * colorMaskCreator.VegetationScale, scale.y * colorMaskCreator.VegetationScale, scale.z * colorMaskCreator.VegetationScale); // } // else // { // newScale = scale; // } // // Matrix4x4 newMatrix = Matrix4x4.TRS(newPosition, rotation, newScale); // instanceList[k] = newMatrix; // } // // for (int l = 0; l <= vegetationSystem.VegetationModelInfoList[i].VegetationMeshLod0.subMeshCount - 1; l++) // { // // Material tempMaterial = new Material(vegetationSystem.VegetationModelInfoList[i].VegetationMaterialsLOD0[l]); // tempMaterial.shader = Shader.Find("AwesomeTechnologies/Vegetation/RenderVegetationColorMask"); // tempMaterial.SetPass(0); // for (int k = 0; k <= instanceList.Count - 1; k++) // { // Graphics.DrawMeshNow(vegetationSystem.VegetationModelInfoList[i].VegetationMeshLod0, // instanceList[k]); // //Graphics.DrawMesh(vegetationSystem.VegetationModelInfoList[i].VegetationMeshLod0, instanceList[k], // // vegetationSystem.VegetationModelInfoList[i].VegetationMaterialsLOD0[l], // // colorMaskCreator.InvisibleLayer, null, l); // } // // DestroyImmediate(tempMaterial); // } // } // EditorUtility.ClearProgressBar(); // } }
/// <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 }
private void AddVegetationCellCollider(VegetationCell vegetationCell, GameObject parentGameObject = null, bool bakeNavmesh = false) { vegetationCell.OnSpawnVegetationDelegate += OnSpawnVegetation; GameObject currentParent = ColliderParent; if (bakeNavmesh && parentGameObject != null) { currentParent = parentGameObject; } VegetationCellCollider vegetationCellCollider = new VegetationCellCollider { VegetationCell = vegetationCell }; VegetationPackage vegetationPackage = VegetationSystem.CurrentVegetationPackage; int colliderCount = 0; if (vegetationCell.InitDone) { for (int i = 0; i <= vegetationPackage.VegetationInfoList.Count - 1; i++) { VegetationItemInfo vegetationItemInfo = vegetationPackage.VegetationInfoList[i]; if (bakeNavmesh && !vegetationItemInfo.ColliderUseForBake) { continue; } if (vegetationPackage.VegetationInfoList[i].ColliderType != ColliderType.Disabled) { if (vegetationPackage.VegetationInfoList[i].VegetationType == VegetationType.Tree || vegetationPackage.VegetationInfoList[i].VegetationType == VegetationType.Objects || vegetationPackage.VegetationInfoList[i].VegetationType == VegetationType.LargeObjects) { if (vegetationItemInfo.VegetationRenderType == VegetationRenderType.InstancedIndirect && Application.isPlaying) { CustomList <Matrix4x4> currentVegetationList; if (bakeNavmesh) { currentVegetationList = vegetationCell.DirectSpawnVegetationIndirect(vegetationItemInfo.VegetationItemID, true); } else { currentVegetationList = vegetationCell.GetCurrentIndirectVegetationList(i); } if (currentVegetationList != null) { colliderCount += currentVegetationList.Count; for (int j = 0; j <= currentVegetationList.Count - 1; j++) { Matrix4x4 colliderMatrix = currentVegetationList[j]; CreateCollider(colliderMatrix, vegetationPackage, i, j, bakeNavmesh, vegetationCell, currentParent, vegetationItemInfo, vegetationCellCollider); } } } else { List <Matrix4x4> currentVegetationList; if (bakeNavmesh) { currentVegetationList = vegetationCell.DirectSpawnVegetation(vegetationItemInfo.VegetationItemID, true); } else { currentVegetationList = vegetationCell.GetCurrentVegetationList(i); } if (currentVegetationList != null) { colliderCount += currentVegetationList.Count; for (int j = 0; j <= currentVegetationList.Count - 1; j++) { Matrix4x4 colliderMatrix = currentVegetationList[j]; CreateCollider(colliderMatrix, vegetationPackage, i, j, bakeNavmesh, vegetationCell, currentParent, vegetationItemInfo, vegetationCellCollider); } } } } } } } if (!bakeNavmesh) { vegetationCellCollider.ColliderCount = colliderCount; _vegetationCellColliderList.Add(vegetationCellCollider); } }
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); } }
private void OnSpawnVegetation(VegetationCell vegetationCell) { RemoveVegetationCellCollider(vegetationCell); AddVegetationCellCollider(vegetationCell); }