Пример #1
0
        private static ToolResult CopyUsedTexturesObj(string objFile, string outputDirectory, string textureDirectory)
        {
            bool    success = false;
            string  message = "";
            ObjData objData = ObjParser.TryParseObj(objFile);

            if (objData != null)
            {
                objData.UpdateMtlData();
                MtlData mtlData = objData.Mtl;

                if (mtlData != null)
                {
                    success = ObjModifier.CopyUsedTextures(mtlData, outputDirectory, textureDirectory);
                    message = MessageBoxConstants.GetMessageExecutionGeneric(success);
                }
                else
                {
                    message = MessageBoxConstants.MessageErrorCopyTextures + MessageBoxConstants.MessageMtlNotFound;
                }
            }
            else
            {
                message = MessageBoxConstants.GetMessageExecutionErrorParse(objFile);
            }
            return(new ToolResult(message, success));
        }
Пример #2
0
        private static ToolResult MergeObjFiles(List <string> fileList, string objOutput)
        {
            bool          success = false;
            StringBuilder sb      = new StringBuilder();

            if (fileList != null)
            {
                List <ObjData> objDatas = ObjParser.ParseObjs(fileList);
                ObjData        objData  = ObjModifier.MergeObjFiles(objDatas);
                if (objData != null)
                {
                    success = ObjExporter.WriteObj(objData, objData.Mtl, objOutput, makeMtl: true, useExistingMtl: true);
                    sb.AppendLine($"Merged {objDatas.Count} obj files into one");
                    sb.Append(MessageBoxConstants.GetMessageExecutionCreation(success, objOutput));
                }
                else
                {
                    sb.Append(MessageBoxConstants.MessageErrorMergeObj + MessageBoxConstants.MessageErrorExecution);
                }
            }
            else
            {
                sb.Append(MessageBoxConstants.MessageErrorMergeObj + MessageBoxConstants.MessageNoFilesMerge);
            }
            return(new ToolResult(sb.ToString(), success));
        }
Пример #3
0
        private static ToolResult DeleteUnusedMaterialsObj(string objFile, string objOutput)
        {
            bool    success = false;
            string  message = "";
            ObjData objData = ObjParser.TryParseObj(objFile);

            if (objData != null)
            {
                objData.UpdateMtlData();
                MtlData mtlData = objData.Mtl;
                if (mtlData != null)
                {
                    ObjModifier.DeleteUnusedMaterials(objData, mtlData);
                    success = ObjExporter.WriteObj(objData, mtlData, objOutput, makeMtl: true, useExistingMtl: true);
                    message = MessageBoxConstants.GetMessageExecutionCreation(success, objOutput);
                }
                else
                {
                    message = MessageBoxConstants.MessageErrorDeleteUnusedMaterials +
                              MessageBoxConstants.MessageMtlNotFound;
                }
            }
            else
            {
                message = MessageBoxConstants.GetMessageExecutionErrorParse(objFile);
            }
            return(new ToolResult(message, success));
        }
Пример #4
0
        private static ToolResult DeleteMaterialsObj(string objFile, string objOutput)
        {
            bool    success = false;
            string  message = "";
            ObjData objData = ObjParser.TryParseObj(objFile);

            if (objData != null)
            {
                ObjModifier.DeleteMaterials(objData);
                success = ObjExporter.WriteObj(objData, null, objOutput, makeMtl: false, useExistingMtl: false);
                message = MessageBoxConstants.GetMessageExecutionCreation(success, objOutput);
            }
            else
            {
                message = MessageBoxConstants.GetMessageExecutionErrorParse(objFile);
            }
            return(new ToolResult(message, success));
        }
Пример #5
0
        private static ToolResult ConvertRefModelSmd(string objFile, string smdOutput, Dictionary <string, string> inputs)
        {
            bool          success = false;
            bool          warn    = false;
            StringBuilder sb      = new StringBuilder();
            ObjData       objData = ObjParser.TryParseObj(objFile);

            if (objData != null)
            {
                // Delete all materials
                ObjModifier.DeleteMaterials(objData);

                // Scale the model by the given value
                if (inputs.TryGetValue(DictConstants.ScaleValue, out string scaleStr))
                {
                    if (Double.TryParse(scaleStr, out double scaleValue))
                    {
                        ObjModifier.UniformScale(objData, scaleValue); // Scale Model
                    }
                    else
                    {
                        warn = true;
                        sb.AppendLine(MessageBoxConstants.MessageErrorScale + MessageBoxConstants.MessageInvalidScaleValue);
                    }
                }

                // The SMD will use a default material name for all objects
                success = SmdExporter.WriteSmd(objData, null, smdOutput, false);
                sb.Append(MessageBoxConstants.GetMessageExecutionCreation(success, smdOutput));
            }
            else
            {
                sb.Append(MessageBoxConstants.GetMessageExecutionErrorParse(objFile));
            }
            return(new ToolResult(sb.ToString(), success, warn));
        }
Пример #6
0
        /// <summary>
        /// Create the smd file with the given data
        /// </summary>
        /// <param name="objData">Data parsed from the obj file</param>
        /// <param name="mtlData">Data parsed from the mtl file</param>
        /// <param name="smdFilename">Output path</param>
        /// <returns></returns>
        public static bool WriteSmd(ObjData objData, MtlData mtlData, string smdFilename, bool useTextureName)
        {
            if (useTextureName)
            {
                ObjModifier.UpdateUsedTexture(objData, mtlData);
            }
            try
            {
                using (StreamWriter smd = new StreamWriter(smdFilename))
                {
                    // version 1
                    smd.WriteLine(SmdUtils.GetNewHeader());

                    // nodes
                    smd.WriteLine(SmdUtils.GetNewNodes());
                    smd.WriteLine(SmdUtils.GetNewBone());
                    smd.WriteLine(SmdUtils.GetNewEnd());

                    // skeleton
                    smd.WriteLine(SmdUtils.GetNewSkeleton());
                    smd.WriteLine(SmdUtils.GetNewTime());
                    smd.WriteLine(SmdUtils.GetNewPosition());
                    smd.WriteLine(SmdUtils.GetNewEnd());

                    // triangles
                    smd.WriteLine(SmdUtils.GetNewTriangles());
                    foreach (ObjectObj objectObj in objData.ObjectsList)
                    {
                        string smdMaterial = GetSmdMaterial(objectObj.MaterialName, objectObj.TextureName, useTextureName);

                        int coordLength  = objectObj.VerticesList.Count;
                        int uvLength     = objectObj.UVsList.Count;
                        int normalLength = objectObj.NormalsList.Count;

                        foreach (VertIndexesObj vertIndexes in objectObj.VertIndexesList)
                        {
                            smd.WriteLine(smdMaterial);
                            foreach (VertIndexObj vertIndex in vertIndexes.VertIndexList)
                            {
                                string coordStr  = DefaultCoord;
                                string uvStr     = DefaultUV;
                                string normalStr = DefaultCoord;


                                if (vertIndex.V != null)
                                {
                                    if (vertIndex.V < coordLength) // Should always be the case
                                    {
                                        // Source Engine use 'z' for the up-down axis
                                        coordStr = objectObj.VerticesList[(int)vertIndex.V].ToSmd();
                                    }
                                }

                                if (vertIndex.Vt != null)
                                {
                                    if (vertIndex.Vt < uvLength) // Should always be the case
                                    {
                                        uvStr = objectObj.UVsList[(int)vertIndex.Vt].ToString();
                                    }
                                }

                                if (vertIndex.Vn != null)
                                {
                                    if (vertIndex.Vn < normalLength) // Should always be the case
                                    {
                                        // Source Engine use 'z' for the up-down axis
                                        normalStr = objectObj.NormalsList[(int)vertIndex.Vn].ToSmd();
                                    }
                                }

                                smd.WriteLine(SmdUtils.GetNewTriangle(0, coordStr, normalStr, uvStr));
                            }
                        }
                    }
                    smd.WriteLine(SmdUtils.GetNewEnd());
                }
            }
            catch
            {
                return(false);
            }
            return(true);
        }
Пример #7
0
        private static ToolResult ModifyObj(string objFile, string objOutput, ObjOptions objOptions, Dictionary <string, string> inputs)
        {
            bool          success = false;
            bool          warn    = false;
            StringBuilder sb      = new StringBuilder();
            ObjData       objData = ObjParser.TryParseObj(objFile);

            if (objData != null)
            {
                objData.UpdateMtlData();
                MtlData mtlData = objData.Mtl;

                if (objOptions.HasFlag(ObjOptions.BlackList))
                {
                    // Delete Blacklisted Objects first
                    if (mtlData != null)
                    {
                        if (!ObjModifier.TryDeleteMatchingGroups(objData, mtlData, texturesList))
                        {
                            warn = true;
                            sb.AppendLine(MessageBoxConstants.MessageErrorDeleteBlackList + MessageBoxConstants.MessageErrorExecution);
                        }
                    }
                    else
                    {
                        warn = true;
                        sb.AppendLine(MessageBoxConstants.MessageErrorDeleteBlackList + MessageBoxConstants.MessageMtlNotFound);
                    }
                }

                ObjModifier.CalculateNormal(objData);

                if (objOptions.HasFlag(ObjOptions.UniformScale))
                {
                    if (inputs.TryGetValue(DictConstants.ScaleValue, out string scaleStr))
                    {
                        if (Double.TryParse(scaleStr, out double scaleValue))
                        {
                            ObjModifier.UniformScale(objData, scaleValue); // Scale Model
                        }
                        else
                        {
                            warn = true;
                            sb.AppendLine(MessageBoxConstants.MessageErrorScale + MessageBoxConstants.MessageInvalidScaleValue);
                        }
                    }
                }

                if (objOptions.HasFlag(ObjOptions.NonUniformScale))
                {
                    bool   isScaleValueX = false;
                    bool   isScaleValueY = false;
                    bool   isScaleValueZ = false;
                    double scaleValueX   = 0.0;
                    double scaleValueY   = 0.0;
                    double scaleValueZ   = 0.0;
                    if (inputs.TryGetValue(DictConstants.ScaleValueX, out string scaleStr))
                    {
                        isScaleValueX = Double.TryParse(scaleStr, out scaleValueX);
                    }
                    if (inputs.TryGetValue(DictConstants.ScaleValueY, out scaleStr))
                    {
                        isScaleValueY = Double.TryParse(scaleStr, out scaleValueY);
                    }
                    if (inputs.TryGetValue(DictConstants.ScaleValueZ, out scaleStr))
                    {
                        isScaleValueZ = Double.TryParse(scaleStr, out scaleValueZ);
                    }
                    if (isScaleValueX && isScaleValueY && isScaleValueZ)
                    {
                        // Scale Model
                        ObjModifier.NonUniformScale(objData, scaleValueX, scaleValueY, scaleValueZ);
                    }
                    else
                    {
                        warn = true;
                        sb.AppendLine(MessageBoxConstants.MessageErrorScale + MessageBoxConstants.MessageInvalidScaleValues);
                    }
                }

                if (objOptions.HasFlag(ObjOptions.Rotate))
                {
                    bool   isRotateValueX = false;
                    bool   isRotateValueY = false;
                    bool   isRotateValueZ = false;
                    double rotateValueX   = 0.0;
                    double rotateValueY   = 0.0;
                    double rotateValueZ   = 0.0;
                    if (inputs.TryGetValue(DictConstants.RotateValueX, out string rotationStr))
                    {
                        isRotateValueX = Double.TryParse(rotationStr, out rotateValueX);
                    }
                    if (inputs.TryGetValue(DictConstants.RotateValueY, out rotationStr))
                    {
                        isRotateValueY = Double.TryParse(rotationStr, out rotateValueY);
                    }
                    if (inputs.TryGetValue(DictConstants.RotateValueZ, out rotationStr))
                    {
                        isRotateValueZ = Double.TryParse(rotationStr, out rotateValueZ);
                    }
                    if (isRotateValueX && isRotateValueY && isRotateValueZ)
                    {
                        // Rotate Model
                        ObjModifier.RotateModel(objData, rotateValueX, rotateValueY, rotateValueZ);
                    }
                    else
                    {
                        warn = true;
                        sb.AppendLine(MessageBoxConstants.MessageErrorRotate + MessageBoxConstants.MessageInvalidRotateValues);
                    }
                }

                if (objOptions.HasFlag(ObjOptions.ReverseVertex))
                {
                    // Reverse Vertex Order
                    ObjModifier.ReverseVertexOrder(objData);
                }

                if (objOptions.HasFlag(ObjOptions.Merge))
                {
                    if (mtlData != null)
                    {
                        // Merge groups and materials
                        ObjModifier.MergeGroups(objData, mtlData);
                    }
                    else
                    {
                        warn = true;
                        sb.AppendLine(MessageBoxConstants.MessageErrorMergeGroups + MessageBoxConstants.MessageMtlNotFound);
                    }
                }
                if (objOptions.HasFlag(ObjOptions.Sort))
                {
                    // Sort groups
                    if (!ObjModifier.SortGroups(objData))
                    {
                        warn = true;
                        sb.AppendLine(MessageBoxConstants.MessageErrorSortGroups + MessageBoxConstants.MessageInvalidGroupName);
                    }

                    if (mtlData != null)
                    {
                        // Sort materials
                        if (!ObjModifier.SortMaterials(mtlData))
                        {
                            warn = true;
                            sb.AppendLine(MessageBoxConstants.MessageErrorSortMaterials + MessageBoxConstants.MessageInvalidMaterialName);
                        }
                    }
                    else
                    {
                        warn = true;
                        sb.AppendLine(MessageBoxConstants.MessageErrorSortMaterials + MessageBoxConstants.MessageMtlNotFound);
                    }
                }
                success = ObjExporter.WriteObj(objData, mtlData, objOutput, makeMtl: true, useExistingMtl: true);
                sb.Append(MessageBoxConstants.GetMessageExecutionCreation(success, objOutput));
            }
            else
            {
                sb.Append(MessageBoxConstants.GetMessageExecutionErrorParse(objFile));
            }
            return(new ToolResult(sb.ToString(), success, warn));
        }