Ejemplo n.º 1
0
        public IEnumerator CreateBrushContainerAssetWithBrushMaterial_ModifyBrushMaterialPhysicsMaterial_InstanceChangedEventIsCalled()
        {
            var newPhysicsMaterial = new PhysicMaterial();

            var hasBeenCalled = false;
            OnBrushContainerAssetDelegate localDelegate = delegate(ChiselBrushContainerAsset brushContainerAsset)
            { hasBeenCalled = true; };


            using (var newBrushMaterial = ChiselBrushMaterial.CreateInstance())
            {
                newBrushMaterial.PhysicsMaterial = null;
                var newBrushContainerAsset = CreateBox(Vector3.one, newBrushMaterial);
                yield return(null);

                ChiselBrushContainerAssetManager.OnBrushMeshInstanceChanged -= localDelegate;
                ChiselBrushContainerAssetManager.OnBrushMeshInstanceChanged += localDelegate;
                newBrushMaterial.PhysicsMaterial = newPhysicsMaterial;
                yield return(null);

                Assert.IsTrue(hasBeenCalled);

                ChiselBrushContainerAssetManager.OnBrushMeshInstanceChanged -= localDelegate;
                UnityEngine.Object.DestroyImmediate(newBrushContainerAsset);
                UnityEngine.Object.DestroyImmediate(newPhysicsMaterial);
            }
        }
Ejemplo n.º 2
0
        public IEnumerator CreateBrushMaterialWithPhysicMaterial_ChangePhysicMaterial_ManagerOnlyKnowsNewMaterial()
        {
            using (var newBrushMaterial = ChiselBrushMaterial.CreateInstance())
            {
                var newPhysicsMaterial1 = new PhysicMaterial();
                var newPhysicsMaterial2 = new PhysicMaterial();

                newBrushMaterial.PhysicsMaterial = newPhysicsMaterial1;
                yield return(null);

                var foundPhysicsMaterial = ChiselBrushMaterialManager.GetPhysicsMaterialByInstanceID(newPhysicsMaterial2.GetInstanceID(), false);

                Assert.AreNotEqual(newPhysicsMaterial1, newPhysicsMaterial2);
                Assert.AreEqual(newPhysicsMaterial1, ChiselBrushMaterialManager.GetPhysicsMaterialByInstanceID(newPhysicsMaterial1.GetInstanceID(), false));
                Assert.AreEqual(1, ChiselBrushMaterialManager.GetPhysicsMaterialRefCountByInstanceID(newPhysicsMaterial1.GetInstanceID()));
                LogAssert.Expect(LogType.Error, new Regex("Could not find"));
                Assert.IsNull(foundPhysicsMaterial);
                newBrushMaterial.PhysicsMaterial = newPhysicsMaterial2;
                yield return(null);

                ChiselBrushMaterialManager.Update();

                LogAssert.Expect(LogType.Error, new Regex("Could not find"));
                Assert.IsNull(ChiselBrushMaterialManager.GetPhysicsMaterialByInstanceID(newPhysicsMaterial1.GetInstanceID(), false));
                Assert.AreEqual(newPhysicsMaterial2, ChiselBrushMaterialManager.GetPhysicsMaterialByInstanceID(newPhysicsMaterial2.GetInstanceID(), false));
                Assert.AreEqual(1, ChiselBrushMaterialManager.GetPhysicsMaterialRefCountByInstanceID(newPhysicsMaterial2.GetInstanceID()));

                UnityEngine.Object.DestroyImmediate(newPhysicsMaterial1);
                UnityEngine.Object.DestroyImmediate(newPhysicsMaterial2);
            }
        }
Ejemplo n.º 3
0
        public IEnumerator CreateBrushWithBrushContainerAssetWithBrushMaterial_ModifyBrushMaterial_BrushIsDirty()
        {
            using (var newBrushMaterial = ChiselBrushMaterial.CreateInstance())
            {
                newBrushMaterial.LayerUsage = LayerUsageFlags.None;
                var newBrushContainerAsset = CreateBox(Vector3.one, newBrushMaterial);
                var brushGameObject        = EditorUtility.CreateGameObjectWithHideFlags("Brush", HideFlags.None);
                var brush = brushGameObject.AddComponent <ChiselBrush>();
                brush.BrushContainerAsset = newBrushContainerAsset;

                yield return(null);

                ChiselBrushContainerAssetManager.Update();
                ChiselNodeHierarchyManager.Update();

                newBrushMaterial.LayerUsage = LayerUsageFlags.Renderable;
                ChiselBrushContainerAssetManager.Update();

                Assert.IsTrue(brush.Dirty);
                yield return(null);

                UnityEngine.Object.DestroyImmediate(brushGameObject);
                UnityEngine.Object.DestroyImmediate(newBrushContainerAsset);
            }
        }
Ejemplo n.º 4
0
        public IEnumerator RemoveAndAddPhysicsMaterialToAnotherBrushMaterial_MaterialStillRegistered()
        {
            using (ChiselBrushMaterial newBrushMaterial1 = ChiselBrushMaterial.CreateInstance(), newBrushMaterial2 = ChiselBrushMaterial.CreateInstance())
            {
                var newPhysicsMaterial1 = new PhysicMaterial();
                var newPhysicsMaterial2 = new PhysicMaterial();

                newBrushMaterial1.PhysicsMaterial = newPhysicsMaterial1;
                newBrushMaterial2.PhysicsMaterial = newPhysicsMaterial2;
                yield return(null);

                ChiselBrushMaterialManager.Update();

                Assert.AreEqual(newPhysicsMaterial1, ChiselBrushMaterialManager.GetPhysicsMaterialByInstanceID(newPhysicsMaterial1.GetInstanceID(), false));
                Assert.AreEqual(newPhysicsMaterial2, ChiselBrushMaterialManager.GetPhysicsMaterialByInstanceID(newPhysicsMaterial2.GetInstanceID(), false));
                Assert.AreEqual(1, ChiselBrushMaterialManager.GetPhysicsMaterialRefCountByInstanceID(newPhysicsMaterial1.GetInstanceID()));
                Assert.AreEqual(1, ChiselBrushMaterialManager.GetPhysicsMaterialRefCountByInstanceID(newPhysicsMaterial2.GetInstanceID()));
                newBrushMaterial1.PhysicsMaterial = newPhysicsMaterial2;
                newBrushMaterial2.PhysicsMaterial = newPhysicsMaterial1;
                yield return(null);

                ChiselBrushMaterialManager.Update();

                Assert.AreEqual(newPhysicsMaterial1, ChiselBrushMaterialManager.GetPhysicsMaterialByInstanceID(newPhysicsMaterial1.GetInstanceID(), false));
                Assert.AreEqual(newPhysicsMaterial2, ChiselBrushMaterialManager.GetPhysicsMaterialByInstanceID(newPhysicsMaterial2.GetInstanceID(), false));
                Assert.AreEqual(1, ChiselBrushMaterialManager.GetPhysicsMaterialRefCountByInstanceID(newPhysicsMaterial1.GetInstanceID()));
                Assert.AreEqual(1, ChiselBrushMaterialManager.GetPhysicsMaterialRefCountByInstanceID(newPhysicsMaterial2.GetInstanceID()));

                UnityEngine.Object.DestroyImmediate(newPhysicsMaterial1);
                UnityEngine.Object.DestroyImmediate(newPhysicsMaterial2);
            }
        }
 public static void RegisterAllSurfaces(ChiselBrushContainerAsset brushContainerAsset)
 {
     if (!brushContainerAsset || brushContainerAsset.SubMeshCount == 0)
     {
         return;
     }
     foreach (var brushMesh in brushContainerAsset.BrushMeshes)
     {
         if (brushMesh.polygons == null)
         {
             continue;
         }
         foreach (var polygon in brushMesh.polygons)
         {
             if (polygon.surface == null)
             {
                 continue;
             }
             if (polygon.surface.brushMaterial == null)
             {
                 polygon.surface.brushMaterial = ChiselBrushMaterial.CreateInstance();
                 if (polygon.surface.brushMaterial != null)
                 {
                     polygon.surface.brushMaterial.LayerUsage      = polygon.surface.brushMaterial.LayerUsage;
                     polygon.surface.brushMaterial.PhysicsMaterial = polygon.surface.brushMaterial.PhysicsMaterial;
                     polygon.surface.brushMaterial.RenderMaterial  = polygon.surface.brushMaterial.RenderMaterial;
                 }
             }
             ChiselBrushMaterialManager.Register(polygon.surface.brushMaterial);
         }
     }
 }
Ejemplo n.º 6
0
        static BrushMeshInstance CreateBox(Vector3 size, CSGOperationType operation = CSGOperationType.Additive, Material material = null)
        {
            if (material == null)
            {
                material = material2;
            }
            var       brushMaterial = ChiselBrushMaterial.CreateInstance(material);
            BrushMesh brushMesh     = TestUtility.CreateBox(size, brushMaterial);

            return(BrushMeshInstance.Create(brushMesh));
        }
Ejemplo n.º 7
0
        public IEnumerator CreateBrushMaterial_BrushMaterialIsPartOfManager()
        {
            using (var newBrushMaterial = ChiselBrushMaterial.CreateInstance())
            {
                yield return(null);

                ChiselBrushMaterialManager.Update();

                Assert.True(ChiselBrushMaterialManager.IsRegistered(newBrushMaterial));
            }
        }
Ejemplo n.º 8
0
        public IEnumerator CreateBrushContainerAsset_HasValidInstance()
        {
            using (var newBrushMaterial = ChiselBrushMaterial.CreateInstance())
            {
                var newBrushContainerAsset = CreateBox(Vector3.one, newBrushMaterial);
                Assert.IsFalse(newBrushContainerAsset.Instances != null && newBrushContainerAsset.Instances[0].Valid);
                yield return(null);

                ChiselBrushContainerAssetManager.Update();

                //Assert.False(ChiselBrushContainerAssetManager.IsInUpdateQueue(newBrushContainerAsset)); // should already be done
                Assert.IsTrue(newBrushContainerAsset.Instances != null && newBrushContainerAsset.Instances[0].Valid);
                UnityEngine.Object.DestroyImmediate(newBrushContainerAsset);
            }
        }
Ejemplo n.º 9
0
        public IEnumerator CreateBrushMaterial_Destroy_BrushMaterialIsNotPartOfManager()
        {
            using (var newBrushMaterial = ChiselBrushMaterial.CreateInstance())
            {
                yield return(null);

                ChiselBrushMaterialManager.Update();

                newBrushMaterial.Dispose();
                yield return(null);

                ChiselBrushMaterialManager.Update();

                Assert.False(ChiselBrushMaterialManager.IsRegistered(newBrushMaterial));
            }
        }
Ejemplo n.º 10
0
        public IEnumerator CreateBrushMaterial_ChangeUsageFlag_BrushMaterialChangeEventIsCalled()
        {
            using (var newBrushMaterial = ChiselBrushMaterial.CreateInstance())
            {
                var hasBeenCalled = false;
                OnBrushMaterialDelegate localDelegate = delegate(ChiselBrushMaterial brushMaterial) { hasBeenCalled = true; };
                newBrushMaterial.LayerUsage = LayerUsageFlags.None;
                ChiselBrushMaterialManager.OnBrushMaterialChanged -= localDelegate;
                ChiselBrushMaterialManager.OnBrushMaterialChanged += localDelegate;
                yield return(null);

                newBrushMaterial.LayerUsage = LayerUsageFlags.Collidable;
                Assert.IsTrue(hasBeenCalled);

                ChiselBrushMaterialManager.OnBrushMaterialChanged -= localDelegate;
            }
        }
Ejemplo n.º 11
0
        public IEnumerator CreateBrushMaterial_BrushMaterialAddedEventIsCalled()
        {
            var hasBeenCalled = false;
            OnBrushMaterialDelegate localDelegate = delegate(ChiselBrushMaterial brushMaterial)
            { hasBeenCalled = true; };

            ChiselBrushMaterialManager.OnBrushMaterialAdded -= localDelegate;
            ChiselBrushMaterialManager.OnBrushMaterialAdded += localDelegate;
            yield return(null);

            using (var newBrushMaterial = ChiselBrushMaterial.CreateInstance())
            {
                Assert.IsTrue(hasBeenCalled);

                ChiselBrushMaterialManager.OnBrushMaterialAdded -= localDelegate;
            }
        }
Ejemplo n.º 12
0
        public IEnumerator CreateBrushMaterialWithPhysicMaterial_ManagerKnowsMaterial()
        {
            using (var newBrushMaterial = ChiselBrushMaterial.CreateInstance())
            {
                var newPhysicsMaterial = new PhysicMaterial();

                newBrushMaterial.PhysicsMaterial = newPhysicsMaterial;
                yield return(null);

                ChiselBrushMaterialManager.Update();

                Assert.True(ChiselBrushMaterialManager.IsRegistered(newBrushMaterial));
                Assert.AreEqual(newPhysicsMaterial, ChiselBrushMaterialManager.GetPhysicsMaterialByInstanceID(newPhysicsMaterial.GetInstanceID(), false));
                Assert.AreEqual(1, ChiselBrushMaterialManager.GetPhysicsMaterialRefCountByInstanceID(newPhysicsMaterial.GetInstanceID()));

                UnityEngine.Object.DestroyImmediate(newPhysicsMaterial);
            }
        }
Ejemplo n.º 13
0
        public IEnumerator CreateBrushMaterialWithPhysicMateriall_RetrieveRenderMaterialWithPhysicsMaterialInstanceID_ReturnsNull()
        {
            using (var newBrushMaterial = ChiselBrushMaterial.CreateInstance())
            {
                var newPhysicsMaterial = new PhysicMaterial();

                newBrushMaterial.PhysicsMaterial = newPhysicsMaterial;
                yield return(null);

                ChiselBrushMaterialManager.Update();

                Assert.True(ChiselBrushMaterialManager.IsRegistered(newBrushMaterial));
                LogAssert.Expect(LogType.Error, new Regex("Trying to use PhysicMaterial with"));
                Assert.IsNull(ChiselBrushMaterialManager.GetRenderMaterialByInstanceID(newPhysicsMaterial.GetInstanceID(), false));

                UnityEngine.Object.DestroyImmediate(newPhysicsMaterial);
            }
        }
Ejemplo n.º 14
0
        public IEnumerator CreateBrushMaterialWithPhysicMaterial_DestroyBrushMaterial_ManagerDoesNotKnowMaterial()
        {
            using (var newBrushMaterial = ChiselBrushMaterial.CreateInstance())
            {
                var newPhysicsMaterial = new PhysicMaterial();

                newBrushMaterial.PhysicsMaterial = newPhysicsMaterial;
                newBrushMaterial.Dispose();
                yield return(null);

                ChiselBrushMaterialManager.Update();

                LogAssert.Expect(LogType.Error, new Regex("Could not find"));
                Assert.IsNull(ChiselBrushMaterialManager.GetPhysicsMaterialByInstanceID(newPhysicsMaterial.GetInstanceID(), false));

                UnityEngine.Object.DestroyImmediate(newPhysicsMaterial);
            }
        }
Ejemplo n.º 15
0
        public IEnumerator CreateTwoBrushMaterialsWithSamePhysicsMaterial_RefCountIsTwo()
        {
            using (ChiselBrushMaterial newBrushMaterial1 = ChiselBrushMaterial.CreateInstance(), newBrushMaterial2 = ChiselBrushMaterial.CreateInstance())
            {
                var newPhysicsMaterial = new PhysicMaterial();

                newBrushMaterial1.PhysicsMaterial = newPhysicsMaterial;
                newBrushMaterial2.PhysicsMaterial = newPhysicsMaterial;
                yield return(null);

                ChiselBrushMaterialManager.Update();

                Assert.AreEqual(newPhysicsMaterial, ChiselBrushMaterialManager.GetPhysicsMaterialByInstanceID(newPhysicsMaterial.GetInstanceID(), false));
                Assert.AreEqual(2, ChiselBrushMaterialManager.GetPhysicsMaterialRefCountByInstanceID(newPhysicsMaterial.GetInstanceID()));

                UnityEngine.Object.DestroyImmediate(newPhysicsMaterial);
            }
        }
Ejemplo n.º 16
0
        public IEnumerator CreateTwoBrushMaterialsWithSameRenderMaterial_DestroyOneBrushMaterial_ManagerKnowsMaterial()
        {
            using (ChiselBrushMaterial newBrushMaterial1 = ChiselBrushMaterial.CreateInstance(), newBrushMaterial2 = ChiselBrushMaterial.CreateInstance())
            {
                var newRenderMaterial = new Material(Shader.Find("Specular"));

                newBrushMaterial1.RenderMaterial = newRenderMaterial;
                newBrushMaterial2.RenderMaterial = newRenderMaterial;
                newBrushMaterial1.Dispose();
                yield return(null);

                ChiselBrushMaterialManager.Update();

                Assert.AreEqual(newRenderMaterial, ChiselBrushMaterialManager.GetRenderMaterialByInstanceID(newRenderMaterial.GetInstanceID(), false));
                Assert.AreEqual(1, ChiselBrushMaterialManager.GetRenderMaterialRefCountByInstanceID(newRenderMaterial.GetInstanceID()));

                UnityEngine.Object.DestroyImmediate(newRenderMaterial);
            }
        }
Ejemplo n.º 17
0
        public IEnumerator CreateBrushContainerAsset_Unregister_DoesNotHaveValidInstance()
        {
            using (var newBrushMaterial = ChiselBrushMaterial.CreateInstance())
            {
                var newBrushContainerAsset = CreateBox(Vector3.one, newBrushMaterial);
                Assert.IsFalse(newBrushContainerAsset.Instances != null && newBrushContainerAsset.Instances[0].Valid);
                yield return(null);

                ChiselBrushContainerAssetManager.Update();

                ChiselBrushContainerAssetManager.Unregister(newBrushContainerAsset);
                yield return(null);

                ChiselBrushContainerAssetManager.Update();

                //Assert.False(ChiselBrushContainerAssetManager.IsInUpdateQueue(newBrushContainerAsset)); // should already be done
                Assert.IsFalse(newBrushContainerAsset.Instances != null && newBrushContainerAsset.Instances[0].Valid);
            }
        }
Ejemplo n.º 18
0
        public IEnumerator CreateTwoBrushMaterialsWithSameRenderMaterial_DestroyBothBrushMaterials_ManagerDoesNotKnowMaterial()
        {
            using (ChiselBrushMaterial newBrushMaterial1 = ChiselBrushMaterial.CreateInstance(), newBrushMaterial2 = ChiselBrushMaterial.CreateInstance())
            {
                var newRenderMaterial = new Material(Shader.Find("Specular"));

                newBrushMaterial1.RenderMaterial = newRenderMaterial;
                newBrushMaterial2.RenderMaterial = newRenderMaterial;
                newBrushMaterial1.Dispose();
                newBrushMaterial2.Dispose();
                yield return(null);

                ChiselBrushMaterialManager.Update();

                LogAssert.Expect(LogType.Error, new Regex("Could not find"));
                Assert.IsNull(ChiselBrushMaterialManager.GetRenderMaterialByInstanceID(newRenderMaterial.GetInstanceID(), false));

                UnityEngine.Object.DestroyImmediate(newRenderMaterial);
            }
        }
Ejemplo n.º 19
0
        public IEnumerator CreateBrushWithBrushContainerAsset_GetUsedBrushContainerAssets_IsNotNull()
        {
            using (var newBrushMaterial = ChiselBrushMaterial.CreateInstance())
            {
                var newBrushContainerAsset = CreateBox(Vector3.one, newBrushMaterial);
                var brushGameObject        = EditorUtility.CreateGameObjectWithHideFlags("Brush", HideFlags.None);
                var brush = brushGameObject.AddComponent <ChiselBrush>();
                brush.BrushContainerAsset = newBrushContainerAsset;

                yield return(null);

                ChiselBrushContainerAssetManager.Update();
                ChiselNodeHierarchyManager.Update();

                Assert.IsNotNull(brush.GetUsedGeneratedBrushes());
                Assert.AreNotEqual(0, brush.GetUsedGeneratedBrushes());
                yield return(null);

                UnityEngine.Object.DestroyImmediate(newBrushContainerAsset);
            }
        }
Ejemplo n.º 20
0
        public IEnumerator CreateBrushMaterial_ChangePhysicsMaterial_BrushMaterialChangeEventIsCalled()
        {
            using (var newBrushMaterial = ChiselBrushMaterial.CreateInstance())
            {
                var newPhysicsMaterial = new PhysicMaterial();

                var hasBeenCalled = false;
                OnBrushMaterialDelegate localDelegate = delegate(ChiselBrushMaterial brushMaterial)
                { hasBeenCalled = true; };
                newBrushMaterial.PhysicsMaterial = null;
                ChiselBrushMaterialManager.OnBrushMaterialChanged -= localDelegate;
                ChiselBrushMaterialManager.OnBrushMaterialChanged += localDelegate;
                yield return(null);

                newBrushMaterial.PhysicsMaterial = newPhysicsMaterial;
                Assert.IsTrue(hasBeenCalled);

                ChiselBrushMaterialManager.OnBrushMaterialChanged -= localDelegate;
                UnityEngine.Object.DestroyImmediate(newPhysicsMaterial);
            }
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Imports the specified world into the SabreCSG model.
        /// </summary>
        /// <param name="model">The model to import into.</param>
        /// <param name="world">The world to be imported.</param>
        /// <param name="scale">The scale modifier.</param>
        public static void Import(Transform rootTransform, MapWorld world)
        {
            _conversionScale = 1.0f / _Scale;

            // create a material searcher to associate materials automatically.
            MaterialSearcher materialSearcher = new MaterialSearcher();

            var mapTransform = CreateGameObjectWithUniqueName(world.mapName, rootTransform);

            mapTransform.position = Vector3.zero;

            // Index of entities by trenchbroom id
            var entitiesById = new Dictionary <int, EntityContainer>();

            var layers = new List <EntityContainer>();

            for (int e = 0; e < world.Entities.Count; e++)
            {
                var entity = world.Entities[e];

                //EntityContainer eContainer = null;

                if (entity.tbId >= 0)
                {
                    var name       = String.IsNullOrEmpty(entity.tbName) ? "Unnamed" : entity.tbName;
                    var t          = CreateGameObjectWithUniqueName(name, mapTransform);
                    var eContainer = new EntityContainer(t, entity);
                    entitiesById.Add(entity.tbId, eContainer);

                    if (entity.tbType == "_tb_layer")
                    {
                        layers.Add(eContainer);
                        eContainer.transform.SetParent(null); // unparent until layers are sorted by sort index
                    }
                }
            }

            var defaultLayer = CreateGameObjectWithUniqueName("Default Layer", mapTransform);

            layers = layers.OrderBy(l => l.entity.tbLayerSortIndex).ToList(); // sort layers by layer sort index

            foreach (var l in layers)
            {
                l.transform.SetParent(mapTransform); // parent layers to map in order
            }

            bool valveFormat = world.valveFormat;

            // iterate through all entities.
            for (int e = 0; e < world.Entities.Count; e++)
            {
#if UNITY_EDITOR
                UnityEditor.EditorUtility.DisplayProgressBar("Importing Quake 1 Map", "Converting Quake 1 Entities To Brushes (" + (e + 1) + " / " + world.Entities.Count + ")...", e / (float)world.Entities.Count);
#endif
                MapEntity entity = world.Entities[e];

                Transform brushParent = mapTransform;

                bool isLayer   = false;
                bool isTrigger = false;

                if (entity.ClassName == "worldspawn")
                {
                    brushParent = defaultLayer;
                }
                else if (entity.tbType == "_tb_layer")
                {
                    isLayer = true;
                    if (entitiesById.TryGetValue(entity.tbId, out EntityContainer eContainer))
                    {
                        brushParent = eContainer.transform;
                    }
                }
                else if (entity.tbType == "_tb_group")
                {
                    if (entitiesById.TryGetValue(entity.tbId, out EntityContainer eContainer))
                    {
                        brushParent = eContainer.transform;
                    }
                }
                else
                {
                    if (entity.ClassName.Contains("trigger"))
                    {
                        isTrigger = true;
                    }

                    brushParent = CreateGameObjectWithUniqueName(entity.ClassName, mapTransform);
                }

                if (brushParent != mapTransform && brushParent != defaultLayer)
                {
                    if (entity.tbGroup > 0)
                    {
                        if (entitiesById.TryGetValue(entity.tbGroup, out EntityContainer eContainer))
                        {
                            brushParent.SetParent(eContainer.transform);
                        }
                    }
                    else if (entity.tbLayer > 0)
                    {
                        if (entitiesById.TryGetValue(entity.tbLayer, out EntityContainer eContainer))
                        {
                            brushParent.SetParent(eContainer.transform);
                        }
                    }
                    else if (!isLayer)
                    {
                        brushParent.SetParent(defaultLayer);
                    }
                }

                //if(entity.)

                if (entity.Brushes.Count == 0)
                {
                    continue;
                }

                var model = ChiselModelManager.CreateNewModel(brushParent);
                // var model = OperationsUtility.CreateModelInstanceInScene(brushParent);
                var parent = model.transform;

                if (isTrigger)
                {
                    //model.Settings = (model.Settings | ModelSettingsFlags.IsTrigger | ModelSettingsFlags.SetColliderConvex | ModelSettingsFlags.DoNotRender);
                }

                // iterate through all entity brushes.
                for (int i = 0; i < entity.Brushes.Count; i++)
                {
                    MapBrush brush = entity.Brushes[i];


                    // build a very large cube brush.
                    ChiselBrush go = ChiselComponentFactory.Create <ChiselBrush>(model);
                    go.definition.surfaceDefinition = new ChiselSurfaceDefinition();
                    go.definition.surfaceDefinition.EnsureSize(6);
                    BrushMesh brushMesh = new BrushMesh();
                    go.definition.brushOutline = brushMesh;
                    BrushMeshFactory.CreateBox(ref brushMesh, new Vector3(-4096, -4096, -4096), new Vector3(4096, 4096, 4096), in go.definition.surfaceDefinition);

                    // prepare for uv calculations of clip planes after cutting.
                    var planes        = new float4[brush.Sides.Count];
                    var planeSurfaces = new ChiselSurface[brush.Sides.Count];

                    // compute all the sides of the brush that will be clipped.
                    for (int j = brush.Sides.Count; j-- > 0;)
                    {
                        MapBrushSide side = brush.Sides[j];

                        // detect excluded polygons.
                        //if (IsExcludedMaterial(side.Material))
                        //polygon.UserExcludeFromFinal = true;
                        // detect collision-only brushes.
                        //if (IsInvisibleMaterial(side.Material))
                        //pr.IsVisible = false;

                        // find the material in the unity project automatically.
                        Material material;

                        string materialName = side.Material.Replace("*", "#");
                        material = materialSearcher.FindMaterial(new string[] { materialName });
                        if (material == null)
                        {
                            material = ChiselMaterialManager.DefaultFloorMaterial;
                        }

                        // create chisel surface for the clip.
                        ChiselSurface surface = new ChiselSurface();
                        surface.brushMaterial      = ChiselBrushMaterial.CreateInstance(material, ChiselMaterialManager.DefaultPhysicsMaterial);
                        surface.surfaceDescription = SurfaceDescription.Default;

                        // detect collision-only polygons.
                        if (IsInvisibleMaterial(side.Material))
                        {
                            surface.brushMaterial.LayerUsage &= ~LayerUsageFlags.RenderReceiveCastShadows;
                        }
                        // detect excluded polygons.
                        if (IsExcludedMaterial(side.Material))
                        {
                            surface.brushMaterial.LayerUsage &= LayerUsageFlags.CastShadows;
                            surface.brushMaterial.LayerUsage |= LayerUsageFlags.Collidable;
                        }

                        // calculate the clipping planes.
                        Plane clip = new Plane(go.transform.InverseTransformPoint(new Vector3(side.Plane.P1.X, side.Plane.P1.Z, side.Plane.P1.Y) * _conversionScale), go.transform.InverseTransformPoint(new Vector3(side.Plane.P2.X, side.Plane.P2.Z, side.Plane.P2.Y) * _conversionScale), go.transform.InverseTransformPoint(new Vector3(side.Plane.P3.X, side.Plane.P3.Z, side.Plane.P3.Y) * _conversionScale));
                        planes[j]        = new float4(clip.normal, clip.distance);
                        planeSurfaces[j] = surface;
                    }

                    // cut all the clipping planes out of the brush in one go.
                    brushMesh.Cut(planes, planeSurfaces);

                    // now iterate over the planes to calculate UV coordinates.
                    int[] indices = new int[brush.Sides.Count];
                    for (int k = 0; k < planes.Length; k++)
                    {
                        var   plane           = planes[k];
                        int   closestIndex    = 0;
                        float closestDistance = math.lengthsq(plane - brushMesh.planes[0]);
                        for (int j = 1; j < brushMesh.planes.Length; j++)
                        {
                            float testDistance = math.lengthsq(plane - brushMesh.planes[j]);
                            if (testDistance < closestDistance)
                            {
                                closestIndex    = j;
                                closestDistance = testDistance;
                            }
                        }
                        indices[k] = closestIndex;
                    }

                    for (int j = 0; j < indices.Length; j++)
                    {
                        brushMesh.planes[indices[j]] = planes[j];
                    }

                    for (int j = brush.Sides.Count; j-- > 0;)
                    {
                        MapBrushSide side = brush.Sides[j];

                        var surface  = brushMesh.polygons[indices[j]].surface;
                        var material = surface.brushMaterial.RenderMaterial;

                        // calculate the texture coordinates.
                        int w = 256;
                        int h = 256;
                        if (material.mainTexture != null)
                        {
                            w = material.mainTexture.width;
                            h = material.mainTexture.height;
                        }
                        var clip = new Plane(planes[j].xyz, planes[j].w);

                        if (world.valveFormat)
                        {
                            var uAxis = new VmfAxis(side.t1, side.Offset.X, side.Scale.X);
                            var vAxis = new VmfAxis(side.t2, side.Offset.Y, side.Scale.Y);
                            CalculateTextureCoordinates(go, surface, clip, w, h, uAxis, vAxis);
                        }
                        else
                        {
                            if (GetTextureAxises(clip, out MapVector3 t1, out MapVector3 t2))
                            {
                                var uAxis = new VmfAxis(t1, side.Offset.X, side.Scale.X);
                                var vAxis = new VmfAxis(t2, side.Offset.Y, side.Scale.Y);
                                CalculateTextureCoordinates(go, surface, clip, w, h, uAxis, vAxis);
                            }
                        }
                    }

                    try
                    {
                        // finalize the brush by snapping planes and centering the pivot point.
                        go.transform.position += brushMesh.CenterAndSnapPlanes();
                    }
                    catch
                    {
                        // Brush failed, destroy brush
                        GameObject.DestroyImmediate(go);
                    }
                }
            }

#if UNITY_EDITOR
            UnityEditor.EditorUtility.ClearProgressBar();
#endif
        }
        /// <summary>
        /// Imports the specified world into the Chisel model.
        /// </summary>
        /// <param name="model">The model to import into.</param>
        /// <param name="world">The world to be imported.</param>
        public static void Import(ChiselModel model, VmfWorld world)
        {
            // create a material searcher to associate materials automatically.
            MaterialSearcher materialSearcher         = new MaterialSearcher();
            HashSet <string> materialSearcherWarnings = new HashSet <string>();

            // iterate through all world solids.
            for (int i = 0; i < world.Solids.Count; i++)
            {
#if UNITY_EDITOR
                UnityEditor.EditorUtility.DisplayProgressBar("Chisel: Importing Source Engine Map (1/3)", "Converting Hammer Solids To Chisel Brushes (" + (i + 1) + " / " + world.Solids.Count + ")...", i / (float)world.Solids.Count);
#endif
                VmfSolid solid = world.Solids[i];

                // don't add triggers to the scene.
                if (solid.Sides.Count > 0 && IsSpecialMaterial(solid.Sides[0].Material))
                {
                    continue;
                }

                // HACK: Fix me in the future!
                // HACK: Chisel doesn't support collision brushes yet- skip them completely!
                if (solid.Sides.Count > 0 && IsInvisibleMaterial(solid.Sides[0].Material))
                {
                    continue;
                }
                // HACK: Fix me in the future!

                // build a very large cube brush.
                ChiselBrush go = ChiselComponentFactory.Create <ChiselBrush>(model);
                go.definition.surfaceDefinition = new ChiselSurfaceDefinition();
                go.definition.surfaceDefinition.EnsureSize(6);
                BrushMesh brushMesh = new BrushMesh();
                go.definition.brushOutline = brushMesh;
                BrushMeshFactory.CreateBox(ref brushMesh, new Vector3(-4096, -4096, -4096), new Vector3(4096, 4096, 4096), in go.definition.surfaceDefinition);

                // prepare for any displacements.
                List <DisplacementSide> DisplacementSurfaces = new List <DisplacementSide>();

                // prepare for uv calculations of clip planes after cutting.
                var planes        = new float4[solid.Sides.Count];
                var planeSurfaces = new ChiselSurface[solid.Sides.Count];

                // compute all the sides of the brush that will be clipped.
                for (int j = solid.Sides.Count; j-- > 0;)
                {
                    VmfSolidSide side = solid.Sides[j];

                    // detect excluded polygons.
                    //if (IsExcludedMaterial(side.Material))
                    //polygon.UserExcludeFromFinal = true;
                    // detect collision-only brushes.
                    //if (IsInvisibleMaterial(side.Material))
                    //pr.IsVisible = false;

                    // find the material in the unity project automatically.
                    Material material;

                    // try finding the fully qualified texture name with '/' replaced by '.' so 'BRICK.BRICKWALL052D'.
                    string materialName = side.Material.Replace("/", ".");
                    if (materialName.Contains("."))
                    {
                        // try finding both 'BRICK.BRICKWALL052D' and 'BRICKWALL052D'.
                        string tiny = materialName.Substring(materialName.LastIndexOf('.') + 1);
                        material = materialSearcher.FindMaterial(new string[] { materialName, tiny });
                        if (material == null && materialSearcherWarnings.Add(materialName))
                        {
                            Debug.Log("Chisel: Tried to find material '" + materialName + "' and also as '" + tiny + "' but it couldn't be found in the project.");
                        }
                    }
                    else
                    {
                        // only try finding 'BRICKWALL052D'.
                        material = materialSearcher.FindMaterial(new string[] { materialName });
                        if (material == null && materialSearcherWarnings.Add(materialName))
                        {
                            Debug.Log("Chisel: Tried to find material '" + materialName + "' but it couldn't be found in the project.");
                        }
                    }

                    // fallback to default material.
                    if (material == null)
                    {
                        material = ChiselMaterialManager.DefaultFloorMaterial;
                    }

                    // create chisel surface for the clip.
                    ChiselSurface surface = new ChiselSurface();
                    surface.brushMaterial      = ChiselBrushMaterial.CreateInstance(material, ChiselMaterialManager.DefaultPhysicsMaterial);
                    surface.surfaceDescription = SurfaceDescription.Default;

                    // detect collision-only polygons.
                    if (IsInvisibleMaterial(side.Material))
                    {
                        surface.brushMaterial.LayerUsage &= ~LayerUsageFlags.RenderReceiveCastShadows;
                    }
                    // detect excluded polygons.
                    if (IsExcludedMaterial(side.Material))
                    {
                        surface.brushMaterial.LayerUsage &= LayerUsageFlags.CastShadows;
                        surface.brushMaterial.LayerUsage |= LayerUsageFlags.Collidable;
                    }

                    // calculate the clipping planes.
                    Plane clip = new Plane(go.transform.InverseTransformPoint(new Vector3(side.Plane.P1.X, side.Plane.P1.Z, side.Plane.P1.Y) * inchesInMeters), go.transform.InverseTransformPoint(new Vector3(side.Plane.P2.X, side.Plane.P2.Z, side.Plane.P2.Y) * inchesInMeters), go.transform.InverseTransformPoint(new Vector3(side.Plane.P3.X, side.Plane.P3.Z, side.Plane.P3.Y) * inchesInMeters));
                    planes[j]        = new float4(clip.normal, clip.distance);
                    planeSurfaces[j] = surface;

                    // check whether this surface is a displacement.
                    if (side.Displacement != null)
                    {
                        // disable the brush.
                        go.gameObject.GetComponent <ChiselBrush>().enabled = false;

                        // keep track of the surface used to cut the mesh.
                        DisplacementSurfaces.Add(new DisplacementSide {
                            side = side, surface = surface
                        });
                    }
                }

                // cut all the clipping planes out of the brush in one go.
                brushMesh.Cut(planes, planeSurfaces);

                // now iterate over the planes to calculate UV coordinates.
                int[] indices = new int[solid.Sides.Count];
                for (int k = 0; k < planes.Length; k++)
                {
                    var   plane           = planes[k];
                    int   closestIndex    = 0;
                    float closestDistance = math.lengthsq(plane - brushMesh.planes[0]);
                    for (int j = 1; j < brushMesh.planes.Length; j++)
                    {
                        float testDistance = math.lengthsq(plane - brushMesh.planes[j]);
                        if (testDistance < closestDistance)
                        {
                            closestIndex    = j;
                            closestDistance = testDistance;
                        }
                    }
                    indices[k] = closestIndex;
                }

                for (int j = 0; j < indices.Length; j++)
                {
                    brushMesh.planes[indices[j]] = planes[j];
                }

                for (int j = solid.Sides.Count; j-- > 0;)
                {
                    VmfSolidSide side     = solid.Sides[j];
                    var          surface  = brushMesh.polygons[indices[j]].surface;
                    var          material = surface.brushMaterial.RenderMaterial;

                    // calculate the texture coordinates.
                    int w = 256;
                    int h = 256;
                    if (material.mainTexture != null)
                    {
                        w = material.mainTexture.width;
                        h = material.mainTexture.height;
                    }
                    var clip = new Plane(planes[j].xyz, planes[j].w);
                    CalculateTextureCoordinates(go, surface, clip, w, h, side.UAxis, side.VAxis);
                }

                // build displacements.
                foreach (DisplacementSide displacement in DisplacementSurfaces)
                {
                    // find the brush mesh polygon:
                    for (int polyidx = 0; polyidx < brushMesh.polygons.Length; polyidx++)
                    {
                        if (brushMesh.polygons[polyidx].surface == displacement.surface)
                        {
                            // find the polygon plane.
                            Plane plane = new Plane(brushMesh.planes[polyidx].xyz, brushMesh.planes[polyidx].w);

                            // find all vertices that belong to this polygon:
                            List <Vector3> vertices = new List <Vector3>();
                            {
                                var polygon   = brushMesh.polygons[polyidx];
                                var firstEdge = polygon.firstEdge;
                                var edgeCount = polygon.edgeCount;
                                var lastEdge  = firstEdge + edgeCount;
                                for (int e = firstEdge; e < lastEdge; e++)
                                {
                                    vertices.Add(brushMesh.vertices[brushMesh.halfEdges[e].vertexIndex]);
                                }
                            }
                            // reverse the winding order.
                            vertices.Reverse();

                            var first = vertices[0];
                            vertices.RemoveAt(0);
                            vertices.Add(first);

                            // build displacement:
                            BuildDisplacementSurface(go, displacement.side, displacement.surface, vertices, plane);
                        }
                    }
                }

                // finalize the brush by snapping planes and centering the pivot point.
                go.transform.position += brushMesh.CenterAndSnapPlanes();
                foreach (Transform child in go.transform)
                {
                    child.position -= go.transform.position;
                }
            }

            // iterate through all entities.
            for (int e = 0; e < world.Entities.Count; e++)
            {
#if UNITY_EDITOR
                UnityEditor.EditorUtility.DisplayProgressBar("Chisel: Importing Source Engine Map (2/3)", "Converting Hammer Entities To Chisel Brushes (" + (e + 1) + " / " + world.Entities.Count + ")...", e / (float)world.Entities.Count);
#endif
                VmfEntity entity = world.Entities[e];

                // skip entities that chisel can't handle.
                switch (entity.ClassName)
                {
                case "func_areaportal":
                case "func_areaportalwindow":
                case "func_capturezone":
                case "func_changeclass":
                case "func_combine_ball_spawner":
                case "func_dustcloud":
                case "func_dustmotes":
                case "func_nobuild":
                case "func_nogrenades":
                case "func_occluder":
                case "func_precipitation":
                case "func_proprespawnzone":
                case "func_regenerate":
                case "func_respawnroom":
                case "func_smokevolume":
                case "func_viscluster":
                    continue;
                }

                // iterate through all entity solids.
                for (int i = 0; i < entity.Solids.Count; i++)
                {
                    VmfSolid solid = entity.Solids[i];

                    // don't add triggers to the scene.
                    if (solid.Sides.Count > 0 && IsSpecialMaterial(solid.Sides[0].Material))
                    {
                        continue;
                    }

                    // HACK: Fix me in the future!
                    // HACK: Chisel doesn't support collision brushes yet- skip them completely!
                    if (solid.Sides.Count > 0 && IsInvisibleMaterial(solid.Sides[0].Material))
                    {
                        continue;
                    }
                    // HACK: Fix me in the future!

                    // build a very large cube brush.
                    ChiselBrush go = ChiselComponentFactory.Create <ChiselBrush>(model);
                    go.definition.surfaceDefinition = new ChiselSurfaceDefinition();
                    go.definition.surfaceDefinition.EnsureSize(6);
                    BrushMesh brushMesh = new BrushMesh();
                    go.definition.brushOutline = brushMesh;
                    BrushMeshFactory.CreateBox(ref brushMesh, new Vector3(-4096, -4096, -4096), new Vector3(4096, 4096, 4096), in go.definition.surfaceDefinition);

                    // prepare for uv calculations of clip planes after cutting.
                    var planes        = new float4[solid.Sides.Count];
                    var planeSurfaces = new ChiselSurface[solid.Sides.Count];

                    // clip all the sides out of the brush.
                    for (int j = solid.Sides.Count; j-- > 0;)
                    {
                        VmfSolidSide side = solid.Sides[j];

                        // detect excluded polygons.
                        //if (IsExcludedMaterial(side.Material))
                        //    polygon.UserExcludeFromFinal = true;
                        // detect collision-only brushes.
                        //if (IsInvisibleMaterial(side.Material))
                        //    pr.IsVisible = false;

                        // find the material in the unity project automatically.
                        Material material;

                        // try finding the fully qualified texture name with '/' replaced by '.' so 'BRICK.BRICKWALL052D'.
                        string materialName = side.Material.Replace("/", ".");
                        if (materialName.Contains("."))
                        {
                            // try finding both 'BRICK.BRICKWALL052D' and 'BRICKWALL052D'.
                            string tiny = materialName.Substring(materialName.LastIndexOf('.') + 1);
                            material = materialSearcher.FindMaterial(new string[] { materialName, tiny });
                            if (material == null && materialSearcherWarnings.Add(materialName))
                            {
                                Debug.Log("Chisel: Tried to find material '" + materialName + "' and also as '" + tiny + "' but it couldn't be found in the project.");
                            }
                        }
                        else
                        {
                            // only try finding 'BRICKWALL052D'.
                            material = materialSearcher.FindMaterial(new string[] { materialName });
                            if (material == null && materialSearcherWarnings.Add(materialName))
                            {
                                Debug.Log("Chisel: Tried to find material '" + materialName + "' but it couldn't be found in the project.");
                            }
                        }

                        // fallback to default material.
                        if (material == null)
                        {
                            material = ChiselMaterialManager.DefaultFloorMaterial;
                        }

                        // create chisel surface for the clip.
                        ChiselSurface surface = new ChiselSurface();
                        surface.brushMaterial      = ChiselBrushMaterial.CreateInstance(material, ChiselMaterialManager.DefaultPhysicsMaterial);
                        surface.surfaceDescription = SurfaceDescription.Default;

                        // detect collision-only polygons.
                        if (IsInvisibleMaterial(side.Material))
                        {
                            surface.brushMaterial.LayerUsage &= ~LayerUsageFlags.RenderReceiveCastShadows;
                        }
                        // detect excluded polygons.
                        if (IsExcludedMaterial(side.Material))
                        {
                            surface.brushMaterial.LayerUsage &= LayerUsageFlags.CastShadows;
                            surface.brushMaterial.LayerUsage |= LayerUsageFlags.Collidable;
                        }

                        // calculate the clipping planes.
                        Plane clip = new Plane(go.transform.InverseTransformPoint(new Vector3(side.Plane.P1.X, side.Plane.P1.Z, side.Plane.P1.Y) * inchesInMeters), go.transform.InverseTransformPoint(new Vector3(side.Plane.P2.X, side.Plane.P2.Z, side.Plane.P2.Y) * inchesInMeters), go.transform.InverseTransformPoint(new Vector3(side.Plane.P3.X, side.Plane.P3.Z, side.Plane.P3.Y) * inchesInMeters));
                        planes[j]        = new float4(clip.normal, clip.distance);
                        planeSurfaces[j] = surface;
                    }

                    // cut all the clipping planes out of the brush in one go.
                    brushMesh.Cut(planes, planeSurfaces);

                    // now iterate over the planes to calculate UV coordinates.
                    int[] indices = new int[solid.Sides.Count];
                    for (int k = 0; k < planes.Length; k++)
                    {
                        var   plane           = planes[k];
                        int   closestIndex    = 0;
                        float closestDistance = math.lengthsq(plane - brushMesh.planes[0]);
                        for (int j = 1; j < brushMesh.planes.Length; j++)
                        {
                            float testDistance = math.lengthsq(plane - brushMesh.planes[j]);
                            if (testDistance < closestDistance)
                            {
                                closestIndex    = j;
                                closestDistance = testDistance;
                            }
                        }
                        indices[k] = closestIndex;
                    }

                    for (int j = 0; j < indices.Length; j++)
                    {
                        brushMesh.planes[indices[j]] = planes[j];
                    }

                    for (int j = solid.Sides.Count; j-- > 0;)
                    {
                        VmfSolidSide side     = solid.Sides[j];
                        var          surface  = brushMesh.polygons[indices[j]].surface;
                        var          material = surface.brushMaterial.RenderMaterial;

                        // calculate the texture coordinates.
                        int w = 256;
                        int h = 256;
                        if (material.mainTexture != null)
                        {
                            w = material.mainTexture.width;
                            h = material.mainTexture.height;
                        }
                        var clip = new Plane(planes[j].xyz, planes[j].w);
                        CalculateTextureCoordinates(go, surface, clip, w, h, side.UAxis, side.VAxis);
                    }

                    // finalize the brush by snapping planes and centering the pivot point.
                    go.transform.position += brushMesh.CenterAndSnapPlanes();

                    // detail brushes that do not affect the CSG world.
                    //if (entity.ClassName == "func_detail")
                    //pr.IsNoCSG = true;
                    // collision only brushes.
                    //if (entity.ClassName == "func_vehicleclip")
                    //pr.IsVisible = false;
                }
            }
        }