Beispiel #1
0
        public void ClearMeshCombineJobs()
        {
#if MCSCaves
            if (removeOverlappingTriangles)
            {
                CreateOverlapColliders.DestroyOverlapColliders(overlappingCollidersGO);
            }
#endif

            meshCombineJobs.Clear();
            totalMeshCombineJobs = 0;
        }
Beispiel #2
0
        public void ExecuteOnCombiningReady()
        {
            if (OnCombiningReady != null)
            {
                OnCombiningReady();
            }

            totalMeshCombineJobs = 0;

#if MCSCaves
            if (removeOverlappingTriangles)
            {
                CreateOverlapColliders.DestroyOverlapColliders(overlappingCollidersGO);
            }
#endif

            stopwatch.Stop();
            combineTime = (float)stopwatch.ElapsedMilliseconds / 1000;
        }
        static public void RemoveOverlap(Transform t, MeshCombineJobManager.MeshCombineJob meshCombineJob, MeshCache.SubMeshCache newMeshCache, ref byte[] vertexIsInsideCollider)
        {
            if (vertexIsInsideCollider == null)
            {
                vertexIsInsideCollider = new byte[65534];
            }

            int overlapLayerMask = meshCombineJob.meshCombiner.overlapLayerMask;
            int voxelizeLayer    = meshCombineJob.meshCombiner.voxelizeLayer;

            int voxelizeLayerMask = 1 << voxelizeLayer;
            int lodGroupLayer     = meshCombineJob.meshCombiner.lodGroupLayer;
            int lodGroupLayerMask = 1 << lodGroupLayer;

            int lodLevel = meshCombineJob.meshObjectsHolder.lodLevel;

            Vector3 cellOffset = meshCombineJob.position;

            CreateOverlapColliders.newT.position = -cellOffset;
            t.parent.position -= cellOffset;

#if !UNITY_2017
            if (!Physics.autoSyncTransforms)
            {
                Physics.SyncTransforms();
            }
#endif

            CreateOverlapColliders.EnableLodLevelCollider(lodLevel, lodGroupLayer);

            Vector3[] newVertices  = newMeshCache.vertices;
            int[]     newTriangles = newMeshCache.triangles;

            FastList <MeshObject> meshObjects = meshCombineJob.meshObjectsHolder.meshObjects;

            int startIndex = meshCombineJob.startIndex;
            int endIndex   = meshCombineJob.endIndex;

            bool queriesHitBackfaces = Physics.queriesHitBackfaces;
            Physics.queriesHitBackfaces = true;

            toCombineGos.Clear();
            for (int i = startIndex; i < endIndex; i++)
            {
                toCombineGos.Add(meshObjects.items[i].cachedGO.go);
            }

            for (int a = startIndex; a < endIndex; a++)
            {
                MeshObject       meshObject = meshObjects.items[a];
                CachedGameObject cachedGO   = meshObject.cachedGO;

                GameObject go;
                CreateOverlapColliders.lookupOrigCollider.TryGetValue(cachedGO.go, out go);

                int startTriangleIndex = meshObject.startNewTriangleIndex;
                int endTriangleIndex   = meshObject.newTriangleCount + startTriangleIndex;

                Bounds bounds = cachedGO.mr.bounds;
                bounds.center -= cellOffset;

                int oldLayer = 0;

                if (go)
                {
                    oldLayer = go.layer;
                    go.layer = voxelizeLayer;
                }

                colliders.SetCount(Physics.OverlapBoxNonAlloc(bounds.center, bounds.extents, colliders.items, Quaternion.identity, overlapLayerMask));

                if (go)
                {
                    go.layer = oldLayer;
                }

                // Debug.Log("collider Count " + colliders.Count);

                if (colliders.Count == 0)
                {
                    continue;
                }

                collidersInfo.SetCount(colliders.Count);

                for (int i = 0; i < colliders.Count; i++)
                {
                    GameObject colliderGo = colliders.items[i].gameObject;
                    collidersInfo.items[i] = new ColliderInfo()
                    {
                        layer = colliderGo.layer, go = colliderGo
                    };
                    colliderGo.layer = voxelizeLayer;
                }

                // Debug.Log("start " + startTriangleIndex + " end " + endTriangleIndex);

                for (int i = startTriangleIndex; i < endTriangleIndex; i += 3)
                {
                    int vertIndexA = newTriangles[i];
                    if (vertIndexA == -1)
                    {
                        continue;
                    }

                    byte isInsideVoxel = vertexIsInsideCollider[vertIndexA];

                    if (isInsideVoxel != outsideVoxel)
                    {
                        tri.a = t.TransformPoint(newVertices[vertIndexA]);

                        hitInfos.SetCount(Physics.RaycastNonAlloc(tri.a, Vector3.up, hitInfos.items, Mathf.Infinity, voxelizeLayerMask));

                        if (!AnythingInside())
                        {
                            vertexIsInsideCollider[vertIndexA] = outsideVoxel; continue;
                        }

                        tri.b = t.TransformPoint(newVertices[newTriangles[i + 1]]);
                        tri.c = t.TransformPoint(newVertices[newTriangles[i + 2]]);

                        if (LinecastAll(tri.a, tri.b, voxelizeLayerMask) && IntersectAny())
                        {
                            continue;
                        }
                        if (LinecastAll(tri.b, tri.c, voxelizeLayerMask) && IntersectAny())
                        {
                            continue;
                        }
                        if (LinecastAll(tri.c, tri.a, voxelizeLayerMask) && IntersectAny())
                        {
                            continue;
                        }
                        if (LinecastAll(tri.a, (tri.b + tri.c) * 0.5f, voxelizeLayerMask) && IntersectAny())
                        {
                            continue;
                        }
                        if (LinecastAll(tri.b, (tri.c + tri.a) * 0.5f, voxelizeLayerMask) && IntersectAny())
                        {
                            continue;
                        }
                        if (LinecastAll(tri.c, (tri.a + tri.b) * 0.5f, voxelizeLayerMask) && IntersectAny())
                        {
                            continue;
                        }

                        //tri.Calc();
                        //Vector3 origin = tri.a + (tri.dirAb / 2) + ((tri.c - tri.h1) / 2);

                        //if (Physics.CheckBox(origin, new Vector3(0.05f, tri.h, tri.ab) / 2, Quaternion.LookRotation(tri.dirAb, tri.dirAc), voxelizeLayerMask))
                        //{
                        //    colliderGO.layer = oldLayer;
                        //    continue;
                        //}

                        if (CreateOverlapColliders.foundLodGroup && AreAllHitInfosALodGroup() && !IsOneColliderGOInToCombineGos() && !CheckAnyInsideOfLodGroups(lodGroupLayerMask, lodLevel))
                        {
                            continue;
                        }

                        meshCombineJob.trianglesRemoved += 3;
                        newTriangles[i] = -1;
                    }
                }

                for (int i = 0; i < colliders.Count; i++)
                {
                    ColliderInfo colliderInfo = collidersInfo.items[i];
                    colliderInfo.go.layer = colliderInfo.layer;
                }
            }

            Array.Clear(vertexIsInsideCollider, 0, newVertices.Length);
            // Debug.Log("Removed " + meshCombineJob.trianglesRemoved);

            newMeshCache.triangles      = newTriangles;
            Physics.queriesHitBackfaces = queriesHitBackfaces;

            t.parent.position += cellOffset;
        }
Beispiel #4
0
        public void CombineAll()
        {
            stopwatch.Reset();
            stopwatch.Start();

#if MCSCaves
            RemoveOverlappingTris.triangles.Clear();
#endif

            addMeshCollidersList.Clear();
            unreadableMeshes.Clear();
            selectImportSettingsMeshes.Clear();

            AddObjectsAutomatically();
            if (!octreeContainsObjects)
            {
                return;
            }

            SetOriginalCollidersActive(false);

#if MCSCaves
            if (removeOverlappingTriangles)
            {
                if (CreateOverlapColliders.IsAnythingOnFreeLayers(voxelizeLayer, lodGroupLayer))
                {
                    return;
                }
                CreateOverlapColliders.Create(transform, overlapLayerMask, lodGroupLayer, ref overlappingCollidersGO);
            }
#endif

            validRebakeLighting = rebakeLighting && !validCopyBakedLighting && !Application.isPlaying && Application.isEditor;

            totalVertices = totalTriangles = originalTotalVertices = originalTotalTriangles = originalDrawCalls = newDrawCalls = 0;

            for (int i = 0; i < lodParentHolders.Length; i++)
            {
                LodParentHolder lodParentHolder = lodParentHolders[i];
                if (!lodParentHolder.found)
                {
                    continue;
                }

                if (lodParentHolder.go == null)
                {
                    lodParentHolder.Create(this, i);
                }

                octree.CombineMeshes(this, i);
            }

            if (MeshCombineJobManager.instance.jobSettings.combineJobMode == MeshCombineJobManager.CombineJobMode.CombineAtOnce)
            {
                MeshCombineJobManager.instance.ExecuteJobs();
            }

            combinedActive = true;
            combined       = true;

            activeOriginal = false;
            ExecuteHandleObjects(activeOriginal, HandleComponent.Disable, HandleComponent.Disable, false);

#if UNITY_EDITOR
            UnityEditor.EditorUtility.SetDirty(this);
#endif
        }