public static J3D LoadResource(string filePath) { var existRef = m_j3dList.Find(x => string.Compare(x.FilePath, filePath, StringComparison.InvariantCultureIgnoreCase) == 0); if (existRef == null) { J3D j3d = new J3D(Path.GetFileNameWithoutExtension(filePath)); using (EndianBinaryReader reader = new EndianBinaryReader(File.ReadAllBytes(filePath), Endian.Big)) j3d.LoadFromStream(reader, WSettingsManager.GetSettings().DumpTextures, WSettingsManager.GetSettings().DumpShaders); j3d.SetHardwareLight(0, m_mainLight); j3d.SetHardwareLight(1, m_secondaryLight); // Apply patches for Wind Waker by default, since they don't seem to break anything else. j3d.SetTextureOverride("ZBtoonEX", "resources/textures/ZBtoonEX.png"); j3d.SetTextureOverride("ZAtoon", "resources/textures/ZAtoon.png"); j3d.SetColorWriteOverride("eyeLdamA", false); j3d.SetColorWriteOverride("eyeLdamB", false); j3d.SetColorWriteOverride("mayuLdamA", false); j3d.SetColorWriteOverride("mayuLdamB", false); j3d.SetColorWriteOverride("eyeRdamA", false); j3d.SetColorWriteOverride("eyeRdamB", false); j3d.SetColorWriteOverride("mayuRdamA", false); j3d.SetColorWriteOverride("mayuRdamB", false); existRef = new TSharedRef <J3D>(); existRef.FilePath = filePath; existRef.Asset = j3d; m_j3dList.Add(existRef); } existRef.ReferenceCount++; return(existRef.Asset); }
public static J3D LoadModelFromVFS(VirtualFilesystemDirectory fs, string path) { TSharedRef <J3D> existRef = null;//m_j3dList.Find(x => string.Compare(x.FilePath, arc_and_file_path, StringComparison.InvariantCultureIgnoreCase) == 0); J3D model = new J3D(path); VirtualFilesystemFile file = fs.GetFileAtPath(path); using (EndianBinaryReader reader = new EndianBinaryReader(file.Data, Endian.Big)) model.LoadFromStream(reader); existRef = new TSharedRef <J3D>(); existRef.FilePath = fs.Name + '/' + path; existRef.Asset = model; existRef.ReferenceCount++; m_j3dList.Add(existRef); return(model); }
public static J3D LoadModelFromVFS(VirtualFilesystemDirectory fs, string path = null, ushort?fileID = null) { if (path == null && fileID == null) { throw new ArgumentException("Must specify either file path or file ID when loading a model from an archive.", "path"); } TSharedRef <J3D> existRef = null;//m_j3dList.Find(x => string.Compare(x.FilePath, arc_and_file_path, StringComparison.InvariantCultureIgnoreCase) == 0); VirtualFilesystemFile file; if (fileID != null) { file = fs.FindByID((ushort)fileID); } else { file = fs.GetFileAtPath(path); } // This isn't actually a relative path, just the flat filename. // Shouldn't actually matter though, filenames in RARCs must be unique even in completely different foldes. string fileRelativePath = file.NameWithExtension; J3D model = new J3D(fileRelativePath); using (EndianBinaryReader reader = new EndianBinaryReader(file.Data, Endian.Big)) model.LoadFromStream(reader); existRef = new TSharedRef <J3D>(); existRef.FilePath = fs.Name + '/' + fileRelativePath; existRef.Asset = model; existRef.ReferenceCount++; m_j3dList.Add(existRef); model.Tick(1 / (float)60); return(model); }
private void LoadAssetFromFilepath(string filePath, bool unloadExisting) { if (!File.Exists(filePath)) { Console.WriteLine("Cannot load: \"{0}\", not a file!", filePath); } string fileName = Path.GetFileNameWithoutExtension(filePath); string fileExtension = Path.GetExtension(filePath).ToLowerInvariant(); switch (fileExtension) { case ".bdl": case ".bmd": { if (unloadExisting) { foreach (var model in m_loadedModels) { model.Dispose(); } m_loadedModels.Clear(); m_sceneGraphs.Clear(); } var newModel = new J3D(fileName); using (EndianBinaryReader reader = new EndianBinaryReader(new FileStream(filePath, FileMode.Open, FileAccess.Read), Endian.Big)) newModel.LoadFromStream(reader, true, true); newModel.SetHardwareLight(0, m_mainLight); newModel.SetHardwareLight(1, m_secondaryLight); // Apply patches for Wind Waker by default, since they don't seem to break anything else. newModel.SetTextureOverride("ZBtoonEX", "resources/textures/ZBtoonEX.png"); newModel.SetTextureOverride("ZAtoon", "resources/textures/ZAtoon.png"); newModel.SetColorWriteOverride("eyeLdamA", false); newModel.SetColorWriteOverride("eyeLdamB", false); newModel.SetColorWriteOverride("mayuLdamA", false); newModel.SetColorWriteOverride("mayuLdamB", false); newModel.SetColorWriteOverride("eyeRdamA", false); newModel.SetColorWriteOverride("eyeRdamB", false); newModel.SetColorWriteOverride("mayuRdamA", false); newModel.SetColorWriteOverride("mayuRdamB", false); m_loadedModels.Add(newModel); m_sceneGraphs.Add(new SceneGraphViewModel(newModel, newModel.INF1Tag.HierarchyRoot, "")); } break; case ".bck": { if (MainModel != null) { if (unloadExisting) { MainModel.UnloadBoneAnimations(); } MainModel.LoadBoneAnimation(filePath); // Automatically play the latest animation loaded. MainModel.SetBoneAnimation(fileName); } } break; case ".btk": { if (MainModel != null) { if (unloadExisting) { MainModel.UnloadMaterialAnimations(); } MainModel.LoadMaterialAnim(filePath); // Automatically play the latest animation loaded. MainModel.SetMaterialAnimation(fileName); } } break; case ".brk": { if (MainModel != null) { if (unloadExisting) { MainModel.UnloadRegisterAnimations(); } MainModel.LoadRegisterAnim(filePath); MainModel.SetRegisterAnimation(fileName); } } break; case ".bmt": { if (MainModel != null) { if (unloadExisting) { MainModel.UnloadExternalMaterials(); } MainModel.LoadExternalMaterial(filePath); // Automatically set the latest external material loaded. MainModel.SetExternalMaterial(fileName); } } break; } // m_loadedModels.Sort((x,y) => x.Name.CompareTo(y.Name)); if (PropertyChanged != null) { PropertyChanged.Invoke(this, new PropertyChangedEventArgs("MainModel")); PropertyChanged.Invoke(this, new PropertyChangedEventArgs("MainScenegraph")); PropertyChanged.Invoke(this, new PropertyChangedEventArgs("HasLoadedModel")); } }
private static J3D LoadModelFromResource(WActorResource.ModelResource res, string archive) { J3D j3d = null; if (string.IsNullOrEmpty(res.Path) || string.IsNullOrEmpty(archive)) { return(null); } string archivePath = Path.Combine(WSettingsManager.GetSettings().RootDirectoryPath, "files", "res/Object/", archive + ".arc"); if (!File.Exists(archivePath)) { return(null); } VirtualFilesystemDirectory model_arc = ArchiveUtilities.LoadArchive(archivePath); VirtualFilesystemFile archiveFile = model_arc.GetFileAtPath(res.Path); if (archiveFile == null) { Console.WriteLine("LoadActorByName failed because the specified path \"{0}\" does not exist in archive \"{1}\"!", res.Path, archive); return(null); } byte[] j3dData = archiveFile.Data; j3d = new J3D(archiveFile.Name); using (EndianBinaryReader reader = new EndianBinaryReader(j3dData, Endian.Big)) j3d.LoadFromStream(reader, WSettingsManager.GetSettings().DumpTextures, WSettingsManager.GetSettings().DumpShaders); if (res.Position != null) { j3d.SetOffsetTranslation((Vector3)res.Position); } if (res.Rotation != null) { j3d.SetOffsetRotation((Vector3)res.Rotation); } if (res.Scale != null) { j3d.SetOffsetScale((Vector3)res.Scale); } j3d.SetHardwareLight(0, m_mainLight); j3d.SetHardwareLight(1, m_secondaryLight); j3d.SetTextureOverride("ZBtoonEX", "resources/textures/ZBtoonEX.png"); j3d.SetTextureOverride("ZAtoon", "resources/textures/ZAtoon.png"); if (res.Animations == null) { res.Animations = new WActorResource.AnimationResource[0]; } foreach (var anim in res.Animations) { VirtualFilesystemDirectory anim_arc = model_arc; if (!string.IsNullOrEmpty(anim.ArchiveName)) { string anim_arc_path = Path.Combine(WSettingsManager.GetSettings().RootDirectoryPath, "files", "res/Object/", anim.ArchiveName + ".arc"); if (!File.Exists(anim_arc_path)) { return(null); } anim_arc = ArchiveUtilities.LoadArchive(anim_arc_path); } VirtualFilesystemFile anim_file = anim_arc.GetFileAtPath(anim.Path); if (anim_file == null) { continue; } byte[] anim_data = anim_file.Data; // Decompress the file if necessary if (anim_data[0] == 'Y') { MemoryStream decompressed_data = null; using (EndianBinaryReader decompressor = new EndianBinaryReader(anim_data, Endian.Big)) { decompressed_data = Yaz0.Decode(decompressor); } anim_data = decompressed_data.ToArray(); } switch (anim.Type) { case "bck": BCK loaded_bck = new BCK(anim_file.Name); using (EndianBinaryReader reader = new EndianBinaryReader(anim_data, Endian.Big)) loaded_bck.LoadFromStream(reader); j3d.BoneAnimations.Add(loaded_bck); j3d.SetBoneAnimation(anim_file.Name); loaded_bck.Tick(anim.StartTime); if (anim.PausedOnLoad) { loaded_bck.Pause(); } break; case "btk": BTK loaded_btk = new BTK(anim_file.Name); using (EndianBinaryReader reader = new EndianBinaryReader(anim_data, Endian.Big)) loaded_btk.LoadFromStream(reader); j3d.MaterialAnimations.Add(loaded_btk); j3d.SetMaterialAnimation(anim_file.Name); loaded_btk.Tick(anim.StartTime); if (anim.PausedOnLoad) { loaded_btk.Pause(); } break; case "brk": BRK loaded_brk = new BRK(anim_file.Name); using (EndianBinaryReader reader = new EndianBinaryReader(anim_data, Endian.Big)) loaded_brk.LoadFromStream(reader); j3d.RegisterAnimations.Add(loaded_brk); j3d.SetRegisterAnimation(anim_file.Name); loaded_brk.Tick(anim.StartTime); if (anim.PausedOnLoad) { loaded_brk.Pause(); } break; case "bmt": BMT loaded_bmt = new BMT(anim_file.Name); using (EndianBinaryReader reader = new EndianBinaryReader(anim_data, Endian.Big)) loaded_bmt.LoadFromStream(reader); j3d.ExternalMaterials.Add(loaded_bmt); j3d.SetExternalMaterial(anim_file.Name); if (loaded_bmt.MAT3 != null) { // a hack to get bmts working Material dummyMat = null; j3d.AssignVertexAttributesToMaterialsRecursive(j3d.INF1Tag.HierarchyRoot, ref dummyMat, loaded_bmt.MAT3); j3d.GenerateShadersForMaterials(loaded_bmt.MAT3); } break; default: break; } } j3d.Tick(1 / (float)60); if (res.ChildModels == null) { res.ChildModels = new WActorResource.ModelResource[0]; } foreach (var childRes in res.ChildModels) { var childJ3d = LoadModelFromResource(childRes, archive); j3d.AddChildModel(childJ3d, childRes.ParentJointName); } return(j3d); }
public static J3D LoadActorByName(string actorName) { // Stop to check if we've already loaded this model. var existRef = m_j3dList.Find(x => string.Compare(x.FilePath, actorName, StringComparison.InvariantCultureIgnoreCase) == 0); if (existRef != null) { existRef.ReferenceCount++; return(existRef.Asset); } if (actorName == null) { return(null); } // Check to see if we have an Actor Descriptor for this actor. if (!m_actorDescriptors.ContainsKey(actorName)) { return(null); } WActorDescriptor descriptor = m_actorDescriptors[actorName]; // Check to see that this actor descriptor specifies a model path. if (string.IsNullOrEmpty(descriptor.ModelPath) || string.IsNullOrEmpty(descriptor.ArchiveName)) { return(null); } string archivePath = Path.Combine(Properties.Settings.Default.RootDirectory, "res/Object/", descriptor.ArchiveName + ".arc"); // Check to see that the archive exists if (!File.Exists(archivePath)) { return(null); } // Finally, open the archive so we can look insdie of it to see if it exists. VirtualFilesystemDirectory archive = ArchiveUtilities.LoadArchive(archivePath); VirtualFilesystemFile archiveFile = archive.GetFileAtPath(descriptor.ModelPath); if (archiveFile == null) { Console.WriteLine("LoadActorByName failed because the specified path \"{0}\" does not exist in archive \"{1}\"!", descriptor.ModelPath, descriptor.ArchiveName); return(null); } // Now that we finally have the file, we can try to load a J3D from it. byte[] j3dData = archiveFile.Data; J3D j3d = new J3D(archiveFile.Name); using (EndianBinaryReader reader = new EndianBinaryReader(j3dData, Endian.Big)) j3d.LoadFromStream(reader, Properties.Settings.Default.DumpTexturesToDisk, Properties.Settings.Default.DumpShadersToDisk); j3d.SetHardwareLight(0, m_mainLight); j3d.SetHardwareLight(1, m_secondaryLight); // Apply patches for Wind Waker by default, since they don't seem to break anything else. j3d.SetTextureOverride("ZBtoonEX", "resources/textures/ZBtoonEX.png"); j3d.SetTextureOverride("ZAtoon", "resources/textures/ZAtoon.png"); j3d.SetColorWriteOverride("eyeLdamA", false); j3d.SetColorWriteOverride("eyeLdamB", false); j3d.SetColorWriteOverride("mayuLdamA", false); j3d.SetColorWriteOverride("mayuLdamB", false); j3d.SetColorWriteOverride("eyeRdamA", false); j3d.SetColorWriteOverride("eyeRdamB", false); j3d.SetColorWriteOverride("mayuRdamA", false); j3d.SetColorWriteOverride("mayuRdamB", false); existRef = new TSharedRef <J3D>(); existRef.FilePath = actorName; existRef.Asset = j3d; existRef.ReferenceCount++; m_j3dList.Add(existRef); return(j3d); }