Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class RipImportEngine
- {
- public static uint RipSignature = 0xDEADC0DE;
- public static uint RipFileVersion = 4;
- static private ISuspendSceneRedrawGuard suspendredraw = null;
- //Global vars
- public static int g_ImportType = 1;// - Group / Single / List(0, 1, 2)
- public static string g_InputSrc = "";// - Source Dir/File
- public static int g_VertexFormatRecog = 0;// Auto/Manual(0,1)
- public static uint g_PosX_Idx = 0;
- public static uint g_PosY_Idx = 0;
- public static uint g_PosZ_Idx = 0;
- public static uint g_NormX_Idx = 0;
- public static uint g_NormY_Idx = 0;
- public static uint g_NormZ_Idx = 0;
- public static int g_Tc0_U_Idx = 0;
- public static int g_Tc0_V_Idx = 0;
- public static int g_Tc1_U_Idx = 0;
- public static int g_Tc1_V_Idx = 0;
- //Globals additional
- public static int g_ninjaScale = 100;
- public static float g_ninjarotX = 90;
- public static float g_ninjarotY = 0;
- public static float g_ninjarotZ = 0;
- public static int g_flipUV = 1;
- public static int g_flipXZAxis = 0;
- public static int g_uvScale = 100;
- public static string g_coll_prefix = "col_";
- public static bool g_skip_dupe = false;
- //Debug mode
- public static int g_debugMode = 0;
- public static string CreateMeshName(int num)
- {
- return "Mesh_" + num.ToString("X2") + ".rip";
- }
- public static string StringClear(string s)
- {
- return s.Replace(" ", "");
- }
- public static void EnableSceneRedraw()
- {
- if (suspendredraw != null)
- suspendredraw.Dispose();
- suspendredraw = null;
- }
- public static void DisableSceneRedraw()
- {
- suspendredraw = RipMaxPluginClass.global.Interface.SuspendSceneRedrawGuard.Create();
- suspendredraw.Suspend();
- }
- public static string ReadNullTerminatedString(this System.IO.BinaryReader stream)
- {
- string str = "";
- char ch;
- while ((int)(ch = stream.ReadChar()) != 0)
- str = str + ch;
- return str;
- }
- public static void ImportRipFile(string RipFilePath)
- {
- byte[] RipFileData = File.ReadAllBytes(RipFilePath);
- using (MemoryStream RipMemoryStream = new MemoryStream(RipFileData))
- using (BinaryReader RipFileReader = new BinaryReader(RipMemoryStream))
- {
- uint Signature = RipFileReader.ReadUInt32();
- uint Version = RipFileReader.ReadUInt32();
- if (Signature != RipSignature)
- {
- RipMaxPluginClass.core.PushPrompt("Bad RIP file signature");
- return;
- }
- if (Version != RipFileVersion)
- {
- RipMaxPluginClass.core.PushPrompt("Bad RIP file version");
- return;
- }
- uint dwFacesCnt = RipFileReader.ReadUInt32();
- uint dwVertexesCnt = RipFileReader.ReadUInt32();
- uint VertexSize = RipFileReader.ReadUInt32();
- uint TextureFilesCnt = RipFileReader.ReadUInt32();
- uint ShaderFilesCnt = RipFileReader.ReadUInt32();
- uint VertexAttributesCnt = RipFileReader.ReadUInt32();
- List<uint> VertexAttribTypesArray = new List<uint>();
- int TempPosIdx = 0;
- int TempNormalIdx = 0;
- int TempTexCoordIdx = 0;
- List<int[]> UV_Idx_array = new List<int[]>();
- List<float[][]> UV_array = new List<float[][]>();
- List<float[]> Vert_array = new List<float[]>();
- List<float[]> Normal_array = new List<float[]>();
- List<string> TextureFiles = new List<string>();
- List<string> ShaderFiles = new List<string>();
- List<uint[]> Face_array = new List<uint[]>();
- List<IMultiMtl> CreatedMaterials = new List<IMultiMtl>();
- for (int i = 0; i < VertexAttributesCnt; i++)
- {
- string Semantic = RipFileReader.ReadNullTerminatedString();
- uint SemanticIndex = RipFileReader.ReadUInt32();
- uint Offset = RipFileReader.ReadUInt32();
- uint Size = RipFileReader.ReadUInt32();
- uint TypeMapElements = RipFileReader.ReadUInt32();
- for (int n = 0; n < TypeMapElements; n++)
- {
- VertexAttribTypesArray.Add(RipFileReader.ReadUInt32());
- }
- if (g_VertexFormatRecog == 0)
- {
- if (Semantic == "POSITION")
- {
- if (TempPosIdx == 0)
- {
- g_PosX_Idx = Offset / 4;
- g_PosY_Idx = g_PosX_Idx + 1;
- g_PosZ_Idx = g_PosX_Idx + 2;
- TempPosIdx = TempPosIdx + 1;
- }
- }
- if (Semantic == "NORMAL")
- {
- if (TempNormalIdx == 0)
- {
- g_NormX_Idx = Offset / 4;
- g_NormY_Idx = g_NormX_Idx + 1;
- g_NormZ_Idx = g_NormX_Idx + 2;
- TempNormalIdx = TempNormalIdx + 1;
- }
- }
- if (Semantic == "TEXCOORD")
- {
- if (TempNormalIdx == 0)
- {
- int tc_u_idx = (int)Offset / 4;
- int tc_v_idx = tc_u_idx + 1;
- UV_Idx_array.Add(new int[] { tc_u_idx, tc_v_idx });
- }
- }
- }
- else
- {
- UV_Idx_array.Add(new int[] { g_Tc0_U_Idx, g_Tc0_V_Idx });
- UV_Idx_array.Add(new int[] { g_Tc1_U_Idx, g_Tc1_V_Idx });
- }
- }
- for (int i = 0; i < UV_Idx_array.Count; i++)
- {
- UV_array.Add(null);
- }
- for (int i = 0; i < TextureFilesCnt; i++)
- {
- TextureFiles.Add(RipFileReader.ReadNullTerminatedString());
- }
- for (int i = 0; i < ShaderFilesCnt; i++)
- {
- ShaderFiles.Add(RipFileReader.ReadNullTerminatedString());
- }
- for (int i = 0; i < dwFacesCnt; i++)
- {
- uint i0, i1, i2;
- i0 = RipFileReader.ReadUInt32();
- i1 = RipFileReader.ReadUInt32();
- i2 = RipFileReader.ReadUInt32();
- if (g_flipXZAxis == 1)
- {
- Face_array.Add(new uint[] { i2 + 1, i1 + 1, i0 + 1 });
- }
- else
- {
- Face_array.Add(new uint[] { i0 + 1, i1 + 1, i2 + 1 });
- }
- }
- for (int k = 0; k < dwVertexesCnt; k++)
- {
- float vx = 0.0f;
- float vy = 0.0f;
- float vz = 0.0f;
- // float vw = 0.0f;
- float nx = 0.0f;
- float ny = 0.0f;
- float nz = 0.0f;
- // float nw = 0.0f;
- List<float[]> texUv = new List<float[]>();
- for (int n = 0; n < UV_Idx_array.Count; n++)
- {
- texUv.Add(new float[] { 0.0f, 0.0f });
- }
- for (int j = 0; j < VertexAttribTypesArray.Count; j++)
- {
- uint ElementType = VertexAttribTypesArray[j];
- float z = 0.0f;
- if (ElementType == 0)
- {
- z = (float)RipFileReader.ReadSingle();
- }
- else if (ElementType == 1)
- {
- z = (float)RipFileReader.ReadUInt32();
- }
- else if (ElementType == 2)
- {
- z = (float)RipFileReader.ReadInt32();
- }
- else
- {
- z = (float)RipFileReader.ReadUInt32();
- }
- if (j == g_PosX_Idx) vx = z;
- if (j == g_PosY_Idx) vy = z;
- if (j == g_PosZ_Idx) vz = z;
- if (j == g_NormX_Idx) nx = z;
- if (j == g_NormY_Idx) ny = z;
- if (j == g_NormZ_Idx) nz = z;
- for (int i = 0; i < UV_Idx_array.Count; i++)
- {
- if (j == UV_Idx_array[i][0])
- {
- texUv[i][0] = z;
- }
- if (j == UV_Idx_array[i][1])
- {
- texUv[i][1] = z * g_flipUV;
- }
- }
- }
- Vert_array.Add(new float[] { (vx * g_ninjaScale), (vy * g_ninjaScale), (vz * g_ninjaScale) });
- Normal_array.Add(new float[] { nz, ny, nx });
- for (int i = 0; i < texUv.Count; i++)
- {
- if (texUv[i][0] == 0.0f && texUv[i][1] == 0.0f) continue;
- List<float[]> cur_UV_array = new List<float[]>();
- if (UV_array[i] != null)
- cur_UV_array.AddRange(UV_array[i]);
- cur_UV_array.Add(new float[] { texUv[i][0], texUv[i][1], 0.0f });
- UV_array[i] = cur_UV_array.ToArray();
- }
- }
- for (int i = 0; i < UV_array.Count; i++)
- {
- if (UV_array[i].Length == 0)
- {
- UV_array.RemoveAt(i);
- i--;
- }
- }
- List<string> FileNames = new List<string>();
- for (int i = 0; i < TextureFiles.Count; i++)
- {
- string TexFile = "notexture.dds";
- if (TextureFiles[i].Length > 0)
- TexFile = TextureFiles[i];
- FileNames.Add(TexFile);
- }
- // Поиск точно такого же материала
- IMultiMtl foundmaterial = null;
- foreach (var mat in CreatedMaterials)
- {
- if (mat.NumSubMtls > 0 && mat.NumSubMtls == FileNames.Count)
- {
- bool foundalllocal = true;
- for (int i = 0; i < mat.NumSubMtls; i++)
- {
- foreach (string name in FileNames)
- {
- if (mat.GetSubMtl(i) == null || mat.GetSubMtl(i).Name != name)
- foundalllocal = false;
- }
- if (!foundalllocal)
- {
- break;
- }
- }
- if (foundalllocal)
- {
- foundmaterial = mat;
- break;
- }
- }
- else
- {
- }
- }
- IMultiMtl thenewmaterial = null;
- if (foundmaterial == null)
- {
- thenewmaterial = RipMaxPluginClass.global.NewEmptyMultiMtl;
- thenewmaterial.SetNumSubMtls(TextureFiles.Count);
- for (int i = 0; i < TextureFiles.Count; i++)
- {
- string TexFile = "notexture.dds";
- if (TextureFiles[i].Length > 0)
- TexFile = TextureFiles[i];
- string TexFileName = Path.GetDirectoryName(RipFilePath) + "\\" + TexFile;
- IStdMat2 asdf = RipMaxPluginClass.global.NewDefaultStdMat;
- ...
- }
- if (foundmaterial != null)
- CreatedMaterials.Add(foundmaterial);
- }
- else
- {
- thenewmaterial = foundmaterial;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement