public static Transform Load(string ModelName) { Clear(); string OpenPath = string.Concat(Configuration.GameFld, Configuration.Mod, ModelName); ModelObject = new GameObject(ModelName); if (!File.Exists(OpenPath + ".mdl")) return ModelObject.transform; CRead = new CustomReader(File.OpenRead(OpenPath + ".mdl")); MDL_Header = CRead.ReadType<studiohdr_t>(); ParseMdlFile(); CRead = new CustomReader(File.OpenRead(OpenPath + ".vvd")); VVD_Header = CRead.ReadType<vertexFileHeader_t>(); ParseVvdFile(); CRead = new CustomReader(File.OpenRead(OpenPath + ".dx90.vtx")); VTX_Header = CRead.ReadType<FileHeader_t>(); ParseVtxFile(); if (!Configuration.Models.ContainsKey(ModelObject.name)) Configuration.Models.Add(ModelName, ModelObject.transform); CRead.Dispose(); return ModelObject.transform; }
public static Texture2D Load(string TextureName) { if (!File.Exists(Configuration.GameFld + Configuration.Mod + "/materials/" + TextureName + ".vtf")) return new Texture2D(1, 1); CRead = new CustomReader(File.OpenRead(Configuration.GameFld + Configuration.Mod + "/materials/" + TextureName + ".vtf")); VTF_Header = CRead.ReadType<tagVTFHEADER>(); Texture2D VTF_Texture = default(Texture2D); TextureFormat ImageFormat; long OffsetInFile = VTF_Header.Width * VTF_Header.Height * uiBytesPerPixels[(int)VTF_Header.HighResImageFormat]; switch (VTF_Header.HighResImageFormat) { case VTFImageFormat.IMAGE_FORMAT_DXT1: OffsetInFile = ((VTF_Header.Width + 3) / 4) * ((VTF_Header.Height + 3) / 4) * 8; ImageFormat = TextureFormat.DXT1; break; case VTFImageFormat.IMAGE_FORMAT_DXT3: case VTFImageFormat.IMAGE_FORMAT_DXT5: OffsetInFile = ((VTF_Header.Width + 3) / 4) * ((VTF_Header.Height + 3) / 4) * 16; ImageFormat = TextureFormat.DXT5; break; case VTFImageFormat.IMAGE_FORMAT_RGB888: case VTFImageFormat.IMAGE_FORMAT_BGR888: ImageFormat = TextureFormat.RGB24; break; case VTFImageFormat.IMAGE_FORMAT_RGBA8888: ImageFormat = TextureFormat.RGBA32; break; case VTFImageFormat.IMAGE_FORMAT_ARGB8888: ImageFormat = TextureFormat.ARGB32; break; case VTFImageFormat.IMAGE_FORMAT_BGRA8888: ImageFormat = TextureFormat.BGRA32; break; default: return new Texture2D(1, 1); } VTF_Texture = new Texture2D(VTF_Header.Width, VTF_Header.Height, ImageFormat, false); byte[] VTF_File = CRead.GetBytes((int)OffsetInFile, CRead.InputStream.Length - OffsetInFile); if (VTF_Header.HighResImageFormat == VTFImageFormat.IMAGE_FORMAT_BGR888) { for (int i = 0; i < VTF_File.Length - 1; i += 3) { byte Temp = VTF_File[i]; VTF_File[i] = VTF_File[i + 2]; VTF_File[i + 2] = Temp; } } VTF_Texture.LoadRawTextureData(VTF_File); Texture2D Mip_Texture = new Texture2D(VTF_Header.Width, VTF_Header.Height, TextureFormat.RGBA32, true); Mip_Texture.SetPixels32(VTF_Texture.GetPixels32()); Mip_Texture.Apply(); Mip_Texture.Compress(false); Object.DestroyImmediate(VTF_Texture); CRead.Dispose(); return Mip_Texture; }
public static Texture2D Load(string TextureName) { string TextureDestinationPath = Configuration.GameFld + Configuration.Mod + "/materials/" + TextureName + ".vtf"; if (!File.Exists(TextureDestinationPath)) { if (File.Exists(Configuration.GameFld + Configuration._Mod + "/materials/" + TextureName + ".vtf")) TextureDestinationPath = Configuration.GameFld + Configuration._Mod + "/materials/" + TextureName + ".vtf"; else return new Texture2D(1, 1); } CRead = new CustomReader(File.OpenRead(TextureDestinationPath)); VTF_Header = CRead.ReadType<tagVTFHEADER>(); Texture2D VTF_Texture; TextureFormat ImageFormat; long OffsetInFile = VTF_Header.Width * VTF_Header.Height * uiBytesPerPixels[(int)VTF_Header.HighResImageFormat]; switch (VTF_Header.HighResImageFormat) { case VTFImageFormat.IMAGE_FORMAT_DXT1: OffsetInFile = ((VTF_Header.Width + 3) / 4) * ((VTF_Header.Height + 3) / 4) * 8; ImageFormat = TextureFormat.DXT1; break; case VTFImageFormat.IMAGE_FORMAT_DXT3: case VTFImageFormat.IMAGE_FORMAT_DXT5: OffsetInFile = ((VTF_Header.Width + 3) / 4) * ((VTF_Header.Height + 3) / 4) * 16; ImageFormat = TextureFormat.DXT5; break; case VTFImageFormat.IMAGE_FORMAT_RGB888: case VTFImageFormat.IMAGE_FORMAT_BGR888: ImageFormat = TextureFormat.RGB24; break; case VTFImageFormat.IMAGE_FORMAT_RGBA8888: ImageFormat = TextureFormat.RGBA32; break; case VTFImageFormat.IMAGE_FORMAT_ARGB8888: ImageFormat = TextureFormat.ARGB32; break; case VTFImageFormat.IMAGE_FORMAT_BGRA8888: ImageFormat = TextureFormat.BGRA32; break; default: return new Texture2D(1, 1); } VTF_Texture = new Texture2D(VTF_Header.Width, VTF_Header.Height, ImageFormat, false); byte[] VTF_File = CRead.GetBytes((int)OffsetInFile, CRead.InputStream.Length - OffsetInFile); if (VTF_Header.HighResImageFormat == VTFImageFormat.IMAGE_FORMAT_BGR888) { for (int i = 0; i < VTF_File.Length - 1; i += 3) { byte Temp = VTF_File[i]; VTF_File[i] = VTF_File[i + 2]; VTF_File[i + 2] = Temp; } } VTF_Texture.LoadRawTextureData(VTF_File); CRead.Dispose(); // MIPMAP NOT WORK!!! VERY SLOWLY!!! if (Configuration.AndroidCompression) { Texture2D Mip_Texture = new Texture2D(VTF_Header.Width, VTF_Header.Height); Mip_Texture.SetPixels32(VTF_Texture.GetPixels32()); EditorUtility.CompressTexture(Mip_Texture, TextureFormat.ETC_RGB4, TextureCompressionQuality.Fast); Mip_Texture.Apply(); return Mip_Texture; } VTF_Texture.Apply(); return VTF_Texture; }
public void Load() { Clear(); if (!File.Exists(Configuration.GameFld + Configuration.Mod + "/maps/" + LevelName + ".bsp")) throw new FileNotFoundException(Configuration.GameFld + Configuration.Mod + "/maps/" + LevelName + ".bsp"); Configuration.Initialize(LevelName); Configuration.AndroidCompression = AndroidCompression; CRead = new CustomReader(File.OpenRead(Configuration.GameFld + Configuration.Mod + "/maps/" + LevelName + ".bsp")); BSP_Header = CRead.ReadType<BspSpecification.dheader_t>(); if (BSP_Header.ident != (('P' << 24) + ('S' << 16) + ('B' << 8) + 'V')) throw new FileLoadException("Wrong magic number"); if (BSP_Header.version < 19 || BSP_Header.version > 21) throw new FileLoadException(string.Format("BSP version ({0}) isn't supported", BSP_Header.version)); if (BSP_Header.lumps[0].fileofs == 0) { Debug.Log("Found Left 4 Dead 2 header"); for (int i = 0; i < BSP_Header.lumps.Length; i++) { BSP_Header.lumps[i].fileofs = BSP_Header.lumps[i].filelen; BSP_Header.lumps[i].filelen = BSP_Header.lumps[i].version; } } string input = Encoding.ASCII.GetString(CRead.GetBytes(BSP_Header.lumps[0].filelen, BSP_Header.lumps[0].fileofs)); foreach (Match match in Regex.Matches(input, @"{[^}]*}", RegexOptions.IgnoreCase)) BSP_Entities.Add(match.Value); if (!HighDynamicRange) BSP_Faces.AddRange(CRead.ReadType<BspSpecification.dface_t>(BSP_Header.lumps[7].filelen / 56, BSP_Header.lumps[7].fileofs)); else BSP_Faces.AddRange(CRead.ReadType<BspSpecification.dface_t>(BSP_Header.lumps[58].filelen / 56, BSP_Header.lumps[58].fileofs)); BSP_Models.AddRange(CRead.ReadType<BspSpecification.dmodel_t>(BSP_Header.lumps[14].filelen / 48, BSP_Header.lumps[14].fileofs)); BSP_DispInfo.AddRange(CRead.ReadType<BspSpecification.ddispinfo_t>(BSP_Header.lumps[26].filelen / 176, BSP_Header.lumps[26].fileofs)); BSP_DispVerts.AddRange(CRead.ReadType<BspSpecification.dDispVert>(BSP_Header.lumps[33].filelen / 20, BSP_Header.lumps[33].fileofs)); BSP_TexData.AddRange(CRead.ReadType<BspSpecification.dtexdata_t>(BSP_Header.lumps[2].filelen / 32, BSP_Header.lumps[2].fileofs)); BSP_TexInfo.AddRange(CRead.ReadType<BspSpecification.texinfo_t>(BSP_Header.lumps[6].filelen / 72, BSP_Header.lumps[6].fileofs)); int[] BSP_TexStrTable = CRead.ReadType<int>(BSP_Header.lumps[44].filelen / 4, BSP_Header.lumps[44].fileofs); BSP_TexStrData.AddRange(CRead.ReadNullTerminatedString(BSP_TexStrTable, BSP_Header.lumps[43].fileofs)); BSP_Edges.AddRange(CRead.ReadType<BspSpecification.dedge_t>(BSP_Header.lumps[12].filelen / 4, BSP_Header.lumps[12].fileofs)); BSP_Vertices.AddRange(CRead.ReadType<Vector3>(BSP_Header.lumps[3].filelen / 12, BSP_Header.lumps[3].fileofs)); BSP_Surfedges.AddRange(CRead.ReadType<int>(BSP_Header.lumps[13].filelen / 4, BSP_Header.lumps[13].fileofs)); UnpackPakFile(CRead.GetBytes(BSP_Header.lumps[40].filelen, BSP_Header.lumps[40].fileofs)); for (int i = 0; i < BSP_Entities.Count; i++) LoadEntity(i); CRead.Dispose(); }