Unity将模型的世界坐标下的中心点归到模型中心
效果对比:
调整前:
调整后:
需求:由于建模时的疏忽或者网上找的一些测试模型不规范,会出现导入unity中,模型的世界坐标下的中心点与模型实际的位置差出很多。
方法一:改变模型原文件的网格位置,通过添加空父物体调整
#region 重置模型父物体和父父物体的中心点
Bounds bounds = new Bounds(Vector3.zero, Vector3.zero);
//获取所有MeshRenderer 包括子物体
var mrs = transform.GetComponentsInChildren<MeshRenderer>(true);
Vector3 center = Vector3.zero;
for (int i = 0; i < mrs.Length; i++)
{
center += mrs[i].bounds.center;
//Encapsulate函数重新计算bounds
bounds.Encapsulate(mrs[i].bounds);
}
center /= mrs.Length;
//设置父物体中心点
GameObject obj = transform.parent.gameObject;
transform.SetParent(obj.transform.parent);
obj.transform.position = center;
obj.transform.rotation = Quaternion.identity;
transform.SetParent(obj.transform);
//设置物体中心点
List<Transform> list = new List<Transform>();
for (int i = 0; i < transform.childCount; i++)
{
list.Add(transform.GetChild(i).transform);
transform.GetChild(i).SetParent(obj.transform);
}
transform.localPosition = Vector3.zero;
transform.localRotation = Quaternion.identity;
for (int i = 0; i < list.Count; i++)
{
list[i].SetParent(transform);
}
#endregion
注意:物体中心点和父物体中心点的设置代码隐忍而异,看模型结构需求
方法二:通过改变模型原文件的网格位置来达到目的
#region 重置模型的中心点
Vector3 position = transform.position;
Quaternion quaternion = transform.rotation;
Vector3 scale = transform.localScale;
transform.position = Vector3.zero;
transform.rotation = Quaternion.Euler(Vector3.zero);
transform.localScale = Vector3.one;
MeshFilter[] meshFilters = transform.GetComponentsInChildren<MeshFilter>();
for (int i = 0; i < meshFilters.Length; i++)
{
MeshFilter meshFilter = meshFilters[i];
Vector3 meshCenterPos = meshFilter.sharedMesh.bounds.center;
Vector3 worldPos = meshFilter.transform.position;
//模型中心和世界坐标的差值向量
Vector3 offSet = worldPos - meshCenterPos;
Vector3[] vectices = meshFilter.sharedMesh.vertices;
for (int j = 0; j < vectices.Length; j++)
{
vectices[j] += offSet;
}
meshFilter.sharedMesh.vertices = vectices;
//重新构建mesh
meshFilter.sharedMesh.RecalculateBounds();
meshFilter.sharedMesh.RecalculateNormals();
meshFilter.sharedMesh.RecalculateTangents();
//若最终坐标位置不对,可能由于原模型有旋转等造成,可根据实际调整坐标赋值。
//meshFilter.transform.position = new Vector3(meshCenterPos.x, meshCenterPos.z, -meshCenterPos.y);
meshFilter.transform.position = meshCenterPos;
}
transform.position = position;
transform.rotation = quaternion;
transform.localScale = scale;
#endregion
注意:这里模型的位置必须Reset,这样才能保证计算准确。计算完以后,再把原位置赋值回去即可。文章来源:https://www.toymoban.com/news/detail-771706.html
总结:因为需求,我们的模型都是代码生成的,所以最后我们采用了二者结合的方式。
/// <summary>
/// 设置模型的中心点
/// </summary>
/// <param name="transform"></param>
private void SetModelCenter(Transform transform)
{
#region 重置模型的中心点
Vector3 position = transform.position;
Quaternion quaternion = transform.rotation;
Vector3 scale = transform.localScale;
transform.position = Vector3.zero;
transform.rotation = Quaternion.Euler(Vector3.zero);
transform.localScale = Vector3.one;
MeshFilter[] meshFilters = transform.GetComponentsInChildren<MeshFilter>();
for (int i = 0; i < meshFilters.Length; i++)
{
MeshFilter meshFilter = meshFilters[i];
Vector3 meshCenterPos = meshFilter.sharedMesh.bounds.center;
Vector3 worldPos = meshFilter.transform.position;
//模型中心和世界坐标的差值向量
Vector3 offSet = worldPos - meshCenterPos;
Vector3[] vectices = meshFilter.sharedMesh.vertices;
for (int j = 0; j < vectices.Length; j++)
{
vectices[j] += offSet;
}
meshFilter.sharedMesh.vertices = vectices;
//重新构建mesh
meshFilter.sharedMesh.RecalculateBounds();
meshFilter.sharedMesh.RecalculateNormals();
meshFilter.sharedMesh.RecalculateTangents();
//若最终坐标位置不对,可能由于原模型有旋转等造成,可根据实际调整坐标赋值。
//meshFilter.transform.position = new Vector3(meshCenterPos.x, meshCenterPos.z, -meshCenterPos.y);
meshFilter.transform.position = meshCenterPos;
}
transform.position = position;
transform.rotation = quaternion;
transform.localScale = scale;
#endregion
#region 重置模型父物体和父父物体的中心点
Bounds bounds = new Bounds(Vector3.zero, Vector3.zero);
//获取所有MeshRenderer 包括子物体
var mrs = transform.GetComponentsInChildren<MeshRenderer>(true);
Vector3 center = Vector3.zero;
for (int i = 0; i < mrs.Length; i++)
{
center += mrs[i].bounds.center;
//Encapsulate函数重新计算bounds
bounds.Encapsulate(mrs[i].bounds);
}
center /= mrs.Length;
//设置父物体中心点
GameObject obj = transform.parent.gameObject;
transform.SetParent(obj.transform.parent);
obj.transform.position = center;
obj.transform.rotation = Quaternion.identity;
transform.SetParent(obj.transform);
//设置物体中心点
List<Transform> list = new List<Transform>();
for (int i = 0; i < transform.childCount; i++)
{
list.Add(transform.GetChild(i).transform);
transform.GetChild(i).SetParent(obj.transform);
}
transform.localPosition = Vector3.zero;
transform.localRotation = Quaternion.identity;
for (int i = 0; i < list.Count; i++)
{
list[i].SetParent(transform);
}
#endregion
}
参考博文:
https://blog.csdn.net/weixin_39562801/article/details/107842345文章来源地址https://www.toymoban.com/news/detail-771706.html
到了这里,关于Unity将模型的世界坐标下的中心点归到模型中心的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!