コード例 #1
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.Image;
                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);
    }
コード例 #2
0
    public MDLFile(DataStream ds, string name)
    {
        this.name = name;

        // read header
        var header = ds.readStruct <HEADER_T>();

        this.header = header;

        // read skins
        var skins       = new DynamicArray <ImageData>();    // image_data (see ImageUtil.newImageData)
        var skin_groups = new DynamicArray <SKIN_GROUP_T>(); // { index, num_skins }

        var skin_size = header.skin_width * header.skin_height;

        for (var i = 0; i < header.num_skins; ++i)
        {
            var group     = ds.readInt32();
            var num_skins = 1;
            if (group != 0)
            {
                // create and add skin group
                num_skins = ds.readInt32();

                SKIN_GROUP_T skin_group;
                skin_group.index                = skins.length;
                skin_group.num_skins            = num_skins;
                skin_group.times                = ds.readArray <float>(num_skins);
                skin_groups[skin_groups.length] = skin_group;
            }
            // add skins
            for (var j = 0; j < num_skins; ++j)
            {
                var image_data = new ImageData(name + i, header.skin_width, header.skin_height);
                image_data.pixels   = ds.readBuffer(skin_size);
                skins[skins.length] = image_data;
            }
        }
        this.skins       = expandSkins(skins.ToArray());
        this.skin_groups = skin_groups.ToArray();

        // read geometry
        GEOMETRY_T geometry;

        geometry.expanded = false;

        // read skin verts and triangles
        geometry.skin_verts = ds.readArray <SKINVERT_T>(this.header.num_verts);
        geometry.triangles  = ds.readArray <TRIANGLE_T>(this.header.num_tris);

        // read frames
        var frames       = new DynamicArray <FRAME_T>();
        var frame_groups = new DynamicArray <FRAME_GROUP_T>();

        for (var i = 0; i < header.num_frames; ++i)
        {
            var group      = ds.readInt32();
            var num_frames = 1;
            if (group != 0)
            {
                // create and add frame group
                FRAME_GROUP_T frame_group;
                frame_group.index                 = frames.length;
                frame_group.num_frames            = ds.readInt32();
                frame_group.min                   = ds.readStruct <VERTEX_T>();
                frame_group.max                   = ds.readStruct <VERTEX_T>();
                num_frames                        = frame_group.num_frames;
                frame_group.times                 = ds.readArray <float>(num_frames);
                frame_groups[frame_groups.length] = frame_group;
            }
            // add frames
            for (var j = 0; j < num_frames; ++j)
            {
                var frame = ds.readStruct <FRAME_T>();
                frame.verts           = ds.readArray <VERTEX_T>(this.header.num_verts);
                frame.name            = FileUtilEx.trimNullTerminatedString(frame.name);
                frames[frames.length] = frame;
            }
        }
        geometry.frames       = frames.ToArray();
        geometry.frame_groups = frame_groups.ToArray();

        this.geometry   = this.expandGeometry(geometry);
        this.animations = this.detectAnimations();
    }