private float GetRealScale(int size, DynamicRenderMeshes hairMeshes, DynamicRenderMeshes accesoryMeshes, List <MeshInfo> headMeshInfos) { if (size < 0) { return(headMeshesController.RenderMesh.RealScale); } else { var meshInfos = new List <MeshInfo>(headMeshInfos); float finalHeight = GetFinalHeight(size); float minY = float.MaxValue, maxY = float.MinValue; foreach (var mesh in hairMeshes) { meshInfos.Add(mesh.GetMeshInfo(1.0f, ManType.Custom)); } foreach (var mesh in accesoryMeshes) { meshInfos.Add(mesh.GetMeshInfo(1.0f, ManType.Custom)); } foreach (var meshInfo in meshInfos) { foreach (var position in meshInfo.Positions) { maxY = Math.Max(maxY, position.Y); minY = Math.Min(minY, position.Y); } } var height = maxY - minY; const float fakeScale = 0.6908f; return(fakeScale * (finalHeight / height)); } }
public static void SaveObjFile(string filePath, DynamicRenderMeshes meshes, MeshType type, float scale, ManType manType, string sessionId, bool saveBrushesToTexture = false, bool isCollada = false) { var meshInfos = new List <MeshInfo>(); foreach (var mesh in meshes) { var meshInfo = mesh.GetMeshInfo(scale, manType); meshInfos.Add(meshInfo); } SaveObjFile(filePath, meshInfos, type, saveBrushesToTexture, isCollada, sessionId); }
public static void SaveObjFile(string filePath, DynamicRenderMeshes meshes, MeshType type) { var meshInfos = new List <MeshInfo>(); foreach (var mesh in meshes) { var meshInfo = mesh.GetMeshInfo(); meshInfos.Add(meshInfo); } SaveObjFile(filePath, meshInfos, type); }
public PickingController(Camera c) { camera = c; AccesoryMeshes = new DynamicRenderMeshes(); HairMeshes = new DynamicRenderMeshes(); SelectedMeshes = new DynamicRenderMeshes(); SelectedMeshes.CollectionChanged += SelectedMeshes_CollectionChanged; SelectedMeshes.BeforeClear += SelectedMeshes_BeforeClear; SelectedMeshes.ItemsRemoved += SelectedMeshes_ItemsRemoved; }
public PickingController(Camera c) { camera = c; AccesoryMeshes = new DynamicRenderMeshes(); HairMeshes = new DynamicRenderMeshes(); SelectedMeshes = new DynamicRenderMeshes(); SelectedMeshes.CollectionChanged += SelectedMeshes_CollectionChanged; SelectedMeshes.BeforeClear += SelectedMeshes_BeforeClear; SelectedMeshes.ItemsRemoved += SelectedMeshes_ItemsRemoved; }
public void AttachMeshes(DynamicRenderMeshes meshes) { var tmpVertexArray = new List<Vertex>(); var indices = new List<uint>(); vertexAnimatedArray = null; foreach (var m in meshes) { foreach (var v in m.vertexArray) { tmpVertexArray.Add(v); indices.Add((uint)indices.Count); } } NumIndices = indices.Count; GL.BindBuffer(BufferTarget.ElementArrayBuffer, IndexBuffer); GL.BufferData(BufferTarget.ElementArrayBuffer, new IntPtr(indices.Count * sizeof(uint)), indices.ToArray(), BufferUsageHint.DynamicDraw); vertexArray = tmpVertexArray.ToArray(); UpdateBuffer(); UpdatePointIndices(); }
public void Initialize(Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3, DynamicRenderMeshes m, Camera camera, int t) { type = t; var verticesMap = new Dictionary <Vector3, int>(new VectorEqualityComparer()); MeshesInfo.Clear(); var invProjection = camera.ProjectMatrix.Inverted(); point0 = SliceController.UnprojectPoint(p0, camera.WindowWidth, camera.WindowHeight, invProjection); point1 = SliceController.UnprojectPoint(p1, camera.WindowWidth, camera.WindowHeight, invProjection); point2 = SliceController.UnprojectPoint(p2, camera.WindowWidth, camera.WindowHeight, invProjection); point3 = SliceController.UnprojectPoint(p3, camera.WindowWidth, camera.WindowHeight, invProjection); var d0 = (point0 + point1) * 0.5f; var d1 = (point2 + point3) * 0.5f; center = (d0 + d1) * 0.5f; directionHorisontal = d1 - d0; halfSize.X = directionHorisontal.Length * 0.5f; directionHorisontal.Normalize(); directionVertical = point0 - point1; halfSize.Y = directionVertical.Length * 0.5f; directionVertical.Normalize(); meshes = m; foreach (var mesh in meshes) { verticesMap.Clear(); var meshInfo = new ShapeMeshInfo(); var tempVertices = mesh.GetVertices(); var tempNormals = mesh.GetNormals(); var transform = mesh.Transform * camera.ViewMatrix; meshInfo.InverceTransform = transform.Inverted(); var normals = new List <Vector3>(); for (var i = 0; i < tempVertices.Count; i++) { var vertex = tempVertices[i]; int index; if (!verticesMap.TryGetValue(vertex, out index)) { index = meshInfo.Vertices.Count; meshInfo.Vertices.Add(vertex); normals.Add(tempNormals[i]); verticesMap.Add(vertex, index); } meshInfo.Indices.Add(index); } var dt = halfSize.X / linesCount; foreach (var p in verticesMap) { var position = Vector3.Transform(p.Key, transform); if (PointInRectangle(ref point0, ref point1, ref point2, ref point3, ref position)) { var shapePoint = new ShapePoint(); meshInfo.Points.Add(p.Value, shapePoint); shapePoint.OriginalPosition = position; var v = position.Xy - center; shapePoint.LocalPosition.X = Vector2.Dot(directionHorisontal, v); shapePoint.LocalPosition.Y = Vector2.Dot(directionVertical, v); switch (type) { case 0: //shape shapePoint.K = 1.0f - Math.Abs(shapePoint.LocalPosition.Y / halfSize.Y); break; case 1: //stretch shapePoint.K = Math.Abs((shapePoint.LocalPosition.X + halfSize.X) / (halfSize.X * 2.0f)); break; case 2: //pleat var x = shapePoint.LocalPosition.X + halfSize.X; var l0 = (int)Math.Floor(x / dt); x -= l0 * dt; var ky = 1.0f - Math.Abs(shapePoint.LocalPosition.Y / halfSize.Y); shapePoint.K = Math.Abs((x / dt) * 2.0f - 1.0f) * ky; var normal = Vector3.Transform(normals[p.Value], transform); if (normal.Z < 0.0f) { shapePoint.K = -shapePoint.K; } break; } } } MeshesInfo.Add(mesh.Id, meshInfo); } }
/// <summary> </summary> /// <param name="filePath"></param> /// <param name="HairMeshes"></param> /// <param name="AccesoryMeshes"></param> /// <param name="faceParts"></param> /// <param name="morphScale"></param> /// <param name="saveBrushesToTexture">При экспорте в ДАЗ или в колладу - нужно сохранять то что поправили кисточкой в туже текстуру </param> /// <param name="isCollada">Если коллада - то текстуры должны лежать в той же папке. Заказ старикана</param> public static void ExportMergedModel(string filePath, DynamicRenderMeshes HairMeshes, DynamicRenderMeshes AccesoryMeshes, List<MeshInfo> faceParts, float morphScale, bool saveBrushesToTexture = false, bool isCollada = false) { //if (meshInfos.Count == 0) // return; var fi = new FileInfo(filePath); if (fi.Exists) fi.Delete(); var mtlPath = Path.Combine(fi.DirectoryName, Path.GetFileNameWithoutExtension(fi.Name) + ".mtl"); var fiMtl = new FileInfo(mtlPath); if (fiMtl.Exists) fiMtl.Delete(); var materials = new Dictionary<string, ObjMaterial>(); // group title, diffuse color, texture path using (var sw = new StreamWriter(filePath, false, Encoding.Default)) { sw.WriteLine("#Produced by HeadShop"); //if (type == MeshType.Accessory) // sw.WriteLine("#Accessories"); //else sw.WriteLine("#HeadShop Model"); sw.WriteLine("#" + DateTime.Now.ToShortDateString()); sw.WriteLine("mtllib " + fiMtl.Name); sw.WriteLine(); var meshInfos = new List<MeshInfo>(); foreach (var mesh in HairMeshes) { var meshInfo = mesh.GetMeshInfo(morphScale); MeshInfo.FindCenter(meshInfo.Positions, "Волосы до трансформации ObjSaver::ExportMergedModel()"); TransformForPluginMode(mesh, meshInfo); meshInfos.Add(meshInfo); MeshInfo.FindCenter(meshInfo.Positions, "Волосы после трансформации ObjSaver::ExportMergedModel()"); } foreach (var mesh in AccesoryMeshes) { var meshInfo = mesh.GetMeshInfo(morphScale); MeshInfo.FindCenter(meshInfo.Positions, "Аксесуары до трансформации ObjSaver::ExportMergedModel()"); TransformForPluginMode(mesh, meshInfo); meshInfos.Add(meshInfo); MeshInfo.FindCenter(meshInfo.Positions, "Аксесуары после трансформации ObjSaver::ExportMergedModel()"); } if (meshInfos.Count > 0) { var scale = ProgramCore.PluginMode ? 1.0f : PickingController.GetHairScale(ProgramCore.Project.ManType); ProgramCore.EchoToLog(String.Format("На это умножаем волосы или аксесуары при экспорте ObjSaver::ExportMergedModel(): {0}", scale), EchoMessageType.Information); var transformMatrix = Matrix4.CreateScale(scale); SaveVerticles(meshInfos, sw, transformMatrix); //write only additional meshes first, with translated positions MeshInfo.FindCenter(meshInfos, "Аксесуары и волосы, которые ушли на экспорт ObjSaver::ExportMergedModel()"); SaveTextureCoords(meshInfos, sw); SaveNormals(meshInfos, sw, transformMatrix); } if (faceParts.Count > 0) { ProgramCore.EchoToLog(String.Format("На это умножаем бошку при экспорте ObjSaver::ExportMergedModel(): {0}", morphScale), EchoMessageType.Information); var transformMatrix = Matrix4.CreateScale(morphScale); SaveVerticles(faceParts, sw, transformMatrix); MeshInfo.FindCenter(faceParts, "Бошка, которая ушла на экспорт ObjSaver::ExportMergedModel()"); SaveTextureCoords(faceParts, sw); SaveNormals(faceParts, sw, Matrix4.Zero); } meshInfos.AddRange(faceParts); MeshInfo.FindCenter(meshInfos, "Итоговая модель после экспорта ObjSaver::ExportMergedModel()"); var groupIndex = 0; var startPositionIndex = 1; var startTexIndex = 1; var startNormalIndex = 1; foreach (var meshInfo in meshInfos) // faces should write differently { if (meshInfo.IndicesNormals.Count == 0) continue; var groupTitle = string.Empty; if (string.IsNullOrEmpty(meshInfo.Title) || materials.ContainsKey(meshInfo.Title)) { groupTitle = "Element_" + groupIndex; while (materials.ContainsKey(groupTitle)) { ++groupIndex; groupTitle = "Element_" + groupIndex; } ++groupIndex; } else groupTitle = meshInfo.Title; materials.Add(groupTitle, meshInfo.Material); sw.WriteLine("g " + groupTitle); sw.WriteLine("usemtl " + groupTitle); #region Faces var resStr = "f "; var index = 0; for (var i = 0; i < meshInfo.IndicesTexCoords.Count; i++) { resStr += (startPositionIndex + meshInfo.IndicesPositions[i]).ToString(ProgramCore.Nfi) + "/"; resStr += (startTexIndex + meshInfo.IndicesTexCoords[i]).ToString(ProgramCore.Nfi) + "/"; resStr += (startNormalIndex + meshInfo.IndicesNormals[i]).ToString(ProgramCore.Nfi) + " "; ++index; if (index == 3) { index = 0; sw.WriteLine(resStr.Remove(resStr.Length - 1)); resStr = "f "; } } startPositionIndex += (meshInfo.IndicesPositions.Max() + 1); startTexIndex += (meshInfo.IndicesTexCoords.Max() + 1); startNormalIndex += (meshInfo.IndicesNormals.Max() + 1); #endregion } } SaveMaterial(mtlPath, materials, fi, saveBrushesToTexture, isCollada); }
public static void SaveObjFile(string filePath, DynamicRenderMeshes meshes, MeshType type, float scale, bool saveBrushesToTexture = false, bool isCollada = false) { var meshInfos = new List<MeshInfo>(); foreach (var mesh in meshes) { var meshInfo = mesh.GetMeshInfo(scale); meshInfos.Add(meshInfo); } SaveObjFile(filePath, meshInfos, type, saveBrushesToTexture, isCollada); }
internal void SaveSelectedHairToPartsLibrary() { if (pickingController.SelectedMeshes.Count == 0) return; if (pickingController.SelectedMeshes.All(x => x.meshType != MeshType.Hair)) return; pickingController.SelectedMeshes[0].AttachMeshes(pickingController.SelectedMeshes); var firstMesh = pickingController.SelectedMeshes.First(); pickingController.SelectedMeshes.RemoveAt(0); DeleteSelectedHair(); pickingController.SelectedMeshes.Add(firstMesh); using (var sfd = new SaveFileDialogEx("Save part", "OBJ files|*.obj")) { if (sfd.ShowDialog() != DialogResult.OK) return; var meshes = new DynamicRenderMeshes(); foreach (var mesh in pickingController.SelectedMeshes) meshes.Add(mesh); pickingController.SelectedMeshes.Clear(); ObjSaver.SaveObjFile(sfd.FileName, meshes, MeshType.Hair, headMeshesController.RenderMesh.RealScale); var fileName = Path.GetFileNameWithoutExtension(sfd.FileName); var title = fileName; var index = 0; while (PartsLibraryMeshes.ContainsKey(title)) // prevent duplicated names, because it's not occure save correct to obj { title = fileName + "_" + index; ++index; } if (!PartsLibraryMeshes.ContainsKey(title)) PartsLibraryMeshes.Add(title, new DynamicRenderMeshes()); for (var i = 0; i < meshes.Count; i++) { var mesh = meshes[i]; mesh.Title = title + "_" + i; if (!PartsLibraryMeshes[title].Contains(mesh)) PartsLibraryMeshes[title].Add(mesh); } ProgramCore.MainForm.frmParts.UpdateList(); } }
public void Initialize(Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3, DynamicRenderMeshes m, Camera camera, int t) { type = t; var verticesMap = new Dictionary<Vector3, int>(new VectorEqualityComparer()); MeshesInfo.Clear(); var invProjection = camera.ProjectMatrix.Inverted(); point0 = SliceController.UnprojectPoint(p0, camera.WindowWidth, camera.WindowHeight, invProjection); point1 = SliceController.UnprojectPoint(p1, camera.WindowWidth, camera.WindowHeight, invProjection); point2 = SliceController.UnprojectPoint(p2, camera.WindowWidth, camera.WindowHeight, invProjection); point3 = SliceController.UnprojectPoint(p3, camera.WindowWidth, camera.WindowHeight, invProjection); var d0 = (point0 + point1) * 0.5f; var d1 = (point2 + point3) * 0.5f; center = (d0 + d1) * 0.5f; directionHorisontal = d1 - d0; halfSize.X = directionHorisontal.Length * 0.5f; directionHorisontal.Normalize(); directionVertical = point0 - point1; halfSize.Y = directionVertical.Length * 0.5f; directionVertical.Normalize(); meshes = m; foreach (var mesh in meshes) { verticesMap.Clear(); var meshInfo = new ShapeMeshInfo(); var tempVertices = mesh.GetVertices(); var tempNormals = mesh.GetNormals(); var transform = mesh.Transform * camera.ViewMatrix; meshInfo.InverceTransform = transform.Inverted(); var normals = new List<Vector3>(); for (var i = 0; i < tempVertices.Count; i++) { var vertex = tempVertices[i]; int index; if (!verticesMap.TryGetValue(vertex, out index)) { index = meshInfo.Vertices.Count; meshInfo.Vertices.Add(vertex); normals.Add(tempNormals[i]); verticesMap.Add(vertex, index); } meshInfo.Indices.Add(index); } var dt = halfSize.X / linesCount; foreach (var p in verticesMap) { var position = Vector3.Transform(p.Key, transform); if (PointInRectangle(ref point0, ref point1, ref point2, ref point3, ref position)) { var shapePoint = new ShapePoint(); meshInfo.Points.Add(p.Value, shapePoint); shapePoint.OriginalPosition = position; var v = position.Xy - center; shapePoint.LocalPosition.X = Vector2.Dot(directionHorisontal, v); shapePoint.LocalPosition.Y = Vector2.Dot(directionVertical, v); switch (type) { case 0://shape shapePoint.K = 1.0f - Math.Abs(shapePoint.LocalPosition.Y / halfSize.Y); break; case 1://stretch shapePoint.K = Math.Abs((shapePoint.LocalPosition.X + halfSize.X) / (halfSize.X * 2.0f)); break; case 2://pleat var x = shapePoint.LocalPosition.X + halfSize.X; var l0 = (int)Math.Floor(x / dt); x -= l0 * dt; var ky = 1.0f - Math.Abs(shapePoint.LocalPosition.Y / halfSize.Y); shapePoint.K = Math.Abs((x / dt) * 2.0f - 1.0f) * ky; var normal = Vector3.Transform(normals[p.Value], transform); if (normal.Z < 0.0f) shapePoint.K = -shapePoint.K; break; } } } MeshesInfo.Add(mesh.Id, meshInfo); } }