123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- using BeinLab.Util;
- using ShadowStudio.Tool;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using XRTool.Util;
- public class ModelC : MonoBehaviour
- {
- private BoxCollider colliderBody;
- private Transform modelRoot;
- //private NearInterationGrabbable nearInteration;
- public Vector3 offset = Vector3.one * 0.1f;
- private bool isInit;
- private Renderer[] modelRenderers;
- /// <summary>
- /// 初始化模型的辅助工具
- /// 仅在计算模型Bound时使用,用完即销毁
- /// </summary>
- private GameObject containerHelper;
- private GameObject downInfo;
- private TextMesh processLabel;
- public BoxCollider ColliderBody
- {
- get
- {
- if (!colliderBody)
- {
- colliderBody = UnityUtil.GetBreadthChild<BoxCollider>(transform, "ColliderBody");
- }
- return colliderBody;
- }
- }
- public Transform ModelRoot
- {
- get
- {
- if (!modelRoot)
- {
- modelRoot = UnityUtil.GetBreadthChild<Transform>(transform, "ModelRoot");
- }
- return modelRoot;
- }
- }
- public void CalculateRendererBounds()
- {
- if (ModelRoot.childCount < 1)
- {
- DownInfo.SetActive(true);
- return;
- }
- DownInfo.SetActive(false);
- //transform.DOKill(true);
- if (ModelRenderers == null || ModelRenderers.Length < 1)
- ModelRenderers = ModelRoot.GetComponentsInChildren<Renderer>();
- 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<Collider>();
- 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<ArtTaskAction>() && !this.GetComponentInChildren<ArtTaskChild>())
- {
- while (ColliderBody.size.magnitude > (Vector3.one * 0.5f).magnitude)
- {
- ColliderBody.size /= 2;
- ModelRoot.transform.localScale /= 2;
- ModelRoot.transform.localPosition /= 2;
- // yield return new WaitForFixedUpdate();
- }
- }
- 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;
- GetComponent<TransferCell>().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);
-
- CalculateRendererBounds();
-
- //body.SetParent(ModelRoot.transform);
- //body.transform.localPosition = Vector3.zero;
- //body.localRotation = Quaternion.identity;
- //body.transform.localScale = Vector3.one;
- }
- }
- 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<TextMesh>(DownInfo.transform, "ProcessLabel");
- }
- return processLabel;
- }
- }
- }
|