/// <summary> /// Converts the given texture dictionary to a field texture archive, and returns a new texture dictionary filled with dummy textures for each texture in the given texture dictionary. /// </summary> /// <param name="textureDictionary"></param> /// <param name="archiveFilePath"></param> /// <returns></returns> public static TextureDictionary ConvertToFieldTextureArchive(TextureDictionary textureDictionary, string archiveFilePath, bool usePS4Format = false) { var archiveBuilder = new ArchiveBuilder(); // Create bgTexArcData00.txt var fieldTextureArchiveDataInfoStream = new MemoryStream(); using (var streamWriter = new StreamWriter(fieldTextureArchiveDataInfoStream, Encoding.Default, 4096, true)) { streamWriter.WriteLine("1,"); streamWriter.WriteLine($"{textureDictionary.Count},"); } archiveBuilder.AddFile("bgTexArcData00.txt", fieldTextureArchiveDataInfoStream); // Convert textures foreach (var texture in textureDictionary.Textures) { var textureInfo = TextureInfo.GetTextureInfo(texture); var texturePixelData = TextureUtilities.GetRawPixelData(texture); // Create field texture & save it Stream textureStream = new MemoryStream(); if (!usePS4Format) { var fieldTexture = new FieldTexturePS3(textureInfo.PixelFormat, ( byte )textureInfo.MipMapCount, ( short )textureInfo.Width, ( short )textureInfo.Height, texturePixelData); fieldTexture.Save(textureStream); } else { var fieldTexture = new GNFTexture(textureInfo.PixelFormat, ( byte )textureInfo.MipMapCount, ( short )textureInfo.Width, ( short )textureInfo.Height, texturePixelData); fieldTexture.Save(textureStream); } archiveBuilder.AddFile(texture.Name, textureStream); } // Finally build archive file archiveBuilder.BuildFile(archiveFilePath); // Dummy out textures in texture dictionary var newTextureDictionary = new TextureDictionary(textureDictionary.Version); foreach (var texture in textureDictionary.Textures) { newTextureDictionary.Add(Texture.CreateDefaultTexture(texture.Name)); } return(newTextureDictionary); }
private static Resource Load(Stream stream, bool leaveOpen, Type type) { if (stream.Length < ResourceFileHeader.SIZE) { throw new InvalidDataException("Stream is too small to be a valid resource file."); } using (var reader = new ResourceReader(stream, leaveOpen)) { var header = reader.ReadFileHeader(); Resource res; switch (header.Type) { case ResourceType.ModelPack: res = new ModelPack(header.Version); break; case ResourceType.ShaderCachePS3: if (type == typeof(Epl)) { res = new Epl(header.Version); } else { res = new ShaderCachePS3(header.Version); } break; case ResourceType.ShaderCachePSP2: res = new ShaderCachePSP2(header.Version); break; case ResourceType.ShaderCachePS4: res = new ShaderCachePS4(header.Version); break; // Custom case ResourceType.TextureMap: res = new TextureMap(header.Version); break; case ResourceType.TextureDictionary: res = new TextureDictionary(header.Version); break; case ResourceType.Texture: res = new Texture(header.Version); break; case ResourceType.ShaderPS3: res = new ShaderPS3(header.Version); break; case ResourceType.ShaderPSP2: res = new ShaderPSP2(header.Version); break; case ResourceType.ShaderPS4: res = new ShaderPS4(header.Version); break; case ResourceType.Model: res = new Model(header.Version); break; case ResourceType.Node: return(Node.ReadRecursive(reader, header.Version)); case ResourceType.UserPropertyDictionary: res = new UserPropertyDictionary(header.Version); break; case ResourceType.Morph: res = new Morph(header.Version); break; case ResourceType.MorphTarget: res = new MorphTarget(header.Version); break; case ResourceType.MorphTargetList: res = new MorphTargetList(header.Version); break; case ResourceType.MaterialDictionary: res = new MaterialDictionary(header.Version); break; case ResourceType.ChunkType000100F9: res = new ChunkType000100F9(header.Version); break; case ResourceType.ChunkType000100F8: return(new ChunkType000100F8(header.Version) { Data = reader.ReadBytes(reader.ReadInt32()) }); case ResourceType.AnimationPack: res = new AnimationPack(header.Version); break; case ResourceType.MaterialAttribute: return(MaterialAttribute.Read(reader, header.Version)); case ResourceType.Material: res = new Material(header.Version); break; case ResourceType.Light: res = new Light(header.Version); break; case ResourceType.Mesh: res = new Mesh(header.Version); break; case ResourceType.Camera: res = new Camera(header.Version); break; case ResourceType.Epl: { res = new Epl(header.Version); break; } case ResourceType.EplLeaf: res = new EplLeaf(header.Version); break; case ResourceType.Animation: res = new Animation(header.Version); break; case ResourceType.AnimationBit29Data: res = new AnimationBit29Data(header.Version); break; case ResourceType.AnimationLayer: res = new AnimationLayer(header.Version); break; case ResourceType.AnimationController: res = new AnimationController(header.Version); break; default: throw new InvalidDataException("Unknown/Invalid resource type"); } res.ReadCore(reader); if (res.ResourceType == ResourceType.ModelPack && type == typeof(AnimationPack)) { // Identify AnimationPack from a file with a model resource header var model = ( ModelPack )res; if (model.AnimationPack != null && model.ChunkType000100F8 == null && model.ChunkType000100F9 == null && model.Materials == null && model.Model == null && model.Textures == null) { res = model.AnimationPack; } } return(res); } }
public static Resource Load(Stream stream, bool leaveOpen) { if (stream.Length < ResourceFileHeader.SIZE) { throw new InvalidDataException("Stream is too small to be a valid resource file."); } using (var reader = new ResourceReader(stream, leaveOpen)) { var header = reader.ReadFileHeader(); Resource res; switch (header.Type) { case ResourceType.Model: res = new Model(header.Version); break; case ResourceType.ShaderCachePS3: res = new ShaderCachePS3(header.Version); break; case ResourceType.ShaderCachePSP2: res = new ShaderCachePSP2(header.Version); break; case ResourceType.ShaderCachePS4: res = new ShaderCachePS4(header.Version); break; // Custom case ResourceType.TextureMap: res = new TextureMap(header.Version); break; case ResourceType.TextureDictionary: res = new TextureDictionary(header.Version); break; case ResourceType.Texture: res = new Texture(header.Version); break; case ResourceType.ShaderPS3: res = new ShaderPS3(header.Version); break; case ResourceType.ShaderPSP2: res = new ShaderPSP2(header.Version); break; case ResourceType.ShaderPS4: res = new ShaderPS4(header.Version); break; case ResourceType.Scene: res = new Scene(header.Version); break; case ResourceType.Node: res = new Node(header.Version); break; case ResourceType.UserPropertyCollection: res = new UserPropertyCollection(header.Version); break; case ResourceType.Morph: res = new Morph(header.Version); break; case ResourceType.MorphTarget: res = new MorphTarget(header.Version); break; case ResourceType.MorphTargetList: res = new MorphTargetList(header.Version); break; case ResourceType.MaterialDictionary: res = new MaterialDictionary(header.Version); break; case ResourceType.ChunkType000100F9: res = new ChunkType000100F9(header.Version); break; case ResourceType.ChunkType000100F8: res = new ChunkType000100F8(header.Version); break; case ResourceType.AnimationPack: res = new AnimationPack(header.Version); break; case ResourceType.MaterialAttribute: return(MaterialAttribute.Read(reader, header.Version)); case ResourceType.Material: res = new Material(header.Version); break; case ResourceType.Light: res = new Light(header.Version); break; case ResourceType.Geometry: res = new Geometry(header.Version); break; case ResourceType.Camera: res = new Camera(header.Version); break; case ResourceType.Epl: { return(new Epl { // Make sure to also read the extra boolean we wrote to the file to keep track of this. IncludesProperties = reader.ReadBoolean(), RawData = reader.ReadBytes(( int )(stream.Length - stream.Position)) }); } case ResourceType.EplLeaf: res = new EplLeaf(header.Version); break; case ResourceType.Animation: res = new Animation(header.Version); break; case ResourceType.AnimationExtraData: res = new AnimationExtraData(header.Version); break; case ResourceType.KeyframeTrack: res = new KeyframeTrack(header.Version); break; case ResourceType.AnimationController: res = new AnimationController(header.Version); break; default: throw new InvalidDataException("Unknown/Invalid resource type"); } res.Read(reader); if (res.ResourceType == ResourceType.Model) { // Identifier AnimationPack from a file with a model resource header var model = ( Model )res; if (model.AnimationPack != null && model.ChunkType000100F8 == null && model.ChunkType000100F9 == null && model.Materials == null && model.Scene == null && model.Textures == null) { res = model.AnimationPack; } } return(res); } }
private static Material ConvertMaterialAndTextures(Ai.Material aiMaterial, ModelConverterOptions options, string baseDirectoryPath, TextureDictionary textureDictionary) { // Convert all textures TextureInfo diffuseTexture = null; if (aiMaterial.HasTextureDiffuse) { diffuseTexture = ConvertTexture(aiMaterial.TextureDiffuse, baseDirectoryPath); } TextureInfo lightmapTexture = null; if (aiMaterial.HasTextureLightMap) { lightmapTexture = ConvertTexture(aiMaterial.TextureLightMap, baseDirectoryPath); } TextureInfo displacementTexture = null; if (aiMaterial.HasTextureDisplacement) { displacementTexture = ConvertTexture(aiMaterial.TextureDisplacement, baseDirectoryPath); } TextureInfo opacityTexture = null; if (aiMaterial.HasTextureOpacity) { opacityTexture = ConvertTexture(aiMaterial.TextureOpacity, baseDirectoryPath); } TextureInfo normalTexture = null; if (aiMaterial.HasTextureNormal) { normalTexture = ConvertTexture(aiMaterial.TextureNormal, baseDirectoryPath); } TextureInfo heightTexture = null; if (aiMaterial.HasTextureHeight) { heightTexture = ConvertTexture(aiMaterial.TextureHeight, baseDirectoryPath); } TextureInfo emissiveTexture = null; if (aiMaterial.HasTextureEmissive) { emissiveTexture = ConvertTexture(aiMaterial.TextureEmissive, baseDirectoryPath); } TextureInfo ambientTexture = null; if (aiMaterial.HasTextureAmbient) { ambientTexture = ConvertTexture(aiMaterial.TextureAmbient, baseDirectoryPath); } TextureInfo specularTexture = null; if (aiMaterial.HasTextureSpecular) { specularTexture = ConvertTexture(aiMaterial.TextureSpecular, baseDirectoryPath); } TextureInfo reflectionTexture = null; if (aiMaterial.HasTextureReflection) { reflectionTexture = ConvertTexture(aiMaterial.TextureReflection, baseDirectoryPath); } // Convert material Material material = null; string materialName = AssimpConverterCommon.UnescapeName(aiMaterial.Name); switch (options.MaterialPreset) { case MaterialPreset.FieldTerrain: { if (diffuseTexture != null) { textureDictionary.Add(diffuseTexture.Texture); material = MaterialFactory.CreateFieldTerrainMaterial(materialName, diffuseTexture.Name, HasAlpha(diffuseTexture.PixelFormat)); } } break; case MaterialPreset.FieldTerrainCastShadow: { if (diffuseTexture != null) { textureDictionary.Add(diffuseTexture.Texture); material = MaterialFactory.CreateFieldTerrainCastShadowMaterial(materialName, diffuseTexture.Name, HasAlpha(diffuseTexture.PixelFormat)); } } break; case MaterialPreset.CharacterSkinP5: { if (diffuseTexture != null) { textureDictionary.Add(diffuseTexture.Texture); string shadowTextureName = diffuseTexture.Name; if (ambientTexture != null) { textureDictionary.Add(ambientTexture.Texture); shadowTextureName = ambientTexture.Name; } // TODO: transparency material = MaterialFactory.CreateCharacterSkinP5Material(materialName, diffuseTexture.Name, shadowTextureName, HasAlpha(diffuseTexture.PixelFormat)); } } break; case MaterialPreset.CharacterClothP4D: { if (diffuseTexture != null) { textureDictionary.Add(diffuseTexture.Texture); material = MaterialFactory.CreateCharacterClothP4DMaterial(materialName, diffuseTexture.Name, HasAlpha(diffuseTexture.PixelFormat)); } } break; } // Create dummy material if none was created if (material == null) { material = new Material(materialName); } return(material); }