模型旋转预览

This commit is contained in:
Bob.Song
2025-10-29 11:49:56 +08:00
parent 5a6c1821aa
commit b390cf2051
2 changed files with 79 additions and 27 deletions

View File

@@ -25,7 +25,7 @@ namespace NBF
int _width;
int _height;
bool _cacheTexture;
Vector3 _rotating;
// Vector3 _rotating;
const int RENDER_LAYER = 0;
const int HIDDEN_LAYER = 10;
@@ -80,7 +80,7 @@ namespace NBF
ViewerSettings = settings;
Object prefab = Resources.Load(model);
if(prefab == null) return;
if (prefab == null) return;
GameObject go = ((GameObject)Object.Instantiate(prefab));
var joint = go.GetComponent<Joint>();
if (joint != null)
@@ -111,7 +111,7 @@ namespace NBF
_model = null;
}
_rotating = Vector3.zero;
// _rotating = Vector3.zero;
}
@@ -205,11 +205,11 @@ namespace NBF
void Render(object param = null)
{
if (_rotating != Vector3.zero && this.modelRoot != null)
{
// 使用 Quaternion 来避免欧拉角带来的问题
modelRoot.Rotate(_rotating, Space.World);
}
// if (_rotating != Vector3.zero && this.modelRoot != null)
// {
// // 使用 Quaternion 来避免欧拉角带来的问题
// modelRoot.Rotate(_rotating, Space.World);
// }
SetLayer(_root.gameObject, RENDER_LAYER);
@@ -236,18 +236,51 @@ namespace NBF
#region
public void StartRotate(Vector3 delta)
// 在 ModelViewRenderImage 中添加新字段
private Vector2 _lastMousePosition;
private bool _isDragging;
public void StartRotate(Vector2 currentMousePosition)
{
_rotating = delta;
_lastMousePosition = currentMousePosition;
_isDragging = true;
}
public void UpdateRotation(Vector2 currentMousePosition)
{
if (!_isDragging || modelRoot == null || _camera == null) return;
Vector2 delta = currentMousePosition - _lastMousePosition;
// 使用适中的灵敏度
float sensitivity = 0.2f;
float rotX = -delta.y * sensitivity;
float rotY = -delta.x * sensitivity;
Quaternion yRotation = Quaternion.AngleAxis(rotY, _camera.transform.up);
Quaternion xRotation = Quaternion.AngleAxis(rotX, _camera.transform.right);
// 组合旋转并应用
modelRoot.rotation = yRotation * xRotation * modelRoot.rotation;
_lastMousePosition = currentMousePosition;
}
public void StopRotate()
{
_rotating = Vector3.zero;
_isDragging = false;
}
// 重置到初始状态的方法
public void ResetToInitialRotation()
{
modelRoot.localRotation = Quaternion.identity;
_isDragging = false;
}
#endregion
#region MyRegion

View File

@@ -64,33 +64,52 @@ namespace NBF
{
var dragObj = TouchHolder;
var gesture1 = new SwipeGesture(dragObj);
gesture1.onBegin.Set(OnSwipeBegin);
gesture1.onMove.Set(OnSwipeMove);
gesture1.onEnd.Set(OnSwipeEnd);
}
private void OnSwipeBegin(EventContext context)
{
var gesture = context.sender as SwipeGesture;
if (gesture == null) return;
if (context.inputEvent.button == 0)
{
_renderImage.StartRotate(gesture.position);
}
}
private void OnSwipeMove(EventContext context)
{
var gesture = context.sender as SwipeGesture;
if (gesture == null) return;
var v = Vector3.zero;
if (context.inputEvent.button == 0)
{
v.y = -gesture.delta.x * 0.2f;
v.z = -gesture.delta.y * 0.2f;
if (!gesture.snapping)
{
v.y = 0;
v.z = 0;
}
if (Mathf.Abs(v.y) < 1) //消除手抖影响
v.y = 0;
if (Mathf.Abs(v.z) < 1) //消除手抖影响
v.z = 0;
_renderImage.StartRotate(v);
_renderImage.UpdateRotation(gesture.position);
}
// var gesture = context.sender as SwipeGesture;
// if (gesture == null) return;
// var v = Vector3.zero;
//
//
// if (context.inputEvent.button == 0)
// {
// v.y = -gesture.delta.x * 0.2f;
// v.z = -gesture.delta.y * 0.2f;
// if (!gesture.snapping)
// {
// v.y = 0;
// v.z = 0;
// }
//
// if (Mathf.Abs(v.y) < 1) //消除手抖影响
// v.y = 0;
// if (Mathf.Abs(v.z) < 1) //消除手抖影响
// v.z = 0;
// _renderImage.StartRotate(v);
// }
}
private void OnSwipeEnd(EventContext context)