예제 #1
0
        public static GUIStyle GetTextFieldStyle1()
        {
            GUIStyle style = new GUIStyle(EditorStyles.textField);

            if (textFieldTexture1 == null)
            {
                textFieldTexture1 = UnityEditor.AssetDatabase.LoadMainAssetAtPath(CSGModel.GetSabreCSGPath() + "Gizmos/TextField.png") as Texture2D;
            }
            style.normal.background  = textFieldTexture1;
            style.normal.textColor   = Color.white;
            style.focused.background = textFieldTexture1;
            style.focused.textColor  = Color.white;
            return(style);
        }
예제 #2
0
        //	    public static Material GetMarqueeBorderMaterial()
        //	    {
        //	        if (marqueeBorderMaterial == null)
        //	        {
        //	            marqueeBorderMaterial = new Material(Shader.Find("Transparent/Diffuse"));
        //	        }
        //	        return marqueeBorderMaterial;
        //	    }
        //
        //	    public static Material GetMarqueeFillMaterial()
        //	    {
        //	        if (marqueeFillMaterial == null)
        //	        {
        //	            marqueeFillMaterial = new Material(Shader.Find("Transparent/Diffuse"));
        //	        }
        //	        return marqueeFillMaterial;
        //	    }

        public static Material GetExcludedMaterial()
        {
            if (excludedTexture == null)
            {
                excludedTexture = UnityEditor.AssetDatabase.LoadMainAssetAtPath(CSGModel.GetSabreCSGPath() + "Internal/Excluded.png") as Texture2D;
            }
            if (excludedMaterial == null)
            {
                excludedMaterial                  = new Material(Shader.Find("SabreCSG/SeeExcluded"));
                excludedMaterial.hideFlags        = HideFlags.HideAndDontSave;
                excludedMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
                excludedMaterial.mainTexture      = excludedTexture;
            }
            return(excludedMaterial);
        }
예제 #3
0
        /// <summary>
        /// Loads an object from a path, or if the object is already loaded returns it
        /// </summary>
        /// <param name="sabrePath">Path local to the SabreCSG folder</param>
        /// <returns></returns>
        private static Object LoadObject(string sabrePath)
        {
            bool found = false;

            Object loadedObject = null;

            // First of all see if there's a cached record
            if (loadedObjects.ContainsKey(sabrePath))
            {
                found        = true;
                loadedObject = loadedObjects[sabrePath];

                // Now make sure the cached record actually points to something
                if (loadedObject != null)
                {
                    return(loadedObject);
                }
            }

            // Failed to load from cache, so load it from the Asset Database
            loadedObject = AssetDatabase.LoadMainAssetAtPath(Path.Combine(CSGModel.GetSabreCSGPath(), sabrePath));
            if (loadedObject != null)
            {
                if (found)
                {
                    // A cache record was found but empty, so set the existing record to the newly loaded object
                    loadedObjects[sabrePath] = loadedObject;
                }
                else
                {
                    // We know that it's not already in the cache, so add it to the end
                    loadedObjects.Add(sabrePath, loadedObject);
                }
            }
            return(loadedObject);
        }
예제 #4
0
        public override void OnInspectorGUI()
        {
            CSGModel csgModel = (CSGModel)target;

            // Ensure the default material is set
            csgModel.EnsureDefaultMaterialSet();

            DrawDefaultInspector();

            this.serializedObject.Update();
            using (NamedVerticalScope scope = new NamedVerticalScope("Build Settings"))
            {
                scope.WikiLink = "Build-Settings";

                EditorGUIUtility.fieldWidth = 0;
                EditorGUIUtility.labelWidth = 160;

                EditorGUILayout.PropertyField(generateCollisionMeshesProperty, new GUIContent("Generate Collision Meshes"));
                EditorGUILayout.PropertyField(generateTangentsProperty, new GUIContent("Generate Tangents"));

                EditorGUILayout.PropertyField(generateLightmapUVsProperty, new GUIContent("Generate Lightmap UVs"));
                EditorGUIUtility.labelWidth = 125;

                GUI.enabled           = generateLightmapUVsProperty.boolValue;
                EditorGUI.indentLevel = 1;
                EditorGUILayout.PropertyField(unwrapAngleErrorProperty, new GUIContent("Unwrap Angle Error"));
                EditorGUILayout.PropertyField(unwrapAreaErrorProperty, new GUIContent("Unwrap Area Error"));
                EditorGUILayout.PropertyField(unwrapHardAngleProperty, new GUIContent("Unwrap Hard Angle"));
                EditorGUILayout.PropertyField(unwrapPackMarginProperty, new GUIContent("Unwrap Pack Margin"));
                EditorGUI.indentLevel       = 0;
                EditorGUIUtility.labelWidth = 0;
                GUI.enabled = true;

                EditorGUILayout.PropertyField(shadowCastingModeProperty, new GUIContent("Shadow Casting Mode"));
                EditorGUILayout.PropertyField(reflectionProbeUsageProperty, new GUIContent("Reflection Probes"));

                // Experimental build settings to enable features that are not yet completely stable
                GUILayout.Label("Experimental", EditorStyles.boldLabel);
                EditorGUI.indentLevel = 1;

                EditorGUILayout.PropertyField(optimizeGeometryProperty, new GUIContent("Optimize Geometry"));
                EditorGUILayout.PropertyField(saveMeshesAsAssetsProperty, new GUIContent("Save Meshes As Assets"));
                EditorGUI.indentLevel = 0;
            }

            using (new NamedVerticalScope("Default Material"))
            {
                PhysicMaterial lastPhysicsMaterial = defaultPhysicsMaterialProperty.objectReferenceValue as PhysicMaterial;

                EditorGUI.BeginChangeCheck();
                EditorGUILayout.PropertyField(defaultPhysicsMaterialProperty, new GUIContent("Default Physics Material"));
                if (EditorGUI.EndChangeCheck())
                {
                    PhysicMaterial newPhysicsMaterial = defaultPhysicsMaterialProperty.objectReferenceValue as PhysicMaterial;

                    // Update the built mesh colliders that use the old material
                    UpdatePhysicsMaterial(lastPhysicsMaterial, newPhysicsMaterial);
                }

                // Track the last visual material, so that if the user changes it we can update built renderers instantly
                Material lastVisualMaterial = defaultVisualMaterialProperty.objectReferenceValue as Material;

                EditorGUI.BeginChangeCheck();
                EditorGUILayout.PropertyField(defaultVisualMaterialProperty, new GUIContent("Default Visual Material"));
                if (EditorGUI.EndChangeCheck())
                {
                    // User has changed the material, so grab the new material
                    Material newVisualMaterial = defaultVisualMaterialProperty.objectReferenceValue as Material;
                    // EnsureDefaultMaterialSet hasn't had a chance to run yet, so make sure we have a solid material reference
                    if (newVisualMaterial == null)
                    {
                        newVisualMaterial = csgModel.GetDefaultFallbackMaterial();
                        defaultVisualMaterialProperty.objectReferenceValue = newVisualMaterial;
                    }

                    // Update the built renderers that use the old material, also update source brush polygons
                    UpdateVisualMaterial(lastVisualMaterial, newVisualMaterial);

                    // Update the last build's default material because we don't need to build again
                    lastBuildDefaultVisualMaterialProperty.objectReferenceValue = newVisualMaterial;
                }

                EditorGUILayout.BeginHorizontal();
                if (GUILayout.Button("Lit Texture (No Tint)"))
                {
                    Material newVisualMaterial = AssetDatabase.LoadMainAssetAtPath(CSGModel.GetSabreCSGPath() + "Resources/Materials/Default_Map.mat") as Material;
                    SetVisualMaterial(lastVisualMaterial, newVisualMaterial);
                }
                if (GUILayout.Button("Lit Vertex Tint"))
                {
                    Material newVisualMaterial = AssetDatabase.LoadMainAssetAtPath(CSGModel.GetSabreCSGPath() + "Resources/Materials/Default_LitWithTint.mat") as Material;
                    SetVisualMaterial(lastVisualMaterial, newVisualMaterial);
                }
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.BeginHorizontal();
                if (GUILayout.Button("Unlit Vertex Color"))
                {
                    Material newVisualMaterial = AssetDatabase.LoadMainAssetAtPath(CSGModel.GetSabreCSGPath() + "Resources/Materials/Default_VertexColor.mat") as Material;
                    SetVisualMaterial(lastVisualMaterial, newVisualMaterial);
                }
                if (GUILayout.Button("Lit Vertex Color"))
                {
                    Material newVisualMaterial = AssetDatabase.LoadMainAssetAtPath(CSGModel.GetSabreCSGPath() + "Resources/Materials/Default_VertexColorLit.mat") as Material;
                    SetVisualMaterial(lastVisualMaterial, newVisualMaterial);
                }
                EditorGUILayout.EndHorizontal();
            }

            using (NamedVerticalScope scope = new NamedVerticalScope("Export"))
            {
                scope.WikiLink = "Build-Settings#exporting-obj-files";

                if (GUILayout.Button("Export All To OBJ"))
                {
                    csgModel.ExportOBJ(false);
                }

                if (GUILayout.Button("Export Selected To OBJ"))
                {
                    csgModel.ExportOBJ(true);
                }
            }

            using (new NamedVerticalScope("Import"))
            {
                GuiLayoutBeginImporterSection(SabreCSGResources.ImporterUnrealGoldTexture, "Unreal Gold Importer", "Henry de Jongh");

                importerUnrealGoldScale = EditorGUILayout.IntField("Scale", importerUnrealGoldScale);
                if (importerUnrealGoldScale < 1)
                {
                    importerUnrealGoldScale = 1;
                }

                EditorGUILayout.BeginHorizontal();
                if (GUILayout.Button("Import Unreal Gold Map (*.t3d)"))
                {
                    try
                    {
                        string path = EditorUtility.OpenFilePanel("Import Unreal Gold Map", "", "t3d");
                        if (path.Length != 0)
                        {
                            EditorUtility.DisplayProgressBar("SabreCSG: Importing Unreal Gold Map", "Parsing Unreal Text File (*.t3d)...", 0.0f);
                            var importer = new Importers.UnrealGold.T3dImporter();
                            var map      = importer.Import(path);
                            Importers.UnrealGold.T3dMapConverter.Import(csgModel, map, importerUnrealGoldScale);
                        }
                    }
                    catch (Exception ex)
                    {
                        EditorUtility.ClearProgressBar();
                        EditorUtility.DisplayDialog("Unreal Gold Map Import", "An exception occurred while importing the map:\r\n" + ex.Message, "Ohno!");
                    }
                }
                if (GUILayout.Button("?", GUILayout.Width(16)))
                {
                    EditorUtility.DisplayDialog("Unreal Gold Importer", "This importer was created using Unreal Gold 227 (http://oldunreal.com/).\n\nImportant Notes:\n* It will try to find the materials in your project automatically. First it looks for the full name like 'PlayrShp.Ceiling.Hullwk' then the last word 'Hullwk'. The latter option could cause some false positives, try creating a material with the full name if this happens.\n* This importer requires you to place a massive additive cube around your whole level as Unreal Editor uses the subtractive workflow.\n\nKnown Issues:\n* Concave brushes may cause corruptions.", "Okay");
                }
                EditorGUILayout.EndHorizontal();
                GuiLayoutEndImporterSection();

                //
                EditorGUILayout.Space();
                //

                GuiLayoutBeginImporterSection(SabreCSGResources.ImporterImporterValveMapFormat2006Texture, "Source Engine 2006 Importer", "Henry de Jongh");

                EditorGUILayout.BeginHorizontal();
                if (GUILayout.Button("Import Source Engine Map (*.vmf)"))
                {
                    try
                    {
                        string path = EditorUtility.OpenFilePanel("Import Source Engine Map", "", "vmf");
                        if (path.Length != 0)
                        {
                            EditorUtility.DisplayProgressBar("SabreCSG: Importing Source Engine Map", "Parsing Valve Map Format File (*.vmf)...", 0.0f);
                            var importer = new Importers.ValveMapFormat2006.VmfImporter();
                            var map      = importer.Import(path);
                            Importers.ValveMapFormat2006.VmfWorldConverter.Import(csgModel, map);
                        }
                    }
                    catch (Exception ex)
                    {
                        EditorUtility.ClearProgressBar();
                        EditorUtility.DisplayDialog("Source Engine Map Import", "An exception occurred while importing the map:\r\n" + ex.Message, "Ohno!");
                    }
                }

                if (GUILayout.Button("?", GUILayout.Width(16)))
                {
                    EditorUtility.DisplayDialog("Source Engine 2006 Importer", "This importer was created using Source SDK maps and Hammer 4.1.\n\nImportant Notes:\n* It will try to find the materials in your project automatically. First it looks for the full name with forward slashes '/' replaced by periods '.' like 'BRICK.BRICKFLOOR001A' then the last word 'BRICKFLOOR001A'. The latter option could cause some false positives, try creating a material with the full name if this happens.", "Okay");
                }
                EditorGUILayout.EndHorizontal();

                GuiLayoutEndImporterSection();
            }

            using (new NamedVerticalScope("Stats"))
            {
                BuildMetrics buildMetrics = csgModel.BuildMetrics;

                GUILayout.Label("Vertices: " + buildMetrics.TotalVertices);
                GUILayout.Label("Triangles: " + buildMetrics.TotalTriangles);
                GUILayout.Label("Meshes: " + buildMetrics.TotalMeshes);
                GUILayout.Label("Build Time: " + buildMetrics.BuildTime.ToString());
            }

            // Make sure any serialize property changes are committed to the underlying Unity Object
            bool anyApplied = this.serializedObject.ApplyModifiedProperties();

            if (anyApplied)
            {
                // Make sure that nothing else is included in the Undo, as an immediate rebuild may take place and
                // we don't want the rebuild's lastBuildSettings being included in the undo step
                Undo.IncrementCurrentGroup();
            }
        }
예제 #5
0
        public override void Invalidate(bool polygonsChanged)
        {
            if (!gameObject.activeInHierarchy)
            {
                return;
            }

            // Make sure there is a mesh filter on this object
            MeshFilter   meshFilter   = gameObject.AddOrGetComponent <MeshFilter>();
            MeshRenderer meshRenderer = gameObject.AddOrGetComponent <MeshRenderer>();

            // Used to use mesh colliders for ray collision, but not any more so clean them up
            MeshCollider[] meshColliders = GetComponents <MeshCollider>();

            if (meshColliders.Length > 0)
            {
                for (int i = 0; i < meshColliders.Length; i++)
                {
                    DestroyImmediate(meshColliders[i]);
                }
            }

            bool requireRegen = false;

            // If the cached ID hasn't been set or we mismatch
            if (cachedInstanceID == 0 ||
                gameObject.GetInstanceID() != cachedInstanceID)
            {
                requireRegen     = true;
                cachedInstanceID = gameObject.GetInstanceID();
            }


            Mesh renderMesh = meshFilter.sharedMesh;

            if (requireRegen)
            {
                renderMesh = new Mesh();
            }

            if (polygons != null)
            {
                List <int> polygonIndices;
                PolygonFactory.GenerateMeshFromPolygons(polygons, ref renderMesh, out polygonIndices);
            }

            if (mode == CSGMode.Subtract)
            {
                PolygonFactory.Invert(ref renderMesh);
            }
            // Displace the triangles for display along the normals very slightly (this is so we can overlay built
            // geometry with semi-transparent geometry and avoid depth fighting)
            PolygonFactory.Displace(ref renderMesh, 0.001f);

            meshFilter.sharedMesh = renderMesh;

            meshRenderer.receiveShadows    = false;
            meshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;

            meshFilter.hideFlags   = HideFlags.NotEditable;          // | HideFlags.HideInInspector;
            meshRenderer.hideFlags = HideFlags.NotEditable;          // | HideFlags.HideInInspector;

#if UNITY_EDITOR
            meshRenderer.sharedMaterial = UnityEditor.AssetDatabase.LoadMainAssetAtPath(CSGModel.GetSabreCSGPath() + "Materials/" + this.mode.ToString() + ".mat") as Material;
#endif
            isBrushConvex = PolygonFactory.IsMeshConvex(polygons);

            if (polygonsChanged)
            {
                RecalculateBrushCache();
            }

            UpdateVisibility();

            objectVersionSerialized++;
            objectVersionUnserialized = objectVersionSerialized;
        }
예제 #6
0
        public override void OnInspectorGUI()
        {
            CSGModel csgModel = (CSGModel)target;

            // Ensure the default material is set
            csgModel.EnsureDefaultMaterialSet();

            DrawDefaultInspector();

            this.serializedObject.Update();
            GUILayout.Label("Build Settings", EditorStyles.boldLabel);
            EditorGUILayout.PropertyField(generateCollisionMeshesProperty, new GUIContent("Generate Collision Meshes"));
            EditorGUILayout.PropertyField(generateTangentsProperty, new GUIContent("Generate Tangents"));
            EditorGUILayout.PropertyField(generateLightmapUVsProperty, new GUIContent("Generate Lightmap UVs"));

            GUI.enabled           = generateLightmapUVsProperty.boolValue;
            EditorGUI.indentLevel = 1;
            EditorGUILayout.PropertyField(unwrapAngleErrorProperty, new GUIContent("Unwrap Angle Error"));
            EditorGUILayout.PropertyField(unwrapAreaErrorProperty, new GUIContent("Unwrap Area Error"));
            EditorGUILayout.PropertyField(unwrapHardAngleProperty, new GUIContent("Unwrap Hard Angle"));
            EditorGUILayout.PropertyField(unwrapPackMarginProperty, new GUIContent("Unwrap Pack Margin"));
            EditorGUI.indentLevel = 0;
            GUI.enabled           = true;

            PhysicMaterial lastPhysicsMaterial = defaultPhysicsMaterialProperty.objectReferenceValue as PhysicMaterial;

            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(defaultPhysicsMaterialProperty, new GUIContent("Default Physics Material"));
            if (EditorGUI.EndChangeCheck())
            {
                PhysicMaterial newPhysicsMaterial = defaultPhysicsMaterialProperty.objectReferenceValue as PhysicMaterial;

                // Update the built mesh colliders that use the old material
                UpdatePhysicsMaterial(lastPhysicsMaterial, newPhysicsMaterial);
            }

            // Track the last visual material, so that if the user changes it we can update built renderers instantly
            Material lastVisualMaterial = defaultVisualMaterialProperty.objectReferenceValue as Material;

            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(defaultVisualMaterialProperty, new GUIContent("Default Visual Material"));
            if (EditorGUI.EndChangeCheck())
            {
                // User has changed the material, so grab the new material
                Material newVisualMaterial = defaultVisualMaterialProperty.objectReferenceValue as Material;
                // EnsureDefaultMaterialSet hasn't had a chance to run yet, so make sure we have a solid material reference
                if (newVisualMaterial == null)
                {
                    newVisualMaterial = csgModel.GetDefaultFallbackMaterial();
                    defaultVisualMaterialProperty.objectReferenceValue = newVisualMaterial;
                }

                // Update the built renderers that use the old material, also update source brush polygons
                UpdateVisualMaterial(lastVisualMaterial, newVisualMaterial);

                // Update the last build's default material because we don't need to build again
                lastBuildDefaultVisualMaterialProperty.objectReferenceValue = newVisualMaterial;
            }

            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("Lit Texture (No Tint)"))
            {
                Material newVisualMaterial = AssetDatabase.LoadMainAssetAtPath(CSGModel.GetSabreCSGPath() + "Resources/Materials/Default_Map.mat") as Material;

                // EnsureDefaultMaterialSet hasn't had a chance to run yet, so make sure we have a solid material reference
                if (newVisualMaterial == null)
                {
                    newVisualMaterial = csgModel.GetDefaultFallbackMaterial();
                }

                // Update the built renderers that use the old material, also update source brush polygons
                UpdateVisualMaterial(lastVisualMaterial, newVisualMaterial);

                defaultVisualMaterialProperty.objectReferenceValue = newVisualMaterial;
                // Update the last build's default material because we don't need to build again
                lastBuildDefaultVisualMaterialProperty.objectReferenceValue = newVisualMaterial;
            }
            if (GUILayout.Button("Lit Vertex Tint"))
            {
                Material newVisualMaterial = AssetDatabase.LoadMainAssetAtPath(CSGModel.GetSabreCSGPath() + "Resources/Materials/Default_LitWithTint.mat") as Material;

                // EnsureDefaultMaterialSet hasn't had a chance to run yet, so make sure we have a solid material reference
                if (newVisualMaterial == null)
                {
                    newVisualMaterial = csgModel.GetDefaultFallbackMaterial();
                }

                // Update the built renderers that use the old material, also update source brush polygons
                UpdateVisualMaterial(lastVisualMaterial, newVisualMaterial);

                defaultVisualMaterialProperty.objectReferenceValue = newVisualMaterial;
                // Update the last build's default material because we don't need to build again
                lastBuildDefaultVisualMaterialProperty.objectReferenceValue = newVisualMaterial;
            }
            if (GUILayout.Button("Unlit Vertex Color"))
            {
                Material newVisualMaterial = AssetDatabase.LoadMainAssetAtPath(CSGModel.GetSabreCSGPath() + "Resources/Materials/Default_VertexColor.mat") as Material;

                // EnsureDefaultMaterialSet hasn't had a chance to run yet, so make sure we have a solid material reference
                if (newVisualMaterial == null)
                {
                    newVisualMaterial = csgModel.GetDefaultFallbackMaterial();
                }

                // Update the built renderers that use the old material, also update source brush polygons
                UpdateVisualMaterial(lastVisualMaterial, newVisualMaterial);

                defaultVisualMaterialProperty.objectReferenceValue = newVisualMaterial;
                // Update the last build's default material because we don't need to build again
                lastBuildDefaultVisualMaterialProperty.objectReferenceValue = newVisualMaterial;
            }
            EditorGUILayout.EndHorizontal();
            // Divider line
            GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1));

            if (GUILayout.Button("Export OBJ"))
            {
                csgModel.ExportOBJ();
            }
            BuildMetrics buildMetrics = csgModel.BuildMetrics;

            GUILayout.Label("Vertices: " + buildMetrics.TotalVertices);
            GUILayout.Label("Triangles: " + buildMetrics.TotalTriangles);
            GUILayout.Label("Meshes: " + buildMetrics.TotalMeshes);
            GUILayout.Label("Build Time: " + buildMetrics.BuildTime.ToString());

            // Make sure any serialize property changes are committed to the underlying Unity Object
            bool anyApplied = this.serializedObject.ApplyModifiedProperties();

            if (anyApplied)
            {
                // Make sure that nothing else is included in the Undo, as an immediate rebuild may take place and
                // we don't want the rebuild's lastBuildSettings being included in the undo step
                Undo.IncrementCurrentGroup();
            }
        }
예제 #7
0
 static string GetPluginPath()
 {
     return(CSGModel.GetSabreCSGPath() + "Scripts/SabreCSG.Core.dll");
 }
예제 #8
0
 public static Material GetVertexMaterial()
 {
     if (vertexMaterial == null)
     {
         Shader shader = Shader.Find("SabreCSG/Handle");
         vertexMaterial                  = new Material(shader);
         vertexMaterial.hideFlags        = HideFlags.HideAndDontSave;
         vertexMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
         vertexMaterial.mainTexture      = UnityEditor.AssetDatabase.LoadMainAssetAtPath(CSGModel.GetSabreCSGPath() + "Gizmos/CircleGizmo8x8.png") as Texture;
     }
     return(vertexMaterial);
 }