Advertisement
Guest User

Untitled

a guest
Mar 31st, 2017
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.56 KB | None | 0 0
  1.     public static class RipImportEngine
  2.     {
  3.         public static uint RipSignature = 0xDEADC0DE;
  4.         public static uint RipFileVersion = 4;
  5.         static private ISuspendSceneRedrawGuard suspendredraw = null;
  6.  
  7.  
  8.  
  9.  
  10.         //Global vars
  11.         public static int g_ImportType = 1;// - Group / Single / List(0, 1, 2)
  12.         public static string g_InputSrc = "";// - Source Dir/File
  13.         public static int g_VertexFormatRecog = 0;// Auto/Manual(0,1)
  14.  
  15.         public static uint g_PosX_Idx = 0;
  16.         public static uint g_PosY_Idx = 0;
  17.         public static uint g_PosZ_Idx = 0;
  18.         public static uint g_NormX_Idx = 0;
  19.         public static uint g_NormY_Idx = 0;
  20.         public static uint g_NormZ_Idx = 0;
  21.         public static int g_Tc0_U_Idx = 0;
  22.         public static int g_Tc0_V_Idx = 0;
  23.         public static int g_Tc1_U_Idx = 0;
  24.         public static int g_Tc1_V_Idx = 0;
  25.  
  26.         //Globals additional
  27.         public static int g_ninjaScale = 100;
  28.         public static float g_ninjarotX = 90;
  29.         public static float g_ninjarotY = 0;
  30.         public static float g_ninjarotZ = 0;
  31.         public static int g_flipUV = 1;
  32.         public static int g_flipXZAxis = 0;
  33.         public static int g_uvScale = 100;
  34.  
  35.         public static string g_coll_prefix = "col_";
  36.         public static bool g_skip_dupe = false;
  37.  
  38.         //Debug mode
  39.         public static int g_debugMode = 0;
  40.  
  41.  
  42.  
  43.         public static string CreateMeshName(int num)
  44.         {
  45.             return "Mesh_" + num.ToString("X2") + ".rip";
  46.         }
  47.  
  48.         public static string StringClear(string s)
  49.         {
  50.             return s.Replace(" ", "");
  51.         }
  52.  
  53.         public static void EnableSceneRedraw()
  54.         {
  55.             if (suspendredraw != null)
  56.                 suspendredraw.Dispose();
  57.             suspendredraw = null;
  58.         }
  59.  
  60.         public static void DisableSceneRedraw()
  61.         {
  62.             suspendredraw = RipMaxPluginClass.global.Interface.SuspendSceneRedrawGuard.Create();
  63.             suspendredraw.Suspend();
  64.         }
  65.  
  66.         public static string ReadNullTerminatedString(this System.IO.BinaryReader stream)
  67.         {
  68.             string str = "";
  69.             char ch;
  70.             while ((int)(ch = stream.ReadChar()) != 0)
  71.                 str = str + ch;
  72.             return str;
  73.         }
  74.  
  75.         public static void ImportRipFile(string RipFilePath)
  76.         {
  77.             byte[] RipFileData = File.ReadAllBytes(RipFilePath);
  78.  
  79.  
  80.             using (MemoryStream RipMemoryStream = new MemoryStream(RipFileData))
  81.             using (BinaryReader RipFileReader = new BinaryReader(RipMemoryStream))
  82.             {
  83.                 uint Signature = RipFileReader.ReadUInt32();
  84.                 uint Version = RipFileReader.ReadUInt32();
  85.  
  86.                 if (Signature != RipSignature)
  87.                 {
  88.                     RipMaxPluginClass.core.PushPrompt("Bad RIP file signature");
  89.                     return;
  90.                 }
  91.  
  92.                 if (Version != RipFileVersion)
  93.                 {
  94.                     RipMaxPluginClass.core.PushPrompt("Bad RIP file version");
  95.                     return;
  96.                 }
  97.  
  98.                 uint dwFacesCnt = RipFileReader.ReadUInt32();
  99.                 uint dwVertexesCnt = RipFileReader.ReadUInt32();
  100.                 uint VertexSize = RipFileReader.ReadUInt32();
  101.                 uint TextureFilesCnt = RipFileReader.ReadUInt32();
  102.                 uint ShaderFilesCnt = RipFileReader.ReadUInt32();
  103.                 uint VertexAttributesCnt = RipFileReader.ReadUInt32();
  104.  
  105.                 List<uint> VertexAttribTypesArray = new List<uint>();
  106.  
  107.                 int TempPosIdx = 0;
  108.                 int TempNormalIdx = 0;
  109.                 int TempTexCoordIdx = 0;
  110.  
  111.                 List<int[]> UV_Idx_array = new List<int[]>();
  112.                 List<float[][]> UV_array = new List<float[][]>();
  113.                 List<float[]> Vert_array = new List<float[]>();
  114.                 List<float[]> Normal_array = new List<float[]>();
  115.  
  116.  
  117.                 List<string> TextureFiles = new List<string>();
  118.                 List<string> ShaderFiles = new List<string>();
  119.                 List<uint[]> Face_array = new List<uint[]>();
  120.  
  121.                 List<IMultiMtl> CreatedMaterials = new List<IMultiMtl>();
  122.  
  123.                 for (int i = 0; i < VertexAttributesCnt; i++)
  124.                 {
  125.                     string Semantic = RipFileReader.ReadNullTerminatedString();
  126.                     uint SemanticIndex = RipFileReader.ReadUInt32();
  127.                     uint Offset = RipFileReader.ReadUInt32();
  128.                     uint Size = RipFileReader.ReadUInt32();
  129.                     uint TypeMapElements = RipFileReader.ReadUInt32();
  130.  
  131.  
  132.                     for (int n = 0; n < TypeMapElements; n++)
  133.                     {
  134.                         VertexAttribTypesArray.Add(RipFileReader.ReadUInt32());
  135.                     }
  136.  
  137.  
  138.                     if (g_VertexFormatRecog == 0)
  139.                     {
  140.  
  141.                         if (Semantic == "POSITION")
  142.                         {
  143.                             if (TempPosIdx == 0)
  144.                             {
  145.                                 g_PosX_Idx = Offset / 4;
  146.                                 g_PosY_Idx = g_PosX_Idx + 1;
  147.                                 g_PosZ_Idx = g_PosX_Idx + 2;
  148.                                 TempPosIdx = TempPosIdx + 1;
  149.                             }
  150.                         }
  151.  
  152.  
  153.                         if (Semantic == "NORMAL")
  154.                         {
  155.                             if (TempNormalIdx == 0)
  156.                             {
  157.                                 g_NormX_Idx = Offset / 4;
  158.                                 g_NormY_Idx = g_NormX_Idx + 1;
  159.                                 g_NormZ_Idx = g_NormX_Idx + 2;
  160.                                 TempNormalIdx = TempNormalIdx + 1;
  161.                             }
  162.                         }
  163.  
  164.                         if (Semantic == "TEXCOORD")
  165.                         {
  166.                             if (TempNormalIdx == 0)
  167.                             {
  168.                                 int tc_u_idx = (int)Offset / 4;
  169.                                 int tc_v_idx = tc_u_idx + 1;
  170.                                 UV_Idx_array.Add(new int[] { tc_u_idx, tc_v_idx });
  171.                             }
  172.                         }
  173.  
  174.  
  175.  
  176.                     }
  177.                     else
  178.                     {
  179.                         UV_Idx_array.Add(new int[] { g_Tc0_U_Idx, g_Tc0_V_Idx });
  180.                         UV_Idx_array.Add(new int[] { g_Tc1_U_Idx, g_Tc1_V_Idx });
  181.                     }
  182.  
  183.                 }
  184.  
  185.                 for (int i = 0; i < UV_Idx_array.Count; i++)
  186.                 {
  187.                     UV_array.Add(null);
  188.                 }
  189.  
  190.  
  191.                 for (int i = 0; i < TextureFilesCnt; i++)
  192.                 {
  193.                     TextureFiles.Add(RipFileReader.ReadNullTerminatedString());
  194.                 }
  195.  
  196.                 for (int i = 0; i < ShaderFilesCnt; i++)
  197.                 {
  198.                     ShaderFiles.Add(RipFileReader.ReadNullTerminatedString());
  199.                 }
  200.  
  201.                 for (int i = 0; i < dwFacesCnt; i++)
  202.                 {
  203.                     uint i0, i1, i2;
  204.                     i0 = RipFileReader.ReadUInt32();
  205.                     i1 = RipFileReader.ReadUInt32();
  206.                     i2 = RipFileReader.ReadUInt32();
  207.  
  208.                     if (g_flipXZAxis == 1)
  209.                     {
  210.                         Face_array.Add(new uint[] { i2 + 1, i1 + 1, i0 + 1 });
  211.                     }
  212.                     else
  213.                     {
  214.                         Face_array.Add(new uint[] { i0 + 1, i1 + 1, i2 + 1 });
  215.                     }
  216.                 }
  217.  
  218.                 for (int k = 0; k < dwVertexesCnt; k++)
  219.                 {
  220.                     float vx = 0.0f;
  221.                     float vy = 0.0f;
  222.                     float vz = 0.0f;
  223.  
  224.                     // float vw = 0.0f;
  225.  
  226.                     float nx = 0.0f;
  227.                     float ny = 0.0f;
  228.                     float nz = 0.0f;
  229.  
  230.                     //  float nw = 0.0f;
  231.  
  232.  
  233.                     List<float[]> texUv = new List<float[]>();
  234.                     for (int n = 0; n < UV_Idx_array.Count; n++)
  235.                     {
  236.                         texUv.Add(new float[] { 0.0f, 0.0f });
  237.                     }
  238.  
  239.  
  240.                     for (int j = 0; j < VertexAttribTypesArray.Count; j++)
  241.                     {
  242.                         uint ElementType = VertexAttribTypesArray[j];
  243.                         float z = 0.0f;
  244.  
  245.                         if (ElementType == 0)
  246.                         {
  247.                             z = (float)RipFileReader.ReadSingle();
  248.                         }
  249.                         else if (ElementType == 1)
  250.                         {
  251.                             z = (float)RipFileReader.ReadUInt32();
  252.                         }
  253.                         else if (ElementType == 2)
  254.                         {
  255.                             z = (float)RipFileReader.ReadInt32();
  256.                         }
  257.                         else
  258.                         {
  259.                             z = (float)RipFileReader.ReadUInt32();
  260.                         }
  261.  
  262.                         if (j == g_PosX_Idx) vx = z;
  263.                         if (j == g_PosY_Idx) vy = z;
  264.                         if (j == g_PosZ_Idx) vz = z;
  265.  
  266.  
  267.                         if (j == g_NormX_Idx) nx = z;
  268.                         if (j == g_NormY_Idx) ny = z;
  269.                         if (j == g_NormZ_Idx) nz = z;
  270.  
  271.  
  272.  
  273.  
  274.                         for (int i = 0; i < UV_Idx_array.Count; i++)
  275.                         {
  276.                             if (j == UV_Idx_array[i][0])
  277.                             {
  278.                                 texUv[i][0] = z;
  279.                             }
  280.                             if (j == UV_Idx_array[i][1])
  281.                             {
  282.                                 texUv[i][1] = z * g_flipUV;
  283.                             }
  284.                         }
  285.  
  286.  
  287.                     }
  288.  
  289.  
  290.  
  291.                     Vert_array.Add(new float[] { (vx * g_ninjaScale), (vy * g_ninjaScale), (vz * g_ninjaScale) });
  292.                     Normal_array.Add(new float[] { nz, ny, nx });
  293.  
  294.                     for (int i = 0; i < texUv.Count; i++)
  295.                     {
  296.                         if (texUv[i][0] == 0.0f && texUv[i][1] == 0.0f) continue;
  297.  
  298.                         List<float[]> cur_UV_array = new List<float[]>();
  299.                         if (UV_array[i] != null)
  300.                             cur_UV_array.AddRange(UV_array[i]);
  301.  
  302.                         cur_UV_array.Add(new float[] { texUv[i][0], texUv[i][1], 0.0f });
  303.  
  304.  
  305.                         UV_array[i] = cur_UV_array.ToArray();
  306.  
  307.                     }
  308.                 }
  309.  
  310.  
  311.  
  312.                 for (int i = 0; i < UV_array.Count; i++)
  313.                 {
  314.                     if (UV_array[i].Length == 0)
  315.                     {
  316.                         UV_array.RemoveAt(i);
  317.                         i--;
  318.                     }
  319.                 }
  320.  
  321.  
  322.                 List<string> FileNames = new List<string>();
  323.  
  324.  
  325.                 for (int i = 0; i < TextureFiles.Count; i++)
  326.                 {
  327.                     string TexFile = "notexture.dds";
  328.                     if (TextureFiles[i].Length > 0)
  329.                         TexFile = TextureFiles[i];
  330.  
  331.                     FileNames.Add(TexFile);
  332.                 }
  333.  
  334.                 // Поиск точно такого же материала
  335.                 IMultiMtl foundmaterial = null;
  336.                 foreach (var mat in CreatedMaterials)
  337.                 {
  338.                     if (mat.NumSubMtls > 0 && mat.NumSubMtls == FileNames.Count)
  339.                     {
  340.                         bool foundalllocal = true;
  341.                         for (int i = 0; i < mat.NumSubMtls; i++)
  342.                         {
  343.                             foreach (string name in FileNames)
  344.                             {
  345.                                 if (mat.GetSubMtl(i) == null || mat.GetSubMtl(i).Name != name)
  346.                                     foundalllocal = false;
  347.                             }
  348.  
  349.                             if (!foundalllocal)
  350.                             {
  351.                                 break;
  352.                             }
  353.                         }
  354.  
  355.                         if (foundalllocal)
  356.                         {
  357.                             foundmaterial = mat;
  358.                             break;
  359.                         }
  360.  
  361.                     }
  362.                     else
  363.                     {
  364.  
  365.                     }
  366.                 }
  367.  
  368.  
  369.                 IMultiMtl thenewmaterial = null;
  370.  
  371.  
  372.  
  373.                 if (foundmaterial == null)
  374.                 {
  375.                     thenewmaterial = RipMaxPluginClass.global.NewEmptyMultiMtl;
  376.                     thenewmaterial.SetNumSubMtls(TextureFiles.Count);
  377.  
  378.                     for (int i = 0; i < TextureFiles.Count; i++)
  379.                     {
  380.                         string TexFile = "notexture.dds";
  381.                         if (TextureFiles[i].Length > 0)
  382.                             TexFile = TextureFiles[i];
  383.  
  384.                         string TexFileName = Path.GetDirectoryName(RipFilePath) + "\\" + TexFile;
  385.  
  386.                         IStdMat2 asdf = RipMaxPluginClass.global.NewDefaultStdMat;
  387.                        
  388.                         ...
  389.  
  390.                     }
  391.                     if (foundmaterial != null)
  392.                         CreatedMaterials.Add(foundmaterial);
  393.                 }
  394.                 else
  395.                 {
  396.                     thenewmaterial = foundmaterial;
  397.                 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement