Пример #1
0
        public GrabbitSettings GetOrFetchSettings(bool noWarning = false)
        {
            if (!CurrentSettings)
            {
                var ids = AssetDatabase.FindAssets("t:GrabbitSettings");
                if (ids.Length == 0)
                {
                    if (!noWarning)
                    {
                        Debug.LogWarning("No Grabbit settings found, please contact us on Discord!");
                    }
                    return(null);
                }

                CurrentSettings = AssetDatabase.LoadAssetAtPath <GrabbitSettings>(AssetDatabase.GUIDToAssetPath(ids[0]));

                if (!CurrentSettings)
                {
                    if (!noWarning)
                    {
                        Debug.LogWarning("Grabbit could not locate the settings: please contact us on Discord!");
                    }
                    return(null);
                }

                CurrentSettings.InitializeStyle(true);
            }

            return(CurrentSettings);
        }
Пример #2
0
        public void GenerateAllColliders(GrabbitSettings settings)
        {
            var ids = AssetDatabase.FindAssets("t:Mesh", new[] { "Assets" });

            Debug.LogFormat("Grabbit Analysis: {0} meshes found. Generating Colliders...", ids.Length);

            var vhacdGenerator = CreateAndConfigureGenerator(settings);
            var i = 0;

            foreach (var id in ids)
            {
                var mesh = AssetDatabase.LoadAssetAtPath <Mesh>(AssetDatabase.GUIDToAssetPath(id));

                if (colliderMeshes.ContainsKey(mesh))
                {
                    continue;
                }

                if (EditorUtility.DisplayCancelableProgressBar(
                        "Grabbit Is Generating Colliders To Be Used In The Scene",
                        $"Analyzing {mesh.name} ({i + 1} out of {ids.Length})",
                        (float)i / ids.Length))
                {
                    break;
                }

                try
                {
                    if (!mesh)
                    {
                        continue;
                    }
                    var meshes = vhacdGenerator.GenerateConvexMeshes(mesh);

                    foreach (var collidingMesh in meshes)
                    {
                        AssetDatabase.AddObjectToAsset(collidingMesh, this);
                    }

                    colliderMeshes.Add(mesh,
                                       new MeshList
                    {
                        maxMeshCount = settings.MaxMeshCollidersCreated, resolution = settings.ColliderResolution,
                        Meshes       = meshes
                    });
                }
                catch (Exception)
                {
                    // ignored
                }


                i++;
            }

            EditorUtility.ClearProgressBar();
            EditorUtility.SetDirty(this);
            Debug.LogFormat("Grabbit Colliders Generated!", ids.Length);
            AssetDatabase.SaveAssets();
        }
Пример #3
0
        public void EnableSelectionModeColliders(GrabbitSettings settings)
        {
            if (settings.UsePredefinedColliders)
            {
                foreach (var preExistingCollider in PreExistingColliders)
                {
                    preExistingCollider.enabled = true;
                }

                foreach (var col in AddedStaticConvexColliders)
                {
                    if (!settings.UseDynamicNonConvexColliders)
                    {
                        col.convex = true;
                    }
                }
            }
            else
            {
                foreach (var col in AddedStaticConvexColliders)
                {
                    if (!settings.UseDynamicNonConvexColliders)
                    {
                        col.convex = true;
                    }
                }

                foreach (var col in AddedDynamicConvexColliders)
                {
                    col.enabled = true;
                }
            }
        }
Пример #4
0
 public void RegenerateFromMesh(Mesh mesh, GrabbitSettings settings)
 {
     if (colliderMeshes.ContainsKey(mesh))
     {
         RemoveMesh(mesh);
     }
     GenerateFromMesh(mesh, settings);
 }
Пример #5
0
        public void ConfigureSelectionMode(GrabbitSettings settings,
                                           ColliderMeshContainer colliderMeshContainer = null)
        {
            if (!Body)
            {
                Body = GetComponent <Rigidbody>();

                if (!Body)
                {
                    Body = gameObject.AddComponent <Rigidbody>();
                    WasRigidBodyAdded = true;
                }
                else
                {
                    RigidBodySave.RegisterRigidBody(Body);
                }
            }

            UndoState = new UndoState(Body);

            if (settings.UseDynamicNonConvexColliders && colliderMeshContainer)
            {
                //averaging the estimated added convex mesh colliders to 3
                AddedDynamicConvexColliders = new List <MeshCollider>(AddedStaticConvexColliders.Count * 3);

                foreach (var watcher in AddedCollidersList)
                {
                    foreach (var col in watcher.AddedStaticColliders)
                    {
                        if (!colliderMeshContainer.IsMeshDefined(col.sharedMesh))
                        {
                            //then it needs to be generated first
                            colliderMeshContainer.RegisterCollidersFromSelection(col, settings);
                        }

                        var meshes = colliderMeshContainer.GetMeshListAndRegenerateIfNeeded(col.sharedMesh, settings);

                        if (meshes.Count > 0)
                        {
                            foreach (var mesh in meshes)
                            {
                                AddMeshColliderToDynamicColliders(settings, col, mesh, watcher);
                            }
                        }
                        else
                        {
                            AddMeshColliderToDynamicColliders(settings, col, col.sharedMesh, watcher);
                        }
                    }
                }
            }
            else
            {
                AddedDynamicConvexColliders = new List <MeshCollider>(0);
            }

            SelectionConfigured = true;
        }
Пример #6
0
        private static VhacdGenerator CreateAndConfigureGenerator(GrabbitSettings settings)
        {
            var vhacdGenerator = new VhacdGenerator();

            vhacdGenerator.parameters.m_maxConvexHulls =
                (uint)settings.MaxMeshCollidersCreated;
            vhacdGenerator.parameters.m_resolution = (uint)settings.ColliderResolution;
            return(vhacdGenerator);
        }
Пример #7
0
        public bool ShouldRegenerate(MeshCollider collider, GrabbitSettings settings)
        {
            var mesh = collider.sharedMesh;

            if (!colliderMeshes.ContainsKey(mesh))
            {
                return(true);
            }

            return(colliderMeshes[mesh].resolution != settings.ColliderResolution);
        }
Пример #8
0
        public void RegenerateFromCollider(MeshCollider collider, GrabbitSettings settings)
        {
            var mesh = collider.sharedMesh;

            if (colliderMeshes.ContainsKey(mesh))
            {
                RemoveMesh(mesh);
            }

            RegisterCollidersFromSelection(collider, settings);
        }
Пример #9
0
        public List <Mesh> GetMeshListAndRegenerateIfNeeded(Mesh mesh, GrabbitSettings settings)
        {
            var list = colliderMeshes[mesh];

            if (list.resolution != settings.ColliderResolution || list.maxMeshCount != settings.MaxMeshCollidersCreated)
            {
                RegenerateFromMesh(mesh, settings);
            }

            return(list.Meshes);
        }
Пример #10
0
        public void RegisterCollidersFromMeshFiltersInScene(GrabbitSettings settings, params MeshFilter[] filters)
        {
            var vhacdGenerator = CreateAndConfigureGenerator(settings);
            var i = 0;

            foreach (var filter in filters)
            {
                var mesh = filter.sharedMesh;

                if (EditorUtility.DisplayCancelableProgressBar(
                        "Grabbit Is Generating Colliders In The Scene",
                        $"Analyzing {mesh.name} ({i + 1} out of {filters.Length})",
                        (float)i / filters.Length))
                {
                    break;
                }

                //TODO: check for hidden assets and so on
                if (colliderMeshes.ContainsKey(mesh))
                {
                    continue;
                }

                var meshes = vhacdGenerator.GenerateConvexMeshes(mesh);

                foreach (var collidingMesh in meshes)
                {
                    AssetDatabase.AddObjectToAsset(collidingMesh, this);
                }

                colliderMeshes.Add(mesh,
                                   new MeshList
                {
                    maxMeshCount = settings.MaxMeshCollidersCreated, resolution = settings.ColliderResolution,
                    Meshes       = meshes
                });
                i++;
            }

            EditorUtility.ClearProgressBar();
            EditorUtility.SetDirty(this);
            AssetDatabase.SaveAssets();
        }
        public static void GetOrFetchSettings()
        {
            if (!CurrentSettings)
            {
                var ids = AssetDatabase.FindAssets("t:GrabbitSettings");
                if (ids.Length == 0)
                {
                    return;
                }

                CurrentSettings = AssetDatabase.LoadAssetAtPath <GrabbitSettings>(AssetDatabase.GUIDToAssetPath(ids[0]));

                if (!CurrentSettings)
                {
                    return;
                }
            }

            return;
        }
Пример #12
0
        private void FetchSettings()
        {
            if (!CurrentSettings)
            {
                var ids = AssetDatabase.FindAssets("t:GrabbitSettings");
                if (ids.Length == 0)
                {
                    Debug.LogWarning("No Grabbit settings found, please contact us on Discord!");
                    return;
                }

                CurrentSettings = AssetDatabase.LoadAssetAtPath <GrabbitSettings>(AssetDatabase.GUIDToAssetPath(ids[0]));

                if (!CurrentSettings)
                {
                    Debug.LogWarning("Grabbit could not locate the settings: please contact us on Discord!");
                }
                else
                {
                    CurrentSettings.InitializeStyle(true);
                }
            }

            if (!ColliderMeshContainer)
            {
                var ids = AssetDatabase.FindAssets("t:ColliderMeshContainer");
                if (ids.Length == 0)
                {
                    //   GUIHelper.DisplayMessage("Grabbit Error: No Collider Mesh Container, please create one!");
                    return;
                }

                ColliderMeshContainer =
                    AssetDatabase.LoadAssetAtPath <ColliderMeshContainer>(AssetDatabase.GUIDToAssetPath(ids[0]));
            }

            titleContent = new GUIContent(" Grabbit", CurrentSettings.GrabbitLogo, "It's a Rabbit that Grabs!");
        }
Пример #13
0
        private void AddMeshColliderToDynamicColliders(GrabbitSettings settings, MeshCollider col, Mesh mesh,
                                                       GrabbitCollidersWatcher watcher)
        {
            var existingColliders = col.gameObject.GetComponents <MeshCollider>();

            //check for existing colliders to not have to duplicate the ones of other handlers, if subobjects are involved
            var existing = existingColliders.FirstOrDefault(_ => _.sharedMesh == mesh);

            var mc = existing ? existing : col.gameObject.AddComponent <MeshCollider>();

            if (!existing)
            {
                if (!settings.useLowQualityConvexCollidersOnSelection)
                {
                    mc.cookingOptions &= ~MeshColliderCookingOptions.UseFastMidphase;
                }
                mc.sharedMesh = mesh;
                mc.convex     = true;
            }

            AddedDynamicConvexColliders.Add(mc);
            watcher.AddedDynamicColliders.Add(mc);
        }
Пример #14
0
 private List <Mesh> NonThreadedMeshGeneration(Mesh mesh, GrabbitSettings settings, VhacdGenerator vhacdGenerator)
 {
     //TODO: check for hidden assets and so on
     return(vhacdGenerator.GenerateConvexMeshes(mesh));
 }
Пример #15
0
        private void GenerateFromMesh(Mesh mesh, GrabbitSettings settings)
        {
            if (IsMeshDefined(mesh))
            {
                return;
            }

            var vhacdGenerator = CreateAndConfigureGenerator(settings);

            vhacdGenerator.ThreadedGenerateConvexMeshes(mesh);

            bool shouldGenerate = true;
            int  i = 0;

            while (!vhacdGenerator.IsThreadedMeshGenerationDone())
            {
                if (EditorUtility.DisplayCancelableProgressBar(
                        $"Grabbit is generating colliders for {mesh.name}",
                        "Generating...",
                        (float)i / MaxEstimatedTickForGeneration))
                {
                    vhacdGenerator.AbortThread();
                    shouldGenerate = false;
                    break;
                }

                i++;
            }

            if (shouldGenerate)
            {
                vhacdGenerator.GenerateMeshesAfterThreadCompletion();
            }
            List <Mesh> meshes = shouldGenerate ? vhacdGenerator.RetrieveThreadMesh() : new List <Mesh>();

            //NonThreadedMeshGeneration(mesh, settings, vhacdGenerator);

            if (meshes.Count == 0 && mesh.vertexCount > 0)
            {
                if (shouldGenerate)
                {
                    Debug.LogWarning(
                        $"Grabbit Warning: The collider generation failed for {mesh.name}, only convex collider available.");
                }
                else
                {
                    Debug.Log(
                        $"Grabbit Warning: The collider generation was cancelled for {mesh.name}, only convex collider available.");
                }

                EditorUtility.ClearProgressBar();
            }

            foreach (var collidingMesh in meshes)
            {
                AssetDatabase.AddObjectToAsset(collidingMesh, this);
            }

            if (colliderMeshes.ContainsKey(mesh))
            {
                colliderMeshes[mesh] = new MeshList
                {
                    maxMeshCount = settings.MaxMeshCollidersCreated, resolution = settings.ColliderResolution,
                    Meshes       = meshes
                };
            }
            else
            {
                colliderMeshes.Add(mesh,
                                   new MeshList
                {
                    maxMeshCount = settings.MaxMeshCollidersCreated, resolution = settings.ColliderResolution,
                    Meshes       = meshes
                });
            }


            EditorUtility.DisplayProgressBar(
                $"Grabbit is generating colliders for {mesh.name}",
                "Generating...",
                1);
            EditorUtility.ClearProgressBar();
            EditorUtility.SetDirty(this);
        }
Пример #16
0
        public void RegisterCollidersFromSelection(MeshCollider collider, GrabbitSettings settings)
        {
            var mesh = collider.sharedMesh;

            GenerateFromMesh(mesh, settings);
        }
Пример #17
0
        public void ActivateSelectionMode(GrabbitSettings settings, ColliderMeshContainer colliderMeshContainer = null)
        {
            lastSettings = settings;

            //raises the overall perfs
            InternalEditorUtility.SetIsInspectorExpanded(this, true);
            InternalEditorUtility.SetIsInspectorExpanded(transform, false);
            InternalEditorUtility.SetIsInspectorExpanded(Body, false);

            if (!SelectionConfigured)
            {
                ConfigureSelectionMode(settings, colliderMeshContainer);
            }

            IsInSelectionMode = true;

            if (settings.UsePredefinedColliders && PreExistingColliders.Count > 0)
            {
                foreach (var preExistingCollider in PreExistingColliders)
                {
                    preExistingCollider.enabled = true;
                }

                foreach (var col in AddedStaticConvexColliders)
                {
                    col.enabled = false;
                    if (!settings.UseDynamicNonConvexColliders)
                    {
                        col.convex = true;
                        InternalEditorUtility.SetIsInspectorExpanded(col, false);
                    }
                }


                foreach (var col in AddedDynamicConvexColliders)
                {
                    col.enabled = false;
                    InternalEditorUtility.SetIsInspectorExpanded(col, false);
                }
            }
            else
            {
                foreach (var col in AddedStaticConvexColliders)
                {
                    if (settings.UseDynamicNonConvexColliders)
                    {
                        col.enabled = false;
                    }
                    else
                    {
                        col.convex = true;
                        InternalEditorUtility.SetIsInspectorExpanded(col, false);
                    }
                }

                foreach (var col in AddedDynamicConvexColliders)
                {
                    col.enabled = true;
                    InternalEditorUtility.SetIsInspectorExpanded(col, false);
                }
            }

            //ensuring sub rigidbodies are detected and handled properly
            foreach (var body in GetComponentsInChildren <Rigidbody>())
            {
                if (body == Body)
                {
                    continue;
                }
                body.collisionDetectionMode = CollisionDetectionMode.ContinuousSpeculative;
                body.isKinematic            = false;
                body.angularDrag            = 99999;

                FixedJoint join = gameObject.AddComponent <FixedJoint>();
                join.connectedBody   = body;
                join.enableCollision = false;
                AddedJoints.Add(join);
            }


            Body.isKinematic            = false;
            Body.useGravity             = false;
            Body.detectCollisions       = true;
            Body.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
            Body.WakeUp();

            UndoState.RecordUndoBodyMove(Body);
            enabled           = true;
            IsInSelectionMode = true;
        }