Exemple #1
0
        public RenderTexture LoadBakedMask(Terrain terrain, BakedMaskTypeInternal type, CollisionMask collisionMask = null, string worldMaskGUID = "", int treeID = 0) //float radius, int id = 0, string tag = "", string GUID = "")
        {
            if (m_cacheEntries == null)
            {
                m_cacheEntries = new BakedMaskCacheEntry[0];
            }

            string fileName = "";

            switch (type)
            {
            case BakedMaskTypeInternal.RadiusTree:
                fileName = GetTreeCollisionMaskFileName(terrain, collisionMask.m_treeSpawnRuleGUID, collisionMask.m_Radius);
                break;

            case BakedMaskTypeInternal.RadiusTag:
                fileName = GetTagCollisionMaskFileName(terrain, collisionMask.m_tag, collisionMask.m_Radius);
                break;

            case BakedMaskTypeInternal.WorldBiomeMask:
                fileName = GetWorldBiomeMaskFilename(terrain, worldMaskGUID);
                break;

            case BakedMaskTypeInternal.LayerGameObject:
                fileName = GetLayerGameObjectCollisionMaskFileName(terrain, collisionMask.m_layerMask, collisionMask.m_growShrinkDistance);
                break;

            case BakedMaskTypeInternal.LayerTree:
                fileName = GetLayerTreeCollisionMaskFileName(terrain, collisionMask.m_layerMask, collisionMask.m_growShrinkDistance);
                break;

            default:
                return(null);
            }

            if (fileName != "")
            {
                //file in cache? If yes, return from there
                int length = m_cacheEntries.Length;
                for (int i = 0; i < length; i++)
                {
                    if (m_cacheEntries[i].fileName == fileName)
                    {
                        //does it have a texture as well?
                        if (m_cacheEntries[i].texture != null)
                        {
                            //found it, we can return it & are done
                            return(m_cacheEntries[i].texture);
                        }
                    }
                }

                //not in cache? We need to bake it then!
                switch (type)
                {
                case BakedMaskTypeInternal.RadiusTree:
                    return(BakeTerrainTreeCollisions(terrain, treeID, collisionMask.m_treeSpawnRuleGUID, collisionMask.m_Radius));

                case BakedMaskTypeInternal.RadiusTag:
                    return(BakeTerrainTagCollisions(terrain, collisionMask.m_tag, collisionMask.m_Radius));

                case BakedMaskTypeInternal.WorldBiomeMask:
                    return(BakeTerrainWorldBiomeMask(terrain, worldMaskGUID));

                case BakedMaskTypeInternal.LayerGameObject:
                    return(BakeLayerGameObjectCollisions(terrain, collisionMask.m_layerMask, collisionMask.m_growShrinkDistance));

                case BakedMaskTypeInternal.LayerTree:
                    return(BakeLayerTreeCollisions(terrain, collisionMask.m_layerMask, collisionMask.m_growShrinkDistance));

                default:
                    return(null);
                }


                //not in cache? try reading from disk...
#if UNITY_EDITOR
                //Texture2D texture = (Texture2D)AssetDatabase.LoadAssetAtPath(GaiaDirectories.GetTerrainCollisionDirectory(terrain) + "/" + GetTreeCollisionMaskFileName(terrain, id, radius), typeof(Texture2D));


                //if (texture != null)
                //{
                //    //found it on disk, write a copy of it in cache for next access attempt, then return the copy.
                //    //We need to work with a copy here because otherwise we can run into issue when trying to overwrite the image file
                //    Texture2D copy = new Texture2D(texture.width, texture.height, texture.format, true);
                //    Graphics.CopyTexture(texture, copy);
                //    AddNewCollisionMaskCacheEntry(copy, terrain, fileName);
                //    return copy;
                //}
#endif
            }
            else
            {
                return(null);
            }
        }
Exemple #2
0
        public static void DrawMaskListElement(Rect rect, int index, CollisionMask collisionMask, EditorUtils m_editorUtils, Terrain currentTerrain, GaiaConstants.FeatureOperation operation)
        {
            int oldIndent = EditorGUI.indentLevel;

            EditorGUI.indentLevel = 0;
            //Active Label
            Rect fieldRect = new Rect(rect.x, rect.y, rect.width * 0.1f, EditorGUIUtility.singleLineHeight);

            EditorGUI.LabelField(fieldRect, m_editorUtils.GetContent("MaskActive"));
            //Active Checkbox
            fieldRect.x           += rect.width * 0.1f;
            fieldRect.width        = rect.width * 0.05f;
            collisionMask.m_active = EditorGUI.Toggle(fieldRect, collisionMask.m_active);
            //Invert Label
            fieldRect.x    += rect.width * 0.05f;
            fieldRect.width = rect.width * 0.1f;
            EditorGUI.LabelField(fieldRect, m_editorUtils.GetContent("MaskInvert"));
            //Invert Checkbox
            fieldRect.x           += rect.width * 0.1f;
            fieldRect.width        = rect.width * 0.05f;
            collisionMask.m_invert = EditorGUI.Toggle(fieldRect, collisionMask.m_invert);
            //Type dropdown
            fieldRect.x    += rect.width * 0.05f;
            fieldRect.width = rect.width * 0.2f;
            BakedMaskType oldType = collisionMask.m_type;

            collisionMask.m_type = (BakedMaskType)EditorGUI.EnumPopup(fieldRect, collisionMask.m_type);
            EditorGUI.LabelField(fieldRect, m_editorUtils.GetContent("CollisionMaskTypeTooltip"));
            switch (collisionMask.m_type)
            {
            case BakedMaskType.RadiusTree:
                //Tree dropdown
                string oldTreeId = collisionMask.m_treeSpawnRuleGUID;
                fieldRect.x += rect.width * 0.2f;
                int selectedGUIDIndex = 0;
                if (collisionMask.m_treeSpawnRuleGUID != "")
                {
                    SpawnRule selectedRule = CollisionMask.m_allTreeSpawnRules.FirstOrDefault(x => x.GUID == collisionMask.m_treeSpawnRuleGUID);
                    if (selectedRule != null)
                    {
                        selectedGUIDIndex = Array.IndexOf(CollisionMask.m_allTreeSpawnRules, selectedRule);
                    }
                }
                selectedGUIDIndex = EditorGUI.IntPopup(fieldRect, selectedGUIDIndex, CollisionMask.m_allTreeSpawnRuleNames, CollisionMask.m_allTreeSpawnRuleIndices);
                if (selectedGUIDIndex >= 0 && selectedGUIDIndex < CollisionMask.m_allTreeSpawnRules.Length)
                {
                    collisionMask.m_treeSpawnRuleGUID = CollisionMask.m_allTreeSpawnRules[selectedGUIDIndex].GUID;
                }
                if (oldType != collisionMask.m_type || oldTreeId != collisionMask.m_treeSpawnRuleGUID)
                {
                    SpawnRule selectedRule = CollisionMask.m_allTreeSpawnRules.FirstOrDefault(x => x.GUID == collisionMask.m_treeSpawnRuleGUID);
                    if (selectedRule != null)
                    {
                        Spawner spawner = CollisionMask.m_allTreeSpawners.FirstOrDefault(x => x.m_settings.m_spawnerRules.Contains(selectedRule));
                        if (spawner != null)
                        {
                            GameObject treePrefab = spawner.m_settings.m_resources.m_treePrototypes[selectedRule.m_resourceIdx].m_desktopPrefab;
                            collisionMask.m_Radius = GaiaUtils.GetTreeRadius(treePrefab);
                        }
                    }
                }

                fieldRect.x           += rect.width * 0.2f;
                fieldRect.width        = rect.width * 0.1f;
                collisionMask.m_Radius = EditorGUI.FloatField(fieldRect, collisionMask.m_Radius);
                fieldRect.x           += rect.width * 0.1f;
                fieldRect.width        = rect.width * 0.2f;
                break;

            case BakedMaskType.RadiusTag:
                //Tree dropdown
                fieldRect.x += rect.width * 0.2f;
                ////Building up a value array of incrementing ints of the size of the available tags this array will then match the displayed string selection in the popup
                //int[] tagValueArray = Enumerable
                //                    .Repeat(0, (int)((currentTerrain.terrainData.treePrototypes.Length - 0) / 1) + 1)
                //                    .Select((tr, ti) => tr + (1 * ti))
                //                    .ToArray();
                string oldTag = collisionMask.m_tag;
                collisionMask.m_tag = EditorGUI.TagField(fieldRect, collisionMask.m_tag);

                if (oldType != collisionMask.m_type || oldTag != collisionMask.m_tag)
                {
                    collisionMask.m_Radius = GaiaUtils.GetBoundsForTaggedObject(collisionMask.m_tag);
                }


                fieldRect.x           += rect.width * 0.2f;
                fieldRect.width        = rect.width * 0.1f;
                collisionMask.m_Radius = EditorGUI.FloatField(fieldRect, collisionMask.m_Radius);
                fieldRect.x           += rect.width * 0.1f;
                fieldRect.width        = rect.width * 0.2f;
                break;

            case (BakedMaskType.LayerGameObject):
                //Layer mask selection
                fieldRect.x += rect.width * 0.2f;
                EditorGUI.BeginChangeCheck();
                collisionMask.m_layerMask = GaiaEditorUtils.LayerMaskFieldRect(fieldRect, new GUIContent(""), collisionMask.m_layerMask);
                if (EditorGUI.EndChangeCheck())
                {
                    collisionMask.m_layerMaskLayerNames = GaiaUtils.LayerMaskToString(collisionMask.m_layerMask);
                }
                EditorGUI.LabelField(fieldRect, m_editorUtils.GetContent("CollisionMaskLayerSelectionTooltip"));
                fieldRect.x    += rect.width * 0.2f;
                fieldRect.width = rect.width * 0.1f;
                collisionMask.m_growShrinkDistance = EditorGUI.FloatField(fieldRect, collisionMask.m_growShrinkDistance);
                EditorGUI.LabelField(fieldRect, m_editorUtils.GetContent("CollisionMaskGrowShrinkDistanceTooltip"));
                fieldRect.x    += rect.width * 0.1f;
                fieldRect.width = rect.width * 0.2f;

                break;

            case (BakedMaskType.LayerTree):
                //Layer mask selection
                fieldRect.x += rect.width * 0.2f;
                EditorGUI.BeginChangeCheck();
                collisionMask.m_layerMask = GaiaEditorUtils.LayerMaskFieldRect(fieldRect, new GUIContent(""), collisionMask.m_layerMask);
                if (EditorGUI.EndChangeCheck())
                {
                    collisionMask.m_layerMaskLayerNames = GaiaUtils.LayerMaskToString(collisionMask.m_layerMask);
                }
                EditorGUI.LabelField(fieldRect, m_editorUtils.GetContent("CollisionMaskLayerSelectionTooltip"));
                fieldRect.x    += rect.width * 0.2f;
                fieldRect.width = rect.width * 0.1f;
                collisionMask.m_growShrinkDistance = EditorGUI.FloatField(fieldRect, collisionMask.m_growShrinkDistance);
                EditorGUI.LabelField(fieldRect, m_editorUtils.GetContent("CollisionMaskGrowShrinkDistanceTooltip"));
                fieldRect.x    += rect.width * 0.1f;
                fieldRect.width = rect.width * 0.2f;

                break;
            }

            if (GUI.Button(fieldRect, m_editorUtils.GetContent("MaskCollisionBake")))
            {
                switch (collisionMask.m_type)
                {
                case BakedMaskType.RadiusTree:
                    GaiaSessionManager.GetSessionManager().m_bakedMaskCache.BakeAllTreeCollisions(collisionMask.m_treeSpawnRuleGUID, collisionMask.m_Radius);
                    break;

                case BakedMaskType.RadiusTag:
                    GaiaSessionManager.GetSessionManager().m_bakedMaskCache.BakeAllTagCollisions(collisionMask.m_tag, collisionMask.m_Radius);
                    break;

                case BakedMaskType.LayerGameObject:
                    GaiaSessionManager.GetSessionManager().m_bakedMaskCache.BakeAllLayerGameObjectCollisions(collisionMask.m_layerMask, collisionMask.m_growShrinkDistance);     //, collisionMask.m_Radius);
                    break;

                case BakedMaskType.LayerTree:
                    GaiaSessionManager.GetSessionManager().m_bakedMaskCache.BakeAllLayerTreeCollisions(collisionMask.m_layerMask, collisionMask.m_growShrinkDistance);     //, collisionMask.m_Radius);
                    break;
                }
            }
            EditorGUI.indentLevel = oldIndent;
        }