Exemplo n.º 1
0
    static void LoadMDL()
    {
        string path      = AssetUtils.GetAbsoluteAssetPath("Assets/Editor/Data/progs");
        string modelsDir = "Assets/Models";

        var files = ListModelFiles(path, "*.mdl", "*.bsp");
        int index = 0;

        try
        {
            foreach (var file in files)
            {
                float progress = ((float)++index) / files.Count;
                if (EditorUtility.DisplayCancelableProgressBar("Generating models", UnityEditor.FileUtil.GetProjectRelativePath(file), progress))
                {
                    break;
                }

                GenerateModel(FileUtilEx.FixOSPath(file), modelsDir);
            }
        }
        finally
        {
            EditorUtility.ClearProgressBar();
        }
    }
Exemplo n.º 2
0
    public void WriteTexture(string path)
    {
        var       texWidth  = this.width;
        var       texHeight = this.height;
        Texture2D tex       = new Texture2D(texWidth, texHeight);

        Color32[] texPixels = new Color32[texWidth * texHeight];

        foreach (var entry in m_entries)
        {
            var t = entry.texture;

            Color32[] pixels = GetTexturePixels(t);
            for (int y = 0; y < t.height; ++y)
            {
                for (int x = 0; x < t.width; ++x)
                {
                    texPixels[(entry.y + y) * texWidth + (entry.x + x)] = pixels[y * t.width + x];
                }
            }
        }

        tex.SetPixels32(texPixels);
        File.WriteAllBytes(AssetUtils.GetAbsoluteAssetPath(path), tex.EncodeToPNG());
    }
Exemplo n.º 3
0
    static void LoadBSP()
    {
        try
        {
            string path = EditorUtility.OpenFilePanelWithFilters("Open BSP", AssetUtils.GetAbsoluteAssetPath("Assets/Editor/Data/maps"), new string[] { "BSP files", "bsp" });
            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            using (FileStream stream = File.OpenRead(path))
            {
                DataStream ds  = new DataStream(stream);
                BSPFile    bsp = new BSPFile(ds);

                string name     = Path.GetFileNameWithoutExtension(path);
                string destPath = "Assets/Scenes/Maps/" + name;

                AssetUtils.CreateFolder(destPath);

                var materials = GenerateMaterials(bsp, destPath);
                if (materials == null) // cancelled
                {
                    return;
                }

                GenerateLevel(bsp, materials);
            }
        }
        finally
        {
            EditorUtility.ClearProgressBar();
        }
    }
Exemplo n.º 4
0
    static Material[] GenerateSkins(MDLFile mdl, string destPath)
    {
        List <string>   textures  = new List <string>();
        List <Material> materials = new List <Material>();

        var skinsPath = destPath + "/skins";

        AssetUtils.CreateFolder(skinsPath);

        int skinId = 0;

        foreach (var skin in mdl.skins)
        {
            var textureName = mdl.skins.Length > 1 ?
                              string.Format("{0}_skin_{1}.png", mdl.name, skinId++) :
                              string.Format("{0}_skin.png", mdl.name);
            var texturePath = skinsPath + "/" + textureName;
            if (!AssetUtils.AssetPathExists(texturePath))
            {
                Texture2D tex    = new Texture2D(skin.width, skin.height);
                Color32[] pixels = new Color32[skin.width * skin.height];
                for (int i = 0, j = 0; i < pixels.Length; ++i)
                {
                    pixels[i] = new Color32(skin.data[j++], skin.data[j++], skin.data[j++], skin.data[j++]);
                }

                for (int x = 0; x < skin.width; ++x)
                {
                    for (int y = 0; y < skin.height / 2; ++y)
                    {
                        int from = y * skin.width + x;
                        int to   = (skin.height - y - 1) * skin.width + x;
                        var temp = pixels[to];
                        pixels[to]   = pixels[from];
                        pixels[from] = temp;
                    }
                }

                tex.SetPixels32(pixels);
                File.WriteAllBytes(AssetUtils.GetAbsoluteAssetPath(texturePath), tex.EncodeToPNG());
            }

            textures.Add(texturePath);
        }
        AssetDatabase.Refresh();

        foreach (var texture in textures)
        {
            int    index        = texture.LastIndexOf('.');
            string materialPath = texture.Substring(0, index) + ".mat";
            if (!AssetUtils.AssetPathExists(materialPath))
            {
                TextureImporter importer = TextureImporter.GetAtPath(texture) as TextureImporter;
                importer.textureType    = TextureImporterType.Default;
                importer.wrapMode       = TextureWrapMode.Repeat;
                importer.filterMode     = FilterMode.Point;
                importer.maxTextureSize = 2048;
                importer.textureFormat  = TextureImporterFormat.DXT1;
                importer.SaveAndReimport();

                var material = new Material(Shader.Find("Standard"));
                material.mainTexture = AssetDatabase.LoadAssetAtPath <Texture2D>(texture);
                material.SetFloat("_Glossiness", 0.0f);

                AssetDatabase.CreateAsset(material, materialPath);
            }

            materials.Add(AssetDatabase.LoadAssetAtPath <Material>(materialPath));
        }

        AssetDatabase.SaveAssets();

        return(materials.ToArray());
    }
Exemplo n.º 5
0
    static IList <Material> GenerateMaterials(BSPFile bsp, string destPath)
    {
        List <string>   textures  = new List <string>();
        List <Material> materials = new List <Material>();

        string materialsPath = destPath + "/materials";

        AssetUtils.CreateFolder(materialsPath);

        int tex_id = 0;

        foreach (var t in bsp.textures)
        {
            string textureName = string.Format("{0}.png", FileUtilEx.FixFilename(t.name));
            string texturePath = materialsPath + "/" + textureName;

            float progress = ((float)++tex_id) / bsp.textures.Length;
            if (EditorUtility.DisplayCancelableProgressBar("Level", "Reading texture: " + texturePath, progress))
            {
                return(null);
            }

            if (!AssetUtils.AssetPathExists(texturePath))
            {
                Texture2D tex    = new Texture2D(t.width, t.height);
                Color32[] pixels = new Color32[t.width * t.height];
                for (int i = 0, j = 0; i < pixels.Length; ++i)
                {
                    pixels[i] = new Color32(t.data[j++], t.data[j++], t.data[j++], t.data[j++]);
                }

                for (int x = 0; x < t.width; ++x)
                {
                    for (int y = 0; y < t.height / 2; ++y)
                    {
                        int from = y * t.width + x;
                        int to   = (t.height - y - 1) * t.width + x;
                        var temp = pixels[to];
                        pixels[to]   = pixels[from];
                        pixels[from] = temp;
                    }
                }

                tex.SetPixels32(pixels);
                File.WriteAllBytes(AssetUtils.GetAbsoluteAssetPath(texturePath), tex.EncodeToPNG());
            }

            textures.Add(texturePath);
        }
        AssetDatabase.Refresh();

        int materialNum = 0;

        foreach (var texture in textures)
        {
            int    index        = texture.LastIndexOf('.');
            string materialPath = texture.Substring(0, index) + ".mat";

            float progress = ((float)++materialNum) / bsp.textures.Length;
            if (EditorUtility.DisplayCancelableProgressBar("Level", "Generating material: " + materialPath, progress))
            {
                return(null);
            }

            if (!AssetUtils.AssetPathExists(materialPath))
            {
                TextureImporter importer = TextureImporter.GetAtPath(texture) as TextureImporter;
                importer.textureType    = TextureImporterType.Default;
                importer.wrapMode       = TextureWrapMode.Repeat;
                importer.filterMode     = FilterMode.Point;
                importer.maxTextureSize = 2048;
                importer.textureFormat  = TextureImporterFormat.DXT1;
                importer.SaveAndReimport();

                var material = new Material(Shader.Find("Standard"));
                material.mainTexture = AssetDatabase.LoadAssetAtPath <Texture2D>(texture);
                material.SetFloat("_Glossiness", 0.0f);

                AssetDatabase.CreateAsset(material, materialPath);
            }

            materials.Add(AssetDatabase.LoadAssetAtPath <Material>(materialPath));
        }

        return(materials);
    }