/// <summary> /// メッシュを拡大する。現在のscale値の何倍するかで指定 /// </summary> public void ScaleByRatio(float rx, float ry) { if (Math.Abs(rx) <= 0 || Math.Abs(ry) <= 0) { throw new Exception(string.Format("ScaleByRatio: |rx| (={0}) and |ry| (={1}) cannot be less than 0", Math.Abs(rx), Math.Abs(ry))); } scale.X *= rx; scale.Y *= ry; // メッシュ・スケルトン・ボーン(と制御点)を(rx, ry)倍する mesh.ScaleByRatio(rx, ry); skl.ScaleByRatio(rx, ry); foreach (var kv in skeletalControlPointDict) { PatchSkeletonBone b = kv.Key; b.src.position = new PointF(b.src.position.X * rx, b.src.position.Y * ry); b.dst.position = new PointF(b.dst.position.X * rx, b.dst.position.Y * ry); List <PointF> ls = kv.Value; for (int i = 0; i < ls.Count; i++) { ls[i] = new PointF(ls[i].X * rx, ls[i].Y * ry); } } }
/// <summary> /// meshのboneと交差している切り口を1つ返す /// dir は交差の仕方。切り口の逆なら符号の異なる値を返す /// </summary> static bool FindCrossingSection(PatchSkeletalMesh smesh, PatchSkeletonBone refBone, out List <PatchSection> sections, out List <float> dirs) { sections = new List <PatchSection>(); dirs = new List <float>(); // _boneに対応するmesh.skl内のボーンを探す PatchSkeletonBone bone = null; foreach (var b in smesh.skl.bones) { if (b == refBone) { bone = b; } } if (bone == null) { return(false); } foreach (var sec in smesh.sections) { for (int i = sec.First; i < sec.First + sec.Length - 1; i++) { int n = smesh.mesh.pathIndices.Count; var p1 = smesh.mesh.vertices[smesh.mesh.pathIndices[FMath.Rem(i, n)]].position; var p2 = smesh.mesh.vertices[smesh.mesh.pathIndices[FMath.Rem(i + 1, n)]].position; if (FLib.FMath.IsCrossed(p1, p2, bone.src.position, bone.dst.position)) { sections.Add(sec); // 交差点からボーン方向に少し動かした点がメッシュ内か否かで切り口の向きを判定 PointF crossPoint = FLib.FMath.CrossPoint(p1, p2, bone.src.position, bone.dst.position); float ratio = 1f / FLib.FMath.Distance(bone.src.position, bone.dst.position); PointF boneDir = new PointF(bone.src.position.X - bone.dst.position.X, bone.src.position.Y - bone.dst.position.Y); float sampleX = crossPoint.X + boneDir.X * ratio; float sampleY = crossPoint.Y + boneDir.Y * ratio; bool inMesh = FLib.FMath.IsPointInPolygon(new PointF(sampleX, sampleY), GetPath(smesh).Select(v => v.position).ToList()); if (inMesh) { dirs.Add(-1); } else { dirs.Add(1); } } } } if (sections.Count >= 1) { return(true); } return(false); }
/// <param name="checkLength">sectionから何pixel分の輪郭を計算に使うか/param> static float SectionHeight(PatchSkeletalMesh mesh, PatchSection section, PatchSkeletonBone b, float curveLength) { List <PatchVertex> path = GetPath(mesh); var rawcurves = section2adjCurves(path, section); if (path == null || rawcurves == null || b == null) { return(0); } var curves = TrimCurves(path, rawcurves, 0, curveLength); if (curves.Item1 == null || curves.Item2 == null) { return(0); } float boneLen = FMath.Distance(b.src.position, b.dst.position); if (boneLen <= 1e-4) { return(0); } PointF x, y; BoneCoordinate(b, out x, out y); // 各セグメントのボーンからのズレを求める float height = 0; int cnt = Math.Min(curves.Item1.Length, curves.Item2.Length); for (int i = 0; i < cnt; i++) { int idx1 = curves.Item1.First + curves.Item1.Length - 1 - i; int idx2 = curves.Item2.First + i; while (idx1 < 0) { idx1 += path.Count; } while (idx2 < 0) { idx2 += path.Count; } PointF pt1 = path[idx1 % path.Count].position; PointF pt2 = path[idx2 % path.Count].position; pt1.X -= b.src.position.X; pt1.Y -= b.src.position.Y; pt2.X -= b.src.position.X; pt2.Y -= b.src.position.Y; height += pt1.X * y.X + pt1.Y * y.Y; height += pt2.X * y.X + pt2.Y * y.Y; } height /= 2 * cnt; return(height); }
static float SectionHeight(PatchSkeletalMesh mesh, PatchSection section, PatchSkeletonBone b) { List <PointF> path = GetPath(mesh).Select(v => v.position).ToList(); var curves = SectionToAdjuscentCurves(path, section, 5, 30); if (path == null || curves == null || b == null) { return(0); } if (curves.Item1 == null || curves.Item2 == null) { return(0); } float height = 0; float boneLen = FMath.Distance(b.src.position, b.dst.position); if (boneLen <= 1e-4) { return(0); } PointF x, y; CalcBoneCoordinate(b, out x, out y); // 各セグメントのボーンからのズレを求める int cnt = Math.Min(curves.Item1.Length, curves.Item2.Length); for (int i = 0; i < cnt; i++) { int idx1 = curves.Item1.First + curves.Item1.Length - 1 - i; int idx2 = curves.Item2.First + i; while (idx1 < 0) { idx1 += path.Count; } while (idx2 < 0) { idx2 += path.Count; } PointF pt1 = path[idx1 % path.Count]; PointF pt2 = path[idx2 % path.Count]; pt1.X -= b.src.position.X; pt1.Y -= b.src.position.Y; pt2.X -= b.src.position.X; pt2.Y -= b.src.position.Y; height += pt1.X * y.X + pt1.Y * y.Y; height += pt2.X * y.X + pt2.Y * y.Y; } height /= 2 * cnt; return(height); }
public static void Fitting(PatchSkeletalMesh smesh, PatchSkeleton skl) { FTimer.Resume("Fitting:SetCtrl"); // スケルトンに合わせて制御点を移動 foreach (var kv in smesh.skeletalControlPointDict) { PatchSkeletonBone b = kv.Key; List <PointF> orgPts = kv.Value; PatchSkeletonBone br = CorrespondingBone(skl, b); if (br == null) { continue; } if (orgPts.Count <= 1) { continue; } for (int i = 0; i < orgPts.Count; i++) { float t = (float)i / (orgPts.Count - 1); // smesh.skeletalControlPointExpandToXXXの値にしたがって、src, dst方向にそれぞれ伸長させる float exSrc = smesh.endJoints.Contains(b.src.name) ? smesh.srcStretchRatio : 0; float exDst = smesh.endJoints.Contains(b.dst.name) ? smesh.dstStretchRatio : 0; float dx = br.dst.position.X - br.src.position.X; float dy = br.dst.position.Y - br.src.position.Y; t *= 1 + exSrc + exDst; float lx = dx * t; float ly = dy * t; float ox = -dx * exSrc; float oy = -dy * exSrc; float x = br.src.position.X + ox + lx; float y = br.src.position.Y + oy + ly; // float x = br.src.position.X * (1 - t) + br.dst.position.X * t; // float y = br.src.position.Y * (1 - t) + br.dst.position.Y * t; PatchControlPoint c = smesh.mesh.FindControlPoint(orgPts[i]); if (c == null) { continue; } smesh.mesh.TranslateControlPoint(c.position, new PointF(x, y), false); } // スケルトン(mesh.skl)も動かす b.src.position = br.src.position; b.dst.position = br.dst.position; } FTimer.Pause("Fitting:SetCtrl"); // 制御点の移動を反映してメッシュ変形 FTimer.Resume("Fitting:FlushDefomation"); smesh.mesh.FlushDefomation(); FTimer.Pause("Fitting:FlushDefomation"); }
//----------------------------------------------------------------------------------- // FindConnectingSections() //----------------------------------------------------------------------------------- /// <summary> /// skl内の同一のボーンと交差していて、なおかつ向きが逆の切り口を探す /// </summary> private static bool FindConnectingSections( PatchSkeletalMesh smesh1, PatchSkeletalMesh smesh2, PatchSkeleton skl, out PatchSection section1, out PatchSection section2, out PatchSkeletonBone crossingBone) { section1 = new PatchSection(0, -1); section2 = new PatchSection(0, -1); crossingBone = null; if (skl == null) { return(false); } if (smesh1 == null || smesh2 == null) { return(false); } foreach (var bone in skl.bones) { List <PatchSection> sections1, sections2; List <float> dir1, dir2; // 各切り口がboneと交差しているか if (!FindCrossingSection(smesh1, bone, out sections1, out dir1)) { continue; } if (!FindCrossingSection(smesh2, bone, out sections2, out dir2)) { continue; } for (int i = 0; i < sections1.Count; i++) { for (int j = 0; j < sections2.Count; j++) { // 2つの切り口が逆向きか if (dir1[i] * dir2[j] >= 0) { continue; } section1 = sections1[i]; section2 = sections2[j]; crossingBone = bone; return(true); } } } return(false); }
static float CurveHeight(List <PointF> curve, PatchSkeletonBone bone) { PointF x, y; BoneCoordinate(bone, out x, out y); float h = 0; foreach (var p in curve) { h += p.X * y.X + p.Y * y.Y; } return(h / curve.Count); }
static PatchSkeletonBone RefBone(PatchSkeleton refSkeleton, PatchSkeletonBone bone) { PatchSkeletonBone refBone = null; foreach (var b in refSkeleton.bones) { if (bone == b) { refBone = b; break; } } return(refBone); }
// ボーンのdst方向にあるのがpatch2 static void OverlayPatches(PatchSkeletalMesh patch1, PatchSkeletalMesh patch2, PatchSkeleton refSkeleton, PatchSection section1, PatchSection section2, PatchSkeletonBone bone, float border) { PatchSkeletonBone refBone = RefBone(refSkeleton, bone); float min1 = float.MaxValue; float max2 = float.MinValue; List <PatchVertex> path1 = GetPath(patch1); List <PatchVertex> path2 = GetPath(patch2); for (int i = section1.First; i < section1.First + section1.Length; i++) { var pt = path1[FMath.Rem(i, path1.Count)].position; float param = FMath.ParameterOnLine(pt, refBone.src.position, refBone.dst.position); if (float.IsNaN(param)) { continue; } min1 = Math.Min(min1, param); } for (int i = section2.First; i < section2.First + section2.Length; i++) { var pt = path2[FMath.Rem(i, path2.Count)].position; float param = FMath.ParameterOnLine(pt, refBone.src.position, refBone.dst.position); if (float.IsNaN(param)) { continue; } max2 = Math.Max(max2, param); } PointF x, y; BoneCoordinate(refBone, out x, out y); float boneLength = FMath.Distance(refBone.src.position, refBone.dst.position); float dparam = border / boneLength; float delta = (min1 - max2 - dparam) * boneLength; float dx = x.X * delta; float dy = x.Y * delta; // mesh2の頂点・制御点を平行移動する foreach (var v in patch2.mesh.vertices) { v.position = new PointF(v.position.X + dx, v.position.Y + dy); } foreach (var c in patch2.mesh.CopyControlPoints()) { patch2.mesh.TranslateControlPoint(c.position, new PointF(c.position.X + dx, c.position.Y + dy), false); } }
// スケルトン内のbと同じ(jointの名前が同じ)ボーンを探す static PatchSkeletonBone CorrespondingBone(PatchSkeleton skl, PatchSkeletonBone b) { if (skl == null || b == null || skl.bones == null) { return(null); } foreach (var bb in skl.bones) { // オーバーライドしてるjointの名前が同じならtrue if (b == bb) { return(bb); } } return(null); }
// // AdjustPosition() // static void AdjustPosition( PatchSkeletalMesh smesh1, PatchSkeletalMesh smesh2, PatchSkeleton skl, PatchSection section1, PatchSection section2, PatchSkeletonBone bone) { PatchSkeletonBone refBone = null; foreach (var b in skl.bones) { if (bone == b) { refBone = b; break; } } if (refBone == null) { return; } // 切り口の中心とボーンの軸とのずれ(ボーンと垂直な方向について) float height1 = SectionHeight(smesh1, section1, refBone); float height2 = SectionHeight(smesh2, section2, refBone); // ボーンと水平なベクトルと垂直なベクトルをそれぞれx, yとして取得 PointF x, y; CalcBoneCoordinate(refBone, out x, out y); // smesh2の切り口の中心がsmesh1の切り口の中心に重なるようにsmesh2をずらす if (Math.Abs(height1 - height2) > 1e-4) { float dx = y.X * (height1 - height2); float dy = y.Y * (height1 - height2); // mesh2の頂点・制御点を平行移動する foreach (var v in smesh2.mesh.vertices) { v.position = new PointF(v.position.X + dx, v.position.Y + dy); } foreach (var c in smesh2.mesh.CopyControlPoints()) { smesh2.mesh.TranslateControlPoint(c.position, new PointF(c.position.X + dx, c.position.Y + dy), false); } } }
static void CalcBoneCoordinate(PatchSkeletonBone b, out PointF x, out PointF y) { x = new PointF(1, 0); y = new PointF(0, 1); float boneLen = FMath.Distance(b.src.position, b.dst.position); if (boneLen <= 1e-4) { return; } float dx = (b.dst.position.X - b.src.position.X) / boneLen; float dy = (b.dst.position.Y - b.src.position.Y) / boneLen; x = new PointF(dx, dy); y = new PointF(dy, -dx); }
private static PatchSkeleton CombineSkeleton(PatchSkeleton skl1, PatchSkeleton skl2) { PatchSkeleton skl = new PatchSkeleton(); // 関節を追加 foreach (var j in skl1.joints) { skl.joints.Add(new PatchSkeletonJoint(j.name, j.position)); } foreach (var j in skl2.joints) { if (!skl.joints.Any(_j => _j.name == j.name)) { skl.joints.Add(new PatchSkeletonJoint(j.name, j.position)); } } // ボーンを追加 foreach (var b in skl1.bones) { if (skl.joints.Any(j => j.name == b.src.name) && skl.joints.Any(j => j.name == b.dst.name)) { var newBone = new PatchSkeletonBone( skl.joints.First(j => j.name == b.src.name), skl.joints.First(j => j.name == b.dst.name)); skl.bones.Add(newBone); } } foreach (var b in skl2.bones) { if (skl.joints.Any(j => j.name == b.src.name) && skl.joints.Any(j => j.name == b.dst.name)) { var newBone = new PatchSkeletonBone( skl.joints.First(j => j.name == b.src.name), skl.joints.First(j => j.name == b.dst.name)); if (!skl.bones.Contains(newBone)) { skl.bones.Add(newBone); } } } return(skl); }
public PatchSkeletonBone GetNearestBone(PointF point, int threshold, Matrix transform) { float minDist = threshold; PatchSkeletonBone bone = null; for (int i = 0; i < bones.Count; i++) { PointF[] bonePts = new[] { bones[i].src.position, bones[i].dst.position }; transform.TransformPoints(bonePts); float dist = FMath.GetDistanceToLine(point, bonePts[0], bonePts[1]); if (dist < minDist) { minDist = dist; bone = bones[i]; } } return(bone); }
public bool Equals(PatchSkeletonBone t) { if (Object.ReferenceEquals(t, null)) { return(false); } if (Object.ReferenceEquals(this, t)) { return(true); } if (this.GetType() != t.GetType()) { return(false); } if (src == null || dst == null) { return(false); } return(src.name == t.src.name && dst.name == t.dst.name); }
//----------------------------------------------------------------------------------- // Deform() //----------------------------------------------------------------------------------- /// <summary> /// 切り口付近で2つのsmeshが重なるように変形する /// patch2がdst方向 /// </summary> private static void Deform(PatchSkeletalMesh patch1, PatchSkeletalMesh patch2, PatchSkeleton refSkeleton, PatchSection section1, PatchSection section2, PatchSkeletonBone crossingBone) { if (patch1 == null || patch2 == null) { return; } // 各メッシュを大雑把にスケルトンに合わせる PatchSkeletonFitting.Fitting(patch1, refSkeleton); PatchSkeletonFitting.Fitting(patch2, refSkeleton); // TODO: サイズの修正 // 回転はFitting()でやってるから必要ない // 位置の調整 float overlap = OverlapWidth(patch1, patch2, refSkeleton, section1, section2, crossingBone, 100, 45); AdjustHeight(patch1, patch2, refSkeleton, section1, section2, crossingBone); OverlayPatches(patch1, patch2, refSkeleton, section1, section2, crossingBone, overlap); #if _DEBUG PatchSkeletalMeshRenderer.ToBitmap(patch1).Save("output_Connector2/4_patch1.png"); PatchSkeletalMeshRenderer.ToBitmap(patch2).Save("output_Connector2/4_patch2.png"); #endif // メッシュを伸ばして繋げる Expand(patch1, patch2, refSkeleton, section1, section2, crossingBone, overlap, overlap); }
private static float OverlapWidth(PatchSkeletalMesh patch1, PatchSkeletalMesh patch2, PatchSkeleton refSkeleton, PatchSection section1, PatchSection section2, PatchSkeletonBone bone, int maxLength, int maxAngle) { var refBone = RefBone(refSkeleton, bone); var path1 = GetPath(patch1); var curves1 = section2adjCurves(path1, section1); var sorted1 = GetSortedCurves(path1.Select(v => v.position).ToList(), curves1, refBone); var path2 = GetPath(patch2); var curves2 = section2adjCurves(path2, section2); var sorted2 = GetSortedCurves(path2.Select(v => v.position).ToList(), curves2, refBone); float[] param = new float[] { 0, 0, 0, 0 }; var sortedCurves = new[] { sorted1[0], sorted1[1], sorted2[0], sorted2[1], }; float minCos = (float)Math.Cos(Math.PI / 180 * maxAngle); for (int i = 0; i < sortedCurves.Length; i++) { var c = sortedCurves[i]; var path = i < 2 ? path1 : path2; List <PointF> pts = new List <PointF>(); float total = 0; for (int j = c.Count - 1; j >= 0; j--) { var p = c[j]; pts.Add(p); if (pts.Count >= 3 && FMath.GetAngleCos(pts) < minCos) { pts.RemoveAt(pts.Count - 1); break; } if (pts.Count >= 2) { total += FMath.Distance(pts[pts.Count - 2], pts[pts.Count - 1]); } if (total >= maxLength) { pts.RemoveAt(pts.Count - 1); break; } } param[i] = FMath.ParameterOnLine(pts.Last(), refBone.src.position, refBone.dst.position); } float min1 = Enumerable.Range(section1.First, section1.Length).Min(idx => FMath.ParameterOnLine(path1[FMath.Rem(idx, path1.Count)].position, refBone.src.position, refBone.dst.position)); float max1 = Enumerable.Range(section1.First, section1.Length).Max(idx => FMath.ParameterOnLine(path1[FMath.Rem(idx, path1.Count)].position, refBone.src.position, refBone.dst.position)); int idx1 = Enumerable.Range(section1.First, section1.Length).First(idx => max1 == FMath.ParameterOnLine(path1[FMath.Rem(idx, path1.Count)].position, refBone.src.position, refBone.dst.position)); float min2 = Enumerable.Range(section2.First, section2.Length).Min(idx => FMath.ParameterOnLine(path2[FMath.Rem(idx, path2.Count)].position, refBone.src.position, refBone.dst.position)); float max2 = Enumerable.Range(section2.First, section2.Length).Max(idx => FMath.ParameterOnLine(path2[FMath.Rem(idx, path2.Count)].position, refBone.src.position, refBone.dst.position)); float idx2 = Enumerable.Range(section2.First, section2.Length).First(idx => max2 == FMath.ParameterOnLine(path2[FMath.Rem(idx, path2.Count)].position, refBone.src.position, refBone.dst.position)); System.Diagnostics.Debug.Assert(param[0] <= max1); System.Diagnostics.Debug.Assert(param[1] <= max1); System.Diagnostics.Debug.Assert(param[2] >= min2); System.Diagnostics.Debug.Assert(param[3] >= min2); float[] dParams = new[] { Math.Max(0, min1 - param[0]), Math.Max(0, min1 - param[1]), Math.Max(0, param[2] - max2), Math.Max(0, param[3] - max2), }; float minDParam = dParams.Min(); float boarderWidth = minDParam * FMath.Distance(refBone.src.position, refBone.dst.position); return(boarderWidth); }
//----------------------------------------------------------------------------------- // Deform() //----------------------------------------------------------------------------------- /// <summary> /// 切り口付近で2つのsmeshが重なるように変形する /// </summary> private static void Deform(PatchSkeletalMesh smesh1, PatchSkeletalMesh smesh2, PatchSkeleton skl, PatchSection section1, PatchSection section2, PatchSkeletonBone crossingBone) { if (smesh1 == null || smesh2 == null) { return; } // 各メッシュを大雑把にスケルトンに合わせる PatchSkeletonFitting.Fitting(smesh1, skl); PatchSkeletonFitting.Fitting(smesh2, skl); #if _DEBUG PatchSkeletalMeshRenderer.ToBitmap(smesh1, new List <PatchSection>() { section1 }).Save("output/5_1_mesh1_fitting.png"); PatchSkeletalMeshRenderer.ToBitmap(smesh2, new List <PatchSection>() { section2 }).Save("output/5_2_mesh2_fitting.png"); #endif // サイズの修正は手動でやる // 回転はFitting()でやってるから必要ない // 位置の調整 AdjustPosition(smesh1, smesh2, skl, section1, section2, crossingBone); #if _DEBUG PatchSkeletalMeshRenderer.ToBitmap(smesh1, new List <PatchSection>() { section1 }).Save("output/5_3_mesh1_AdjustPosition.png"); PatchSkeletalMeshRenderer.ToBitmap(smesh2, new List <PatchSection>() { section2 }).Save("output/5_4_mesh2_AdjustPosition.png"); #endif // メッシュを伸ばして繋げる Expand(smesh1, smesh2, skl, section1, section2, crossingBone); #if _DEBUG PatchSkeletalMeshRenderer.ToBitmap(smesh1, new List <PatchSection>() { section1 }).Save("output/5_5_mesh1_ExpandPatches.png"); PatchSkeletalMeshRenderer.ToBitmap(smesh2, new List <PatchSection>() { section2 }).Save("output/5_6_mesh2_ExpandPatches.png"); #endif }
// // Expand() // /// <summary> /// smesh1, smesh2の輪郭をずらして重ねる。輪郭に制御点をおいてARAPする /// </summary> static void Expand(PatchSkeletalMesh smesh1, PatchSkeletalMesh smesh2, PatchSkeleton skl, PatchSection section1, PatchSection section2, PatchSkeletonBone bone) { List <PatchVertex> rawPath1 = GetPath(smesh1); List <PatchVertex> rawPath2 = GetPath(smesh2); List <PointF> path1 = rawPath1.Select(v => v.position).ToList(); List <PointF> path2 = rawPath2.Select(v => v.position).ToList(); // // 輪郭を変形できるように制御点を作り直す // // smesh1 smesh1.mesh.ClearControlPoints(); foreach (var v in rawPath1) { smesh1.mesh.AddControlPoint(v.position, v.orgPosition); } smesh1.mesh.BeginDeformation(); // smesh2 smesh2.mesh.ClearControlPoints(); foreach (var v in rawPath2) { smesh2.mesh.AddControlPoint(v.position, v.orgPosition); } smesh2.mesh.BeginDeformation(); // // 切り口に隣接する2曲線を各切り口について取得し、これらが重なるように輪郭をずらす // // 切り口に隣接する2曲線をそれぞれ取得 var rawCurves1 = SectionToAdjuscentCurves(path1, section1, 5, 30); var rawCurves2 = SectionToAdjuscentCurves(path2, section2, 5, 30); if (rawCurves1 == null || rawCurves2 == null) { return; } PatchSkeletonBone refBone = null; foreach (var b in skl.bones) { if (bone == b) { refBone = b; break; } } // curves1, curves2の第一要素、第二要素がそれぞれ向かい合う(ボーンにとって同じ側の)切り口となるように並び替える var curves1 = GetSortedCurves(path1, rawCurves1, refBone); var curves2 = GetSortedCurves(path2, rawCurves2, refBone); if (curves1.Count != 2 || curves2.Count != 2) { return; } // curves1, curves2の移動履歴を記録 List <Tuple <PointF, PointF> > move1 = new List <Tuple <PointF, PointF> >(); List <Tuple <PointF, PointF> > move2 = new List <Tuple <PointF, PointF> >(); // 対応する曲線間で2点がかぶる(同じ座標になる)ように変形。 for (int i = 0; i < 2; i++) { var p1 = curves1[i].First(); var v1 = new PointF(p1.X - curves1[i].Last().X, p1.Y - curves1[i].Last().Y); var p2 = curves2[i].First(); var v2 = new PointF(curves2[i].Last().X - p2.X, curves2[i].Last().Y - p2.Y); // 2点かぶらせる int cnt = curves1[i].Count + curves2[i].Count - 2; if (cnt <= 1) { continue; } for (int j = 0; j < curves1[i].Count; j++) { PointF to = FMath.HelmitteInterporate(p1, v1, p2, v2, (float)j / (cnt - 1)); if (j == curves1[i].Count - 1) { move1.Add(new Tuple <PointF, PointF>(curves1[i][j], to)); } smesh1.mesh.TranslateControlPoint(curves1[i][j], to, false); } for (int j = 0; j < curves2[i].Count; j++) { PointF to = FMath.HelmitteInterporate(p1, v1, p2, v2, (float)(-j + cnt - 1) / (cnt - 1)); if (j == curves2[i].Count - 1) { move2.Add(new Tuple <PointF, PointF>(curves2[i][j], to)); } smesh2.mesh.TranslateControlPoint(curves2[i][j], to, false); } } // // 各曲線の動きに合わせて切り口を動かす // List <PointF> sections1 = new List <PointF>(); for (int i = section1.First + 1; i < section1.First + section1.Length - 1; i++) { sections1.Add(path1[FMath.Rem(i, path1.Count)]); } List <PointF> newSection1 = ARAPDeformation.ARAPDeformation.Deform(sections1, move1); if (newSection1.Count == sections1.Count) { for (int i = 0; i < newSection1.Count; i++) { smesh1.mesh.TranslateControlPoint(sections1[i], newSection1[i], false); } } List <PointF> sections2 = new List <PointF>(); for (int i = section2.First + 1; i < section2.First + section2.Length - 1; i++) { sections2.Add(path2[FMath.Rem(i, path2.Count)]); } List <PointF> newSection2 = ARAPDeformation.ARAPDeformation.Deform(sections2, move2); if (newSection2.Count == sections2.Count) { for (int i = 0; i < newSection2.Count; i++) { smesh2.mesh.TranslateControlPoint(sections2[i], newSection2[i], false); } } // // 変形 // smesh1.mesh.FlushDefomation(); smesh2.mesh.FlushDefomation(); // // 変形終了 // smesh1.mesh.EndDeformation(); smesh2.mesh.EndDeformation(); }
// 切り口に近づく向きに曲線の点をついかする static List <List <PointF> > GetSortedCurves(List <PointF> path, Tuple <CharacterRange, CharacterRange> ranges, PatchSkeletonBone baseBone) { List <List <PointF> > curves = new List <List <PointF> >(); foreach (var range in new[] { ranges.Item1, ranges.Item2 }) { var ls = new List <PointF>(); for (int i = range.First; i < range.First + range.Length; i++) { ls.Add(path[FMath.Rem(i, path.Count)]); } curves.Add(ls); } // 向きを揃える curves[1].Reverse(); // ボーンに対する位置関係を揃える PointF pt = path[FMath.Rem(ranges.Item2.First, path.Count)]; if (FMath.GetSide(pt, baseBone.src.position, baseBone.dst.position) < 0) { var ls = curves[0]; curves[0] = curves[1]; curves[1] = ls; } return(curves); }
// // Expand() // /// <summary> /// smesh1, smesh2の輪郭をずらして重ねる。輪郭に制御点をおいてARAPする /// curveOffset, curveLengthは切り口付近の補間に使う部分曲線 /// </summary> static void Expand( PatchSkeletalMesh patch1, PatchSkeletalMesh patch2, PatchSkeleton refSkeleton, PatchSection section1, PatchSection section2, PatchSkeletonBone bone, float curveLength, float curveBuffer) { // メッシュを固定 patch1.mesh.FreezeMesh(true); patch2.mesh.FreezeMesh(true); List <PatchVertex> rawPath1 = GetPath(patch1); List <PatchVertex> rawPath2 = GetPath(patch2); List <PointF> path1 = rawPath1.Select(v => v.position).ToList(); List <PointF> path2 = rawPath2.Select(v => v.position).ToList(); // 切り口に隣接する2曲線をそれぞれ取得 var rawCurves1 = section2adjCurves(rawPath1, section1); var rawCurves2 = section2adjCurves(rawPath2, section2); if (rawCurves1 == null || rawCurves2 == null) { return; } var trimCurves1 = TrimCurves(rawPath1, rawCurves1, 0, curveLength); var trimCurves2 = TrimCurves(rawPath2, rawCurves2, 0, curveLength); PatchSkeletonBone refBone = RefBone(refSkeleton, bone); var sorted1 = GetSortedCurves(path1, trimCurves1, refBone); var sorted2 = GetSortedCurves(path2, trimCurves2, refBone); if (sorted1.Count != 2 || sorted2.Count != 2) { return; } // patch1の制御点 patch1.mesh.ClearControlPoints(); for (int i = 0; i < 2; i++) { foreach (var p in sorted1[i]) { patch1.mesh.AddControlPoint(p, p); } } patch1.mesh.BeginDeformation(); // patch2の制御点 patch2.mesh.ClearControlPoints(); for (int i = 0; i < 2; i++) { foreach (var p in sorted2[i]) { patch2.mesh.AddControlPoint(p, p); } } patch2.mesh.BeginDeformation(); // 第一要素、第二要素がそれぞれボーンにとって同じ側の切り口となるように並び替える // 切り口に近づく向きに点が並んでいる float h10 = CurveHeight(sorted1[0], refBone); float h11 = CurveHeight(sorted1[1], refBone); float h20 = CurveHeight(sorted2[0], refBone); float h21 = CurveHeight(sorted2[1], refBone); var curve10 = sorted1[0].ToList(); var curve11 = sorted1[1].ToList(); var curve20 = sorted2[0].ToList(); var curve21 = sorted2[1].ToList(); // curveの重心を揃える PointF x, y; BoneCoordinate(refBone, out x, out y); float c0 = (h10 + h20) * 0.5f; float dy10 = c0 - h10; for (int i = 0; i < curve10.Count; i++) { curve10[i] = new PointF(curve10[i].X + dy10 * y.X, curve10[i].Y + dy10 * y.Y); } float dy11 = c0 - h20; for (int i = 0; i < curve20.Count; i++) { curve20[i] = new PointF(curve20[i].X + dy11 * y.X, curve20[i].Y + dy11 * y.Y); } float c1 = (h11 + h21) * 0.5f; float dy20 = c1 - h11; for (int i = 0; i < curve11.Count; i++) { curve11[i] = new PointF(curve11[i].X + dy20 * y.X, curve11[i].Y + dy20 * y.Y); } float dy21 = c1 - h21; for (int i = 0; i < curve21.Count; i++) { curve21[i] = new PointF(curve21[i].X + dy21 * y.X, curve21[i].Y + dy21 * y.Y); } // curveをエルミート保管して繋げる var ends0 = new[] { curve10[0], curve10.Last(), curve20[0], curve20.Last() }; int min0, max0; FMath.GetMinElement(ends0, p => FMath.ParameterOnLine(p, refBone.src.position, refBone.dst.position), out min0); FMath.GetMinElement(ends0, p => - FMath.ParameterOnLine(p, refBone.src.position, refBone.dst.position), out max0); var ends1 = new[] { curve11[0], curve11.Last(), curve21[0], curve21.Last() }; int min1, max1; FMath.GetMinElement(ends1, p => FMath.ParameterOnLine(p, refBone.src.position, refBone.dst.position), out min1); FMath.GetMinElement(ends1, p => - FMath.ParameterOnLine(p, refBone.src.position, refBone.dst.position), out max1); PointF[] es = new[] { ends0[min0], ends1[min1], ends0[max0], ends1[max1], }; PointF[] vs = new[] { new PointF(curve10[0].X - curve10[curve10.Count - 1].X, curve10[0].Y - curve10[curve10.Count - 1].Y), new PointF(curve11[0].X - curve11[curve11.Count - 1].X, curve11[0].Y - curve11[curve11.Count - 1].Y), new PointF(-(curve20[0].X - curve20[curve20.Count - 1].X), -(curve20[0].Y - curve20[curve20.Count - 1].Y)), new PointF(-(curve21[0].X - curve21[curve21.Count - 1].X), -(curve21[0].Y - curve21[curve21.Count - 1].Y)), }; float[] ps = new[] { FMath.ParameterOnLine(es[0], refBone.src.position, refBone.dst.position), FMath.ParameterOnLine(es[1], refBone.src.position, refBone.dst.position), FMath.ParameterOnLine(es[2], refBone.src.position, refBone.dst.position), FMath.ParameterOnLine(es[3], refBone.src.position, refBone.dst.position), }; for (int i = 0; i < 2; i++) { var e1 = es[i]; var v1 = vs[i]; float param1 = ps[i]; var e2 = es[i + 2]; var v2 = vs[i + 2]; float param2 = ps[i + 2]; if (Math.Abs(param2 - param1) <= 1e-4) { continue; } float paramRatio = 1 / (param2 - param1); for (int j = 0; j < sorted1[i].Count; j++) { float param = FMath.ParameterOnLine(sorted1[i][j], refBone.src.position, refBone.dst.position); float t = (param - param1) * paramRatio; PointF to = FMath.HelmitteInterporate(e1, v1, e2, v2, t); patch1.mesh.TranslateControlPoint(sorted1[i][j], to, false); } for (int j = 0; j < sorted2[i].Count; j++) { float param = FMath.ParameterOnLine(sorted2[i][j], refBone.src.position, refBone.dst.position); float t = (param - param1) * paramRatio; PointF to = FMath.HelmitteInterporate(e1, v1, e2, v2, t); patch2.mesh.TranslateControlPoint(sorted2[i][j], to, false); } } // 変形 patch1.mesh.FlushDefomation(); patch2.mesh.FlushDefomation(); // 変形終了 patch1.mesh.EndDeformation(); patch2.mesh.EndDeformation(); }