private AlienTexture LoadTexture(int EntryIndex, bool loadV1 = true) { AlienTexture toReturn = new AlienTexture(); if (EntryIndex < 0 || EntryIndex >= LevelTextures.PAK.Header.EntryCount) { Debug.LogWarning("Asked to load texture at index " + EntryIndex + ", which is out of bounds!"); return(null); } alien_pak_entry Entry = LevelTextures.PAK.Entries[EntryIndex]; alien_texture_bin_texture InTexture = LevelTextures.BIN.Textures[Entry.BINIndex]; Vector2 textureDims; int textureLength = 0; int mipLevels = 0; if (loadV1) { textureDims = new Vector2(InTexture.Size_V1[0], InTexture.Size_V1[1]); textureLength = InTexture.Length_V1; mipLevels = InTexture.MipLevelsV1; } else { textureDims = new Vector2(InTexture.Size_V2[0], InTexture.Size_V2[1]); textureLength = InTexture.Length_V2; mipLevels = InTexture.MipLevelsV2; } if (textureLength == 0) { Debug.LogWarning("LENGTH ZERO - NOT LOADING"); return(toReturn); } UnityEngine.TextureFormat format = UnityEngine.TextureFormat.BC7; switch (InTexture.Format) { case alien_texture_format.Alien_R32G32B32A32_SFLOAT: format = UnityEngine.TextureFormat.RGBA32; break; case alien_texture_format.Alien_FORMAT_R8G8B8A8_UNORM: format = UnityEngine.TextureFormat.ETC2_RGBA8; //? break; case alien_texture_format.Alien_FORMAT_R8G8B8A8_UNORM_0: format = UnityEngine.TextureFormat.ETC2_RGBA8; //? break; case alien_texture_format.Alien_FORMAT_SIGNED_DISTANCE_FIELD: Debug.LogWarning("SDF! NOT LOADED"); return(toReturn); case alien_texture_format.Alien_FORMAT_R8: format = UnityEngine.TextureFormat.R8; break; case alien_texture_format.Alien_FORMAT_BC1: format = UnityEngine.TextureFormat.DXT1; break; case alien_texture_format.Alien_FORMAT_BC2: Debug.LogWarning("BC2! NOT LOADED"); return(toReturn); case alien_texture_format.Alien_FORMAT_BC5: format = UnityEngine.TextureFormat.BC5; //Is this correct? break; case alien_texture_format.Alien_FORMAT_BC3: format = UnityEngine.TextureFormat.DXT5; break; case alien_texture_format.Alien_FORMAT_BC7: format = UnityEngine.TextureFormat.BC7; break; case alien_texture_format.Alien_FORMAT_R8G8: format = UnityEngine.TextureFormat.BC5; // is this correct? break; } BinaryReader tempReader = new BinaryReader(new MemoryStream(LevelTextures.PAK.DataStart)); tempReader.BaseStream.Position = Entry.Offset; if (InTexture.Type == 7) { Cubemap cubemapTex = new Cubemap((int)textureDims.x, format, true); cubemapTex.name = LevelTextures.BIN.TextureFilePaths[Entry.BINIndex]; cubemapTex.SetPixelData(tempReader.ReadBytes(textureLength / 6), 0, CubemapFace.PositiveX); cubemapTex.SetPixelData(tempReader.ReadBytes(textureLength / 6), 0, CubemapFace.NegativeX); cubemapTex.SetPixelData(tempReader.ReadBytes(textureLength / 6), 0, CubemapFace.PositiveY); cubemapTex.SetPixelData(tempReader.ReadBytes(textureLength / 6), 0, CubemapFace.NegativeY); cubemapTex.SetPixelData(tempReader.ReadBytes(textureLength / 6), 0, CubemapFace.PositiveZ); cubemapTex.SetPixelData(tempReader.ReadBytes(textureLength / 6), 0, CubemapFace.NegativeZ); cubemapTex.Apply(); toReturn.cubemap = cubemapTex; //AssetDatabase.CreateAsset(cubemapTex, "Assets/Cubemaps/" + Path.GetFileNameWithoutExtension(cubemapTex.name) + ".cubemap"); } else { Texture2D texture = new Texture2D((int)textureDims[0], (int)textureDims[1], format, mipLevels, true); texture.name = LevelTextures.BIN.TextureFilePaths[Entry.BINIndex]; texture.LoadRawTextureData(tempReader.ReadBytes(textureLength)); texture.Apply(); toReturn.texture = texture; } tempReader.Close(); return(toReturn); }
private void LoadTextureAssets() { bool[] textureTracker = new bool[levelData.LevelTextures.BIN.Header.EntryCount]; AssetDatabase.StartAssetEditing(); for (int i = 0; i < levelData.LevelTextures.PAK.Entries.Count; i++) { alien_pak_entry Entry = levelData.LevelTextures.PAK.Entries[i]; alien_texture_bin_texture InTexture = levelData.LevelTextures.BIN.Textures[Entry.BINIndex]; Vector2 textureDims; int textureLength = 0; int mipLevels = 0; if (!textureTracker[Entry.BINIndex]) { textureDims = new Vector2(InTexture.Size_V1[0], InTexture.Size_V1[1]); textureLength = InTexture.Length_V1; mipLevels = InTexture.MipLevelsV1; } else { textureDims = new Vector2(InTexture.Size_V2[0], InTexture.Size_V2[1]); textureLength = InTexture.Length_V2; mipLevels = InTexture.MipLevelsV2; } textureTracker[Entry.BINIndex] = true; if (textureLength == 0) { continue; } if (InTexture.Format == alien_texture_format.Alien_FORMAT_SIGNED_DISTANCE_FIELD || InTexture.Format == alien_texture_format.Alien_FORMAT_BC2) { continue; } TextureFormat format = TextureFormat.BC7; switch (InTexture.Format) { case alien_texture_format.Alien_R32G32B32A32_SFLOAT: format = TextureFormat.RGBA32; break; case alien_texture_format.Alien_FORMAT_R8G8B8A8_UNORM: format = TextureFormat.ETC2_RGBA8; //? break; case alien_texture_format.Alien_FORMAT_R8G8B8A8_UNORM_0: format = TextureFormat.ETC2_RGBA8; //? break; case alien_texture_format.Alien_FORMAT_R8: format = TextureFormat.R8; break; case alien_texture_format.Alien_FORMAT_BC1: format = TextureFormat.DXT1; break; case alien_texture_format.Alien_FORMAT_BC5: format = TextureFormat.BC5; //Is this correct? break; case alien_texture_format.Alien_FORMAT_BC3: format = TextureFormat.DXT5; break; case alien_texture_format.Alien_FORMAT_BC7: format = TextureFormat.BC7; break; case alien_texture_format.Alien_FORMAT_R8G8: format = TextureFormat.BC5; // is this correct? break; } BinaryReader tempReader = new BinaryReader(new MemoryStream(levelData.LevelTextures.PAK.DataStart)); tempReader.BaseStream.Position = Entry.Offset; if (InTexture.Type == 7) { Cubemap cubemap = new Cubemap((int)textureDims.x, format, true); cubemap.name = levelData.LevelTextures.BIN.TextureFilePaths[Entry.BINIndex]; cubemap.SetPixelData(tempReader.ReadBytes(textureLength / 6), 0, CubemapFace.PositiveX); cubemap.SetPixelData(tempReader.ReadBytes(textureLength / 6), 0, CubemapFace.NegativeX); cubemap.SetPixelData(tempReader.ReadBytes(textureLength / 6), 0, CubemapFace.PositiveY); cubemap.SetPixelData(tempReader.ReadBytes(textureLength / 6), 0, CubemapFace.NegativeY); cubemap.SetPixelData(tempReader.ReadBytes(textureLength / 6), 0, CubemapFace.PositiveZ); cubemap.SetPixelData(tempReader.ReadBytes(textureLength / 6), 0, CubemapFace.NegativeZ); cubemap.Apply(); string fullFilePath = "Assets/Resources/" + SharedVals.instance.LevelName + "/Cubemaps/" + cubemap.name.Substring(0, cubemap.name.Length - Path.GetExtension(cubemap.name).Length) + ".cubemap"; string fileDirectory = GetDirectory(fullFilePath); if (!Directory.Exists(fileDirectory)) { Directory.CreateDirectory(fileDirectory); } AssetDatabase.CreateAsset(cubemap, fullFilePath); } else { Texture2D texture = new Texture2D((int)textureDims[0], (int)textureDims[1], format, mipLevels, true); texture.name = levelData.LevelTextures.BIN.TextureFilePaths[Entry.BINIndex]; texture.LoadRawTextureData(tempReader.ReadBytes(textureLength)); texture.Apply(); string fullFilePath = "Assets/Resources/" + SharedVals.instance.LevelName + "/Textures/" + texture.name.Substring(0, texture.name.Length - Path.GetExtension(texture.name).Length) + ".asset"; string fileDirectory = GetDirectory(fullFilePath); if (!Directory.Exists(fileDirectory)) { Directory.CreateDirectory(fileDirectory); } AssetDatabase.CreateAsset(texture, fullFilePath); } tempReader.Close(); } AssetDatabase.StopAssetEditing(); }