/// <summary> /// Updates or Adds the Model to the viewport /// </summary> /// <param name="model">The TexTools Model</param> /// <param name="textureDataDictionary">The textures associated with the model</param> /// <param name="item">The item for the model</param> /// <param name="modelRace">The race of the model</param> /// <param name="targetRace">The target race the model should be</param> public void UpdateModel(TTModel model, Dictionary <int, ModelTextureData> textureDataDictionary, IItemModel item, XivRace modelRace, XivRace targetRace) { _targetRace = targetRace; var itemType = $"{item.PrimaryCategory}_{item.SecondaryCategory}"; // If target race is different than the model race Apply racial deforms if (modelRace != targetRace) { ApplyDeformers(model, itemType, modelRace, targetRace); } SharpDX.BoundingBox?boundingBox = null; ModelModifiers.CalculateTangents(model); // Remove any existing models of the same item type RemoveModel(itemType); var totalMeshCount = model.MeshGroups.Count; for (var i = 0; i < totalMeshCount; i++) { var meshGeometry3D = GetMeshGeometry(model, i); var textureData = textureDataDictionary[model.GetMaterialIndex(i)]; Stream diffuse = null, specular = null, normal = null, alpha = null, emissive = null; if (textureData.Diffuse != null && textureData.Diffuse.Length > 0) { using (var img = Image.LoadPixelData <Rgba32>(textureData.Diffuse, textureData.Width, textureData.Height)) { diffuse = new MemoryStream(); img.Save(diffuse, new PngEncoder()); } streamList.Add(diffuse); } if (textureData.Specular != null && textureData.Specular.Length > 0) { using (var img = Image.LoadPixelData <Rgba32>(textureData.Specular, textureData.Width, textureData.Height)) { specular = new MemoryStream(); img.Save(specular, new PngEncoder()); } streamList.Add(specular); } if (textureData.Normal != null && textureData.Normal.Length > 0) { using (var img = Image.LoadPixelData <Rgba32>(textureData.Normal, textureData.Width, textureData.Height)) { normal = new MemoryStream(); img.Save(normal, new PngEncoder()); } streamList.Add(normal); } if (textureData.Alpha != null && textureData.Alpha.Length > 0) { using (var img = Image.LoadPixelData <Rgba32>(textureData.Alpha, textureData.Width, textureData.Height)) { alpha = new MemoryStream(); img.Save(alpha, new PngEncoder()); } streamList.Add(alpha); } if (textureData.Emissive != null && textureData.Emissive.Length > 0) { using (var img = Image.LoadPixelData <Rgba32>(textureData.Emissive, textureData.Width, textureData.Height)) { emissive = new MemoryStream(); img.Save(emissive, new PngEncoder()); } streamList.Add(emissive); } var material = new PhongMaterial { DiffuseColor = PhongMaterials.ToColor(1, 1, 1, 1), SpecularShininess = 1f, DiffuseMap = diffuse, DiffuseAlphaMap = alpha, SpecularColorMap = specular, NormalMap = normal, EmissiveMap = emissive }; // Geometry that contains skeleton data var smgm3d = new CustomBoneSkinMeshGeometry3D { Geometry = meshGeometry3D, Material = material, ItemType = itemType, BoneMatrices = GetMatrices(targetRace), BoneList = model.Bones }; // Keep track of what bones are showing in the view foreach (var modelBone in model.Bones) { if (!shownBonesList.Contains(modelBone)) { shownBonesList.Add(modelBone); } } boundingBox = meshGeometry3D.Bound; smgm3d.CullMode = Properties.Settings.Default.Cull_Mode.Equals("None") ? CullMode.None : CullMode.Back; Models.Add(smgm3d); } SpecularShine = 1; var center = boundingBox.GetValueOrDefault().Center; _lightX = center.X; _lightY = center.Y; _lightZ = center.Z; Light3Direction = new Vector3D(_lightX, _lightY, _lightZ); Camera.UpDirection = new Vector3D(0, 1, 0); Camera.CameraInternal.PropertyChanged += CameraInternal_PropertyChanged; // Add the skeleton node for the target race AddSkeletonNode(targetRace); // Keep track of the models displayed in the viewport shownModels.Add(itemType, new DisplayedModelData { TtModel = model, ItemModel = item, ModelTextureData = textureDataDictionary }); }
/// <summary> /// Updates the model in the 3D viewport /// </summary> /// <param name="mdlData">The model data</param> /// <param name="textureDataDictionary">The texture dictionary for the model</param> public void UpdateModel(TTModel model, Dictionary <int, ModelTextureData> textureDataDictionary) { SharpDX.BoundingBox?boundingBox = null; ModelModifiers.CalculateTangents(model); var totalMeshCount = model.MeshGroups.Count; for (var i = 0; i < totalMeshCount; i++) { var meshGeometry3D = GetMeshGeometry(model, i); var textureData = textureDataDictionary[model.GetMaterialIndex(i)]; Stream diffuse = null, specular = null, normal = null, alpha = null, emissive = null; if (textureData.Diffuse != null && textureData.Diffuse.Length > 0) { using (var img = Image.LoadPixelData <Rgba32>(textureData.Diffuse, textureData.Width, textureData.Height)) { diffuse = new MemoryStream(); img.Save(diffuse, new PngEncoder()); } streamList.Add(diffuse); } if (textureData.Specular != null && textureData.Specular.Length > 0) { using (var img = Image.LoadPixelData <Rgba32>(textureData.Specular, textureData.Width, textureData.Height)) { specular = new MemoryStream(); img.Save(specular, new PngEncoder()); } streamList.Add(specular); } if (textureData.Normal != null && textureData.Normal.Length > 0) { using (var img = Image.LoadPixelData <Rgba32>(textureData.Normal, textureData.Width, textureData.Height)) { normal = new MemoryStream(); img.Save(normal, new PngEncoder()); } streamList.Add(normal); } if (textureData.Alpha != null && textureData.Alpha.Length > 0) { using (var img = Image.LoadPixelData <Rgba32>(textureData.Alpha, textureData.Width, textureData.Height)) { alpha = new MemoryStream(); img.Save(alpha, new PngEncoder()); } streamList.Add(alpha); } if (textureData.Emissive != null && textureData.Emissive.Length > 0) { using (var img = Image.LoadPixelData <Rgba32>(textureData.Emissive, textureData.Width, textureData.Height)) { emissive = new MemoryStream(); img.Save(emissive, new PngEncoder()); } streamList.Add(emissive); } var material = new PhongMaterial { DiffuseColor = PhongMaterials.ToColor(1, 1, 1, 1), SpecularShininess = 1f, DiffuseMap = diffuse, DiffuseAlphaMap = alpha, SpecularColorMap = specular, NormalMap = normal, EmissiveMap = emissive }; var mgm3d = new CustomMeshGeometryModel3D { Geometry = meshGeometry3D, Material = material //, //IsBody = mdlData.LoDList[0].MeshDataList[i].IsBody }; boundingBox = meshGeometry3D.Bound; mgm3d.CullMode = Properties.Settings.Default.Cull_Mode.Equals("None") ? CullMode.None : CullMode.Back; Models.Add(mgm3d); } SpecularShine = 1; var center = boundingBox.GetValueOrDefault().Center; _lightX = center.X; _lightY = center.Y; _lightZ = center.Z; Light3Direction = new Vector3D(_lightX, _lightY, _lightZ); Camera.UpDirection = new Vector3D(0, 1, 0); Camera.CameraInternal.PropertyChanged += CameraInternal_PropertyChanged; }