private const float inchesInMeters = 0.03125f; // 1/32 /// <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(CSGModelBase model, VmfWorld world) { try { model.BeginUpdate(); // create a material searcher to associate materials automatically. MaterialSearcher materialSearcher = new MaterialSearcher(); // group all the brushes together. GroupBrush groupBrush = new GameObject("Source Engine Map").AddComponent <GroupBrush>(); groupBrush.transform.SetParent(model.transform); // iterate through all world solids. for (int i = 0; i < world.Solids.Count; i++) { #if UNITY_EDITOR UnityEditor.EditorUtility.DisplayProgressBar("SabreCSG: Importing Source Engine Map", "Converting Hammer Solids To SabreCSG 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; } // build a very large cube brush. var go = model.CreateBrush(PrimitiveBrushType.Cube, Vector3.zero); var pr = go.GetComponent <PrimitiveBrush>(); BrushUtility.Resize(pr, new Vector3(8192, 8192, 8192)); // clip all the sides out of the brush. for (int j = solid.Sides.Count; j-- > 0;) { VmfSolidSide side = solid.Sides[j]; Plane clip = new Plane(pr.transform.InverseTransformPoint(new Vector3(side.Plane.P1.X, side.Plane.P1.Z, side.Plane.P1.Y) * inchesInMeters), pr.transform.InverseTransformPoint(new Vector3(side.Plane.P2.X, side.Plane.P2.Z, side.Plane.P2.Y) * inchesInMeters), pr.transform.InverseTransformPoint(new Vector3(side.Plane.P3.X, side.Plane.P3.Z, side.Plane.P3.Y) * inchesInMeters)); ClipUtility.ApplyClipPlane(pr, clip, false); // find the polygons associated with the clipping plane. // the normal is unique and can never occur twice as that wouldn't allow the solid to be convex. var polygons = pr.GetPolygons().Where(p => p.Plane.normal == clip.normal); foreach (var polygon in polygons) { // 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) { Debug.Log("SabreCSG: 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) { Debug.Log("SabreCSG: Tried to find material '" + materialName + "' but it couldn't be found in the project."); } } polygon.Material = material; // calculate the texture coordinates. int w = 256; int h = 256; if (polygon.Material != null && polygon.Material.mainTexture != null) { w = polygon.Material.mainTexture.width; h = polygon.Material.mainTexture.height; } CalculateTextureCoordinates(pr, polygon, w, h, side.UAxis, side.VAxis); } } // add the brush to the group. pr.transform.SetParent(groupBrush.transform); } // iterate through all entities. for (int e = 0; e < world.Entities.Count; e++) { #if UNITY_EDITOR UnityEditor.EditorUtility.DisplayProgressBar("SabreCSG: Importing Source Engine Map", "Converting Hammer Entities To SabreCSG Brushes (" + (e + 1) + " / " + world.Entities.Count + ")...", e / (float)world.Entities.Count); #endif VmfEntity entity = world.Entities[e]; // skip entities that sabrecsg 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; } // build a very large cube brush. var go = model.CreateBrush(PrimitiveBrushType.Cube, Vector3.zero); var pr = go.GetComponent <PrimitiveBrush>(); BrushUtility.Resize(pr, new Vector3(8192, 8192, 8192)); // clip all the sides out of the brush. for (int j = solid.Sides.Count; j-- > 0;) { VmfSolidSide side = solid.Sides[j]; Plane clip = new Plane(pr.transform.InverseTransformPoint(new Vector3(side.Plane.P1.X, side.Plane.P1.Z, side.Plane.P1.Y) * inchesInMeters), pr.transform.InverseTransformPoint(new Vector3(side.Plane.P2.X, side.Plane.P2.Z, side.Plane.P2.Y) * inchesInMeters), pr.transform.InverseTransformPoint(new Vector3(side.Plane.P3.X, side.Plane.P3.Z, side.Plane.P3.Y) * inchesInMeters)); ClipUtility.ApplyClipPlane(pr, clip, false); // find the polygons associated with the clipping plane. // the normal is unique and can never occur twice as that wouldn't allow the solid to be convex. var polygons = pr.GetPolygons().Where(p => p.Plane.normal == clip.normal); foreach (var polygon in polygons) { // 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) { Debug.Log("SabreCSG: 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) { Debug.Log("SabreCSG: Tried to find material '" + materialName + "' but it couldn't be found in the project."); } } polygon.Material = material; // calculate the texture coordinates. int w = 256; int h = 256; if (polygon.Material != null && polygon.Material.mainTexture != null) { w = polygon.Material.mainTexture.width; h = polygon.Material.mainTexture.height; } CalculateTextureCoordinates(pr, polygon, w, h, side.UAxis, side.VAxis); } } // 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; } // add the brush to the group. pr.transform.SetParent(groupBrush.transform); } } #if UNITY_EDITOR UnityEditor.EditorUtility.ClearProgressBar(); #endif } catch (Exception) { throw; } finally { model.EndUpdate(); } }
/// <summary> /// Imports the specified Valve Map Format file. /// </summary> /// <param name="path">The file path.</param> /// <returns>A <see cref="VmfWorld"/> containing the imported world data.</returns> public VmfWorld Import(string path) { // create a new world. VmfWorld world = new VmfWorld(); // open the file for reading. we use streams for additional performance. // it's faster than File.ReadAllLines() as that requires two iterations. using (FileStream stream = new FileStream(path, FileMode.Open)) using (StreamReader reader = new StreamReader(stream)) { // read all the lines from the file. //bool inActor = false; T3dActor actor = null; //bool inBrush = false; T3dBrush brush = null; //bool inPolygon = false; T3dPolygon polygon = null; string[] closures = new string[64]; int depth = 0; string line; string previousLine = ""; bool justEnteredClosure = false; string key; object value; VmfSolid solid = null; VmfSolidSide solidSide = null; VmfEntity entity = null; while (!reader.EndOfStream) { line = reader.ReadLine().Trim(); if (line.Length == 0) { continue; } // parse closures and keep track of them. if (line[0] == '{') { closures[depth] = previousLine; depth++; justEnteredClosure = true; continue; } if (line[0] == '}') { depth--; closures[depth] = null; continue; } // parse version info. if (closures[0] == "versioninfo") { if (TryParsekeyValue(line, out key, out value)) { switch (key) { case "editorversion": world.VersionInfoEditorVersion = (int)value; break; case "editorbuild": world.VersionInfoEditorBuild = (int)value; break; case "mapversion": world.VersionInfoMapVersion = (int)value; break; case "formatversion": world.VersionInfoFormatVersion = (int)value; break; case "prefab": world.VersionInfoPrefab = (int)value; break; } } } // parse view settings. if (closures[0] == "viewsettings") { if (TryParsekeyValue(line, out key, out value)) { switch (key) { case "bSnapToGrid": world.ViewSettingsSnapToGrid = (int)value; break; case "bShowGrid": world.ViewSettingsShowGrid = (int)value; break; case "bShowLogicalGrid": world.ViewSettingsShowLogicalGrid = (int)value; break; case "nGridSpacing": world.ViewSettingsGridSpacing = (int)value; break; case "bShow3DGrid": world.ViewSettingsShow3DGrid = (int)value; break; } } } // parse world properties. if (closures[0] == "world" && closures[1] == null) { if (TryParsekeyValue(line, out key, out value)) { switch (key) { case "id": world.Id = (int)value; break; case "mapversion": world.MapVersion = (int)value; break; case "classname": world.ClassName = (string)value; break; case "detailmaterial": world.DetailMaterial = (string)value; break; case "detailvbsp": world.DetailVBsp = (string)value; break; case "maxpropscreenwidth": world.MaxPropScreenWidth = (int)value; break; case "skyname": world.SkyName = (string)value; break; } } } // parse world solid. if (closures[0] == "world" && closures[1] == "solid" && closures[2] == null) { // create a new solid and add it to the world. if (justEnteredClosure) { solid = new VmfSolid(); world.Solids.Add(solid); } // parse solid properties. if (TryParsekeyValue(line, out key, out value)) { switch (key) { case "id": solid.Id = (int)value; break; } } } // parse world solid side. if (closures[0] == "world" && closures[1] == "solid" && closures[2] == "side" && closures[3] == null) { // create a new solid side and add it to the solid. if (justEnteredClosure) { solidSide = new VmfSolidSide(); solid.Sides.Add(solidSide); } // parse solid side properties. if (TryParsekeyValue(line, out key, out value)) { switch (key) { case "id": solidSide.Id = (int)value; break; case "plane": solidSide.Plane = (VmfPlane)value; break; case "material": solidSide.Material = (string)value; break; //case "rotation": solidSide.Rotation = (float)value; break; case "uaxis": solidSide.UAxis = (VmfAxis)value; break; case "vaxis": solidSide.VAxis = (VmfAxis)value; break; case "lightmapscale": solidSide.LightmapScale = (int)value; break; case "smoothing_groups": solidSide.SmoothingGroups = (int)value; break; } } } // HACK: detect displacements. if (closures[0] == "world" && closures[1] == "solid" && closures[2] == "side" && closures[3] == "dispinfo") { solidSide.HasDisplacement = true; } // parse entity. if (closures[0] == "entity" && closures[1] == null) { // create a new entity and add it to the world. if (justEnteredClosure) { entity = new VmfEntity(); world.Entities.Add(entity); } // parse entity properties. if (TryParsekeyValue(line, out key, out value)) { switch (key) { case "id": entity.Id = (int)value; break; case "classname": entity.ClassName = (string)value; break; } } } // parse entity solid. if (closures[0] == "entity" && closures[1] == "solid" && closures[2] == null) { // create a new solid and add it to the entity. if (justEnteredClosure) { solid = new VmfSolid(); entity.Solids.Add(solid); } // parse solid properties. if (TryParsekeyValue(line, out key, out value)) { switch (key) { case "id": solid.Id = (int)value; break; } } } // parse entity solid side. if (closures[0] == "entity" && closures[1] == "solid" && closures[2] == "side" && closures[3] == null) { // create a new solid side and add it to the solid. if (justEnteredClosure) { solidSide = new VmfSolidSide(); solid.Sides.Add(solidSide); } // parse solid side properties. if (TryParsekeyValue(line, out key, out value)) { switch (key) { case "id": solidSide.Id = (int)value; break; case "plane": solidSide.Plane = (VmfPlane)value; break; case "material": solidSide.Material = (string)value; break; //case "rotation": solidSide.Rotation = (float)value; break; case "uaxis": solidSide.UAxis = (VmfAxis)value; break; case "vaxis": solidSide.VAxis = (VmfAxis)value; break; case "lightmapscale": solidSide.LightmapScale = (int)value; break; case "smoothing_groups": solidSide.SmoothingGroups = (int)value; break; } } } previousLine = line; justEnteredClosure = false; } } return(world); }