赞
踩
效果对比:
调整前:
调整后:
需求:由于建模时的疏忽或者网上找的一些测试模型不规范,会出现导入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,这样才能保证计算准确。计算完以后,再把原位置赋值回去即可。
/// <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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。