Ejemplo n.º 1
0
        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;
            }
        }
Ejemplo n.º 2
0
        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);
            }
        }