public ColliderPool(VegetationItemInfoPro vegetationItemInfoPro, VegetationItemModelInfo vegetationItemModelInfo, VegetationSystemPro vegetationSystemPro, Transform colliderParent, bool showColliders) { _vegetationItemInfoPro = vegetationItemInfoPro; _vegetationItemModelInfo = vegetationItemModelInfo; _vegetationSystemPro = vegetationSystemPro; _colliderParent = colliderParent; _showColliders = showColliders; _colliderLayer = vegetationSystemPro.VegetationSettings.GetLayer(vegetationItemInfoPro.VegetationType); _colliderTag = vegetationItemInfoPro.ColliderTag; if (_colliderTag == "") { _colliderTag = "Untagged"; } if (_vegetationItemInfoPro.ColliderType == ColliderType.FromPrefab) { GameObject tmpColliderObject = Object.Instantiate(vegetationItemInfoPro.VegetationPrefab); if (_vegetationItemInfoPro.ColliderTag != "") { tmpColliderObject.tag = vegetationItemInfoPro.ColliderTag; } tmpColliderObject.hideFlags = HideFlags.DontSave; tmpColliderObject.name = "ColliderSource_" + _vegetationItemInfoPro.VegetationItemID; tmpColliderObject.transform.SetParent(_colliderParent); _sourceColliderObject = CreateColliderObject(tmpColliderObject); DestroyObject(tmpColliderObject); } }
public ColliderManager(VisibleVegetationCellSelector visibleVegetationCellSelector, VegetationSystemPro vegetationSystemPro, VegetationItemInfoPro vegetationItemInfoPro, Transform colliderParent, bool showColliders) { _showColliders = showColliders; _vegetationSystemPro = vegetationSystemPro; _vegetationItemInfoPro = vegetationItemInfoPro; float cullingDistance = vegetationSystemPro.VegetationSettings.GetVegetationDistance() * vegetationItemInfoPro.ColliderDistanceFactor; VegetationItemSelector = new VegetationItemSelector(visibleVegetationCellSelector, vegetationSystemPro, vegetationItemInfoPro, false, 1, 0) { CullingDistance = cullingDistance }; VegetationItemSelector.OnVegetationItemVisibleDelegate += OnVegetationItemVisible; VegetationItemSelector.OnVegetationItemInvisibleDelegate += OnVegetationItemInvisible; VegetationItemSelector.OnVegetationCellInvisibleDelegate += OnVegetationCellInvisible; VegetationItemModelInfo vegetationItemModelInfo = vegetationSystemPro.GetVegetationItemModelInfo(vegetationItemInfoPro.VegetationItemID); ColliderPool = new ColliderPool(vegetationItemInfoPro, vegetationItemModelInfo, vegetationSystemPro, colliderParent, _showColliders); RuntimePrefabStorage = new RuntimePrefabStorage(ColliderPool); }
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(); } } }
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(); // } }