【发布时间】:2021-10-11 05:54:23
【问题描述】:
我收到以下代码的“索引超出范围”错误。我已经注释了发生错误的行。此代码附加到由一系列 .obj 文件组成的 Gameobject。它基本上在点击播放时按顺序切换到下一个连续的 .obj 文件,从而使其显示为动画。我正在尝试翻阅这些 .obj 文件,并且在 Raycasting 上,我正在尝试获取命中点的顶点和法线。任何帮助将不胜感激,谢谢。
void Update()
{
// on clicking the left mouse button
if (Input.GetMouseButtonDown(2))
{
// get the currently updated mesh
meshRenderer = GetComponentInChildren<MeshRenderer>();
meshCollider.sharedMesh = null;
meshCollider.sharedMesh = mesh;
// get mesh data
vertices = mesh.vertices;
normals = mesh.normals;
triangles = mesh.triangles;
// transform interpolated point from local to world space and set attached object to that position
hitTransform = meshCollider.transform;
//Ray cast done to identify point on human mesh(on mouse click) and assign sensor to the identified point
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (meshCollider.Raycast(ray, out hit, 200.0f))
{
// store index of the triangle (base index = index*3) and barycentric coordinates of the hitpoint
Debug.Log("hit");
triBaseIndex = hit.triangleIndex * 3;
baryCenter = hit.barycentricCoordinate;
v0 = vertices[triangles[triBaseIndex]];
v1 = vertices[triangles[triBaseIndex + 1]];
v2 = vertices[triangles[triBaseIndex + 2]];
}
interpolated_Position_v = v0 * baryCenter.x + v1 * baryCenter.y + v2 * baryCenter.z;
float length_1 = Vector3.Distance(v0, interpolated_Position_v);
float length_2 = Vector3.Distance(v1, interpolated_Position_v);
float length_3 = Vector3.Distance(v2, interpolated_Position_v);
if((length_1 > length_2) && (length_1 > length_3))
{
refIndex = triBaseIndex;
}
else if((length_2 > length_1) && (length_2 > length_3))
{
refIndex = triBaseIndex + 1;
}
else if((length_3 > length_1) && (length_3 > length_2))
{
refIndex = triBaseIndex + 2;
}
}
// get triangle vertices
p0 = vertices[triangles[triBaseIndex]]; //**THE ERROR POPS UP ON THIS LINE**
p1 = vertices[triangles[triBaseIndex + 1]];
p2 = vertices[triangles[triBaseIndex + 2]];
// get normals at triangles vertices
n0 = normals[triangles[triBaseIndex]];
n1 = normals[triangles[triBaseIndex + 1]];
n2 = normals[triangles[triBaseIndex + 2]];
// interpolate position using the barycentric coordinate of the hitpoint to get accurate position within the triangle
interpolatedPosition = p0 * baryCenter.x + p1 * baryCenter.y + p2 * baryCenter.z;
// interpolate normals using the barycentric coordinate of the hitpoint & normalize it
interpolatedNormal = (n0 * baryCenter.x + n1 * baryCenter.y + n2 * baryCenter.z).normalized;
interpolatedPositionInWorldSpace = hitTransform.TransformPoint(interpolatedPosition);
// determining the normal of the 3 vertices and assigning
interpolatedNormalInWorldSpace = hitTransform.TransformPoint(interpolatedNormal);
right_vector = vertices[triangles[refIndex]] - interpolatedPosition;
objectToAttach.transform.position = interpolatedPositionInWorldSpace;
【问题讨论】:
-
什么是
triBaseIndex,什么是triangles[triBaseIndex],每个数组的长度是多少?如果您现在知道,请在运行时使用调试器检查它。 -
在raycasting上获取mesh三角形的顶点与下面的代码基本相同 - Vector3 p0 = vertices[triangles[hit.triangleIndex * 3 + 0]]; Vector3 p1 = 顶点[三角形[hit.triangleIndex * 3 + 1]]; Vector3 p2 = vertices[triangles[hit.triangleIndex * 3 + 2]];
标签: c# unity3d indexoutofboundsexception