模型预览和旋转

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

@@ -5,7 +5,7 @@ namespace NBF
/// <summary>
/// 鱼饵
/// </summary>
public class BaitAsset : MonoBehaviour
public class BaitAsset : PreviewableAsset
{
public Transform hook;
}

View File

@@ -5,7 +5,7 @@ namespace NBF
/// <summary>
/// 浮漂资产
/// </summary>
public class BobberAsset : MonoBehaviour
public class BobberAsset : PreviewableAsset
{
public Transform body;
public Transform stick;

View File

@@ -2,7 +2,7 @@
namespace NBF
{
public class FishAsset : MonoBehaviour
public class FishAsset : PreviewableAsset
{
public Transform root;

View File

@@ -2,7 +2,7 @@
namespace NBF
{
public class HookAsset : MonoBehaviour
public class HookAsset : PreviewableAsset
{
/// <summary>
/// 鱼饵挂点

View File

@@ -2,7 +2,7 @@
namespace NBF
{
public class LineAsset : MonoBehaviour
public class LineAsset : PreviewableAsset
{
}
}

View File

@@ -2,7 +2,7 @@
namespace NBF
{
public class LureAsset : MonoBehaviour
public class LureAsset : PreviewableAsset
{
/// <summary>
/// 鱼钩挂点

View File

@@ -0,0 +1,38 @@
using System;
using UnityEngine;
namespace NBF
{
/// <summary>
/// 模型拉近拉远配置
/// </summary>
[Serializable]
public class ModelZoomConfig
{
[Tooltip("是否可以放大缩小")] public bool canZoom;
[Tooltip("默认值")] public float zoom;
[Tooltip("最小值")] public float zoomMin;
[Tooltip("最大值")] public float zoomMax;
}
/// <summary>
/// 模型平移配置
/// </summary>
[Serializable]
public class ModelPanConfig
{
[Tooltip("是否可以平移")] public bool canPan;
[Tooltip("水平可移动值")] public float x = 0;
[Tooltip("垂直可移动值")] public float y = 0;
}
/// <summary>
/// 可以3D预览的资产
/// </summary>
public class PreviewableAsset : MonoBehaviour
{
[Tooltip("放大缩小配置")] public ModelZoomConfig zoom;
[Tooltip("平移配置")] public ModelPanConfig pan;
[Tooltip("默认旋转")] public Vector3 rotation;
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a000ba87cd97499eb23feecf39521ecb
timeCreated: 1750736840

View File

@@ -2,7 +2,7 @@
namespace NBF
{
public class PropAsset : MonoBehaviour
public class PropAsset : PreviewableAsset
{
}
}

View File

@@ -5,7 +5,7 @@ namespace NBF
/// <summary>
/// 线轴资产配置
/// </summary>
public class ReelAsset : MonoBehaviour
public class ReelAsset : PreviewableAsset
{
// "previewRotationEnabled": 1,
// "previewRotation": {

View File

@@ -6,7 +6,7 @@ namespace NBF
/// <summary>
/// 鱼竿资产配置
/// </summary>
public class RodAsset : MonoBehaviour
public class RodAsset : PreviewableAsset
{
/// <summary>
/// 根节点

View File

@@ -2,7 +2,7 @@
namespace NBF
{
public class SpinnerLureAsset : MonoBehaviour
public class SpinnerLureAsset : PreviewableAsset
{
}
}

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);
}

View File

@@ -1,5 +1,6 @@
// 本脚本只在不存在时会生成一次。组件逻辑写在当前脚本内。已存在不会再次生成覆盖
using System;
using UnityEngine;
using FairyGUI;
using NBC;
@@ -9,30 +10,99 @@ namespace NBF
public partial class ModelTexture : GComponent
{
ModelRenderImage _renderImage;
private Vector2 _startPos;
private Vector3 _startRot;
private SwipeGesture _swipeGesture;
public float ZoomValue = 5;
public float MinZoom = 1;
public float MaxZoom = 15;
private void OnInited()
{
_renderImage = new ModelRenderImage(ModelHolder.asGraph);
//RenderImage是不透明的可以设置最多两张图片作为背景图
// _renderImage.SetBackground(contentPane.GetChild("frame").asCom.GetChild("n0"), contentPane.GetChild("n20"));
SetRotateListening();
}
public void LoadModel(string model)
/// <summary>
/// 加载资产模型
/// </summary>
/// <param name="configId"></param>
public void LoadAsset(int configId)
{
_renderImage.LoadModel("Role/npc");
_renderImage.modelRoot.localPosition = new Vector3(0, -1.0f, 5f);
_renderImage.modelRoot.localScale = new Vector3(1, 1, 1);
_renderImage.modelRoot.localRotation = Quaternion.Euler(0, 120, 0);
if (_renderImage == null)
{
_renderImage = new ModelRenderImage(ModelHolder.asGraph);
}
var model = _renderImage.LoadModel("Role/npc");
_renderImage.modelRoot.localScale = Vector3.one;
var previewableAsset = model.GetComponent<PreviewableAsset>();
if (previewableAsset != null)
{
_renderImage.modelRoot.localPosition = new Vector3(0, 0, previewableAsset.zoom.zoom);
_renderImage.modelRoot.localRotation = Quaternion.Euler(previewableAsset.rotation);
}
else
{
_renderImage.modelRoot.localPosition = new Vector3(0, 0, 3);
_renderImage.modelRoot.localRotation = Quaternion.Euler(Vector3.zero);
}
}
public void SetBackground(GObject image1)
/// <summary>
/// 加载角色模型
/// </summary>
public void LoadRole()
{
_renderImage.SetBackground(image1);
}
public void SetBackground(GObject image1, GObject image2)
private void SetRotateListening()
{
_renderImage.SetBackground(image1, image2);
var dragObj = TouchHolder;
var gesture1 = new SwipeGesture(dragObj);
gesture1.onMove.Set(OnSwipeMove);
gesture1.onEnd.Set(OnSwipeEnd);
}
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);
}
else
{
v.y = -gesture.delta.y * 0.005f;
v.x = gesture.delta.x * 0.005f;
//平移
var pos = _renderImage.modelRoot.localPosition;
_renderImage.modelRoot.localPosition = new Vector3(pos.x + v.x, pos.y + v.y, pos.z);
}
}
private void OnSwipeEnd(EventContext context)
{
_renderImage.StopRotate();
}
}
}

View File

@@ -11,17 +11,17 @@ namespace NBF
{
public const string URL = "ui://hxr7rc7plvql10";
public ModelTexture Model;
public HomeButtonGroups OpGroup;
public BottomMenu BottomMenu;
public ModelTexture Model;
public override void ConstructFromXML(XML xml)
{
base.ConstructFromXML(xml);
Model = (ModelTexture)GetChild("Model");
OpGroup = (HomeButtonGroups)GetChild("OpGroup");
BottomMenu = (BottomMenu)GetChild("BottomMenu");
Model = (ModelTexture)GetChild("Model");
OnInited();
UILanguage.TrySetComponentLanguage(this);
}

View File

@@ -33,7 +33,7 @@ namespace NBF
UseBottomMenu();
Model.LoadModel(string.Empty);
Model.LoadAsset(0);
// Model.SetBackground(Panel.Back.GetChild("back"));//, Panel.Back.GetChild("icon")
}