模型预览功能
This commit is contained in:
@@ -17,8 +17,6 @@ namespace NBF
|
||||
[ProtoMember(1)]
|
||||
public uint Id { get; set; } // Id
|
||||
[ProtoMember(2)]
|
||||
public uint ItemId { get; set; } // 物品Id
|
||||
[ProtoMember(3)]
|
||||
public int Amount { get; set; } // 数量
|
||||
[ProtoIgnore]
|
||||
public uint Key => Id;
|
||||
|
||||
@@ -13,20 +13,32 @@ namespace NBF
|
||||
[ProtoContract]
|
||||
public sealed partial class ItemConfig : ASerialize, IProto, IConfigTable
|
||||
{
|
||||
[ProtoMember(1)] public uint Id { get; set; } // Id
|
||||
[ProtoMember(2)] public string Model { get; set; } // 模型
|
||||
[ProtoMember(3)] public uint Type { get; set; } // 类型
|
||||
[ProtoMember(4)] public uint Quality { get; set; } // 品质
|
||||
[ProtoMember(5)] public uint Brand { get; set; } // 品牌
|
||||
[ProtoMember(6)] public uint Weight { get; set; } // 重量(克)
|
||||
[ProtoMember(7)] public uint Length { get; set; } // 长度(毫米)
|
||||
[ProtoMember(8)] public uint Max { get; set; } // 最大堆叠数量
|
||||
[ProtoMember(9)] public uint AutoUse { get; set; } // 获得自动使用
|
||||
[ProtoMember(10)] public uint Deal { get; set; } // 交易类型
|
||||
[ProtoIgnore] public uint Key => Id;
|
||||
|
||||
|
||||
[ProtoMember(1)]
|
||||
public uint Id { get; set; } // Id
|
||||
[ProtoMember(2)]
|
||||
public string Model { get; set; } // 模型
|
||||
[ProtoMember(3)]
|
||||
public uint Type { get; set; } // 类型
|
||||
[ProtoMember(4)]
|
||||
public uint Quality { get; set; } // 品质
|
||||
[ProtoMember(5)]
|
||||
public uint Brand { get; set; } // 品牌
|
||||
[ProtoMember(6)]
|
||||
public uint Weight { get; set; } // 重量(克)
|
||||
[ProtoMember(7)]
|
||||
public uint Length { get; set; } // 长度(毫米)
|
||||
[ProtoMember(8)]
|
||||
public uint Max { get; set; } // 最大堆叠数量
|
||||
[ProtoMember(9)]
|
||||
public uint AutoUse { get; set; } // 获得自动使用
|
||||
[ProtoMember(10)]
|
||||
public uint Deal { get; set; } // 交易类型
|
||||
[ProtoIgnore]
|
||||
public uint Key => Id;
|
||||
|
||||
#region Static
|
||||
|
||||
|
||||
private static ConfigContext<ItemConfig> Context => ConfigTableHelper.Table<ItemConfig>();
|
||||
|
||||
public static ItemConfig Get(uint key)
|
||||
@@ -38,7 +50,7 @@ namespace NBF
|
||||
{
|
||||
return Context.Get(match);
|
||||
}
|
||||
|
||||
|
||||
public static ItemConfig Fist()
|
||||
{
|
||||
return Context.Fist();
|
||||
@@ -58,7 +70,7 @@ namespace NBF
|
||||
{
|
||||
return Context.Last(match);
|
||||
}
|
||||
|
||||
|
||||
public static int Count()
|
||||
{
|
||||
return Context.Count();
|
||||
@@ -78,12 +90,10 @@ namespace NBF
|
||||
{
|
||||
return Context.GetList(match);
|
||||
}
|
||||
|
||||
public static void ParseJson(Newtonsoft.Json.Linq.JArray arr)
|
||||
{
|
||||
ConfigTableHelper.ParseLine<ItemConfig>(arr);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
85
Assets/Scripts/Generate/Config/RodRingConfig.cs
Normal file
85
Assets/Scripts/Generate/Config/RodRingConfig.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using ProtoBuf;
|
||||
using Fantasy;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Concurrent;
|
||||
using NBC;
|
||||
using NBC.Serialize;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
[ProtoContract]
|
||||
public sealed partial class RodRingConfig : ASerialize, IProto, IConfigTable
|
||||
{
|
||||
|
||||
[ProtoMember(1)]
|
||||
public uint Id { get; set; } // Id
|
||||
[ProtoMember(2)]
|
||||
public string Model { get; set; } // 模型
|
||||
[ProtoMember(3)]
|
||||
public uint Strength { get; set; } // 强度
|
||||
[ProtoIgnore]
|
||||
public uint Key => Id;
|
||||
|
||||
#region Static
|
||||
|
||||
private static ConfigContext<RodRingConfig> Context => ConfigTableHelper.Table<RodRingConfig>();
|
||||
|
||||
public static RodRingConfig Get(uint key)
|
||||
{
|
||||
return Context.Get(key);
|
||||
}
|
||||
|
||||
public static RodRingConfig Get(Predicate<RodRingConfig> match)
|
||||
{
|
||||
return Context.Get(match);
|
||||
}
|
||||
|
||||
public static RodRingConfig Fist()
|
||||
{
|
||||
return Context.Fist();
|
||||
}
|
||||
|
||||
public static RodRingConfig Last()
|
||||
{
|
||||
return Context.Last();
|
||||
}
|
||||
|
||||
public static RodRingConfig Fist(Predicate<RodRingConfig> match)
|
||||
{
|
||||
return Context.Fist(match);
|
||||
}
|
||||
|
||||
public static RodRingConfig Last(Predicate<RodRingConfig> match)
|
||||
{
|
||||
return Context.Last(match);
|
||||
}
|
||||
|
||||
public static int Count()
|
||||
{
|
||||
return Context.Count();
|
||||
}
|
||||
|
||||
public static int Count(Func<RodRingConfig, bool> predicate)
|
||||
{
|
||||
return Context.Count(predicate);
|
||||
}
|
||||
|
||||
public static List<RodRingConfig> GetList()
|
||||
{
|
||||
return Context.GetList();
|
||||
}
|
||||
|
||||
public static List<RodRingConfig> GetList(Predicate<RodRingConfig> match)
|
||||
{
|
||||
return Context.GetList(match);
|
||||
}
|
||||
public static void ParseJson(Newtonsoft.Json.Linq.JArray arr)
|
||||
{
|
||||
ConfigTableHelper.ParseLine<RodRingConfig>(arr);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Generate/Config/RodRingConfig.cs.meta
Normal file
2
Assets/Scripts/Generate/Config/RodRingConfig.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e47768c4795d2d142a071bcc4edc6767
|
||||
@@ -17,6 +17,7 @@ namespace NBF
|
||||
{
|
||||
var itemConfig = ItemConfig.Get(itemInfo.ConfigId);
|
||||
TextTitle.text = itemInfo.ConfigId.GetName();
|
||||
TextContent.text = itemInfo.ConfigId.GetDesc();
|
||||
TextTitle.SetQuality(itemInfo.Config.Quality);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace NBF
|
||||
[AutoFind(Name = "modelBack")]
|
||||
public GImage modelBack;
|
||||
[AutoFind(Name = "Model")]
|
||||
public ModelTexture Model;
|
||||
public ModelViewer Model;
|
||||
[AutoFind(Name = "Content")]
|
||||
public ItemDetailsContent Content;
|
||||
[AutoFind(Name = "Quality")]
|
||||
|
||||
@@ -21,14 +21,13 @@ namespace NBF
|
||||
ItemInfo = GetData() as ItemInfo;
|
||||
|
||||
Quality.SetQuality(ItemInfo.Config.Quality);
|
||||
|
||||
|
||||
Content.Basic.SetInfo(ItemInfo);
|
||||
Content.Basic.SetInfo(ItemInfo);
|
||||
|
||||
var model = PrefabsHelper.CreatePrefab(ItemInfo.Config.Model);
|
||||
Model.SetModel(model);
|
||||
// LoadModel(ItemInfo.Config.Model);
|
||||
|
||||
// var model = PrefabsHelper.CreatePrefab(ItemInfo.Config.Model);
|
||||
Model.SetData(ItemInfo.Config);
|
||||
|
||||
Game.Input.OnUICanceled += OnUICanceled;
|
||||
}
|
||||
|
||||
@@ -50,7 +49,6 @@ namespace NBF
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected override void OnHide()
|
||||
{
|
||||
Game.Input.OnUICanceled -= OnUICanceled;
|
||||
|
||||
@@ -10,7 +10,6 @@ namespace NBF
|
||||
public static void BindAll()
|
||||
{
|
||||
UIObjectFactory.SetPackageItemExtension(SelectPages.URL, typeof(SelectPages));
|
||||
UIObjectFactory.SetPackageItemExtension(ModelTexture.URL, typeof(ModelTexture));
|
||||
UIObjectFactory.SetPackageItemExtension(BottomMenu.URL, typeof(BottomMenu));
|
||||
UIObjectFactory.SetPackageItemExtension(CommonInput.URL, typeof(CommonInput));
|
||||
UIObjectFactory.SetPackageItemExtension(ClassifyList.URL, typeof(ClassifyList));
|
||||
@@ -23,6 +22,7 @@ namespace NBF
|
||||
UIObjectFactory.SetPackageItemExtension(CommonSubMenu.URL, typeof(CommonSubMenu));
|
||||
UIObjectFactory.SetPackageItemExtension(BtnInputControl.URL, typeof(BtnInputControl));
|
||||
UIObjectFactory.SetPackageItemExtension(ListTitleItem.URL, typeof(ListTitleItem));
|
||||
UIObjectFactory.SetPackageItemExtension(ModelViewer.URL, typeof(ModelViewer));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,320 +0,0 @@
|
||||
using FairyGUI;
|
||||
using FairyGUI.Utils;
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
public class ModelRenderImage
|
||||
{
|
||||
public Transform modelRoot { get; private set; }
|
||||
|
||||
Camera _camera;
|
||||
Image _image;
|
||||
Transform _root;
|
||||
Transform _background;
|
||||
Transform _model;
|
||||
RenderTexture _renderTexture;
|
||||
int _width;
|
||||
int _height;
|
||||
bool _cacheTexture;
|
||||
Vector3 _rotating;
|
||||
|
||||
const int RENDER_LAYER = 22;
|
||||
const int HIDDEN_LAYER = 0;
|
||||
|
||||
public ModelRenderImage(GGraph holder)
|
||||
{
|
||||
_width = (int)holder.width;
|
||||
_height = (int)holder.height;
|
||||
_cacheTexture = true;
|
||||
|
||||
this._image = new Image();
|
||||
holder.SetNativeObject(this._image);
|
||||
|
||||
Object prefab = Resources.Load("RenderTexture/RenderImageCamera");
|
||||
GameObject go = (GameObject)Object.Instantiate(prefab);
|
||||
_camera = go.GetComponent<Camera>();
|
||||
_camera.transform.position = new Vector3(0, 1000, 0);
|
||||
_camera.cullingMask = 1 << RENDER_LAYER;
|
||||
_camera.enabled = false;
|
||||
Object.DontDestroyOnLoad(_camera.gameObject);
|
||||
|
||||
this._root = new GameObject("RenderImage").transform;
|
||||
this._root.SetParent(_camera.transform, false);
|
||||
SetLayer(this._root.gameObject, HIDDEN_LAYER);
|
||||
|
||||
this.modelRoot = new GameObject("model_root").transform;
|
||||
this.modelRoot.SetParent(this._root, false);
|
||||
|
||||
this._background = new GameObject("background").transform;
|
||||
this._background.SetParent(this._root, false);
|
||||
|
||||
this._image.onAddedToStage.Add(OnAddedToStage);
|
||||
this._image.onRemovedFromStage.Add(OnRemoveFromStage);
|
||||
|
||||
if (this._image.stage != null)
|
||||
OnAddedToStage();
|
||||
else
|
||||
_camera.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Object.Destroy(_camera.gameObject);
|
||||
DestroyTexture();
|
||||
|
||||
this._image.Dispose();
|
||||
this._image = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The rendertexture is not transparent. So if you want to the UI elements can be seen in the back of the models/particles in rendertexture,
|
||||
/// you can set a maximunm two images for background.
|
||||
/// Be careful if your image is 9 grid scaling, you must make sure the place holder is inside the middle box(dont cover from border to middle).
|
||||
/// </summary>
|
||||
/// <param name="image"></param>
|
||||
public void SetBackground(GObject image)
|
||||
{
|
||||
SetBackground(image, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The rendertexture is not transparent. So if you want to the UI elements can be seen in the back of the models/particles in rendertexture,
|
||||
/// you can set a maximunm two images for background.
|
||||
/// </summary>
|
||||
/// <param name="image1"></param>
|
||||
/// <param name="image2"></param>
|
||||
public void SetBackground(GObject image1, GObject image2)
|
||||
{
|
||||
Image source1 = (Image)image1.displayObject;
|
||||
Image source2 = image2 != null ? (Image)image2.displayObject : null;
|
||||
|
||||
Vector3 pos = _background.position;
|
||||
pos.z = _camera.farClipPlane;
|
||||
_background.position = pos;
|
||||
|
||||
Vector2[] uv = new Vector2[4];
|
||||
Vector2[] uv2 = null;
|
||||
|
||||
Rect rect = _image.TransformRect(new Rect(0, 0, this._width, this._height), source1);
|
||||
Rect uvRect = GetImageUVRect(source1, rect, uv);
|
||||
|
||||
if (source2 != null)
|
||||
{
|
||||
rect = _image.TransformRect(new Rect(0, 0, this._width, this._height), source2);
|
||||
uv2 = new Vector2[4];
|
||||
GetImageUVRect(source2, rect, uv2);
|
||||
}
|
||||
|
||||
Vector3[] vertices = new Vector3[4];
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
Vector2 v = uv[i];
|
||||
vertices[i] = new Vector3((v.x - uvRect.x) / uvRect.width * 2 - 1,
|
||||
(v.y - uvRect.y) / uvRect.height * 2 - 1, 0);
|
||||
}
|
||||
|
||||
Mesh mesh = new Mesh();
|
||||
mesh.vertices = vertices;
|
||||
mesh.uv = uv;
|
||||
if (uv2 != null)
|
||||
mesh.uv2 = uv2;
|
||||
mesh.colors32 = new Color32[] { Color.white, Color.white, Color.white, Color.white };
|
||||
mesh.triangles = new int[] { 0, 1, 2, 2, 3, 0 };
|
||||
|
||||
MeshFilter meshFilter = this._background.gameObject.GetComponent<MeshFilter>();
|
||||
if (meshFilter == null)
|
||||
meshFilter = this._background.gameObject.AddComponent<MeshFilter>();
|
||||
meshFilter.mesh = mesh;
|
||||
MeshRenderer meshRenderer = this._background.gameObject.GetComponent<MeshRenderer>();
|
||||
if (meshRenderer == null)
|
||||
meshRenderer = this._background.gameObject.AddComponent<MeshRenderer>();
|
||||
#if (UNITY_5 || UNITY_5_3_OR_NEWER)
|
||||
meshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
|
||||
#else
|
||||
meshRenderer.castShadows = false;
|
||||
#endif
|
||||
meshRenderer.receiveShadows = false;
|
||||
Shader shader = Shader.Find("Game/FullScreen");
|
||||
Material mat = new Material(shader);
|
||||
mat.mainTexture = source1.texture.nativeTexture;
|
||||
if (source2 != null)
|
||||
mat.SetTexture("_Tex2", source2.texture.nativeTexture);
|
||||
meshRenderer.material = mat;
|
||||
}
|
||||
|
||||
Rect GetImageUVRect(Image image, Rect localRect, Vector2[] uv)
|
||||
{
|
||||
Rect imageRect = new Rect(0, 0, image.size.x, image.size.y);
|
||||
Rect bound = ToolSet.Intersection(ref imageRect, ref localRect);
|
||||
Rect uvRect = image.texture.uvRect;
|
||||
|
||||
if (image.scale9Grid != null)
|
||||
{
|
||||
Rect gridRect = (Rect)image.scale9Grid;
|
||||
float sourceW = image.texture.width;
|
||||
float sourceH = image.texture.height;
|
||||
uvRect = Rect.MinMaxRect(Mathf.Lerp(uvRect.xMin, uvRect.xMax, gridRect.xMin / sourceW),
|
||||
Mathf.Lerp(uvRect.yMin, uvRect.yMax, (sourceH - gridRect.yMax) / sourceH),
|
||||
Mathf.Lerp(uvRect.xMin, uvRect.xMax, gridRect.xMax / sourceW),
|
||||
Mathf.Lerp(uvRect.yMin, uvRect.yMax, (sourceH - gridRect.yMin) / sourceH));
|
||||
|
||||
float vw = imageRect.width - (sourceW - gridRect.width);
|
||||
float vh = imageRect.height - (sourceH - gridRect.height);
|
||||
uvRect = Rect.MinMaxRect(Mathf.Lerp(uvRect.xMin, uvRect.xMax, (bound.x - gridRect.x) / vw),
|
||||
Mathf.Lerp(uvRect.yMin, uvRect.yMax,
|
||||
(imageRect.height - bound.yMax - (sourceH - gridRect.yMax)) / vh),
|
||||
Mathf.Lerp(uvRect.xMin, uvRect.xMax, (bound.xMax - gridRect.x) / vw),
|
||||
Mathf.Lerp(uvRect.yMin, uvRect.yMax, (imageRect.height - bound.yMin - gridRect.y) / vh));
|
||||
}
|
||||
else
|
||||
{
|
||||
uvRect = Rect.MinMaxRect(Mathf.Lerp(uvRect.xMin, uvRect.xMax, bound.xMin / imageRect.width),
|
||||
Mathf.Lerp(uvRect.yMin, uvRect.yMax, (imageRect.height - bound.yMax) / imageRect.height),
|
||||
Mathf.Lerp(uvRect.xMin, uvRect.xMax, bound.xMax / imageRect.width),
|
||||
Mathf.Lerp(uvRect.yMin, uvRect.yMax, (imageRect.height - bound.yMin) / imageRect.height));
|
||||
}
|
||||
|
||||
uv[0] = uvRect.position;
|
||||
uv[1] = new Vector2(uvRect.xMin, uvRect.yMax);
|
||||
uv[2] = new Vector2(uvRect.xMax, uvRect.yMax);
|
||||
uv[3] = new Vector2(uvRect.xMax, uvRect.yMin);
|
||||
|
||||
if (image.texture.rotated)
|
||||
ToolSet.RotateUV(uv, ref image.texture.uvRect);
|
||||
|
||||
return uvRect;
|
||||
}
|
||||
|
||||
public void SetModel(GameObject model)
|
||||
{
|
||||
this.UnloadModel();
|
||||
_model = model.transform;
|
||||
_model.SetParent(this.modelRoot, false);
|
||||
_model.AlignmentParentCenter();
|
||||
}
|
||||
|
||||
public void UnloadModel()
|
||||
{
|
||||
if (_model != null)
|
||||
{
|
||||
Object.Destroy(_model.gameObject);
|
||||
_model = null;
|
||||
}
|
||||
|
||||
_rotating = Vector3.zero;
|
||||
}
|
||||
|
||||
public void StartRotate(Vector3 delta)
|
||||
{
|
||||
_rotating = delta;
|
||||
}
|
||||
|
||||
public void StopRotate()
|
||||
{
|
||||
_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()
|
||||
{
|
||||
if (_renderTexture != null)
|
||||
return;
|
||||
|
||||
_renderTexture = new RenderTexture(_width, _height, 24, RenderTextureFormat.ARGB32)
|
||||
{
|
||||
antiAliasing = 2,
|
||||
filterMode = FilterMode.Bilinear,
|
||||
anisoLevel = 0,
|
||||
useMipMap = false
|
||||
};
|
||||
this._image.texture = new NTexture(_renderTexture);
|
||||
// this._image.blendMode = BlendMode.Off;
|
||||
}
|
||||
|
||||
void DestroyTexture()
|
||||
{
|
||||
if (_renderTexture != null)
|
||||
{
|
||||
Object.Destroy(_renderTexture);
|
||||
_renderTexture = null;
|
||||
this._image.texture = null;
|
||||
}
|
||||
}
|
||||
|
||||
void OnAddedToStage()
|
||||
{
|
||||
if (_renderTexture == null)
|
||||
CreateTexture();
|
||||
|
||||
Timers.inst.AddUpdate(this.Render);
|
||||
_camera.gameObject.SetActive(true);
|
||||
|
||||
Render();
|
||||
}
|
||||
|
||||
void OnRemoveFromStage()
|
||||
{
|
||||
if (!_cacheTexture)
|
||||
DestroyTexture();
|
||||
|
||||
Timers.inst.Remove(this.Render);
|
||||
_camera.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
void Render(object param = null)
|
||||
{
|
||||
if (_rotating != Vector3.zero && this.modelRoot != null)
|
||||
{
|
||||
Vector3 localRotation = this.modelRoot.localRotation.eulerAngles;
|
||||
localRotation += _rotating;
|
||||
this.modelRoot.localRotation = Quaternion.Euler(localRotation);
|
||||
}
|
||||
|
||||
SetLayer(this._root.gameObject, RENDER_LAYER);
|
||||
|
||||
_camera.targetTexture = this._renderTexture;
|
||||
RenderTexture old = RenderTexture.active;
|
||||
RenderTexture.active = this._renderTexture;
|
||||
GL.Clear(true, true, Color.clear);
|
||||
_camera.Render();
|
||||
RenderTexture.active = old;
|
||||
|
||||
SetLayer(this._root.gameObject, HIDDEN_LAYER);
|
||||
}
|
||||
|
||||
void SetLayer(GameObject go, int layer)
|
||||
{
|
||||
Transform[] transforms = go.GetComponentsInChildren<Transform>(true);
|
||||
foreach (Transform t in transforms)
|
||||
{
|
||||
t.gameObject.layer = layer;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 82ec71c75abc4b739fb8e75078dd718b
|
||||
timeCreated: 1750673117
|
||||
@@ -1,2 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: db45948262d06f64299721282226d4f2
|
||||
@@ -1,204 +0,0 @@
|
||||
// 本脚本只在不存在时会生成一次。组件逻辑写在当前脚本内。已存在不会再次生成覆盖
|
||||
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using FairyGUI;
|
||||
using NBC;
|
||||
using RootMotion.FinalIK;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
public partial class ModelTexture : GComponent
|
||||
{
|
||||
ModelRenderImage _renderImage;
|
||||
private Vector2 _startPos;
|
||||
private Vector3 _startRot;
|
||||
private SwipeGesture _swipeGesture;
|
||||
private bool _focus;
|
||||
|
||||
private bool _canZoom;
|
||||
private bool _canPan;
|
||||
private Vector3 _zoomLimit;
|
||||
private Rect _panLimit;
|
||||
|
||||
private void OnInited()
|
||||
{
|
||||
SetRotateListening();
|
||||
// Stage.inst.on
|
||||
TouchHolder.onRollOver.Set(OnFocusIn);
|
||||
TouchHolder.onRollOut.Set(OnFocusOut);
|
||||
Stage.inst.onMouseWheel.Add(OnMouseWheel);
|
||||
// TouchHolder.onm
|
||||
}
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
Stage.inst.onMouseWheel.Remove(OnMouseWheel);
|
||||
base.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载资产模型
|
||||
/// </summary>
|
||||
/// <param name="configId"></param>
|
||||
public void LoadAsset(int configId)
|
||||
{
|
||||
//Assets/Resources/gfx/hooks/berserk_hooks/clas_20421_20446/clas_20423.prefab
|
||||
//Assets/Resources/gfx/rods/syberia/bolo_10021/bolo_10021_LB400.prefab
|
||||
//"Role/test"
|
||||
var prefab = Resources.Load("gfx/hooks/berserk_hooks/clas_20421_20446/clas_20423");
|
||||
SetModel((GameObject)Object.Instantiate(prefab));
|
||||
}
|
||||
|
||||
public void SetModel(GameObject model)
|
||||
{
|
||||
if (_renderImage == null)
|
||||
{
|
||||
_renderImage = new ModelRenderImage(ModelHolder.asGraph);
|
||||
}
|
||||
|
||||
var ccdIk = model.GetComponent<CCDIK>();
|
||||
if (ccdIk != null)
|
||||
{
|
||||
Object.Destroy(ccdIk);
|
||||
}
|
||||
|
||||
var lineRenderer = model.GetComponent<LineRenderer>();
|
||||
if (lineRenderer != null)
|
||||
{
|
||||
Object.Destroy(lineRenderer);
|
||||
}
|
||||
|
||||
var rigidbody = model.GetComponent<Rigidbody>();
|
||||
if (rigidbody != null)
|
||||
{
|
||||
rigidbody.isKinematic = true;
|
||||
}
|
||||
|
||||
|
||||
_renderImage.SetModel(model);
|
||||
_renderImage.modelRoot.localScale = Vector3.one;
|
||||
|
||||
var previewableAsset = model.GetComponent<PreviewableAsset>();
|
||||
if (previewableAsset != null)
|
||||
{
|
||||
_zoomLimit = previewableAsset.zoom;
|
||||
_panLimit = previewableAsset.pan;
|
||||
_canPan = previewableAsset.canPan;
|
||||
_canZoom = previewableAsset.canZoom;
|
||||
model.transform.localPosition = previewableAsset.position;
|
||||
model.transform.localScale = previewableAsset.scale;
|
||||
model.transform.localRotation = Quaternion.Euler(previewableAsset.rotation);
|
||||
}
|
||||
else
|
||||
{
|
||||
_canPan = true;
|
||||
_canZoom = true;
|
||||
_zoomLimit = Vector3.zero;
|
||||
_panLimit = Rect.zero;
|
||||
model.transform.localPosition = new Vector3(0, 1.3f, 7);
|
||||
model.transform.localScale = new Vector3(20, 20, 20);
|
||||
model.transform.localRotation = Quaternion.Euler(Vector3.zero);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载角色模型
|
||||
/// </summary>
|
||||
public void LoadRole()
|
||||
{
|
||||
}
|
||||
|
||||
private void SetRotateListening()
|
||||
{
|
||||
var dragObj = TouchHolder;
|
||||
var gesture1 = new SwipeGesture(dragObj);
|
||||
gesture1.onMove.Set(OnSwipeMove);
|
||||
gesture1.onEnd.Set(OnSwipeEnd);
|
||||
}
|
||||
|
||||
#region 旋转和平移
|
||||
|
||||
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 if (_canPan)
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Zoom
|
||||
|
||||
private void OnFocusIn()
|
||||
{
|
||||
_focus = true;
|
||||
Log.Info("focus true");
|
||||
}
|
||||
|
||||
private void OnFocusOut()
|
||||
{
|
||||
_focus = false;
|
||||
Log.Info("focus false");
|
||||
}
|
||||
|
||||
private void OnMouseWheel(EventContext context)
|
||||
{
|
||||
if (!_canZoom) return;
|
||||
if (!_focus) return;
|
||||
float delta = context.inputEvent.mouseWheelDelta / Stage.devicePixelRatio;
|
||||
SetZoom(delta * 0.2f);
|
||||
}
|
||||
|
||||
private void SetZoom(float delta)
|
||||
{
|
||||
var pos = _renderImage.modelRoot.localPosition;
|
||||
var targetZ = pos.z + delta;
|
||||
if (targetZ < _zoomLimit.x)
|
||||
{
|
||||
targetZ = _zoomLimit.x;
|
||||
}
|
||||
else if (targetZ > _zoomLimit.y)
|
||||
{
|
||||
targetZ = _zoomLimit.y;
|
||||
}
|
||||
|
||||
_renderImage.modelRoot.localPosition = new Vector3(pos.x, pos.y, targetZ);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6b09786333c422145abf84aaa2750425
|
||||
3
Assets/Scripts/UI/Common/ModelViewer.meta
Normal file
3
Assets/Scripts/UI/Common/ModelViewer.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6ab6eac3efbb4daf8f4051d1a22d3dd8
|
||||
timeCreated: 1761415375
|
||||
227
Assets/Scripts/UI/Common/ModelViewer/ModelViewRenderImage.cs
Normal file
227
Assets/Scripts/UI/Common/ModelViewer/ModelViewRenderImage.cs
Normal file
@@ -0,0 +1,227 @@
|
||||
using System;
|
||||
using FairyGUI;
|
||||
using FairyGUI.Utils;
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering.Universal;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
[Serializable]
|
||||
public class ModelViewRenderImage
|
||||
{
|
||||
private ModelViewerSettings _viewerSettings;
|
||||
public Transform modelRoot { get; private set; }
|
||||
|
||||
Camera _camera;
|
||||
Image _image;
|
||||
Transform _root;
|
||||
Transform _background;
|
||||
Transform _model;
|
||||
RenderTexture _renderTexture;
|
||||
int _width;
|
||||
int _height;
|
||||
bool _cacheTexture;
|
||||
float _rotating;
|
||||
|
||||
const int RENDER_LAYER = 0;
|
||||
const int HIDDEN_LAYER = 10;
|
||||
|
||||
public ModelViewRenderImage(GGraph holder)
|
||||
{
|
||||
_width = (int)holder.width;
|
||||
_height = (int)holder.height;
|
||||
_cacheTexture = true;
|
||||
|
||||
_image = new Image();
|
||||
holder.SetNativeObject(_image);
|
||||
|
||||
|
||||
_root = new GameObject("RenderImage").transform;
|
||||
_root.transform.position = Vector3.zero;
|
||||
// _root.SetParent(_camera.transform, false);
|
||||
SetLayer(_root.gameObject, HIDDEN_LAYER);
|
||||
Object.DontDestroyOnLoad(_root.gameObject);
|
||||
|
||||
Object prefab = Resources.Load("RenderTexture/RenderImageCamera");
|
||||
GameObject go = (GameObject)Object.Instantiate(prefab, _root, false);
|
||||
|
||||
// ConfigurableJoint
|
||||
|
||||
_camera = go.GetComponent<Camera>();
|
||||
_camera.transform.position = Vector3.zero;
|
||||
_camera.cullingMask = 1 << RENDER_LAYER;
|
||||
_camera.enabled = false;
|
||||
Object.DontDestroyOnLoad(_camera.gameObject);
|
||||
|
||||
|
||||
modelRoot = new GameObject("model_root").transform;
|
||||
modelRoot.SetParent(_root, false);
|
||||
|
||||
_background = new GameObject("background").transform;
|
||||
_background.SetParent(_root, false);
|
||||
|
||||
_image.onAddedToStage.Add(OnAddedToStage);
|
||||
_image.onRemovedFromStage.Add(OnRemoveFromStage);
|
||||
|
||||
|
||||
_viewerSettings = new ModelViewerSettings();
|
||||
|
||||
if (_image.stage != null)
|
||||
OnAddedToStage();
|
||||
else
|
||||
_camera.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
public void LoadModel(string model)
|
||||
{
|
||||
this.UnloadModel();
|
||||
|
||||
Object prefab = Resources.Load(model);
|
||||
GameObject go = ((GameObject)Object.Instantiate(prefab));
|
||||
var joint = go.GetComponent<Joint>();
|
||||
if (joint != null)
|
||||
{
|
||||
Object.Destroy(joint);
|
||||
}
|
||||
|
||||
_model = go.transform;
|
||||
_model.SetParent(this.modelRoot, false);
|
||||
_model.localPosition = Vector3.zero;
|
||||
_model.localScale = Vector3.one;
|
||||
_model.localEulerAngles = Vector3.zero;
|
||||
ModelViewerUtils.InitSetting(go, _viewerSettings);
|
||||
Review();
|
||||
}
|
||||
|
||||
public void UnloadModel()
|
||||
{
|
||||
if (_model != null)
|
||||
{
|
||||
_model.gameObject.SetActive(false);
|
||||
Object.Destroy(_model.gameObject);
|
||||
_model = null;
|
||||
}
|
||||
|
||||
_rotating = 0;
|
||||
}
|
||||
|
||||
|
||||
public void Review()
|
||||
{
|
||||
_model.position = _viewerSettings.objectPosition;
|
||||
_model.localScale = _viewerSettings.objectScale;
|
||||
_model.eulerAngles = _viewerSettings.objectRotation;
|
||||
|
||||
|
||||
_camera.clearFlags = CameraClearFlags.Nothing;
|
||||
_camera.transform.position = _viewerSettings.cameraPosition;
|
||||
_camera.transform.LookAt(_viewerSettings.cameraTarget);
|
||||
_camera.orthographic = _viewerSettings.cameraOrtho;
|
||||
_camera.orthographicSize = _viewerSettings.cameraSize;
|
||||
_camera.orthographicSize /= _viewerSettings.camerasScaleFactor;
|
||||
|
||||
_camera.fieldOfView = _viewerSettings.cameraFov;
|
||||
float fov = 2 * Mathf.Rad2Deg * Mathf.Atan2(
|
||||
Mathf.Tan(_camera.fieldOfView * Mathf.Deg2Rad / 2), _viewerSettings.camerasScaleFactor
|
||||
);
|
||||
if (fov < 0)
|
||||
fov += 180;
|
||||
_camera.fieldOfView = fov;
|
||||
|
||||
_camera.nearClipPlane = 0.001f;
|
||||
_camera.farClipPlane = 10000;
|
||||
_camera.depthTextureMode = DepthTextureMode.Depth;
|
||||
_camera.clearFlags = CameraClearFlags.Color;
|
||||
_camera.GetUniversalAdditionalCameraData().renderPostProcessing = false; //URP only
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
UnloadModel();
|
||||
Object.Destroy(_camera.gameObject);
|
||||
DestroyTexture();
|
||||
|
||||
_image.Dispose();
|
||||
_image = null;
|
||||
}
|
||||
|
||||
|
||||
void CreateTexture()
|
||||
{
|
||||
if (_renderTexture != null)
|
||||
return;
|
||||
|
||||
_renderTexture = new RenderTexture(1024, 1024, 24, RenderTextureFormat.ARGB32)
|
||||
{
|
||||
antiAliasing = 1,
|
||||
filterMode = FilterMode.Bilinear,
|
||||
anisoLevel = 0,
|
||||
useMipMap = false
|
||||
};
|
||||
_image.texture = new NTexture(_renderTexture);
|
||||
_image.blendMode = BlendMode.Off;
|
||||
}
|
||||
|
||||
void DestroyTexture()
|
||||
{
|
||||
if (_renderTexture != null)
|
||||
{
|
||||
Object.Destroy(_renderTexture);
|
||||
_renderTexture = null;
|
||||
_image.texture = null;
|
||||
}
|
||||
}
|
||||
|
||||
void OnAddedToStage()
|
||||
{
|
||||
if (_renderTexture == null)
|
||||
CreateTexture();
|
||||
|
||||
Timers.inst.AddUpdate(Render);
|
||||
_camera.gameObject.SetActive(true);
|
||||
|
||||
Render();
|
||||
}
|
||||
|
||||
void OnRemoveFromStage()
|
||||
{
|
||||
if (!_cacheTexture)
|
||||
DestroyTexture();
|
||||
|
||||
Timers.inst.Remove(Render);
|
||||
_camera.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
void Render(object param = null)
|
||||
{
|
||||
if (_rotating != 0 && modelRoot != null)
|
||||
{
|
||||
Vector3 localRotation = modelRoot.localRotation.eulerAngles;
|
||||
localRotation.y += _rotating;
|
||||
modelRoot.localRotation = Quaternion.Euler(localRotation);
|
||||
}
|
||||
|
||||
SetLayer(_root.gameObject, RENDER_LAYER);
|
||||
|
||||
_camera.targetTexture = _renderTexture;
|
||||
RenderTexture old = RenderTexture.active;
|
||||
RenderTexture.active = _renderTexture;
|
||||
GL.Clear(true, true, Color.clear);
|
||||
_camera.Render();
|
||||
RenderTexture.active = old;
|
||||
|
||||
SetLayer(_root.gameObject, HIDDEN_LAYER);
|
||||
}
|
||||
|
||||
void SetLayer(GameObject go, int layer)
|
||||
{
|
||||
Transform[] transforms = go.GetComponentsInChildren<Transform>(true);
|
||||
foreach (Transform t in transforms)
|
||||
{
|
||||
t.gameObject.layer = layer;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 14ecd9c69c8a4eb28e8f102cca593c0b
|
||||
timeCreated: 1761412504
|
||||
@@ -7,9 +7,9 @@ using NBC;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
public partial class ModelTexture
|
||||
public partial class ModelViewer
|
||||
{
|
||||
public const string URL = "ui://6hgkvlau5dtxm9";
|
||||
public const string URL = "ui://6hgkvlauxd2fml";
|
||||
|
||||
public GGraph ModelHolder;
|
||||
public GGraph TouchHolder;
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d1f6b336c0f70ab428b9ab14c0f1bd6f
|
||||
78
Assets/Scripts/UI/Common/ModelViewer/ModelViewer.cs
Normal file
78
Assets/Scripts/UI/Common/ModelViewer/ModelViewer.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
// 本脚本只在不存在时会生成一次。组件逻辑写在当前脚本内。已存在不会再次生成覆盖
|
||||
|
||||
using UnityEngine;
|
||||
using FairyGUI;
|
||||
using NBC;
|
||||
using NBF.Fishing2;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
public partial class ModelViewer : GComponent
|
||||
{
|
||||
ModelViewRenderImage _renderImage;
|
||||
|
||||
private void OnInited()
|
||||
{
|
||||
_renderImage = new ModelViewRenderImage(ModelHolder.asGraph);
|
||||
TouchHolder.onRollOver.Set(OnFocusIn);
|
||||
TouchHolder.onRollOut.Set(OnFocusOut);
|
||||
Stage.inst.onMouseWheel.Add(OnMouseWheel);
|
||||
}
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
Stage.inst.onMouseWheel.Remove(OnMouseWheel);
|
||||
_renderImage.Dispose();
|
||||
base.Dispose();
|
||||
}
|
||||
|
||||
public void SetData(uint itemId)
|
||||
{
|
||||
SetData(ItemConfig.Get(itemId));
|
||||
}
|
||||
|
||||
public void SetData(ItemConfig itemConfig)
|
||||
{
|
||||
//Assets/Resources/gfx/baits/worm_01/worm_01.prefab
|
||||
// _renderImage.LoadModel("gfx/baits/worm_01/worm_01");
|
||||
_renderImage.LoadModel("gfx/" + itemConfig.Model);
|
||||
// var model = PrefabsHelper.CreatePrefab("gfx/baits/worm_01/worm_01");
|
||||
}
|
||||
|
||||
#region 用户输入操作(滑动,拖动等)
|
||||
|
||||
private void SetRotateListening()
|
||||
{
|
||||
var dragObj = TouchHolder;
|
||||
var gesture1 = new SwipeGesture(dragObj);
|
||||
gesture1.onMove.Set(OnSwipeMove);
|
||||
gesture1.onEnd.Set(OnSwipeEnd);
|
||||
}
|
||||
|
||||
private void OnSwipeMove(EventContext context)
|
||||
{
|
||||
}
|
||||
|
||||
private void OnSwipeEnd(EventContext context)
|
||||
{
|
||||
}
|
||||
|
||||
private void OnFocusIn()
|
||||
{
|
||||
// _focus = true;
|
||||
Log.Info("focus true");
|
||||
}
|
||||
|
||||
private void OnFocusOut()
|
||||
{
|
||||
// _focus = false;
|
||||
Log.Info("focus false");
|
||||
}
|
||||
|
||||
private void OnMouseWheel(EventContext context)
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/UI/Common/ModelViewer/ModelViewer.cs.meta
Normal file
2
Assets/Scripts/UI/Common/ModelViewer/ModelViewer.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c4925b571afa6b84382089b71d8603f2
|
||||
148
Assets/Scripts/UI/Common/ModelViewer/ModelViewerSettings.cs
Normal file
148
Assets/Scripts/UI/Common/ModelViewer/ModelViewerSettings.cs
Normal file
@@ -0,0 +1,148 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using UnityEngine;
|
||||
using Color = UnityEngine.Color;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
[Serializable]
|
||||
public class ModelViewerSettings
|
||||
{
|
||||
//Object Settings
|
||||
public Vector3 objectPosition;
|
||||
public Vector3 objectRotation;
|
||||
public Vector3 objectScale;
|
||||
|
||||
//Hierarchy Settings
|
||||
[NonSerialized] public Dictionary<string, bool> subObjectEnables;
|
||||
public List<string> soeStrings;
|
||||
public List<bool> soeBools;
|
||||
|
||||
//Camera Settings
|
||||
public Vector3 cameraPosition;
|
||||
public Vector3 cameraTarget;
|
||||
public bool autoPosition;
|
||||
public bool autoScale;
|
||||
public bool cameraOrtho;
|
||||
public float cameraFov;
|
||||
public float cameraSize;
|
||||
public float camerasScaleFactor;
|
||||
public float perspLastScale;
|
||||
|
||||
//Lighting Settings
|
||||
public Color lightColour;
|
||||
public Vector3 lightDir;
|
||||
public float lightIntensity;
|
||||
public Color ambientLightColour;
|
||||
|
||||
//Animation Settings
|
||||
public AnimationClip animationClip;
|
||||
public float animationOffset;
|
||||
public string animationPath;
|
||||
|
||||
//Post-Processing Settings
|
||||
public List<Material> postProcessingMaterials;
|
||||
public Dictionary<Material, String> materialDisplayNames;
|
||||
public Dictionary<Material, bool> materialToggles;
|
||||
|
||||
public enum FixEdgesModes
|
||||
{
|
||||
None,
|
||||
Regular,
|
||||
WithDepthTexture
|
||||
};
|
||||
|
||||
public FixEdgesModes fixEdgesMode;
|
||||
public FilterMode filterMode;
|
||||
|
||||
|
||||
public ModelViewerSettings()
|
||||
{
|
||||
//Do nothing
|
||||
//---Initialise Icon---//
|
||||
//Object Settings
|
||||
objectPosition = Vector3.zero;
|
||||
objectRotation = Vector3.zero;
|
||||
objectScale = Vector3.one;
|
||||
autoPosition = true;
|
||||
|
||||
//Hierarchy Settings
|
||||
subObjectEnables = new Dictionary<string, bool>();
|
||||
|
||||
//Camera Settings
|
||||
cameraPosition = new Vector3(1, Mathf.Sqrt(2), 1);
|
||||
perspLastScale = 1;
|
||||
cameraOrtho = true;
|
||||
cameraFov = 60;
|
||||
cameraSize = 5;
|
||||
camerasScaleFactor = 1;
|
||||
cameraTarget = Vector3.zero;
|
||||
autoScale = true;
|
||||
|
||||
//Lighting Settings
|
||||
ambientLightColour = Color.gray;
|
||||
lightColour = Color.white;
|
||||
lightDir = new Vector3(50, -30, 0);
|
||||
lightIntensity = 1;
|
||||
|
||||
//Post-Processing Settings
|
||||
postProcessingMaterials = new List<Material>();
|
||||
materialDisplayNames = new Dictionary<Material, string>();
|
||||
materialToggles = new Dictionary<Material, bool>();
|
||||
filterMode = FilterMode.Point;
|
||||
fixEdgesMode = FixEdgesModes.Regular;
|
||||
}
|
||||
|
||||
public ModelViewerSettings(Shader objRenderShader, string rapidIconRootFolder, GameObject rootObject)
|
||||
{
|
||||
//---Initialise Icon---//
|
||||
//Object Settings
|
||||
objectPosition = Vector3.zero;
|
||||
objectRotation = Vector3.zero;
|
||||
objectScale = Vector3.one;
|
||||
autoPosition = true;
|
||||
|
||||
//Hierarchy Settings
|
||||
subObjectEnables = new Dictionary<string, bool>();
|
||||
|
||||
//Camera Settings
|
||||
cameraPosition = new Vector3(1, Mathf.Sqrt(2), 1);
|
||||
perspLastScale = 1;
|
||||
cameraOrtho = true;
|
||||
cameraFov = 60;
|
||||
cameraSize = 5;
|
||||
camerasScaleFactor = 1;
|
||||
cameraTarget = Vector3.zero;
|
||||
autoScale = true;
|
||||
|
||||
//Lighting Settings
|
||||
ambientLightColour = Color.gray;
|
||||
lightColour = Color.white;
|
||||
lightDir = new Vector3(50, -30, 0);
|
||||
lightIntensity = 1;
|
||||
|
||||
//Post-Processing Settings
|
||||
postProcessingMaterials = new List<Material>();
|
||||
materialDisplayNames = new Dictionary<Material, string>();
|
||||
materialToggles = new Dictionary<Material, bool>();
|
||||
filterMode = FilterMode.Point;
|
||||
fixEdgesMode = FixEdgesModes.Regular;
|
||||
// Material defaultRender = new Material(objRenderShader);
|
||||
// postProcessingMaterials.Add(defaultRender);
|
||||
// materialDisplayNames.Add(defaultRender, "Object Render");
|
||||
// materialToggles.Add(defaultRender, true);
|
||||
}
|
||||
|
||||
void SetSubObjectEnables(GameObject obj, int childIdx, string lastPath)
|
||||
{
|
||||
string path = lastPath + "/" + childIdx;
|
||||
subObjectEnables.Add(path, true);
|
||||
|
||||
for (int i = 0; i < obj.transform.childCount; i++)
|
||||
{
|
||||
SetSubObjectEnables(obj.transform.GetChild(i).gameObject, i, path);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7ff80ea34f5f43069392dc52c7b49831
|
||||
timeCreated: 1761415395
|
||||
99
Assets/Scripts/UI/Common/ModelViewer/ModelViewerUtils.cs
Normal file
99
Assets/Scripts/UI/Common/ModelViewer/ModelViewerUtils.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
public class ModelViewerUtils
|
||||
{
|
||||
public static ModelViewerSettings InitSetting(GameObject go, ModelViewerSettings settings)
|
||||
{
|
||||
if (settings.objectPosition.magnitude < 0.0001f)
|
||||
settings.objectPosition = Vector3.zero;
|
||||
|
||||
//---Zero the GameObject euler angles if they're very small in magnitude---//
|
||||
settings.objectRotation = go.transform.eulerAngles;
|
||||
if (settings.objectRotation.magnitude < 0.0001f)
|
||||
settings.objectRotation = Vector3.zero;
|
||||
|
||||
//---Set the object scale variable---//
|
||||
settings.objectScale = go.transform.localScale;
|
||||
|
||||
//将默认对象位置设为使其位于图标中心的位置
|
||||
Bounds bounds = GetObjectBounds(go);
|
||||
settings.objectPosition = GetObjectAutoOffset(bounds);
|
||||
|
||||
// 将默认的相机设置调整为适合图标渲染中对象的呈现效果
|
||||
float camAuto = GetCameraAuto(bounds);
|
||||
settings.cameraSize = camAuto;
|
||||
settings.cameraPosition = Vector3.one * camAuto;
|
||||
|
||||
|
||||
return settings;
|
||||
}
|
||||
|
||||
public static Bounds GetObjectBounds(GameObject go)
|
||||
{
|
||||
// 在将其位置归零之前先保存其位置信息
|
||||
Vector3 prefabPos = go.transform.position;
|
||||
go.transform.position = Vector3.zero;
|
||||
|
||||
// 创建一个边界对象,并将对象网格的边界进行封装。
|
||||
MeshRenderer mr = go.GetComponent<MeshRenderer>();
|
||||
Bounds bounds = new Bounds(Vector3.zero, 0.000001f * Vector3.one);
|
||||
if (mr != null)
|
||||
bounds.Encapsulate(mr.bounds);
|
||||
else
|
||||
{
|
||||
SkinnedMeshRenderer smr = go.GetComponent<SkinnedMeshRenderer>();
|
||||
if (smr != null)
|
||||
bounds.Encapsulate(smr.bounds);
|
||||
}
|
||||
|
||||
//同时将对象的子对象的范围也进行封装起来
|
||||
EncapsulateChildBounds(go.transform, ref bounds);
|
||||
|
||||
//将预制物的位置重置为存储的值
|
||||
go.transform.position = prefabPos;
|
||||
|
||||
return bounds;
|
||||
}
|
||||
|
||||
public static Vector3 GetObjectAutoOffset(Bounds bounds)
|
||||
{
|
||||
//将偏移量应用到图标对象的位置上
|
||||
return -bounds.center;
|
||||
}
|
||||
|
||||
public static float GetCameraAuto(Bounds bounds)
|
||||
{
|
||||
//---Scale camera size and position so that the object fits in the render---//
|
||||
Matrix4x4 trs = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0, 45, 0), 1.05f * Vector3.one);
|
||||
Vector3 corner = new Vector3(bounds.extents.x, bounds.extents.y, bounds.extents.z);
|
||||
corner = trs * corner;
|
||||
|
||||
Vector2 refB2 = new Vector2(0.74f, 0.53f);
|
||||
Vector2 b2 = refB2 * corner.magnitude;
|
||||
|
||||
return b2.magnitude;
|
||||
}
|
||||
|
||||
public static void EncapsulateChildBounds(Transform t, ref Bounds bounds)
|
||||
{
|
||||
// 遍历所有子对象,封装边界
|
||||
MeshRenderer mr;
|
||||
for (int i = 0; i < t.childCount; i++)
|
||||
{
|
||||
mr = t.GetChild(i).GetComponent<MeshRenderer>();
|
||||
if (mr != null)
|
||||
bounds.Encapsulate(mr.bounds);
|
||||
else
|
||||
{
|
||||
SkinnedMeshRenderer smr = t.GetChild(i).GetComponent<SkinnedMeshRenderer>();
|
||||
if (smr != null)
|
||||
bounds.Encapsulate(smr.bounds);
|
||||
}
|
||||
|
||||
EncapsulateChildBounds(t.GetChild(i), ref bounds);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 61157ac2e9594f58ae919731a4a5028c
|
||||
timeCreated: 1761487911
|
||||
@@ -11,7 +11,7 @@ namespace NBF
|
||||
{
|
||||
public const string URL = "ui://hxr7rc7plvql10";
|
||||
|
||||
public ModelTexture Model;
|
||||
public ModelViewer Model;
|
||||
public HomeButtonGroups OpGroup;
|
||||
public BottomMenu BottomMenu;
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace NBF
|
||||
{
|
||||
base.ConstructFromXML(xml);
|
||||
|
||||
Model = (ModelTexture)GetChild("Model");
|
||||
Model = (ModelViewer)GetChild("Model");
|
||||
OpGroup = (HomeButtonGroups)GetChild("OpGroup");
|
||||
BottomMenu = (BottomMenu)GetChild("BottomMenu");
|
||||
OnInited();
|
||||
|
||||
@@ -31,9 +31,8 @@ namespace NBF
|
||||
{
|
||||
Game.Input.OnUICanceled += OnUICanceled;
|
||||
UseBottomMenu();
|
||||
|
||||
|
||||
Model.LoadAsset(0);
|
||||
|
||||
// Model.LoadAsset(0);
|
||||
// Model.SetBackground(Panel.Back.GetChild("back"));//, Panel.Back.GetChild("icon")
|
||||
}
|
||||
|
||||
|
||||
2
Assets/Scripts/UI/Tools/PreviewPanel.Designer.cs
generated
2
Assets/Scripts/UI/Tools/PreviewPanel.Designer.cs
generated
@@ -15,7 +15,7 @@ namespace NBF
|
||||
public override string UIResName => "PreviewPanel";
|
||||
|
||||
[AutoFind(Name = "Model")]
|
||||
public ModelTexture Model;
|
||||
public ModelViewer Model;
|
||||
public override string[] GetDependPackages(){ return new string[] {"Common"}; }
|
||||
|
||||
public static void Show(object param = null){ App.UI.OpenUI<PreviewPanel>(param); }
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace NBF
|
||||
{
|
||||
Instance = Object.Instantiate(prefab);
|
||||
Debug.LogError($"预制体:{prefab.name} 实例={Instance}");
|
||||
Model.SetModel(Instance);
|
||||
// Model.SetModel(Instance);
|
||||
return Instance;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user