模型预览和旋转

This commit is contained in:
bob
2025-06-24 12:41:43 +08:00
parent 197897a323
commit 89cad84802
26 changed files with 350 additions and 2862 deletions

View File

@@ -18,7 +18,7 @@ namespace NBF
int _width;
int _height;
bool _cacheTexture;
float _rotating;
Vector3 _rotating;
const int RENDER_LAYER = 0;
const int HIDDEN_LAYER = 10;
@@ -187,7 +187,7 @@ namespace NBF
return uvRect;
}
public void LoadModel(string model)
public GameObject LoadModel(string model)
{
this.UnloadModel();
@@ -195,6 +195,11 @@ namespace NBF
GameObject go = ((GameObject)Object.Instantiate(prefab));
_model = go.transform;
_model.SetParent(this.modelRoot, false);
var h = CalculateModelHeight(go); //计算包围盒高度
var pos = _model.localPosition;
_model.localPosition = new Vector3(pos.x, pos.y - h * 0.5f, pos.z);
return go;
}
public void UnloadModel()
@@ -205,17 +210,42 @@ namespace NBF
_model = null;
}
_rotating = 0;
_rotating = Vector3.zero;
}
public void StartRotate(float delta)
public void StartRotate(Vector3 delta)
{
_rotating = delta;
}
public void StopRotate()
{
_rotating = 0;
_rotating = Vector3.zero;
}
// 获取模型的高度
private float CalculateModelHeight(GameObject model)
{
// 获取模型的所有Renderer组件包括MeshRenderer和SkinnedMeshRenderer
Renderer[] renderers = model.GetComponentsInChildren<Renderer>();
if (renderers.Length == 0)
{
Debug.LogWarning("模型没有Renderer组件");
return 0f;
}
// 初始化包围盒
Bounds bounds = renderers[0].bounds;
// 合并所有Renderer的包围盒
for (int i = 1; i < renderers.Length; i++)
{
bounds.Encapsulate(renderers[i].bounds);
}
// 返回高度Y轴尺寸
return bounds.size.y;
}
void CreateTexture()
@@ -266,10 +296,10 @@ namespace NBF
void Render(object param = null)
{
if (_rotating != 0 && this.modelRoot != null)
if (_rotating != Vector3.zero && this.modelRoot != null)
{
Vector3 localRotation = this.modelRoot.localRotation.eulerAngles;
localRotation.y += _rotating;
localRotation += _rotating;
this.modelRoot.localRotation = Quaternion.Euler(localRotation);
}