Exemple #1
0
    //Load and setup model prefab
    public IEnumerator LoadPrefab(string modelfile, CASCHandler casc)
    {
        DestroyImmediate(mesh);
        if (modelsPath == null)
        {
            modelsPath = @"creature\";
        }
        bool done = false;

        converter = new System.Drawing.ImageConverter();
        this.casc = casc;
        GameObject prefab = Resources.Load <GameObject>($"{modelsPath}{modelfile}_prefab");

        mesh = Instantiate(prefab, gameObject.transform);
        yield return(null);

        M2Model m2 = GetComponentInChildren <M2Model>();

        byte[] data = m2.data.bytes;
        byte[] skin = m2.skin.bytes;
        byte[] skel = m2.skel.bytes;
        loadBinaries = new Thread(() => { Model = m2.LoadModel(data, skin, skel); done = true; });
        loadBinaries.Start();
        yield return(null);

        while (loadBinaries.IsAlive)
        {
            yield return(null);
        }
        if (done)
        {
            LoadColors();
            textures = new Texture2D[Model.Textures.Length];
            Transform[] bones = GetComponentInChildren <SkinnedMeshRenderer>().bones;
            yield return(null);

            if (Model.Particles.Length > 0)
            {
                GameObject[] particles = new GameObject[Model.Particles.Length];
                for (int i = 0; i < particles.Length; i++)
                {
                    particles[i] = WoWHelper.ParticleEffect(Model.Particles[i]);
                    particles[i].transform.parent        = bones[Model.Particles[i].Bone];
                    particles[i].transform.localPosition = Vector3.zero;
                    particles[i].name = $"Particle{i}";
                    yield return(null);
                }
            }
            time  = new float[Model.TextureAnimations.Length];
            frame = new int[Model.TextureAnimations.Length];
            for (int i = 0; i < time.Length; i++)
            {
                time[i]  = 0f;
                frame[i] = 0;
                yield return(null);
            }
            renderer = GetComponentInChildren <SkinnedMeshRenderer>();
            Change   = true;
            Loaded   = !loadBinaries.IsAlive;
        }
    }
    //Load the model from CASC
    public IEnumerator LoadModel(int file, int texture, CASCHandler casc)
    {
        File = file;
        if (file != 0)
        {
            Texture   = texture;
            this.casc = casc;
            converter = new System.Drawing.ImageConverter();
            byte[] bytes;
            yield return(null);

            using (BinaryReader reader = new BinaryReader(casc.OpenFile(file)))
            {
                bytes = reader.ReadBytes((int)reader.BaseStream.Length);
            }
            yield return(null);

            Model = new M2();
            Model.LoadFile(bytes);
            yield return(null);

            if (Model.SkelFileID != 0)
            {
                using (BinaryReader reader = new BinaryReader(casc.OpenFile(Model.SkelFileID)))
                {
                    bytes = reader.ReadBytes((int)reader.BaseStream.Length);
                }
            }
            yield return(null);

            Model.Skeleton.LoadFile(bytes, Model.SkelFileID);
            yield return(null);

            using (BinaryReader reader = new BinaryReader(casc.OpenFile(Model.SkinFileID)))
            {
                bytes = reader.ReadBytes((int)reader.BaseStream.Length);
            }
            yield return(null);

            Model.Skin.LoadFile(bytes);
            yield return(null);

            //Array.Sort(Model.Skin.Textures, (a, b) => Model.Materials[a.Material].Blend.CompareTo(Model.Materials[b.Material].Blend));
            LoadColors();
            yield return(null);

            textures = new Texture2D[Model.Textures.Length];
            mesh     = WoWHelper.Generate3DMesh(Model);
            mesh.transform.parent           = GetComponent <Transform>();
            mesh.transform.localPosition    = Vector3.zero;
            mesh.transform.localEulerAngles = Vector3.zero;
            mesh.transform.localScale       = Vector3.one;
            renderer = GetComponentInChildren <SkinnedMeshRenderer>();
            yield return(null);

            Transform[] bones = GetComponentInChildren <SkinnedMeshRenderer>().bones;
            if (Model.Particles.Length > 0)
            {
                GameObject[] particles = new GameObject[Model.Particles.Length];
                for (int i = 0; i < particles.Length; i++)
                {
                    particles[i] = WoWHelper.ParticleEffect(Model.Particles[i]);
                    particles[i].transform.parent        = bones[Model.Particles[i].Bone];
                    particles[i].transform.localPosition = Vector3.zero;
                    particles[i].name = $"Particle{i}";
                    yield return(null);
                }
            }
            if (character.Form == 7)
            {
                MatchBones(gilnean);
            }
            else
            {
                MatchBones(character);
            }
            time  = new float[Model.TextureAnimations.Length];
            frame = new int[Model.TextureAnimations.Length];
            for (int i = 0; i < time.Length; i++)
            {
                time[i]  = 0f;
                frame[i] = 0;
                yield return(null);
            }
            Loaded = true;
            Change = true;
        }
    }