static int FinishMeshUpdates(CSGTree tree,
                                     ref VertexBufferContents vertexBufferContents,
                                     ref Mesh.MeshDataArray meshDataArray,
                                     NativeList <ChiselMeshUpdate> colliderMeshUpdates,
                                     NativeList <ChiselMeshUpdate> debugHelperMeshes,
                                     NativeList <ChiselMeshUpdate> renderMeshes,
                                     JobHandle dependencies)
        {
            ChiselModel model = null;

            for (int m = 0; m < registeredModels.Count; m++)
            {
                if (!registeredModels[m])
                {
                    continue;
                }

                if (registeredModels[m].Node == tree)
                {
                    model = registeredModels[m];
                }
            }

            if (model == null)
            {
                if (meshDataArray.Length > 0)
                {
                    meshDataArray.Dispose();
                }
                meshDataArray = default;
                return(0);
            }

            if (!ChiselGeneratedObjects.IsValid(model.generated))
            {
                if (model.generated != null)
                {
                    model.generated.Destroy();
                }
                model.generated = ChiselGeneratedObjects.Create(model.gameObject);
            }

            var count = model.generated.FinishMeshUpdates(model, model.gameObject, ref meshDataArray, ref vertexBufferContents,
                                                          colliderMeshUpdates, debugHelperMeshes, renderMeshes,
                                                          dependencies);

            componentGenerator.Rebuild(model);
            PostUpdateModel?.Invoke(model);
            return(count);
        }
Example #2
0
        public static void UpdateModels()
        {
            // Update the tree meshes
            if (!CSGManager.Flush())
            {
                ChiselGeneratedComponentManager.DelayedUVGeneration();
                if (sharedUnityMeshes.FindAllUnusedUnityMeshes())
                {
                    sharedUnityMeshes.DestroyNonRecycledUnusedUnityMeshes();
                }
                return; // Nothing to update ..
            }

            for (int m = 0; m < registeredModels.Count; m++)
            {
                var model = registeredModels[m];
                if (!model)
                {
                    continue;
                }

                var tree = model.Node;

                // See if the tree has been modified
                if (!tree.Dirty)
                {
                    continue;
                }

                try
                {
                    if (PreUpdateModel != null)
                    {
                        PreUpdateModel(model);
                    }
                }
                catch (Exception ex) // if there's a bug in user-code we don't want to end up in a bad state
                {
                    Debug.LogException(ex);
                }

                UpdateModelMeshDescriptions(model);

                // Re-use existing UnityEngine.Mesh if they exist
                sharedUnityMeshes.ReuseExistingMeshes(model);

                updateList.Add(model);
            }

            bool modifications = false;

            try
            {
                // Find all meshes whose refCounts are 0
                sharedUnityMeshes.FindAllUnusedUnityMeshes();

                // Separate loop so we can re-use garbage collected UnityEngine.Meshes to avoid allocation costs

                for (int m = 0; m < updateList.Count; m++)
                {
                    var model = updateList[m];

                    // Generate new UnityEngine.Mesh instances and fill them with data from the CSG algorithm (if necessary)
                    //	note: reuses garbage collected meshes when possible
                    sharedUnityMeshes.CreateNewMeshes(model);

                    // Generate (or re-use) components and set them up properly
                    componentGenerator.Rebuild(model);
                }
            }
            finally
            {
                for (int m = 0; m < updateList.Count; m++)
                {
                    var model = updateList[m];
                    try
                    {
                        modifications = true;
                        if (PostUpdateModel != null)
                        {
                            PostUpdateModel(model);
                        }
                    }
                    catch (Exception ex) // if there's a bug in user-code we don't want to end up in a bad state
                    {
                        Debug.LogException(ex);
                    }
                }
                updateList.Clear();
            }

            // Destroy all meshes whose refCounts are 0
            sharedUnityMeshes.DestroyNonRecycledUnusedUnityMeshes();

            if (modifications)
            {
                if (PostUpdateModels != null)
                {
                    PostUpdateModels();
                }
            }
        }
        public static void UpdateModels()
        {
            // Update the tree meshes
            Profiler.BeginSample("Flush");
            try
            {
                if (!CSGManager.Flush())
                {
                    ChiselGeneratedComponentManager.DelayedUVGeneration();
                    return; // Nothing to update ..
                }
            }
            finally
            {
                Profiler.EndSample();
            }

#if UNITY_EDITOR
            ChiselGeneratedComponentManager.OnVisibilityChanged();
#endif


            for (int m = 0; m < registeredModels.Count; m++)
            {
                var model = registeredModels[m];
                if (!model)
                {
                    continue;
                }

                var tree = model.Node;

                // See if the tree has been modified
                if (!tree.Dirty)
                {
                    continue;
                }

                Profiler.BeginSample("UpdateModelMeshDescriptions");
                UpdateModelMeshDescriptions(model);
                Profiler.EndSample();

                updateList.Add(model);
            }

            bool modifications = false;
            try
            {
                for (int m = 0; m < updateList.Count; m++)
                {
                    var model = updateList[m];

                    // Generate (or re-use) components and set them up properly
                    Profiler.BeginSample("componentGenerator.Rebuild");
                    componentGenerator.Rebuild(model);
                    Profiler.EndSample();
                }
            }
            finally
            {
                for (int m = 0; m < updateList.Count; m++)
                {
                    var model = updateList[m];
                    try
                    {
                        modifications = true;
                        Profiler.BeginSample("PostUpdateModel");
                        PostUpdateModel?.Invoke(model);
                        Profiler.EndSample();
                    }
                    catch (Exception ex) // if there's a bug in user-code we don't want to end up in a bad state
                    {
                        Debug.LogException(ex);
                    }
                }
                updateList.Clear();
            }

            if (modifications)
            {
                Profiler.BeginSample("PostUpdateModels");
                PostUpdateModels?.Invoke();
                Profiler.EndSample();
            }
        }