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); } }
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); } }