Exemplo n.º 1
0
        public List <DynamicRenderMesh> AddMehes(string path, MeshType type, bool fromDragAndDrop, GenesisType genesisType, ManType manType, string animationPath, bool needExporter, bool isOpenSmile)
        {
            var result = new List <DynamicRenderMesh>();

            var objModel = ObjLoader.LoadObjFile(path, needExporter, isOpenSmile);

            if (objModel == null)
            {
                ProgramCore.EchoToLog($"Can't load obj model '{path}'", EchoMessageType.Error);
                return(result);
            }

            var lastTriangle = 0;

            switch (type)
            {
            case MeshType.Hair:
            {
                var fi           = new FileInfo(path);
                var objModelNull = ObjLoader.LoadObjFile(Path.Combine(fi.DirectoryName, fi.Name.Replace(fi.Extension, string.Format("_null{0}", fi.Extension))), isOpenSmile);
                if (objModelNull != null &&
                    (objModelNull.Groups.Count != objModel.Groups.Count ||
                     objModel.Vertices.Count != objModelNull.Vertices.Count))
                {
                    //  objModel.TextureCoords.Count != objModelNull.TextureCoords.Count))
                    objModelNull = null;
                }

                result = LoadHairMeshes(objModel, objModelNull, fromDragAndDrop, manType, MeshType.Hair, ref lastTriangle);
                foreach (var renderMesh in result)
                {
                    HairMeshes.Add(renderMesh);
                }
                break;
            }

            case MeshType.Accessory:

                if (objModel.accessoryByHeadShop)
                {
                    result.AddRange(LoadSpecialAccessoryMesh(objModel));
                }
                else
                {
                    var accessories = new List <DynamicRenderMesh> {
                        LoadAccessoryMesh(objModel)
                    };
                    foreach (var accessory in accessories)
                    {
                        AccesoryMeshes.Add(accessory);
                    }

                    result.AddRange(accessories);
                }
                break;

            case MeshType.Head:
            {
                var tempPluginTexture      = string.Empty;
                var topPoint               = 11.98351f;
                int tempPluginTextureIndex = 0;
                if (ProgramCore.PluginMode)
                {
                    var folderPath = Path.Combine(Application.StartupPath, "Models", "Model", genesisType.GetGenesisPath(), manType.GetObjDirPath(isOpenSmile));
                    switch (ProgramCore.Project.ManType)
                    {
                    case ManType.Male:
                        switch (ProgramCore.Project.GenesisType)
                        {
                        case GenesisType.Genesis2:
                            tempPluginTexture = Path.Combine(folderPath, "Maps", "RyNevio_faceB.jpg");
                            break;

                        case GenesisType.Genesis3:
                            tempPluginTexture = Path.Combine(folderPath, "Maps", "RyEddie_face_1001.jpg");
                            break;

                        case GenesisType.Genesis8:
                            tempPluginTexture = Path.Combine(folderPath, "Maps", "G8MBaseFaceMapD01_1001.jpg");
                            break;
                        }
                        break;

                    case ManType.Female:
                        switch (ProgramCore.Project.GenesisType)
                        {
                        case GenesisType.Genesis2:
                            tempPluginTexture = Path.Combine(folderPath, "Maps", "V5BreeHeadM.jpg");
                            break;

                        case GenesisType.Genesis3:
                            tempPluginTexture = Path.Combine(folderPath, "Maps", "RyJeane_face_1001.jpg");
                            break;

                        case GenesisType.Genesis8:
                            tempPluginTexture = Path.Combine(folderPath, "Maps", "G8FBaseFaceMapD_1001.jpg");
                            break;
                        }
                        break;

                    case ManType.Child:
                        tempPluginTexture = Path.Combine(folderPath, "Maps", "AC_KidsRRHBy.jpg");
                        break;

                    default:
                        tempPluginTexture = Path.Combine(Application.StartupPath, "Plugin", "fsRndColor.png");
                        break;
                    }
                    tempPluginTextureIndex = ProgramCore.MainForm.ctrlRenderControl.GetTexture(tempPluginTexture);
                }


                float scale = 1.0f;
                switch (ProgramCore.Project.ManType)
                {
                case ManType.Male:
                    topPoint = 11.98351f;
                    break;

                case ManType.Female:
                    topPoint = 11.61f;
                    break;

                case ManType.Child:
                    //scale = 0.85f;
                    topPoint = 9.759598f;
                    break;
                }

                var objModelFull = animationPath == string.Empty ? null : ObjLoader.LoadObjFile(animationPath, isOpenSmile);
                if (objModelFull != null &&
                    (objModelFull.Groups.Count != objModel.Groups.Count ||
                     objModel.Vertices.Count != objModelFull.Vertices.Count ||
                     objModel.TextureCoords.Count != objModelFull.TextureCoords.Count))
                {
                    objModelFull = null;
                }

                var meshPartInfos = LoadHeadMeshes(objModel, fromDragAndDrop, manType, scale, ref lastTriangle);
                ObjExport = objModel.ObjExport;

                //if (ProgramCore.PluginMode)
                {
                    ObjExport.DefaultA = new Vector3(99999.0f, 99999.0f, 99999.0f);
                    ObjExport.DefaultB = new Vector3(-99999.0f, -99999.0f, -99999.0f);

                    foreach (var meshPartInfo in meshPartInfos)
                    {
                        GetAABB(ref ObjExport.DefaultA, ref ObjExport.DefaultB, meshPartInfo.VertexPositions);
                    }
                }

                if (ProgramCore.PluginMode && isOpenSmile)
                {
                    PluginMorphToSmile(meshPartInfos, genesisType, manType, scale);
                }

                //result = LoadHairMeshes(objModel, objModelFull, fromDragAndDrop, manType, MeshType.Head);
                //var meshPartInfos = new List<MeshPartInfo>();
                var a = new Vector3(99999.0f, 99999.0f, 99999.0f);
                var b = new Vector3(-99999.0f, -99999.0f, -99999.0f);
                foreach (var meshPartInfo in meshPartInfos)
                {
                    if (ProgramCore.PluginMode && ProgramCore.MainForm.PluginUvGroups.Contains(meshPartInfo.MaterialName))
                    {
                        if (string.IsNullOrEmpty(meshPartInfo.TextureName) || !File.Exists(meshPartInfo.TextureName))
                        {
                            meshPartInfo.TextureName = tempPluginTexture;
                            meshPartInfo.Texture     = tempPluginTextureIndex;
                        }
                    }
                    GetAABB(ref a, ref b, meshPartInfo.VertexPositions);
                }
                var dv = Vector3.Zero;
                foreach (var meshPartInfo in meshPartInfos)
                {                   // ПОТЕНЦИАЛЬНО ОПАСНОЕ МЕСТО! В случае улетания башки - AlignByTOp сделать для всех, кроме плагина. Для него оставить MoveToPosition
#if (WEB_APP)
                    dv = AlignByTop(ref meshPartInfo.VertexPositions, a, b, topPoint);
#else
                    dv = MoveToPosition(ref meshPartInfo.VertexPositions, a, b, Vector3.Zero);
#endif
                    ProgramCore.Project.RenderMainHelper.headMeshesController.CreateMeshPart(ProgramCore.Project.GenesisType, meshPartInfo);
                }

                if (ObjExport != null)
                {
                    ObjExport.Delta = -dv;
                }
                ProgramCore.Project.RenderMainHelper.headMeshesController.FinishCreating();

                return(null);
                // ProgramCore.Project.RenderMainHelper.headMeshesController.InitializeTexturing(HeadController.GetDots(ProgramCore.Project.ManType), HeadController.GetIndices(ProgramCore.Project.ManType));
            }

            default:
                return(result);
            }

            foreach (var item in result)
            {
                item.Path = path;
            }

            return(result);
        }
Exemplo n.º 2
0
        private void PluginMorphToSmile(List <MeshPartInfo> meshesInfo, GenesisType genesisType, ManType manType, float scale)
        {
            var lastTriangle = 0;
            var path         = Path.Combine(Application.StartupPath, "Models", "Model", genesisType.GetGenesisPath(), manType.GetObjPathSmilePlugin());
            var objModel     = ObjLoader.LoadObjFile(path, true, true);
            var smileMeshes  = LoadHeadMeshes(objModel, false, manType, scale, ref lastTriangle);

            /*var a0 = new Vector3(99999.0f, 99999.0f, 99999.0f);
             * var b0 = new Vector3(-99999.0f, -99999.0f, -99999.0f);
             * foreach (var meshPartInfo in meshesInfo)
             * {
             *  GetAABB(ref a0, ref b0, meshPartInfo.VertexPositions);
             * }
             * var a1 = new Vector3(99999.0f, 99999.0f, 99999.0f);
             * var b1 = new Vector3(-99999.0f, -99999.0f, -99999.0f);
             * foreach (var meshPartInfo in smileMeshes)
             * {
             *  GetAABB(ref a1, ref b1, meshPartInfo.VertexPositions);
             * }
             * var dist0 = (b0 - a0).Length;
             * var dist1 = (b1 - a1).Length;
             * var center0 = (b0 + a0) * 0.5f;
             * var center1 = (b1 + a1) * 0.5f;*/

            float k = 265.4678407f; //! Вот это нужно подобрать
            //dist1 / dist0;

            float MaxY0 = -99999.0f;
            float MaxY1 = -99999.0f;

            foreach (var meshInfo in meshesInfo)
            {
                for (int i = 0; i < meshInfo.VertexPositions.Count; ++i)
                {
                    MaxY0 = Math.Max(MaxY0, meshInfo.VertexPositions[i].Y);
                }
            }

            foreach (var meshInfo in meshesInfo)
            {
                var mesh = smileMeshes.FirstOrDefault(m => m.PartName == meshInfo.PartName);
                if (mesh.VertexPositions.Count == meshInfo.VertexPositions.Count)
                {
                    for (int i = 0; i < mesh.VertexPositions.Count; ++i)
                    {
                        //meshInfo.VertexPositions[i] = center0 + (mesh.VertexPositions[i] - center1) * k;
                        meshInfo.VertexPositions[i] = mesh.VertexPositions[i] / k;
                        MaxY1 = Math.Max(MaxY1, meshInfo.VertexPositions[i].Y);
                    }
                }
            }

            foreach (var meshInfo in meshesInfo)
            {
                for (int i = 0; i < meshInfo.VertexPositions.Count; ++i)
                {
                    Vector3 v = meshInfo.VertexPositions[i];
                    v.Y += (MaxY0 - MaxY1);
                    meshInfo.VertexPositions[i] = v;
                }
            }
        }
Exemplo n.º 3
0
        /// <summary> Создание нового проекта </summary>
        /// <param name="projectName"></param>
        /// <param name="projectPath"></param>
        /// <param name="templateImageName"></param>
        /// <param name="manType"></param>
        /// <param name="headModelPath">Указываем путь до модели головы (в случае если выбрали import OBJ). Иначе - пустая строка</param>
        /// <param name="needCopy"></param>
        public Project(string projectName, string projectPath, string templateImageName, GenesisType genesisType, ManType manType, string headModelPath, bool needCopy, int selectedSize, bool isOpenSmile)
        {
            ProjectName = projectName;
            ProjectPath = projectPath;
            TextureSize = selectedSize;

            ManType     = manType;
            IsOpenSmile = isOpenSmile;
            GenesisType = genesisType;
            switch (manType)
            {
            case ManType.Male:
            case ManType.Female:             // если это обычные модели - копируем их из папки с прогой - в папку с проектом
            case ManType.Child:
                if (!ProgramCore.PluginMode) //тогда хед модел пас оставляем какой был! пиздец важно!
                {                            // я хз почему это важно, но сейчас получается что при загрузке из Даз всегда берется одна модель, независимо от того, что выбрал пользователь (13.02.2018)
#if WEB_APP
                    headModelPath = "ftp://108.167.164.209/public_html/printahead.online/PrintAhead_DefaultModels/" + manType.GetObjPath();
                    headModelPath = headModelPath.Replace(@"\", "/");
#else
                    headModelPath = Path.Combine(Application.StartupPath, "Models", "Model", GenesisType.GetGenesisPath(), manType.GetObjPath(isOpenSmile));
#endif
                }

                break;

            case ManType.Custom:
            {
                BaseDots = new HeadPoints <HeadPoint>();
                foreach (var vector in HeadController.GetBaseDots(ManType.Female))
                {
                    BaseDots.Add(new HeadPoint(vector));
                }

                ProfileBaseDots = new HeadPoints <HeadPoint>();
                foreach (var vector in HeadController.GetProfileBaseDots(ManType.Female))
                {
                    ProfileBaseDots.Add(new HeadPoint(vector));
                }
            }
            break;
            }

            if (needCopy)
            {
                try
                {
                    var di = new DirectoryInfo(projectPath);
                    if (!di.Exists)
                    {
                        di.Create();
                    }

                    if (!string.IsNullOrEmpty(templateImageName))
                    {
                        var fi = new FileInfo(templateImageName);
                        if (fi.Exists)
                        {
                            var newImagePath = Path.Combine(projectPath, fi.Name);
                            File.Copy(templateImageName, newImagePath, true);
                            FrontImagePath = fi.Name;
                        }
                    }

                    #region Копируем модель

                    var directoryPath = Path.Combine(ProjectPath, "Model");
                    FolderEx.CreateDirectory(directoryPath);
                    var oldFileName = Path.GetFileNameWithoutExtension(headModelPath);
                    var newFileName = oldFileName;
                    var filePath    = Path.Combine(directoryPath, newFileName + ".obj");

                    File.Copy(headModelPath, filePath, true); // сама модель
                    HeadModelPath = filePath;

                    #region Обрабатываем mtl файл и папку с текстурами

                    var mtl = oldFileName + ".mtl";
                    using (var ms = new StreamReader(headModelPath))
                    {
                        for (var i = 0; i < 10; i++)
                        {
                            if (ms.EndOfStream)
                            {
                                break;
                            }
                            var line = ms.ReadLine();
                            if (line.ToLower().Contains("mtllib"))
                            // ищем ссылку в obj файле на mtl файл (у них могут быть разные названия, но всегда в одной папке
                            {
                                var lines = line.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                                if (lines.Length > 1)
                                {
                                    mtl = lines[1];
                                    break;
                                }
                            }
                        }
                    }

                    ObjLoader.CopyMtl(mtl, mtl, Path.GetDirectoryName(headModelPath), "", directoryPath, selectedSize);

                    #endregion

                    #endregion
                }
                catch
                {
                    FrontImagePath = templateImageName;
                    HeadModelPath  = headModelPath;
                }
            }
            else
            {
                FrontImagePath = templateImageName;
                HeadModelPath  = headModelPath;
            }
        }