Hi,
I'd like to ask how can I get a normal of hit point using DXEngine raycast.
I can obtain the hit result by using:
DXRayHitTestResult rayHitResult = DXScene.GetClosestHitObject(pickRay, rootNode);
It is possible to get rayHitResult.HitPosition and rayHitResult.TriangleIndex. But to what does the rayHitResult.TriangleIndex point to? MeshBase that can be accessed from rayHitResult.HitSceneNode does only have a buffer. I can cast it to SimpleMesh<PositionNormal> (or any other that I will use). Here it is possible to access IndexBufferArray.
The issue I have is that the TriangleIndex is not divisible by 3 (I expect that the IndexBufferArray is a triangle soup). So it can't be start index of the triangle. Maybe it's triangle strip?
TLDR: How can I get a normal from DXRayHitTestResult? Attached code is based on assumptions made above.
Best regards,
Janovsky Roman
Edit: If the VertexBufferArray is accessed using directly t0, t1, and t2, it produces correct results half of a time, but can be outside of array range.
I'd like to ask how can I get a normal of hit point using DXEngine raycast.
I can obtain the hit result by using:
DXRayHitTestResult rayHitResult = DXScene.GetClosestHitObject(pickRay, rootNode);
It is possible to get rayHitResult.HitPosition and rayHitResult.TriangleIndex. But to what does the rayHitResult.TriangleIndex point to? MeshBase that can be accessed from rayHitResult.HitSceneNode does only have a buffer. I can cast it to SimpleMesh<PositionNormal> (or any other that I will use). Here it is possible to access IndexBufferArray.
The issue I have is that the TriangleIndex is not divisible by 3 (I expect that the IndexBufferArray is a triangle soup). So it can't be start index of the triangle. Maybe it's triangle strip?
TLDR: How can I get a normal from DXRayHitTestResult? Attached code is based on assumptions made above.
Best regards,
Janovsky Roman
Code:
MeshBase mesh = (rayHitResult.HitSceneNode as MeshObjectNode).Mesh;
int t0 = rayHitResult.TriangleIndex;
int t1 = rayHitResult.TriangleIndex + 1;
int t2 = rayHitResult.TriangleIndex + 2;
if (mesh is SimpleMesh<PositionNormal> smPN)
{
int i0 = smPN.IndexBufferArray[t0];
int i1 = smPN.IndexBufferArray[t1];
int i2 = smPN.IndexBufferArray[t2];
var p0 = smPN.VertexBufferArray[i0].Position;
var p1 = smPN.VertexBufferArray[i1].Position;
var p2 = smPN.VertexBufferArray[i2].Position;
// Compute the normal (we want to stick to the plane that is the triangle)
var e1 = p1 - p0;
var e2 = p2 - p0;
var n = Vector3.Cross(e1, e2);
n.Normalize();
forward = n;
}
Edit: If the VertexBufferArray is accessed using directly t0, t1, and t2, it produces correct results half of a time, but can be outside of array range.