Imported model's mesh reading differently than built-in unity meshes
I'm trying to make a system that can smoothly blend one mesh into another mesh. It works great for the built-in unity meshes (sphere, cube, cylinder, and capsule), but absolutely goes mad when I try to do so to an imported fbx file from blender.
My thought is that the imported fbx meshes are rendered differently, but I've been unable to find that information anywhere if it is the case.
Here is the code I'm using to convert the meshes to something usable by me.
public Vector3[] ConvertMeshVerticesToSphericalCast(Vector3[] sphereVertices, Vector3[] meshToConvertVertices, int[] meshToConvertTriangles)
{
// Return vertices
Vector3[] castVertices = new Vector3[sphereVertices.Length];
// Current index of the
int index = 0;
foreach (Vector3 sphVert in sphereVertices)
{
Vector3 rayDir = sphVert.normalized;
float smallDist = float.MaxValue;
for (int i = 2; i < meshToConvertTriangles.Length; i+=3)
{
// Get each of the points of this triangle
Vector3 qA = meshToConvertVertices[meshToConvertTriangles[i - 2]];
Vector3 qB = meshToConvertVertices[meshToConvertTriangles[i - 1]];
Vector3 qC = meshToConvertVertices[meshToConvertTriangles[i]];
// Calculate normal of the triangle
Vector3 triN = -Vector3.Cross((qA - qC), (qB - qC)).normalized;
// Get the denominator first to avoid dividing by 0
float denom = Vector3.Dot(triN, rayDir);
if (Mathf.Abs(denom) > Mathf.Epsilon)
{
// Get the center of the plane
Vector3 qCenter = (1.0f / 3) * (qA + qB + qC);
// Get the distance from the plane the current vertex is
float t = Vector3.Dot((qCenter - sphVert), triN) / denom;
// If the distance is positive and smaller than the current smallest, its the new smallest
if (t >= 0 && t < smallDist)
{
smallDist = t;
}
}
}
// Use the distance to update the
castVertices[index] = smallDist * rayDir + sphVert;
++index;
}
return castVertices;
}
Your answer
Follow this Question
Related Questions
Why does my generated mesh have an offset? 0 Answers
Parts of my mesh aren't rendering and do other goofy things 1 Answer
Terrain tree from 2 crossed sprites/quads 1 Answer
Strange lighting behaviour when generating a mesh. 0 Answers
I need help in "stretching an object like rubber" in Unity3D 0 Answers