Пример #1
0
    private static Material[] GenerateSkins(BSPFile bsp, string name, string destPath, TextureAtlas atlas)
    {
        var skinsPath = destPath + "/skins";

        AssetUtils.CreateFolder(skinsPath);

        string textureName = FileUtilEx.FixFilename(name);
        var    texturePath = skinsPath + "/" + textureName + ".png";

        if (!AssetUtils.AssetPathExists(texturePath))
        {
            atlas.WriteTexture(texturePath);
            AssetDatabase.Refresh();
        }

        int    index        = texturePath.LastIndexOf('.');
        string materialPath = texturePath.Substring(0, index) + ".mat";

        if (!AssetUtils.AssetPathExists(materialPath))
        {
            TextureImporter importer = TextureImporter.GetAtPath(texturePath) 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>(texturePath);
            material.SetFloat("_Glossiness", 0.0f);

            AssetDatabase.CreateAsset(material, materialPath);
        }

        return(new Material[] { AssetDatabase.LoadAssetAtPath <Material>(materialPath) });
    }
Пример #2
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);
    }