/// <summary> /// Add a new Property /// </summary> /// <param name="prop">The propery to add</param> /// <param name="duplicate">true, if you want to allow two occurences of the same Property</param> /// <remarks>If duplicate is false, and the property already exists, it's value will be overwritten</remarks> public void Add(MaterialDefinitionProperty prop, bool duplicate) { if (!duplicate) { MaterialDefinitionProperty ex = null; foreach (MaterialDefinitionProperty mdp in properties) { if (mdp.Name.Trim().ToLower() == prop.Name.Trim().ToLower()) { ex = mdp; break; } } if (ex != null) { ex.Value = prop.Value; } else { this.properties = (MaterialDefinitionProperty[])Helper.Add(properties, prop); } } else { this.properties = (MaterialDefinitionProperty[])Helper.Add(properties, prop); } }
/// <summary> /// Sort the Properties in Alphabetic Order /// </summary> public void Sort() { for (int i = 0; i < this.properties.Length - 1; i++) { for (int j = i + 1; j < this.properties.Length; j++) { if (properties[i].Name.CompareTo(properties[j].Name) > 0) { MaterialDefinitionProperty dum = properties[i]; properties[i] = properties[j]; properties[j] = dum; } } } }
/// <summary> /// This adds all second Leve Textures to the Recolor (like normal Maps) /// </summary> /// <param name="newpkg"></param> /// <param name="md"></param> protected void AddReferencedTxtr(IPackageFile newpkg, GenericRcol txmt, MaterialDefinition md, string unique) { foreach (string k in txmt.ReferenceChains.Keys) { if (k.ToLower() == "stdmatnormalmaptexturename") //at the moment i only know of NormalMaps that need to be added { MaterialDefinitionProperty mdp = md.GetProperty(k); if (mdp != null) { string name = Hashes.StripHashFromName(mdp.Value).Trim(); if (!name.EndsWith("_txtr")) { name += "_txtr"; } //Console.Write("loading second txtr "+mdp.Name+" = "+mdp.Value); IPackageFile pkg = txmt.Package; SimPe.Interfaces.Files.IPackedFileDescriptor[] pfds = pkg.FindFile(name, Data.MetaData.TXTR); if (pfds.Length > 0) { SimPe.Interfaces.Files.IPackedFileDescriptor pfd = pfds[0]; //Console.Write(" [found in local Package]"); GenericRcol txtr = new GenericRcol(); txtr.ProcessData(pfd, pkg); AddTxtr(newpkg, txtr, unique, txmt, md); } /*else //we don't pull from the Filetable, as we expect, that all needed Files are already cloned! * { * SimPe.Interfaces.Scenegraph.IScenegraphFileIndexItem item = FileTable.FileIndex.FindFileByName(name, Data.MetaData.TXTR, Hashes.GetHashGroupFromName(mdp.Value, Data.MetaData.GLOBAL_GROUP), true); * if (item!=null) * { * Console.Write(" [found in FileTable]"); * } * }*/ //Console.WriteLine(); } } } }
/// <summary> /// Creates a Material Object form this Block /// </summary> /// <returns></returns> public Ambertation.Scenes.Material ToSceneMaterial(Ambertation.Scenes.Scene scn, string name) { MaterialDefinitionProperty p; Ambertation.Scenes.Material mat = scn.CreateMaterial(name); p = this.GetProperty("stdMatSpecCoef"); if (p != null) { mat.Specular = p.ToARGB(); } p = this.GetProperty("stdMatDiffCoef"); if (p != null) { mat.Diffuse = p.ToARGB(); } p = this.GetProperty("stdMatEmissiveCoef"); if (p != null) { mat.Emmissive = p.ToARGB(); } p = this.GetProperty("stdMatSpecPower"); if (p != null) { mat.SpecularPower = p.ToValue(); } p = this.GetProperty("stdMatAlphaBlendMode"); if (p != null) { if (p.Value == "blend") { MaterialDefinitionProperty p2 = this.GetProperty("stdMatLightingEnabled"); if (p2 != null) { if (p2.ToValue() == 0) { mat.Mode = Ambertation.Scenes.Material.TextureModes.ShadowTexture; } } } //if (mat.Texture.AlphaBlend) mat.Diffuse = System.Drawing.Color.FromArgb(0x10, mat.Diffuse); } return(mat); }
/// <summary> /// Unserializes a BinaryStream into the Attributes of this Instance /// </summary> /// <param name="reader">The Stream that contains the FileData</param> public override void Unserialize(System.IO.BinaryReader reader) { version = reader.ReadUInt32(); /*byte len = reader.ReadByte(); * fldsc = Helper.ToString(reader.ReadBytes(len));*/ fldsc = reader.ReadString(); uint myid = reader.ReadUInt32(); sgres.Unserialize(reader); sgres.BlockID = myid; /*len = reader.ReadByte(); * fldsc = Helper.ToString(reader.ReadBytes(len));*/ fldsc = reader.ReadString(); /*len = reader.ReadByte(); * mattype = Helper.ToString(reader.ReadBytes(len));*/ mattype = reader.ReadString(); properties = new MaterialDefinitionProperty[reader.ReadUInt32()]; for (int i = 0; i < properties.Length; i++) { properties[i] = new MaterialDefinitionProperty(); properties[i].Unserialize(reader); } if (version == 8) { listing = new String[0]; } else { listing = new String[reader.ReadUInt32()]; for (int i = 0; i < listing.Length; i++) { /*len = reader.ReadByte(); * listing[i] = Helper.ToString(reader.ReadBytes(len));*/ listing[i] = reader.ReadString(); } } }
/// <summary> /// Merges the Material Definition Properties - adds, overwrites or retains as appropriate /// </summary> /// <param name="filename">The name of the file to merge</param> public void MergeProperties(string filename) { System.Xml.XmlReaderSettings xrs = new System.Xml.XmlReaderSettings(); xrs.CloseInput = true; xrs.IgnoreComments = true; xrs.IgnoreProcessingInstructions = true; xrs.IgnoreWhitespace = true; System.Xml.XmlReader xr = System.Xml.XmlReader.Create(filename, xrs); try { xr.ReadStartElement("materialDefinition"); while (xr.IsStartElement()) { if (xr.Name != "materialDefinitionProperty") { xr.Skip(); continue; } MaterialDefinitionProperty p = new MaterialDefinitionProperty(); while (xr.MoveToNextAttribute()) { if (xr.Name == "name") { p.Name = xr.Value; } } xr.MoveToElement(); p.Value = xr.ReadString(); this.Add(p, false); xr.ReadEndElement(); } xr.ReadEndElement(); } finally { xr.Close(); xr = null; } }
/// <summary> /// Updates the SkinTone References in the 3IDR Files /// </summary> /// <param name="md">The Metreial Definition</param> /// <param name="eyecolor">true, if you want to alter the eyecolor</param> /// <param name="makeups">true, if you want to alter the makeup</param> void UpdateMakeup(MaterialDefinition md, bool eyecolor, bool makeups) { string age = md.FindProperty("paramAge").Value; string gender = md.FindProperty("paramGender").Value; //find a matching Package in the arechtype Interfaces.Files.IPackedFileDescriptor[] pfds = this.archetype.FindFiles(Data.MetaData.TXMT); SimPe.Plugin.Rcol atxmt = new GenericRcol(null, false); MaterialDefinition amd = null; foreach (Interfaces.Files.IPackedFileDescriptor pfd in pfds) { atxmt.ProcessData(pfd, this.archetype); amd = (MaterialDefinition)atxmt.Blocks[0]; if ((amd.FindProperty("paramAge").Value == age) && (amd.FindProperty("paramGender").Value == gender)) { break; } } if (amd != null) { int count = 0; md.Add(amd.FindProperty("numTexturesToComposite")); try{ count = Convert.ToInt32(md.FindProperty("numTexturesToComposite").Value); } catch {} string txmtname = ""; for (int i = 0; i < count; i++) { MaterialDefinitionProperty val = amd.FindProperty("baseTexture" + i.ToString()); if (i != 0) { md.Add(val); } if (i == 1) { if (eyecolor) { md.Add(val); } else if (makeups) { md.Add(val); } } string name = val.Value.Trim(); if (!name.ToLower().EndsWith("_txtr")) { name += "_txtr"; } name = this.FindTxtrName(name); if (i != 0) { txmtname += "_"; } txmtname += name; } md.FindProperty("compositeBaseTextureName").Value = txmtname; string[] list = new string[1]; list[0] = txmtname; md.Listing = list; if (makeups) { count = 0; md.Add(amd.FindProperty("cafNumOverlays")); try{ count = Convert.ToInt32(md.FindProperty("cafNumOverlays").Value); } catch {} for (int i = 0; i < count; i++) { MaterialDefinitionProperty val = amd.FindProperty("cafOverlay" + i.ToString()); md.Add(val); } } } }
/// <summary> /// Add a new Property /// </summary> /// <param name="prop">The propery to add</param> /// <remarks>If the property already exists, it's value will be overwritten</remarks> public void Add(MaterialDefinitionProperty prop) { Add(prop, false); }