Ejemplo n.º 1
0
    public static Mesh LoadModel(string scd, string LocalPath, bool SetUnreadable = true)
    {
        string ModelKey = scd + "_" + LocalPath;

        if (LoadedMeshes.ContainsKey(ModelKey))
        {
            return(LoadedMeshes[ModelKey]);
        }

        byte[] FinalMeshBytes = LoadBytes(scd, LocalPath);

        if (FinalMeshBytes == null || FinalMeshBytes.Length == 0)
        {
            return(null);            // File is empty
        }
        Mesh ToReturn = new Mesh()
        {
            indexFormat = UnityEngine.Rendering.IndexFormat.UInt16
        };

        // Create stream from bytes, to read it as binary file
        BinaryReader Stream = new BinaryReader(new MemoryStream(FinalMeshBytes));


        Scm NewScmModel = new Scm();

        NewScmModel.LoadFromStream(Stream);

        // Create Unity Mesh from Scm data
        ToReturn.name = NewScmModel.BoneNames[0];

        ToReturn.vertices = NewScmModel.GetVerts();
        //ToReturn.normals = NewScmModel.GetNormals();
        ToReturn.uv  = NewScmModel.GetUv0();
        ToReturn.uv2 = NewScmModel.GetUv1();

        //ToReturn.subMeshCount = 1;
        ToReturn.subMeshCount = 1;
        ToReturn.SetIndices(NewScmModel.GetTris(), MeshTopology.Triangles, 0);

        ToReturn.RecalculateBounds();
        ToReturn.RecalculateNormals();

        if (SetUnreadable)
        {
            ToReturn.UploadMeshData(true);
        }

        LoadedMeshes.Add(ModelKey, ToReturn);

        NewScmModel = null;

        return(ToReturn);
    }