using DG.Tweening;
using PublicTools.Unity;
using SC.XR.Unity.Module_InputSystem;
using ShadowStudio.Tool;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using XRTool.Util;
namespace ShadowStudio.Model
{
///
/// 模型容器
/// 一个简易模型或者其他的模型资源的容器
/// 自动计算包围盒,大小位置中心等等,支持模型打组编辑(多个模型合为一个模型进行编辑)
/// 拖拽移动缩放组件
///
public class ModelContainer : GoodsContainer
{
public GameObject ChooseKuang;
private BoxCollider colliderBody;
private Transform modelRoot;
//private NearInterationGrabbable nearInteration;
public Vector3 offset = Vector3.one * 0.1f;
private bool isInit;
private Renderer[] modelRenderers;
///
/// 初始化模型的辅助工具
/// 仅在计算模型Bound时使用,用完即销毁
///
private GameObject containerHelper;
private GameObject downInfo;
private TextMesh processLabel;
public override void Start()
{
base.Start();
CalculateRendererBounds();
TimerMgr.Instance.CreateTimer(
PlayEffect, 0);
}
public BoxCollider ColliderBody
{
get
{
if (!colliderBody)
{
colliderBody = UnityUtil.GetBreadthChild(transform, "ColliderBody");
}
return colliderBody;
}
}
public Transform ModelRoot
{
get
{
if (!modelRoot)
{
modelRoot = UnityUtil.GetBreadthChild(transform, "ModelRoot");
}
return modelRoot;
}
}
//private void Awake()
//{
// ToggleComponent(false);
// //if (Transfer && Transfer.Box)
// //{
// //}
//}
//public override void OnStopScale()
//{
// base.OnStopScale();
// while (ColliderBody.size.magnitude * transform.localScale.x < (Vector3.one * 0.1f).magnitude / 2)
// {
// transform.localScale *= 1.5f;
// }
//}
public override void OnZoomTransfer(TransferState state)
{
base.OnZoomTransfer(state);
if (state == TransferState.Stop)
{
while (ColliderBody.size.magnitude * transform.localScale.x < (Vector3.one * 0.1f).magnitude)
{
transform.localScale *= 1.5f;
}
SendTransfer(2, (int)state);
}
}
public void CalculateRendererBounds()
{
if (ModelRoot.childCount < 1)
{
DownInfo.SetActive(true);
return;
}
DownInfo.SetActive(false);
ChooseKuang.SetActive(false);
//transform.DOKill(true);
if (ModelRenderers == null || ModelRenderers.Length < 1)
ModelRenderers = ModelRoot.GetComponentsInChildren();
Vector3 center = Vector3.zero;
Vector3 curScale = transform.localScale;
Vector3 curPos = transform.position;
Quaternion curRot = transform.rotation;
transform.localScale = Vector3.one;
transform.position = Vector3.zero;
transform.rotation = Quaternion.identity;
ModelRoot.localScale = Vector3.one;
ModelRoot.localPosition = Vector3.zero;
ModelRoot.localRotation = Quaternion.identity;
for (int i = 0; i < ModelRenderers.Length; i++)
{
Renderer rend = ModelRenderers[i];
var collider = rend.GetComponent();
if (collider)
{
DestroyImmediate(collider);
}
Vector3 tmpCenter = rend.bounds.center;
center += tmpCenter;
}
//ModelRoot.gameObject.SetActive(false);
Vector3 bScale = transform.lossyScale;
Vector3 bPos = center / ModelRenderers.Length - ModelRoot.transform.position;
bPos.x /= bScale.x;
bPos.y /= bScale.y;
bPos.z /= bScale.z;
ModelRoot.transform.localPosition = bPos;
Bounds bounds = new Bounds(ModelRoot.transform.position, Vector3.zero);
for (int i = 0; i < ModelRenderers.Length; i++)
{
bounds.Encapsulate(ModelRenderers[i].bounds);
}
Vector3 bsize = bounds.size;
bsize.x /= bScale.x;
bsize.y /= bScale.y;
bsize.z /= bScale.z;
ColliderBody.size = bsize;
//if(!this.GetComponentInChildren()&&!this.GetComponentInChildren())
//{
while (ColliderBody.size.magnitude > (Vector3.one * 0.2f).magnitude)
{
ColliderBody.size /= 2;
ModelRoot.transform.localScale /= 2;
ModelRoot.transform.localPosition /= 2;
// yield return new WaitForFixedUpdate();
}
// 根据ColliderBody设置包围盒大小
GetComponent().size = transform.Find("ColliderBody").GetComponent().size;
//}
while (ColliderBody.size.magnitude < (Vector3.one * 0.1f).magnitude)
{
ColliderBody.size *= 2;
ModelRoot.transform.localScale *= 2;
ModelRoot.transform.localPosition *= 2;
//yield return new WaitForFixedUpdate();
}
ModelRoot.transform.localPosition = -ModelRoot.transform.localPosition;
ColliderBody.size += offset;
//ModelRoot.gameObject.SetActive(true);
transform.position = curPos;
transform.rotation = curRot;
transform.localScale = curScale;
Transfer.RemoveBound();
//PlayEffect();
//TimerMgr.Instance.CreateTimer(() =>
//{
// //yield return new WaitForSeconds(1f);
// //transform.DOKill(true);
// //ToggleComponent(true);
// Transfer.RemoveBound();
// //yield return new WaitForEndOfFrame();
// //Transfer.SetBoundActive(false);
// ResetEditorPosition();
//}, 1.05f);
}
public void AddModel(Transform body)
{
if (ModelRoot)
{
UnityUtil.SetParent(ModelRoot, body);
if (Transfer && Transfer.IsInit)
{
CalculateRendererBounds();
}
//body.SetParent(ModelRoot.transform);
//body.transform.localPosition = Vector3.zero;
//body.localRotation = Quaternion.identity;
//body.transform.localScale = Vector3.one;
}
}
//private void Update()
//{
// if (Input.GetKeyDown(KeyCode.O))
// {
// CalculateRendererBounds();
// }
//}
///
/// 添加到容器
///
///
public override void AddToContainer(UnityEngine.Object art, ArtInfo info)
{
base.AddToContainer(art, info);
if (art is GameObject)
{
GameObject obj = art as GameObject;
AddModel(obj.transform);
}
}
public Renderer[] ModelRenderers { get => modelRenderers; set => modelRenderers = value; }
public GameObject DownInfo
{
get
{
if (!downInfo)
{
downInfo = UnityUtil.GetBreadthChild(transform, "DownInfo");
}
return downInfo;
}
}
public TextMesh ProcessLabel
{
get
{
if (!processLabel && DownInfo)
{
processLabel = UnityUtil.GetBreadthChild(DownInfo.transform, "ProcessLabel");
}
return processLabel;
}
}
///
/// 更新下载进度
///
///
public override void UpdateProcess(float process)
{
if (ProcessLabel)
{
ProcessLabel.text = process.ToString("P1");
if (process > 0.99f)
{
ProcessLabel.text = 99 + "%";
}
}
}
}
}