From 19565e7d740b5bfb289bef5704c82a7d888d0e67 Mon Sep 17 00:00:00 2001 From: dexy Date: Sat, 8 Jan 2022 19:37:47 +1100 Subject: [PATCH] Fixed embedded textures replacement from ModelForm --- CodeWalker/Forms/ModelForm.cs | 114 +++++++++++++++++++++++++++++++++- CodeWalker/Forms/YtdForm.cs | 5 +- 2 files changed, 115 insertions(+), 4 deletions(-) diff --git a/CodeWalker/Forms/ModelForm.cs b/CodeWalker/Forms/ModelForm.cs index ecb0364..091b6b3 100644 --- a/CodeWalker/Forms/ModelForm.cs +++ b/CodeWalker/Forms/ModelForm.cs @@ -1349,6 +1349,115 @@ namespace CodeWalker.Forms } + + private void UpdateEmbeddedTextures(DrawableBase dwbl) + { + if (dwbl == null) return; + + var sg = dwbl.ShaderGroup; + var td = sg?.TextureDictionary; + var sd = sg?.Shaders?.data_items; + + if (td == null) return; + if (sd == null) return; + + var updated = false; + foreach (var s in sd) + { + if (s?.ParametersList == null) continue; + foreach (var p in s.ParametersList.Parameters) + { + if (p.Data is TextureBase tex) + { + var tex2 = td.Lookup(tex.NameHash); + if ((tex2 != null) && (tex != tex2)) + { + p.Data = tex2;//swap the parameter out for the new embedded texture + updated = true; + } + } + } + } + + if (!updated) return; + + foreach (var model in dwbl.AllModels) + { + if (model?.Geometries == null) continue; + foreach (var geom in model.Geometries) + { + geom.UpdateRenderableParameters = true; + } + } + } + public void UpdateEmbeddedTextures() + { + if (Ydr != null) + { + if (Ydr.Loaded) + { + UpdateEmbeddedTextures(Ydr.Drawable); + } + } + else if (Ydd != null) + { + if (Ydd.Loaded) + { + foreach (var kvp in Ydd.Dict) + { + UpdateEmbeddedTextures(kvp.Value); + } + } + } + else if (Ypt != null) + { + if ((Ypt.Loaded) && (Ypt.DrawableDict != null)) + { + foreach (var kvp in Ypt.DrawableDict) + { + UpdateEmbeddedTextures(kvp.Value); + } + } + } + else if (Yft != null) + { + if (Yft.Loaded) + { + if (Yft.Fragment != null) + { + var f = Yft.Fragment; + + UpdateEmbeddedTextures(f.Drawable); + UpdateEmbeddedTextures(f.DrawableCloth); + + if (f.DrawableArray?.data_items != null) + { + foreach (var d in f.DrawableArray.data_items) + { + UpdateEmbeddedTextures(d); + } + } + + var c = f.PhysicsLODGroup?.PhysicsLOD1?.Children?.data_items; + if (c != null) + { + foreach (var child in c) + { + if (child != null) + { + UpdateEmbeddedTextures(child.Drawable1); + UpdateEmbeddedTextures(child.Drawable2); + } + } + } + + } + } + } + } + + + public void OnLightFormClosed() { lightForm = null; @@ -2077,10 +2186,9 @@ namespace CodeWalker.Forms if (td != null) { - YtdForm f = new YtdForm(); - f.Show(); + YtdForm f = new YtdForm(null, this); + f.Show(this); f.LoadTexDict(td, fileName); - //f.LoadYtd(ytd); } else { diff --git a/CodeWalker/Forms/YtdForm.cs b/CodeWalker/Forms/YtdForm.cs index 17c7cc4..e064dea 100644 --- a/CodeWalker/Forms/YtdForm.cs +++ b/CodeWalker/Forms/YtdForm.cs @@ -250,6 +250,9 @@ namespace CodeWalker.Forms tex.Name = CurrentTexture.Name; tex.NameHash = CurrentTexture.NameHash; + tex.Usage = CurrentTexture.Usage; + tex.UsageFlags = CurrentTexture.UsageFlags; + tex.Unknown_32h = CurrentTexture.Unknown_32h; var textures = new List(); foreach (var t in TexDict.Textures.data_items) @@ -337,7 +340,7 @@ namespace CodeWalker.Forms { if (ModelForm == null) return; - //TODO: live update of model form view when changes have been made... + ModelForm.UpdateEmbeddedTextures(); } private void UpdateFormTitle()