void LoadModel() { oldSelection = Selection.activeObject; string path = AssetDatabase.GetAssetPath(Selection.activeObject); if (path.ToLower().EndsWith(".rip")) { using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) { using (BinaryReader bin = new BinaryReader(fs)) { model = ReadModel(bin); model.textures = new Texture2D[model.textureFiles.Length]; for (int i = 0; i < model.textureFiles.Length; i++) { string texPath = GetParentPath(path) + "/" + model.textureFiles[i]; model.textures[i] = AssetDatabase.LoadAssetAtPath <Texture2D>(texPath); if (model.textures[i] == null) { model.textures[i] = AssetDatabase.LoadAssetAtPath <Texture2D>(texPath.Replace(".dds", ".tga")); } } } } } for (int i = 0; i < model.meshAttrNames.Length; i++) { model.meshAttrIndex[i] = GetDefaultIndexValue(model.attributes, model.meshAttrDefault[i]); } }
// This is called always when importing something static void OnPostprocessAllAssets ( string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths ) { foreach (string asset in importedAssets) { if (HasExtension(asset)) { using (FileStream fs = new FileStream(asset, FileMode.Open, FileAccess.Read)) { using (BinaryReader bin = new BinaryReader(fs)) { RipModel model = ReadModel(bin); model.textures = new Texture2D[model.textureFiles.Length]; for (int i = 0; i < model.textureFiles.Length; i++) { string texPath = GetParentPath(asset) + "/" + model.textureFiles[i]; model.textures[i] = AssetDatabase.LoadAssetAtPath <Texture2D>(texPath); if (model.textures[i] == null) { model.textures[i] = AssetDatabase.LoadAssetAtPath <Texture2D>(texPath.Replace(".dds", ".tga")); } } string newPath = ConvertToInternalPath(asset); //Object obj = AssetDatabase.LoadAssetAtPath<Object>(asset); //AssetDatabase.AddObjectToAsset(model, obj); AssetDatabase.CreateAsset(model, newPath); AssetDatabase.SaveAssets(); } } } } }
static RipModel ReadModel(BinaryReader bin) { RipModel model = ScriptableObject.CreateInstance <RipModel>(); model.signature = bin.ReadUInt32(); model.version = bin.ReadUInt32(); int facesCnt = bin.ReadInt32(); int vertexesCnt = bin.ReadInt32(); int vertexSize = bin.ReadInt32(); int textureFilesCnt = bin.ReadInt32(); int shaderFilesCnt = bin.ReadInt32(); int vertexAttributesCnt = bin.ReadInt32(); model.vertexesCnt = vertexesCnt; model.faces = new int[facesCnt * 3]; model.textureFiles = new string[textureFilesCnt]; model.shaderFiles = new string[shaderFilesCnt]; model.attributes = new List <RipAttribute>();// new RipAttribute[vertexAttributesCnt];//8 for (int i = 0; i < vertexAttributesCnt; i++) { RipAttribute attr = ScriptableObject.CreateInstance <RipAttribute>(); attr.semantic = ReadString(bin); attr.semanticIndex = bin.ReadInt32(); attr.offset = bin.ReadInt32(); attr.size = bin.ReadInt32(); int elementCnt = bin.ReadInt32();//3 attr.vertexAttribTypesArray = new int[elementCnt]; for (int e = 0; e < elementCnt; e++) { int typeElement = bin.ReadInt32(); attr.vertexAttribTypesArray[e] = typeElement; } model.attributes.Add(attr); } for (int t = 0; t < model.textureFiles.Length; t++) { model.textureFiles[t] = ReadString(bin); } for (int s = 0; s < model.shaderFiles.Length; s++) { model.shaderFiles[s] = ReadString(bin); } for (int f = 0; f < facesCnt; f++) { model.faces[f * 3 + 0] = bin.ReadInt32(); model.faces[f * 3 + 1] = bin.ReadInt32(); model.faces[f * 3 + 2] = bin.ReadInt32(); } for (int a = 0; a < vertexAttributesCnt; a++) { model.attributes[a].values = new Vector4[vertexesCnt]; } for (int v = 0; v < vertexesCnt; v++) { for (int a = 0; a < vertexAttributesCnt; a++) { float[] values = new float[4]; for (int i = 0; i < model.attributes[a].vertexAttribTypesArray.Length; i++) { values[i] = bin.ReadSingle(); } model.attributes[a].values[v] = new Vector4(values[0], values[1], values[2], values[3]); } } return(model); }