static void Main(string[] args) { if (args.Length == 0) { Console.WriteLine("LDDCollada by benji for kaltathenoblemind\n"); Console.WriteLine("When calling LDDCollada, pass in filenames of .lxfml files to convert.\n"); Console.WriteLine("If the directory that contains the extracted contents of db.lif is not stored in the LDDCollada.ini file, you will be prompted to browse to it."); return; } Config = new INIFile(Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location), INI_FILENAME)); // Make sure we have the folder to the part DB string partDBPath = Config.GetValueOrDefault("LDDCollada", "PartDBPath", null); if (partDBPath == null) { FolderBrowserDialog browser = new FolderBrowserDialog(); if (browser.ShowDialog() == DialogResult.OK) { partDBPath = browser.SelectedPath; Config.SetValue("LDDCollada", "PartDBPath", partDBPath); } else { return; } } LDDDB.Load(partDBPath, true, true, true, true); foreach (string filename in args) { if (filename.EndsWith(".lxfml", StringComparison.InvariantCultureIgnoreCase)) { DAEConversion.ConvertLXFML(filename); } else if (filename.EndsWith(".lxf", StringComparison.InvariantCultureIgnoreCase)) { DAEConversion.ConvertLXF(filename); } else { Console.WriteLine("[WARNING]: Filename does not end in .lxf or .lxfml: '" + filename + "'!"); } } Config.Write(Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location), INI_FILENAME)); #if DEBUG Console.ReadKey(); #endif }
private void CreateDecoration(int decorationID, int matID, string outputDirectory) { string name = decorationID + "_on_" + matID; XNamespace ns = COLLADA_NAMESPACE; Material material = LDDDB.GetMaterial(matID); string dbFilename = Path.Combine(LDDDB.PartDBPath, "Decorations", decorationID + ".png"); string destFilename; if (LDDDB.CopyTextures) { if (LDDDB.ColorizeTextures) { destFilename = Path.GetFullPath(Path.Combine(outputDirectory, name + ".png")); ApplyBackgroundColor(dbFilename, destFilename, material.Red, material.Green, material.Blue, material.Alpha); } else { destFilename = Path.Combine(outputDirectory, decorationID + ".png"); File.Copy(dbFilename, destFilename); } } else { destFilename = dbFilename; } ImageLibrary.Add(new XElement(ns + "image", new XAttribute("id", "deco-" + name + "-TEX"), new XAttribute("name", "deco_" + name), new XElement(ns + "init_from", "file://" + destFilename.Replace('\\', '/')))); MaterialLibrary.Add(new XElement(ns + "material", new XAttribute("id", "mat_deco" + name), new XAttribute("name", "Mat_Deco" + name), new XElement(ns + "instance_effect", new XAttribute("url", "#mat_deco" + name + "-FX")))); EffectLibrary.Add(new XElement(ns + "effect", new XAttribute("id", "mat_deco" + name + "-FX"), new XAttribute("name", "Mat_Deco" + name), new XElement(ns + "profile_COMMON", new XElement(ns + "technique", new XAttribute("sid", "standard"), new XElement(ns + "phong", new XElement(ns + "emission", new XElement(ns + "color", new XAttribute("sid", "emission"), "0.0 0.0 0.0 1.0")), new XElement(ns + "ambient", new XElement(ns + "color", new XAttribute("sid", "ambient"), "0 0 0 1.0")), new XElement(ns + "diffuse", new XElement(ns + "texture", new XAttribute("texture", "deco-" + name + "-TEX"), new XAttribute("texcoord", "CHANNEL0")/*, new XElement(ns + "extra", new XElement(ns + "techique", new XAttribute("profile", "MAYA"), new XElement(ns + "wrapU", new XAttribute("sid", "wrapU0"), "FALSE"), new XElement(ns + "wrapV", new XAttribute("sid", "wrapV0"), "FALSE")))*/)), new XElement(ns + "specular", new XElement(ns + "color", new XAttribute("sid", "specular"), "0.5 0.5 0.5 1.0")), new XElement(ns + "shininess", new XElement(ns + "float", new XAttribute("sid", "shininess"), "0.3"))/*, new XElement(ns + "transparent", new XAttribute("opaque", "RGB_ZERO"), new XElement(ns + "texture", new XAttribute("texture", "deco-" + name + "-TEX"), new XAttribute("texcoord", "CHANNEL0")))*/))))); }
private void CreateMaterial(int matID) { XNamespace ns = COLLADA_NAMESPACE; Material material = LDDDB.GetMaterial(matID); MaterialLibrary.Add(new XElement(ns + "material", new XAttribute("id", "mat" + matID), new XAttribute("name", "Mat" + matID), new XElement(ns + "instance_effect", new XAttribute("url", "#mat" + matID + "-FX")))); EffectLibrary.Add(new XElement(ns + "effect", new XAttribute("id", "mat" + matID + "-FX"), new XAttribute("name", "Mat" + matID), new XElement(ns + "profile_COMMON", new XElement(ns + "technique", new XAttribute("sid", "standard"), new XElement(ns + "phong", new XElement(ns + "emission", new XElement(ns + "color", new XAttribute("sid", "emission"), "0.0 0.0 0.0 1.0")), new XElement(ns + "ambient", new XElement(ns + "color", new XAttribute("sid", "ambient"), "0 0 0 1.0")), new XElement(ns + "diffuse", new XElement(ns + "color", new XAttribute("sid", "diffuse"), UnSRGB(material.Red / 255.0f) + " " + UnSRGB(material.Green / 255.0f) + " " + UnSRGB(material.Blue / 255.0f) + " 1.0")), new XElement(ns + "specular", new XElement(ns + "color", new XAttribute("sid", "specular"), "0.5 0.5 0.5 1.0")), new XElement(ns + "shininess", new XElement(ns + "float", new XAttribute("sid", "shininess"), "0.3")), new XElement(ns + "transparency", new XElement(ns + "float", new XAttribute("sid", "transparency"), material.Alpha / 255.0f))))))); }
public void PlaceFile(XDocument doc, Matrix transform, XElement parent, string textureDirectory) { XElement lxfml = doc.Element("LXFML"); if (Int32.Parse(lxfml.Attribute("versionMajor").Value) < 5) { Console.WriteLine(" [ERROR]: LXFML file must be at least version 5.0"); } string name = "file"; XElement designNameAnnotation = lxfml.Element("Meta").Elements("Annotation").FirstOrDefault(element => element.Attribute("designname") != null); if (designNameAnnotation != null) // For builtin assemblies name = designNameAnnotation.Attribute("designname").Value; else if (lxfml.Attribute("name") != null) // For user files name = lxfml.Attribute("name").Value; name = name.Replace(' ', '_'); string nodeID = "part" + AcquireNodeID(); XElement fileElement = new XElement((XNamespace)COLLADA_NAMESPACE + "node", new XAttribute("id", nodeID), new XAttribute("sid", nodeID), new XAttribute("name", name)); foreach (XElement brick in lxfml.Element("Bricks").Elements("Brick")) { foreach (XElement part in brick.Elements("Part")) { XElement bone = part.Element("Bone"); Matrix partTransform = LDDDB.DecodeLDDTransform(bone.Attribute("transformation").Value); string[] materials = part.Attribute("materials").Value.Split(','); string[] decorations = new string[0]; if (part.Attribute("decoration") != null) decorations = part.Attribute("decoration").Value.Split(','); PlaceInstance(part.Attribute("designID").Value, partTransform, fileElement, materials.Select(str => Int32.Parse(str)), decorations.Select(str => Int32.Parse(str)), textureDirectory); } } parent.Add(fileElement); }
public void PlaceInstance(string designID, Matrix transform, XElement parent, IEnumerable<int> materials, IEnumerable<int> decorations, string textureDirectory) { if (LDDDB.HasPrimitive(designID)) { Console.WriteLine(" Primitive '" + designID + "'..."); string name = designID + "_" + String.Join("_", materials) + "_" + String.Join("_", decorations); if (!CreatedPrimitives.Contains(name)) { GeometryLibrary.Add(GFileToGeometry(LDDDB.GetPrimitive(designID), name)); CreatedPrimitives.Add(name); } // Prepare material binds int slot = 0; XElement technique = new XElement((XNamespace)COLLADA_NAMESPACE + "technique_common"); int? firstMaterialID = null; foreach (int materialID in materials) { if (materialID == 0) { int decorationIndex = decorations.ElementAt(slot - 1); if (decorationIndex == 0) { if (firstMaterialID.HasValue) { technique.Add(new XElement((XNamespace)COLLADA_NAMESPACE + "instance_material", new XAttribute("symbol", "slot" + slot), new XAttribute("target", "#mat" + firstMaterialID.Value))); } } else if (firstMaterialID.HasValue) { // Use decorationIndex to find the decoration material if (!CreatedDecorations.Contains(decorationIndex + "_on_" + firstMaterialID.Value)) { CreateDecoration(decorationIndex, firstMaterialID.Value, textureDirectory); CreatedDecorations.Add(decorationIndex + "_on_" + firstMaterialID.Value); } technique.Add(new XElement((XNamespace)COLLADA_NAMESPACE + "instance_material", new XAttribute("symbol", "slot" + slot), new XAttribute("target", "#mat_deco" + decorationIndex + "_on_" + firstMaterialID.Value))); } else { } } else { firstMaterialID = materialID; if (!CreatedMaterials.Contains(materialID)) { CreateMaterial(materialID); CreatedMaterials.Add(materialID); } technique.Add(new XElement((XNamespace)COLLADA_NAMESPACE + "instance_material", new XAttribute("symbol", "slot" + slot), new XAttribute("target", "#mat" + materialID))); } slot++; } string nodeID = "part" + AcquireNodeID() + "_" + designID; XElement nodeElement = new XElement((XNamespace)COLLADA_NAMESPACE + "node", new XAttribute("id", nodeID), new XAttribute("sid", nodeID), new XAttribute("name", nodeID), WriteMatrix(transform), new XElement((XNamespace)COLLADA_NAMESPACE + "instance_geometry", new XAttribute("url", "#" + name + "-GEO"), new XElement((XNamespace)COLLADA_NAMESPACE + "bind_material", technique))); parent.Add(nodeElement); if (decorations.Count() > 0 && LDDDB.DuplicateDecalBricks) { // Do it again, but without decorations // Prepare material binds name = name + "_blank"; if (!CreatedPrimitives.Contains(name)) { GeometryLibrary.Add(GFileToGeometry(LDDDB.GetPrimitive(designID), name)); CreatedPrimitives.Add(name); } slot = 0; technique = new XElement((XNamespace)COLLADA_NAMESPACE + "technique_common"); firstMaterialID = null; foreach (int materialID in materials) { if (materialID == 0) { if (firstMaterialID.HasValue) { technique.Add(new XElement((XNamespace)COLLADA_NAMESPACE + "instance_material", new XAttribute("symbol", "slot" + slot), new XAttribute("target", "#mat" + firstMaterialID.Value))); } } else { firstMaterialID = materialID; if (!CreatedMaterials.Contains(materialID)) { CreateMaterial(materialID); CreatedMaterials.Add(materialID); } technique.Add(new XElement((XNamespace)COLLADA_NAMESPACE + "instance_material", new XAttribute("symbol", "slot" + slot), new XAttribute("target", "#mat" + materialID))); } slot++; } nodeID = nodeID + "_blank"; nodeElement = new XElement((XNamespace)COLLADA_NAMESPACE + "node", new XAttribute("id", nodeID), new XAttribute("sid", nodeID), new XAttribute("name", nodeID), WriteMatrix(transform), new XElement((XNamespace)COLLADA_NAMESPACE + "instance_geometry", new XAttribute("url", "#" + name + "-GEO"), new XElement((XNamespace)COLLADA_NAMESPACE + "bind_material", technique))); parent.Add(nodeElement); } } else if (LDDDB.HasAssembly(designID)) { Console.WriteLine(" Assembly '" + designID + "'..."); PlaceFile(LDDDB.GetAssembly(designID), transform, parent, textureDirectory); } else { Console.WriteLine(" [WARNING]: Design ID '" + designID + "' is not a primitive or an assembly in this brick set!"); } }