[개발/최익필] VLerp -> Vector3f.Lerp

This commit is contained in:
ikpil 2023-05-17 14:30:41 +09:00
parent 575d762c15
commit 63427dce0a
8 changed files with 44 additions and 42 deletions

View File

@ -125,14 +125,6 @@ namespace DotRecast.Core
); );
} }
public static Vector3f VLerp(Vector3f v1, Vector3f v2, float t)
{
return new Vector3f(
v1.x + (v2.x - v1.x) * t,
v1.y + (v2.y - v1.y) * t,
v1.z + (v2.z - v1.z) * t
);
}
public static Vector3f VAdd(Vector3f v1, Vector3f v2) public static Vector3f VAdd(Vector3f v1, Vector3f v2)
{ {
@ -323,7 +315,7 @@ namespace DotRecast.Core
return d < thresholdSqr; return d < thresholdSqr;
} }
/// Derives the xz-plane 2D perp product of the two vectors. (uz*vx - ux*vz) /// Derives the xz-plane 2D perp product of the two vectors. (uz*vx - ux*vz)
/// @param[in] u The LHV vector [(x, y, z)] /// @param[in] u The LHV vector [(x, y, z)]
/// @param[in] v The RHV vector [(x, y, z)] /// @param[in] v The RHV vector [(x, y, z)]

View File

@ -147,6 +147,18 @@ namespace DotRecast.Core
return hash; return hash;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Normalize()
{
float d = (float)(1.0f / Math.Sqrt(RcMath.Sqr(x) + RcMath.Sqr(y) + RcMath.Sqr(z)));
if (d != 0)
{
x *= d;
y *= d;
z *= d;
}
}
public static bool operator ==(Vector3f left, Vector3f right) public static bool operator ==(Vector3f left, Vector3f right)
{ {
return left.Equals(right); return left.Equals(right);
@ -174,15 +186,13 @@ namespace DotRecast.Core
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Normalize() public static Vector3f Lerp(Vector3f v1, Vector3f v2, float t)
{ {
float d = (float)(1.0f / Math.Sqrt(RcMath.Sqr(x) + RcMath.Sqr(y) + RcMath.Sqr(z))); return new Vector3f(
if (d != 0) v1.x + (v2.x - v1.x) * t,
{ v1.y + (v2.y - v1.y) * t,
x *= d; v1.z + (v2.z - v1.z) * t
y *= d; );
z *= d;
}
} }
} }
} }

View File

@ -1355,12 +1355,12 @@ namespace DotRecast.Detour.Crowd
if (anim.t < ta) if (anim.t < ta)
{ {
float u = Tween(anim.t, 0.0f, ta); float u = Tween(anim.t, 0.0f, ta);
ag.npos = VLerp(anim.initPos, anim.startPos, u); ag.npos = Vector3f.Lerp(anim.initPos, anim.startPos, u);
} }
else else
{ {
float u = Tween(anim.t, ta, tb); float u = Tween(anim.t, ta, tb);
ag.npos = VLerp(anim.startPos, anim.endPos, u); ag.npos = Vector3f.Lerp(anim.startPos, anim.endPos, u);
} }
// Update velocity. // Update velocity.

View File

@ -32,7 +32,7 @@ namespace DotRecast.Detour.Extras.Jumplink
GroundSample s = new GroundSample(); GroundSample s = new GroundSample();
seg.gsamples[i] = s; seg.gsamples[i] = s;
Vector3f pt = VLerp(seg.p, seg.q, u); Vector3f pt = Vector3f.Lerp(seg.p, seg.q, u);
Tuple<bool, float> height = heightFunc.Invoke(pt, seg.height); Tuple<bool, float> height = heightFunc.Invoke(pt, seg.height);
s.p.x = pt.x; s.p.x = pt.x;
s.p.y = height.Item2; s.p.y = height.Item2;

View File

@ -836,8 +836,8 @@ namespace DotRecast.Detour
var ext = new Vector3f() var ext = new Vector3f()
{ {
x = targetCon.rad, x = targetCon.rad,
y = target.data.header.walkableClimb, y = target.data.header.walkableClimb,
z = targetCon.rad z = targetCon.rad
}; };
@ -1070,8 +1070,8 @@ namespace DotRecast.Detour
var ext = new Vector3f() var ext = new Vector3f()
{ {
x = con.rad, x = con.rad,
y = tile.data.header.walkableClimb, y = tile.data.header.walkableClimb,
z = con.rad, z = con.rad,
}; };
@ -1160,7 +1160,7 @@ namespace DotRecast.Detour
int index = poly.verts[tris[ti + j]] * 3; int index = poly.verts[tris[ti + j]] * 3;
v[j] = new Vector3f v[j] = new Vector3f
{ {
x = tile.data.verts[index], x = tile.data.verts[index],
y = tile.data.verts[index + 1], y = tile.data.verts[index + 1],
z = tile.data.verts[index + 2] z = tile.data.verts[index + 2]
}; };
@ -1170,7 +1170,7 @@ namespace DotRecast.Detour
int index = (pd.vertBase + (tris[ti + j] - poly.vertCount)) * 3; int index = (pd.vertBase + (tris[ti + j] - poly.vertCount)) * 3;
v[j] = new Vector3f v[j] = new Vector3f
{ {
x = tile.data.detailVerts[index], x = tile.data.detailVerts[index],
y = tile.data.detailVerts[index + 1], y = tile.data.detailVerts[index + 1],
z = tile.data.detailVerts[index + 2] z = tile.data.detailVerts[index + 2]
}; };
@ -1226,7 +1226,7 @@ namespace DotRecast.Detour
} }
} }
return VLerp(pmin, pmax, tmin); return Vector3f.Lerp(pmin, pmax, tmin);
} }
public float? GetPolyHeight(MeshTile tile, Poly poly, Vector3f pos) public float? GetPolyHeight(MeshTile tile, Poly poly, Vector3f pos)
@ -1267,7 +1267,7 @@ namespace DotRecast.Detour
int index = poly.verts[tile.data.detailTris[t + k]] * 3; int index = poly.verts[tile.data.detailTris[t + k]] * 3;
v[k] = new Vector3f v[k] = new Vector3f
{ {
x = tile.data.verts[index], x = tile.data.verts[index],
y = tile.data.verts[index + 1], y = tile.data.verts[index + 1],
z = tile.data.verts[index + 2] z = tile.data.verts[index + 2]
}; };
@ -1277,7 +1277,7 @@ namespace DotRecast.Detour
int index = (pd.vertBase + (tile.data.detailTris[t + k] - poly.vertCount)) * 3; int index = (pd.vertBase + (tile.data.detailTris[t + k] - poly.vertCount)) * 3;
v[k] = new Vector3f v[k] = new Vector3f
{ {
x = tile.data.detailVerts[index], x = tile.data.detailVerts[index],
y = tile.data.detailVerts[index + 1], y = tile.data.detailVerts[index + 1],
z = tile.data.detailVerts[index + 2] z = tile.data.detailVerts[index + 2]
}; };
@ -1344,7 +1344,7 @@ namespace DotRecast.Detour
i = poly.verts[1] * 3; i = poly.verts[1] * 3;
var v1 = new Vector3f { x = tile.data.verts[i], y = tile.data.verts[i + 1], z = tile.data.verts[i + 2] }; var v1 = new Vector3f { x = tile.data.verts[i], y = tile.data.verts[i + 1], z = tile.data.verts[i + 2] };
Tuple<float, float> dt = DistancePtSegSqr2D(pos, v0, v1); Tuple<float, float> dt = DistancePtSegSqr2D(pos, v0, v1);
return new ClosestPointOnPolyResult(false, VLerp(v0, v1, dt.Item2)); return new ClosestPointOnPolyResult(false, Vector3f.Lerp(v0, v1, dt.Item2));
} }
// Outside poly that is not an offmesh connection. // Outside poly that is not an offmesh connection.

View File

@ -364,7 +364,7 @@ namespace DotRecast.Detour
// Cost // Cost
if (neighbourNode.flags == 0) if (neighbourNode.flags == 0)
{ {
neighbourNode.pos = VLerp(va, vb, 0.5f); neighbourNode.pos = Vector3f.Lerp(va, vb, 0.5f);
} }
float total = bestNode.total + VDist(bestNode.pos, neighbourNode.pos); float total = bestNode.total + VDist(bestNode.pos, neighbourNode.pos);
@ -1505,7 +1505,7 @@ namespace DotRecast.Detour
if (null != interect) if (null != interect)
{ {
float t = interect.Item2; float t = interect.Item2;
var pt = VLerp(left, right, t); var pt = Vector3f.Lerp(left, right, t);
stat = AppendVertex(pt, 0, path[i + 1], straightPath, maxStraightPath); stat = AppendVertex(pt, 0, path[i + 1], straightPath, maxStraightPath);
if (!stat.IsInProgress()) if (!stat.IsInProgress())
{ {
@ -1826,7 +1826,7 @@ namespace DotRecast.Detour
bestPos = startPos; bestPos = startPos;
// Search constraints // Search constraints
var searchPos = VLerp(startPos, endPos, 0.5f); var searchPos = Vector3f.Lerp(startPos, endPos, 0.5f);
float searchRadSqr = Sqr(VDist(startPos, endPos) / 2.0f + 0.001f); float searchRadSqr = Sqr(VDist(startPos, endPos) / 2.0f + 0.001f);
float[] verts = new float[m_nav.GetMaxVertsPerPoly() * 3]; float[] verts = new float[m_nav.GetMaxVertsPerPoly() * 3];
@ -2134,7 +2134,7 @@ namespace DotRecast.Detour
t = Clamp(interect.Item2, 0.1f, 0.9f); t = Clamp(interect.Item2, 0.1f, 0.9f);
} }
Vector3f pt = VLerp(left, right, t); Vector3f pt = Vector3f.Lerp(left, right, t);
return Results.Success(pt); return Results.Success(pt);
} }
@ -2571,7 +2571,7 @@ namespace DotRecast.Detour
// Cost // Cost
if (neighbourNode.flags == 0) if (neighbourNode.flags == 0)
{ {
neighbourNode.pos = VLerp(va, vb, 0.5f); neighbourNode.pos = Vector3f.Lerp(va, vb, 0.5f);
} }
float cost = filter.GetCost(bestNode.pos, neighbourNode.pos, parentRef, parentTile, parentPoly, bestRef, float cost = filter.GetCost(bestNode.pos, neighbourNode.pos, parentRef, parentTile, parentPoly, bestRef,
@ -2763,7 +2763,7 @@ namespace DotRecast.Detour
// Cost // Cost
if (neighbourNode.flags == 0) if (neighbourNode.flags == 0)
{ {
neighbourNode.pos = VLerp(va, vb, 0.5f); neighbourNode.pos = Vector3f.Lerp(va, vb, 0.5f);
} }
float cost = filter.GetCost(bestNode.pos, neighbourNode.pos, parentRef, parentTile, parentPoly, bestRef, float cost = filter.GetCost(bestNode.pos, neighbourNode.pos, parentRef, parentTile, parentPoly, bestRef,

View File

@ -236,7 +236,7 @@ public class JumpLinkBuilderTool : Tool
{ {
GroundSample s = link.start.gsamples[i]; GroundSample s = link.start.gsamples[i];
float u = i / (float)(link.start.gsamples.Length - 1); float u = i / (float)(link.start.gsamples.Length - 1);
Vector3f spt = VLerp(link.start.p, link.start.q, u); Vector3f spt = Vector3f.Lerp(link.start.p, link.start.q, u);
int col = DuRGBA(48, 16, 16, 255); // DuRGBA(255,(s->flags & 4)?255:0,0,255); int col = DuRGBA(48, 16, 16, 255); // DuRGBA(255,(s->flags & 4)?255:0,0,255);
float off = 0.1f; float off = 0.1f;
if (!s.validHeight) if (!s.validHeight)
@ -256,7 +256,7 @@ public class JumpLinkBuilderTool : Tool
{ {
GroundSample s = link.start.gsamples[i]; GroundSample s = link.start.gsamples[i];
float u = i / (float)(link.start.gsamples.Length - 1); float u = i / (float)(link.start.gsamples.Length - 1);
Vector3f spt = VLerp(link.start.p, link.start.q, u); Vector3f spt = Vector3f.Lerp(link.start.p, link.start.q, u);
int col = DuRGBA(255, 255, 255, 255); int col = DuRGBA(255, 255, 255, 255);
float off = 0; float off = 0;
if (s.validHeight) if (s.validHeight)
@ -276,7 +276,7 @@ public class JumpLinkBuilderTool : Tool
{ {
GroundSample s = end.gsamples[i]; GroundSample s = end.gsamples[i];
float u = i / (float)(end.gsamples.Length - 1); float u = i / (float)(end.gsamples.Length - 1);
Vector3f spt = VLerp(end.p, end.q, u); Vector3f spt = Vector3f.Lerp(end.p, end.q, u);
int col = DuRGBA(48, 16, 16, 255); // DuRGBA(255,(s->flags & 4)?255:0,0,255); int col = DuRGBA(48, 16, 16, 255); // DuRGBA(255,(s->flags & 4)?255:0,0,255);
float off = 0.1f; float off = 0.1f;
if (!s.validHeight) if (!s.validHeight)
@ -295,7 +295,7 @@ public class JumpLinkBuilderTool : Tool
{ {
GroundSample s = end.gsamples[i]; GroundSample s = end.gsamples[i];
float u = i / (float)(end.gsamples.Length - 1); float u = i / (float)(end.gsamples.Length - 1);
Vector3f spt = VLerp(end.p, end.q, u); Vector3f spt = Vector3f.Lerp(end.p, end.q, u);
int col = DuRGBA(255, 255, 255, 255); int col = DuRGBA(255, 255, 255, 255);
float off = 0; float off = 0;
if (s.validHeight) if (s.validHeight)

View File

@ -380,7 +380,7 @@ public class TestNavmeshTool : Tool
else else
{ {
// Hit // Hit
m_hitPos = VLerp(m_spos, m_epos, hit.result.t); m_hitPos = Vector3f.Lerp(m_spos, m_epos, hit.result.t);
m_hitNormal = hit.result.hitNormal; m_hitNormal = hit.result.hitNormal;
m_hitResult = true; m_hitResult = true;
} }
@ -949,7 +949,7 @@ public class TestNavmeshTool : Tool
private Vector3f GetPolyCenter(NavMesh navMesh, long refs) private Vector3f GetPolyCenter(NavMesh navMesh, long refs)
{ {
Vector3f center = Vector3f.Zero; Vector3f center = Vector3f.Zero;
Result<Tuple<MeshTile, Poly>> tileAndPoly = navMesh.GetTileAndPolyByRef(refs); Result<Tuple<MeshTile, Poly>> tileAndPoly = navMesh.GetTileAndPolyByRef(refs);
if (tileAndPoly.Succeeded()) if (tileAndPoly.Succeeded())
{ {