Example #1
0
        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();
        }
Example #2
0
        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();
                }
            }
        }
Example #3
0
        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
        }
Example #5
0
        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();
//            }
        }