/// <summary> /// Generate new mesh instances /// </summary> /// <param name="areaSize"></param> public static void GenerateFoliageMeshInstances(FoliageResolutions resolution) { if (!prototypeMeshInstances.ContainsKey(resolution)) { prototypeMeshInstances.Add(resolution, new Dictionary <int, GPUMesh>()); } var meshInstances = prototypeMeshInstances[resolution]; if (meshInstances.Count > 0) { for (int i = 0; i < FoliageDB.unSortedPrototypes.Count; i++) { if (meshInstances.ContainsKey(FoliageDB.unSortedPrototypes[i].id)) { meshInstances[FoliageDB.unSortedPrototypes[i].id].Destroy(); } } meshInstances.Clear(); } if (FoliageCore_MainManager.instance.enabled) { for (int prototypeIndex = 0; prototypeIndex < FoliageDB.unSortedPrototypes.Count; prototypeIndex++) { if (FoliageDB.unSortedPrototypes[prototypeIndex].enabled) { GenerateFoliageMeshInstanceForIndex(FoliageDB.unSortedPrototypes[prototypeIndex].id, resolution); } } } }
/// <summary> /// Create Foliage mesh instances for a certain index and foliage size. /// </summary> /// <param name="meshInstances"></param> /// <param name="prototypeIndex"></param> public static void GenerateFoliageMeshInstanceForIndex(int prototypeIndex, FoliageResolutions resolution) { Dictionary <int, GPUMesh> meshInstances = prototypeMeshInstances[resolution]; FoliagePrototype prototype = FoliageDB.sortedPrototypes[prototypeIndex]; if (!FoliageCore_MainManager.instance.enabled) { return; } bool prototypeMeshExists = prototypeMeshInstances != null && meshInstances.ContainsKey(prototypeIndex); if (prototypeMeshExists) { meshInstances[prototypeIndex].Destroy(); meshInstances.Remove(prototypeIndex); } Mesh[] meshes = new Mesh[prototype.meshLodsCount]; int[] densities = new int[prototype.meshLodsCount]; List <UNPhysicsTemplate>[] physicsObjects = new List <UNPhysicsTemplate> [prototype.meshLodsCount]; for (int i = 0; i < prototype.meshLodsCount; i++) { meshes[i] = CreateNewMesh(); densities[i] = Mathf.FloorToInt((float)prototype.maxGeneratedDensity / (i + 1)); FoliageMeshInstance.CreateGPUMesh(prototype, meshes[i], densities[i]); } meshInstances.Add(prototypeIndex, new GPUMesh(meshes, densities, prototypeIndex, physicsObjects, resolution)); }
internal static Dictionary <int, GPUMesh> GetPrototypeMeshInstances(FoliageResolutions resolution) { if (!prototypeMeshInstances.ContainsKey(resolution)) { GenerateFoliageMeshInstances(resolution); } return(prototypeMeshInstances[resolution]); }
public GPUMesh(Mesh[] LODMeshes, int[] LODLevels, int prototypeIndex, List <UNPhysicsTemplate>[] physicsInformation, FoliageResolutions resolution) { if (LODMeshes.Length != LODLevels.Length) { Debug.LogError("uNature Foliage : Generating LODs Failed!, Array sizes are different!"); return; } LODMeshInstances = new FoliageMeshInstancesGroup[LODMeshes.Length]; for (int i = 0; i < LODMeshes.Length; i++) { meshes.Add(new GPUMeshLOD(LODMeshes[i], LODLevels[i], prototypeIndex, physicsInformation[i])); LODMeshInstances[i] = FoliageMeshInstance.CreateFoliageInstances(prototypeIndex, LODLevels[i], resolution); } }
public static FoliageMeshInstancesGroup CreateFoliageInstances(int prototypeIndex, int density, FoliageResolutions resolution) { float resMultiplier = (float)resolution / FoliageCore_MainManager.FOLIAGE_INSTANCE_AREA_SIZE; FoliagePrototype prototype = FoliageDB.sortedPrototypes[prototypeIndex]; int maxGenerationInstancesPerMesh = (int)(CalculatePerMeshInstances(prototype, density) / resMultiplier); int generationAmountPerRadius = GENERATION_AMOUNT_PER_RADIUS(maxGenerationInstancesPerMesh); FoliageMeshInstancesGroup meshGroup = new FoliageMeshInstancesGroup(); Vector3 position; int gAmountX = generationAmountPerRadius - (int)prototype.meshInstancesGenerationOffset.x; if (gAmountX == 0) { gAmountX = 1; // dont clamp, it can be minus } int gAmountZ = generationAmountPerRadius - (int)prototype.meshInstancesGenerationOffset.y; if (gAmountZ == 0) { gAmountZ = 1; // dont clamp, it can be minus } for (int x = 0; x < gAmountX; x++) { for (int z = 0; z < gAmountZ; z++) { position = new Vector3(z * maxGenerationInstancesPerMesh, 0, x * maxGenerationInstancesPerMesh); meshGroup.AddMeshInstance(FoliageMeshInstance.CreateFoliageMesh(prototype, position, maxGenerationInstancesPerMesh)); } } return(meshGroup); }